Sunteți pe pagina 1din 35

POLITECNICO

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

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

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

franco.zappa@polimi.it

3 / 231

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:
rudi.lussana@polimi.it
mauro.buttafava@polimi.it
mirko.sanzaro@polimi.it
davide.portaluppi@polimi.it

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

franco.zappa@polimi.it

4 / 231

What is a microcontroller

Single integrated device,


containing a processing
core, program and data
memories and many
peripheral 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
Flexible and cost effective devices Mainly used for embedded
applications
ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

franco.zappa@polimi.it

6 / 231

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
ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

franco.zappa@polimi.it

7 / 231

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 program
settings and code protection purposes (CONFIG1, CONFIG2)

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

franco.zappa@polimi.it

8 / 231

Instructions and programming


ASSEMBLY:

Hardware-level language
High complexity
Highest code optimization
Predictable timing

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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 Instruction Set

RISC architecture:
only 35 instructions
Predictable Timing:
each instruction
completes in one
(or two) cycles
possibility to create
delay routines

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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

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
franco.zappa@polimi.it

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

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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

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

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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

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

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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

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

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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

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!


ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

franco.zappa@polimi.it

17 / 231

Development Tools (1)


Programming IDE and compiler

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

franco.zappa@polimi.it

18 / 231

Development Tools (2)


In-Circuit programmer/debugger

Microchip PicKit 3

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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;

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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;

Global variable declaration

Global variable used in a function

Local variable declaration


Both types are used here

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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;

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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]);)

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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.

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

franco.zappa@polimi.it

25 / 231

Mathematical Operations
+,-,*,/
% (mod)
=
Other mathematical operators are implemented using specific libraries
but 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

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

franco.zappa@polimi.it

27 / 231

If
if (condition)
{
//true branch
}
else
{
//else branch
}
EX: if ((a == 1) || ((b < 5) && (b > 1)))

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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;
}

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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
}

for (int i = 10; i > 0; i --)


{
//this code is executed ten times
}

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

franco.zappa@polimi.it

31 / 231

Functions
Prototype definition
Type

function_name

(type, type, );

Function definition
Type
{

function_name

(type input1, type input2, )

//code
}

Function call
Output

function_name

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

(input1, input2, );

franco.zappa@polimi.it

32 / 231

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);

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

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.

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 hardware


resources of the microcontroller.
HI-TECH PICC
TRISB = 0x00;
PORTB = 0x01;
Register settings
ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

CCS PICC
set_tris_b(0x00);
output_b(0x01);
Function call
franco.zappa@polimi.it

34 / 231

DESIGN FLOW C LANGUAGE

Programming
HEX file

C code

Assembly
code

Assembler

COF file

Simulation
and debug

If bad firmware

ELECTRONIC SYSTEMS lab - MICROCONTROLLERS 00

franco.zappa@polimi.it

35 / 231

S-ar putea să vă placă și