zsdzs
-
Upload
altruda-samuele -
Category
Documents
-
view
218 -
download
3
description
Transcript of zsdzs
POLITECNICO DI MILANO
www.polimi.it
Microcontrollers
INTRODUCTION and C Review
[email protected] 2 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Outline
• Course introduction
• What is a µController and how it works
• Memory organization and Registers
• Instructions and Programming Languages
• Peripherals
• Development Tools
• Laboratory goals
• C language review
[email protected] 3 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Course Introduction
• Main activities:• Learn the main microcontroller peripherals• Write code• Program and use a real device
• Agenda:From 16.30 to 16.45 From 16.45 to 18.00
19-22/10 Lesson 1 Introduction & C language SW and board overview
26-29/10 Lesson 2 Ports & Interrupts Hands-on
2-5/11 Lesson 3 Timers & 7-seg display Hands-on
9-12/11 Lesson 4 ADC & LCD Hands-on
16-19/11 Lesson 5 Capture Compare PWM Hands-on
23-26/11 Lesson 6 Code optimization with assembly Hands-on
[email protected] 4 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Course Introduction
• Organizational stuff:
• Group A : Mondays, 16.30 – 18.00• Group B : Tuesdays, 16.30 - 18.00• Group C : Wednesdays, 16.30 – 18.00• Group D : Thursdays,16.30 – 18.00
• At least one PC per working-group (2-4 people)• A USB stick may be useful
• Contacts: [email protected]@[email protected]@polimi.it
[email protected] 5 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
What is a microcontroller
Flexible and cost effective devices Mainly used for embeddedapplications
Single integrated device, containing a processing core, program and data
memories and manyperipheral modules(also mixed-signal)
Wide range of devices:FROM 8-bit architecture, 4 MHz, in SOT23-6 package TO 32-bit architecture, 200 MHz, with ethernet and USB
[email protected] 6 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
How it works
• Harvard-based architecture• 35 instructions RISC processor core• Internal program and data memory• Multi-priority interrupt capability• Different kinds of peripheral modules and interfaces
[email protected] 7 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Memory Organization (1)
PIC18F45K22 (used during this course):
• 21-bit program counter, capable of addressing up to
2 MB max program memory space
• Program Memory: 64 kB (EEPROM)
• Data Memory: 3896 kB of STATIC RAM
Data Memory is divided into:
• Special Function Registers, used by the CPU and peripherals for controlling the desired operation of the device
• General Purpose Registers, used for temporary data storage
[email protected] 8 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Memory Organization (2)
There are three types of Special Function Registers (SFR):
• Core Registers:
Deal with CPU operation, interrupts control and input/output ports(W, STATUS, INTCON, TRISA…)
• Peripheral Registers:
Control the operation of peripheral modules: enable, settings and data input/output (ADCON, CCPR, SSPCON, TXSTA…)
• Configuration Registers:
Used for start-up configurations, oscillator settings, in-circuit programsettings and code protection purposes (CONFIG1, CONFIG2…)
[email protected] 9 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Instructions and programming
ASSEMBLY:
• Hardware-level language• High complexity• Highest code optimization• Predictable timing
High-level Languages:
• C (industry standard), Pascal• Better code understanding
(natural language elements)• Code optimization managed by
the compiler (Low)• Higher portability of code
[email protected] 10 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
RISC Instruction Set
• RISC architecture: only 35 instructions
• Predictable Timing: each instructioncompletes in one(or two) cycles → possibility to create delay routines
[email protected] 11 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Peripherals
PIC18F45K22 (used during this lessons):
Why a Microcontroller instead of a Microprocessor?
PERIPHERALS ARE THE REAL BENEFIT OF USING A MICROCONTROLLER
A µC can directly interface with external world
• GPIO with interrupt on change and internal pull-up• Multichannel 10-bit ADC with internal voltage reference• 16-bit Timer/Counter module• Capture/Compare/PWM module• Enhanced USART module• Master Synchronous Serial Port (MSSP)• Analog Comparator module
[email protected] 12 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
PIC18F45
HIGH PERFORMANCE RISC CPU FEATURES• Only 35 single word instructions to learn• Operating speed: DC - 20 MHz clock input• 1024 words of program memory• 4k bytes of Data RAM• 64k bytes of Data EEPROM• 16-bit wide instruction words• 8-bit wide data bytes
PERIPHERAL FEATURES• 35 I/O pins with individual directioncontrol• High current sink/source for direct LED drive- 25 mA sink max. per pin- 25 mA source max. per pin• TMR0: 8-bit timer/counter with 8-bitprogrammable prescaler
CMOS ENHANCED FLASH/EEPROM TECHNOLOGY
• Low power, high speed technology• Fully static design• Wide operating voltage range:- Commercial: 2.0V to 5.5V- Industrial: 2.0V to 5.5V• Low power consumption:- < 2 mA typical @ 5V, 4 MHz- 15 mA typical @ 2V, 32 kHz-< 0.5 mA typical standby current @ 2V
SPECIAL MCU FEATURES• In-Circuit Serial Programming™ (ICSP™)
• Power-on Reset, Power-up Timer,Oscillator Start-up Timer• Watchdog Timer with On-Chip RC oscillator
[email protected] 13 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
PIC18F45123
54
18
1617
1514
1213
11
6
87
109
RA0RA1
RA4
VSS
MCLR/Vpp
RA7/CLKIN
RA6/CLKOUT
VDD
PO
RTE
PO
RTA
PO
RTB
PO
RTD
OS
C
POWER-UP TIMERSTART-UP TIMER
POWER ON RESETWDT
PC
TIMER0
STACK
REG. FILE
W
ALU
EEPROMProgram
Mem
EEPROMData Mem
1920 21
22232425262728293031323334353637383940
VDDVSS
RA2
RA3
RA5
PORTD
ADC
EUSART
MSSP
RE0RE1RE2
PO
RTC
PO
RTC
RC0RC1RC2
RC3RD0RD1
RD3RD2
RC5RC4
RC7RC6
RD5RD4
RD7RD6
RB1RB0
RB3RB2
RB5RB4
RB7RB6
[email protected] 14 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Architecture
Opcode is loaded from Program Memory to Instruction Register
A section of Opcode represents the data to transfer
The data to transfer isstored into Workingregister
The following instruction will load the content of Working register and store it into the destination register
How can I write a number in a register?
[email protected] 15 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Architecture
Opcode is loaded from Program Memory to Instruction Register
A section of Opcode represents the data to transfer
The data to transfer is stored into Working register
The following instruction will load the content of Working register and store it into the destination register
[email protected] 16 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Architecture
Opcode is loaded from Program Memory to Instruction Register
A section of Opcode represents the data to transfer
The data to transfer is stored into Working register
The following instruction will load the content of Working register and store it into the destination register
[email protected] 17 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Architecture
Opcode is loaded from Program Memory to Instruction Register
A section of Opcode represents the data to transfer
The data to transfer is stored into Working register
The following instruction will load the content of Working register and store it into the destination register
So I need two separate instructions to do that!
[email protected] 18 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Development Tools (1)
Programming IDE and compiler
[email protected] 19 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Development Tools (2)
Microchip PicKit 3 Mikroelektronika EasyPIC v7
In-Circuit programmer/debugger Development Board
[email protected] 20 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Course Goals
• Program a µC
• Handle Interrupts and manage timers
• Interface with peripherals (ADC, CCP …)
• Connect to external circuits (displays, real-time clock, …)
• Put all this stuff together in a complex project
[email protected] 21 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Variables
They are used to reserve a determined memory area in order to store one or more numerical values, characters, strings…
They can be divided into several types: int (16 bit), long int (32 bit), short (8 bit), char (8 bit), float, double…
Each variable has to be declared in the function using it (unless it is a global variable) and they can be accessed only in the calling function.
They are identified by a mnemonic label chosen by the programmer.
EX:
void main()
{
int first;
Char second;
…
}
[email protected] 22 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Global Variables
They are declared outside functions.
Their access is limited to the .c they are declared in (unless we use externalvariables).
EX:int b; Global variable declarationvoid function1(){
b = 10; Global variable used in a function…
}void function2(){
int c; Local variable declarationc = b + 1; Both types are used here…
}
[email protected] 23 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Vectors
They are made by contiguous memory areas that can be accessed by a common mnemonic label and an index
char b[6];
b[3] = 1;
[email protected] 24 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Pointers
It is a peculiar type of variable used to store a memory address pointing to another memory area containing data
int *p;
int b;
p = &b;
char b[6];
char *p;
p = b; (p = &(b[0]);)
[email protected] 25 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Cast
It is used to transform a type of variable into another
int b;
char c;
b = 10;
c = (char) b;
• This way of working is called explicit cast because the programmerexpresses his intention of changing the type of a variable.
• Casts can also be implicit. This kind of cast operation is highly dangerouswhen a variable with a certain memory usage is transformed into a smaller variable.
[email protected] 26 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Mathematical Operations
• +,-,*,/
• % (mod)
• =
• Other mathematical operators are implemented using specific librariesbut they require a significant computational time:
– log, exp, sin, cos, sqrt
• Sometimes we can use tricks to make the computational time shorter:
– For instance, multiplying a variable by 2 is equivalent to shift all its bits one position to the left, which is tremendously faster
a * 2 a << 2
[email protected] 27 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Logical Operations
BITWISE OPERATIONS& -- bitwise and| -- bitwise or^ -- bitwise xor! -- bitwise not
COMPARISON OPERATIONS== -- equal!= -- different< -- smaller than> -- greater than<= -- smaller or equal>= -- greater or equal&& -- and|| -- or
[email protected] 28 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
If
if (condition)
{
//true branch
}
else
{
//else branch
}
EX: if ((a == 1) || ((b < 5) && (b > 1)))
[email protected] 29 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Switch
switch (a)
{
case 1:
//code executed when a==1
break;
case 2:
//code executed when a==2
break;
default:
//code executed in all other cases
break;
}
[email protected] 30 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Loops (1/2)
Loops are used to execute portions of code until a condition is met
While loop
While (condition)
{
//code
}
Do … While loop
do
{
//code
}
while(condition)
[email protected] 31 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Loops (2/2)
For loop
for (initial assignments; condition; operation)
{
//code
}
for (int i = 10; i > 0; i --)
{
//this code is executed ten times
}
[email protected] 32 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Functions
Prototype definition
Type function_name (type, type, …);
Function definition
Type function_name (type input1, type input2, …){
//code
}
Function call
Output = function_name (input1, input2, …);
[email protected] 33 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
Functions (example)
Prototype definition
int square (int);
Function definition
int square (int input){
int output = input * input;
return output;
}
Function call
int x = 5;
int y = square (x);
[email protected] 34 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
C for microcontrollesTo write in C language for µC is very similar to writing code for PCs.
DIFFERENCE: There are no standard I/O but physical I/O lines.
Printf( “numero = %d”, 8); PORTB=0b00001000;
Various C compilers have many common characteristics:
• ANSI C instruction set
• Supported data types (bit, char, int, long, double)
Differences are usually found in the syntax used to access hardwareresources of the microcontroller.
TRISB = 0x00;PORTB = 0x01;
set_tris_b(0x00);output_b(0x01);
HI-TECH PICC CCS PICC
Register settings Function call
[email protected] 35 / 231 ELECTRONIC SYSTEMS’ lab - MICROCONTROLLERS 00
DESIGN FLOW – C LANGUAGE
C code Assembler
HEX file
COF file
Programming
Simulationand debug
If bad firmware
Assemblycode