Documente Academic
Documente Profesional
Documente Cultură
DI MILANO
www.polimi.it
Microcontrollers
INTRODUCTION and C Review
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
ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00
franco.zappa@polimi.it
2 / 231
Course Introduction
Main activities:
Learn the main microcontroller peripherals
Write code
Program and use a real device
Agenda:
From 16.30 to 16.45
19-22/10
Lesson 1
26-29/10
Lesson 2
Hands-on
2-5/11
Lesson 3
Hands-on
9-12/11
Lesson 4
Hands-on
16-19/11
Lesson 5
Hands-on
23-26/11
Lesson 6
Hands-on
franco.zappa@polimi.it
3 / 231
Course Introduction
Organizational stuff:
franco.zappa@polimi.it
4 / 231
What is a microcontroller
franco.zappa@polimi.it
5 / 231
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
franco.zappa@polimi.it
6 / 231
Program Memory:
64 kB (EEPROM)
Data Memory:
franco.zappa@polimi.it
7 / 231
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 program
settings and code protection purposes (CONFIG1, CONFIG2)
franco.zappa@polimi.it
8 / 231
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
franco.zappa@polimi.it
9 / 231
RISC architecture:
only 35 instructions
Predictable Timing:
each instruction
completes in one
(or two) cycles
possibility to create
delay routines
franco.zappa@polimi.it
10 / 231
Peripherals
Why a Microcontroller instead of a Microprocessor?
PERIPHERALS ARE THE REAL BENEFIT OF USING A
MICROCONTROLLER
A C can directly interface with external world
PIC18F45K22 (used during this lessons):
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
ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00
franco.zappa@polimi.it
11 / 231
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 direction
control
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-bit
programmable prescaler
ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00
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
12 / 231
PIC18F45
RA5
RE0
RE1
RE2
VDD
VSS
RA7/CLKIN
RA6/CLKOUT
RC0
RC1
RC2
RC3
RD0
RD1
MSSP
TIMER0
STACK
POWER-UP TIMER
START-UP TIMER
POWER ON RESET
WDT
EEPROM
Program
Mem
REG. FILE
EEPROM
Data Mem
W
ALU
PORTD
franco.zappa@polimi.it
PORTB
EUSART
PORTD
RA4
PC
PORTC
RA3
PORTA
RA2
PORTE
RA1
ADC
OSC
RA0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
PORTC
MCLR/Vpp
40
39
38
37
36
35
34
33
32
31
30
29
28
27
26
25
24
23
22
21
13 / 231
RB7
RB6
RB5
RB4
RB3
RB2
RB1
RB0
VDD
VSS
RD7
RD6
RD5
RD4
RC7
RC6
RC5
RC4
RD3
RD2
Architecture
How can I write a number in a register?
Opcode is loaded from
Program Memory to
Instruction Register
A section of Opcode
represents the data to
transfer
franco.zappa@polimi.it
14 / 231
Architecture
Opcode is loaded from
Program Memory to
Instruction Register
A section of Opcode
represents the data to
transfer
franco.zappa@polimi.it
15 / 231
Architecture
Opcode is loaded from
Program Memory to
Instruction Register
A section of Opcode
represents the data to
transfer
franco.zappa@polimi.it
16 / 231
Architecture
Opcode is loaded from
Program Memory to
Instruction Register
A section of Opcode
represents the data to
transfer
franco.zappa@polimi.it
17 / 231
franco.zappa@polimi.it
18 / 231
Microchip PicKit 3
Development Board
Mikroelektronika EasyPIC v7
franco.zappa@polimi.it
19 / 231
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
franco.zappa@polimi.it
20 / 231
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;
franco.zappa@polimi.it
21 / 231
Global Variables
They are declared outside functions.
Their access is limited to the .c they are declared in (unless we use external
variables).
EX:
int b;
void function1()
{
b = 10;
}
void function2()
{
int c;
c = b + 1;
franco.zappa@polimi.it
22 / 231
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;
franco.zappa@polimi.it
23 / 231
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]);)
franco.zappa@polimi.it
24 / 231
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 programmer
expresses his intention of changing the type of a variable.
Casts can also be implicit. This kind of cast operation is highly dangerous
when a variable with a certain memory usage is transformed into a
smaller variable.
franco.zappa@polimi.it
25 / 231
Mathematical Operations
+,-,*,/
% (mod)
=
Other mathematical operators are implemented using specific libraries
but they require a significant computational time:
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
franco.zappa@polimi.it
26 / 231
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
franco.zappa@polimi.it
27 / 231
If
if (condition)
{
//true branch
}
else
{
//else branch
}
EX: if ((a == 1) || ((b < 5) && (b > 1)))
franco.zappa@polimi.it
28 / 231
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;
}
franco.zappa@polimi.it
29 / 231
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)
ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00
franco.zappa@polimi.it
30 / 231
Loops (2/2)
For loop
for (initial assignments; condition; operation)
{
//code
}
franco.zappa@polimi.it
31 / 231
Functions
Prototype definition
Type
function_name
(type, type, );
Function definition
Type
{
function_name
//code
}
Function call
Output
function_name
(input1, input2, );
franco.zappa@polimi.it
32 / 231
Functions (example)
Prototype definition
int
square
(int);
Function definition
int
{
square
(int input)
5;
int y
square
(x);
franco.zappa@polimi.it
33 / 231
C for microcontrolles
To 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.
PORTB=0b00001000;
CCS PICC
set_tris_b(0x00);
output_b(0x01);
Function call
franco.zappa@polimi.it
34 / 231
Programming
HEX file
C code
Assembly
code
Assembler
COF file
Simulation
and debug
If bad firmware
franco.zappa@polimi.it
35 / 231