Академический Документы
Профессиональный Документы
Культура Документы
OVERVIEW
DAY 1 (Morning)
Introduction to PIC Microcontroller
PIC16F877A trainer board and
eICD2/ePickit2 programmer/debugger
MPLAB IDE
Basic C Programming
19-Aug-2011
Franz Duran
OVERVIEW
DAY 1 (Afternoon)
Basic C Programming (cont.)
C Functions
Modular Programming
Interfacing with 2x16 character LCD
Interfacing with 4x3 keypad
19-Aug-2011
Franz Duran
OVERVIEW
DAY 2 (Morning)
Basic of Interrupts
Interrupt sources
Interrupt service routine
RB0 interrupt
PORTB Interrupt on Change
19-Aug-2011
Franz Duran
OVERVIEW
DAY 2 (Afternoon)
Timer module
TMR0 architecture
TMR0 as an interrupt source
19-Aug-2011
Franz Duran
OVERVIEW
DAY 3 (Morning)
Basic of Analog-to-Digital Conversion
Using the A/D module
Interfacing LM35 temp. sensor
19-Aug-2011
Franz Duran
OVERVIEW
DAY 3 (Afternoon)
Basics of Serial Communication
UART module
Basic string processing
Interfacing with Bluetooth module
Interfacing with GSM module
19-Aug-2011
Franz Duran
MICROCONTROLLER
MCU, C
A single-chip computer
Invented in the 1970s
Used as embedded controller
19-Aug-2011
Franz Duran
MICROCONTROLLER
used as dedicated controllers
domestic appliances
consumer electronics
industrial equipments
automotive electronics
19-Aug-2011
Franz Duran
MICROCONTROLLER
Why use?
cheap
flexible
easy to develop applications
easy to maintain/upgrade programs in the field
19-Aug-2011
Franz Duran
10
PIC MICROCONTROLLER
PIC, PICMICRO
by MICROCHIP
Arizona, U.S.A.
1989 (offshoot of General Instrument)
http://www.microchip.com
19-Aug-2011
Franz Duran
11
PIC MICROCONTROLLER
Rank (8-bit microcontroller)
1990 20th
1993 8th
1996 - 5th
1997-2001 2nd
2002 Present 1st
19-Aug-2011
Franz Duran
12
PIC MICROCONTROLLER
Family
Popular among
PIC10, PIC12
students and hobbyists
PIC16
PIC17 / PIC18
PIC24 / DSPICs (16-bit)
PIC32(32-bit)
19-Aug-2011
Franz Duran
13
PIC16 ARCHITECTURE
Program
Memory
General Purpose
Registers
Data RAM
Special Function
Registers
Input/Output
Ports
CPU
Power
Oscillator
Reset, Watchdog
Timer, etc
19-Aug-2011
Internal Peripherals
Franz Duran
14
PIC16 ARCHITECTURE
Program
Memory
General Purpose
Registers
Data RAM
CPU
Special Function
Registers
Input/Output
Ports
Power
Oscillator
Reset, Watchdog
Timer, etc
19-Aug-2011
Internal Peripherals
Franz Duran
15
PIC16 ARCHITECTURE
CPU
Reduced Instruction Set Computer (RISC)
FOSC = 20MHz max. speed
FOSC/4 instruction clock
19-Aug-2011
Franz Duran
16
PIC16 ARCHITECTURE
Program
Memory
General Purpose
Registers
Data RAM
Special Function
Registers
CPU
Input/Output
Ports
Power
Oscillator
Internal Peripherals
Reset, Watchdog
Timer, etc
19-Aug-2011
Franz Duran
17
PIC16 ARCHITECTURE
Program Memory
hard drive where fixed program is stored
flash-based memory
reprogrammable at least 10,000x
19-Aug-2011
Franz Duran
18
PIC16 ARCHITECTURE
Program
Memory
General Purpose
Registers
Data RAM
Special Function
Registers
Input/Output
Ports
CPU
Power
Oscillator
Internal Peripherals
Reset, Watchdog
Timer, etc
19-Aug-2011
Franz Duran
19
PIC16 ARCHITECTURE
File Registers
General Purpose Registers (GPRs)
Data RAM
19-Aug-2011
Franz Duran
20
PIC16 ARCHITECTURE
Program
Memory
General Purpose
Registers
Data RAM
CPU
Special Function
Registers
Input/Output
Ports
Power
Oscillator
Reset, Watchdog
Timer, etc
19-Aug-2011
Internal Peripherals
Franz Duran
21
PIC16 ARCHITECTURE
Program
Memory
General Purpose
Registers
Data RAM
Special Function
Registers
Input/Output
Ports
CPU
Power
Oscillator
Reset, Watchdog
Timer, etc
19-Aug-2011
Internal Peripherals
Franz Duran
22
PIC16 ARCHITECTURE
Program
Memory
General Purpose
Registers
Data RAM
Special Function
Registers
CPU
Power
Input/Output
Ports
Oscillator
Reset, Watchdog
Timer, etc
19-Aug-2011
Internal Peripherals
Franz Duran
23
PIC16 ARCHITECTURE
Input/Output (I/O) Port
Typically consists of 8 I/O pins
Input pin or Output pin
Interfaces the MCU to external peripheral
devices
19-Aug-2011
Franz Duran
24
PIC16 ARCHITECTURE
Program
Memory
General Purpose
Registers
Data RAM
Special Function
Registers
Input/Output
Ports
CPU
Power
Oscillator
Internal Peripherals
Reset, Watchdog
Timer, etc
19-Aug-2011
Franz Duran
25
PIC16 ARCHITECTURE
Internal Peripheral Modules
Provide enhanced functionalities to the MCU
Examples:
Timer modules
A/D module
UART module
19-Aug-2011
Franz Duran
26
PIC16 ARCHITECTURE
Program
Memory
General Purpose
Registers
Data RAM
CPU
Power
Special Function
Registers
Input/Output
Ports
Oscillator
Reset, Watchdog
Timer, etc
19-Aug-2011
Internal Peripherals
Franz Duran
27
PIC16 ARCHITECTURE
Program
Memory
General Purpose
Registers
Data RAM
Special Function
Registers
Input/Output
Ports
CPU
Power
Oscillator
Internal Peripherals
Reset, Watchdog
Timer, etc
19-Aug-2011
Franz Duran
28
PIC16 ARCHITECTURE
Oscillator (FOSC)
Generates astable/clock signal
Synchronizes CPU and peripheral module
operations
19-Aug-2011
Franz Duran
29
PIC16 ARCHITECTURE
Program
Memory
General Purpose
Registers
Data RAM
CPU
Special Function
Registers
Input/Output
Ports
Power
Oscillator
Reset, Watchdog
Timer, etc
19-Aug-2011
Internal Peripherals
Franz Duran
30
10
PIC16 ARCHITECTURE
Features:
Watchdog Timer
SLEEP mode
Power On Reset, Brown-out Reset
CPU
RISC (reduced instruction set computer)
FOSC = 20Mhz typical
19-Aug-2011
Franz Duran
31
PIC16 ARCHITECTURE
Instruction set
35 instructions (PIC16)
Easy to memorize all instructions
19-Aug-2011
Franz Duran
32
PIC16F84A
8-bit microcontrolller
PIC16 family
F flash memory, i.e. reprogrammable
84 variant/model
A - revision
19-Aug-2011
Franz Duran
33
11
PIC16F84A
Program Memory
1024 instruction words
1 word = 14 bit
Data EEPROM
64 bytes
19-Aug-2011
Franz Duran
34
Franz Duran
35
PIC16F84A
13 I/O pins
PORTB 8 pins
PORTA 5 pins
2 power pins
VDD, VSS
2 oscillator pins
OSC1, OSC2
1 RESET pin
MCLR
19-Aug-2011
PIC16F877A
20Mhz (5MIPS), DIP40, +5V
8192 instruction word
368 bytes Data RAM / GPRs
56 SFRs
256 bytes Data EEPROM
19-Aug-2011
Franz Duran
36
12
PIC16F877A
33 I/O pins
PORTA 6 pins
PORTB 8 pins
PORTC 8 pins
PORTD 8 pins
PORTE 3 pins
4 power pins
1 Reset, 2 Clock pins
19-Aug-2011
Franz Duran
37
+5V supply
ICSP connector
SIL connectors
20Mhz Oscillator
19-Aug-2011
PIC16F877A
Franz Duran
38
keypad
4x3 or 4x4
4 pushbuttons
DS1307
real-time IC
serial EEPROM IC
Franz Duran
character LCD
39
13
eICD2
Franz Duran
ICSP connector
40
Franz Duran
41
Franz Duran
42
14
19-Aug-2011
Franz Duran
43
19-Aug-2011
Franz Duran
44
19-Aug-2011
Franz Duran
45
15
19-Aug-2011
Franz Duran
46
19-Aug-2011
Franz Duran
47
19-Aug-2011
Franz Duran
48
16
19-Aug-2011
Franz Duran
49
Output Window
19-Aug-2011
Franz Duran
50
19-Aug-2011
Franz Duran
Editor Window
51
17
Franz Duran
52
19-Aug-2011
Franz Duran
53
main.c
19-Aug-2011
Franz Duran
54
18
Add
main.c
Build project
(F10)
19-Aug-2011
Franz Duran
55
19-Aug-2011
Franz Duran
56
Shorter code
2. Portable
code can run in other target device
19-Aug-2011
57
19
4. Better performance
C can be as fast as ASM
Franz Duran
58
Franz Duran
59
PIC16F84A
+5v supply
Oscillator circuit
Reset circuit
External peripherals
In-circuit serial
programming
connector
19-Aug-2011
Franz Duran
60
20
19-Aug-2011
Franz Duran
61
19-Aug-2011
Franz Duran
62
19-Aug-2011
Franz Duran
63
21
19-Aug-2011
Franz Duran
64
19-Aug-2011
Franz Duran
65
19-Aug-2011
Franz Duran
66
22
19-Aug-2011
Franz Duran
67
IO INTERFACING: LED
LED at RB0
RB0 is output
19-Aug-2011
Franz Duran
68
Franz Duran
69
LED
5mm LED
IF = 5 35 mA
VF = 2V
3mm LED
IF = 1 30 mA
VF = 2V
19-Aug-2011
23
LED
IF
+ VR=3V
VF =
2v
If IF = 10mA,
R = 3V/10mA
R = 300
19-Aug-2011
Franz Duran
70
LED
R should not be too large
LED will not turn on
19-Aug-2011
Franz Duran
71
preprocessor directive
main() function
initialization
program loop
- infinite loop
- super loop
}
}
19-Aug-2011
Franz Duran
72
24
IO PROGRAMMING
Input/Output port - group of 8 pins typical
PORTA 6 I/O pins
RA5, RA4, RA3, RA2, RA1, RA0
PORTA<5:0>
PORTB 8 I/O pins
RB7, RB6, RB5, RB4, RB3, RB2, RB1, RB0
PORTB<7:0>
19-Aug-2011
Franz Duran
73
IO PROGRAMMING
PORTC 8 I/O pins
RC7, RC6, RC5, RC4, RC3, RC2, RC1, RC0
PORTC<7:0>
PORTD 8 I/O pins
RD7, RD6, RD5, RD4, RD3, RD2, RD1, RD0
PORTD<7:0>
Franz Duran
74
IO PROGRAMMING
Special Function Registers for I/O
TRISA,
TRISB,
TRISC,
TRISD,
TRISE,
19-Aug-2011
PORTA
PORTB
PORTC
PORTD
PORTE
Franz Duran
75
25
IO PROGRAMMING
Consider PORTB..
19-Aug-2011
Franz Duran
76
IO PROGRAMMING: SFRs
PORTB port
controlled by 2 special function registers
1. TRISB register
PORTB Data Direction Register
8-bit
2. PORTB register
PORTB Data Latch Register
8-bit
19-Aug-2011
Franz Duran
77
IO PROGRAMMING: SFRs
TRISB
XXXXXXXX
.........
0 output
1 input
TRISB0
TRISB1
.
.
.
TRISB7
19-Aug-2011
Franz Duran
78
26
IO PROGRAMMING: SFRs
TRISB
X
X X X
X X
X
0
TRISB0
TRISB0 = 0;
//RB0 is output
RB0 pin is an
output pin
19-Aug-2011
Franz Duran
79
IO PROGRAMMING: SFRs
PORTB
XXXXXXXX
0 Logic 0
1 Logic 1
.........
RB0
RB1
.
.
.
RB7
19-Aug-2011
Franz Duran
80
IO PROGRAMMING: SFRs
PORTB
X
X X X
X X
X
1
RB0
RB0 = 1;
//LED on
RB0 outputs a
Logic 1 signal;
~5V
19-Aug-2011
Franz Duran
81
27
IO PROGRAMMING: SFRs
TRISB0 = 0;
RB0 = 1;
//RB0 is output
//LED is on
TRISB
X X X
X X
PORTB
X X X
X X
19-Aug-2011
Franz Duran
82
IO PROGRAMMING
EXERCISE:
Create new project
Led_demo_2
19-Aug-2011
Franz Duran
83
IO PROGRAMMING
EXERCISE: (Solution)
TRISB0 =
RB0 = 1;
TRISC3 =
RC3 = 0;
TRISD7 =
RD7 = 1;
ADCON1 =
TRISA1 =
RA1 = 1;
TRISE2 =
RE2 = 0;
19-Aug-2011
0;
//LED1 on
0;
//LED2 on
0;
//LED3 on
0x06;
0;
//LED4 on
0;
//LED5 on
Franz Duran
84
28
IO PROGRAMMING
EXERCISE:
Turn on all 8 LEDs connected to PORTD
19-Aug-2011
Franz Duran
85
IO PROGRAMMING
SOLUTION:
TRISD
PORTD
or
TRISD
PORTD
or
TRISD
PORTD
= 0b00000000;
= 0b11111111;
//binary notation
= 0x00;
= 0xFF;
//hexadecimal notation
= 0;
= 255;
//decimal notation
19-Aug-2011
Franz Duran
86
IO INTERFACING: Button
pushbutton
input device
Tack Switch
19-Aug-2011
Franz Duran
87
29
IO INTERFACING: Button
If button is not pressed
RB2
+5v
R
10k
RIN
V = 5v x 1M / (R+1M)
V 5v (Logic 1)
1M
19-Aug-2011
Franz Duran
88
IO INTERFACING: Button
If button is pressed
RB2
RIN
~1M
+
V = 0v (Logic 0)
19-Aug-2011
Franz Duran
89
IO INTERFACING: Button
void main()
{
TRISB0 = 0;
RB0 = 0;
TRISB2 = 1;
while(1)
{
if(RB2==0)
RB0 = 1;
else
RB0 = 0;
}
}
19-Aug-2011
Franz Duran
90
30
IO INTERFACING: Button
R should be large enough to limit the
current
when button is
pressed
19-Aug-2011
Franz Duran
91
IO INTERFACING: Button
R should be large enough to limit the
current
I < 25mA
is internally
connected
to ground)
19-Aug-2011
Franz Duran
92
BASICS OF C: #defines
#include <pic.h>
__CONFIG(HS & WDTDIS & PWRTDIS & UNPROTECT & LVPDIS);
#define
#define
#define
#define
#define
LED
BUTTON
ON
OFF
PRESSED
void main()
{
TRISB0 = 0;
LED = OFF;
TRISB2 = 1;
RB0
RB2
1
0
0
while(1)
{
if(BUTTON==PRESSED)
LED = ON;
else
LED = OFF;
}
19-Aug-2011
Franz Duran
93
31
BASICS OF C: Conditional
Statements
1. If()
If()-else()
If()-else-if()
2. switch()
19-Aug-2011
Franz Duran
94
BASICS OF C: IF Conditional
Statement
If()
Simplest conditional statement
if (condition)
statement1;
Ex.
if(var>99)
var=0;
19-Aug-2011
Franz Duran
95
BASICS OF C: IF Conditional
Statement
If()
use else clause (optional)
if (condition)
statement1;
else
statement2;
Ex.
if(BUTTON==PRESSED)
LED=1;
else
LED=0;
19-Aug-2011
Franz Duran
96
32
BASICS OF C: IF Conditional
Statement
If()-else-if()
if (condition1)
statement1;
else if (condition2)
statement2;
else if (condition3)
statement3;
else
statement4;
19-Aug-2011
Franz Duran
97
BASICS OF C: Switch()
switch()
Allow comparison of a single variable (or
expression) to multiple values
Code associate with the matching value is
executed
19-Aug-2011
Franz Duran
98
BASICS OF C: Switch()
var1 = get_input_from_user()
switch(var1)
{
case 0x00:
statement1;
break;
case 0x01:
statement2;
break;
case 0x02:
statement3;
break;
case 0x03:
statement4;
break;
default:
statement5;
break
}
19-Aug-2011
Franz Duran
99
33
BASICS OF C: Loops
Loops
used to repeatedly execute specific
statements
3 loop statements in C
1. for() loop
2. while() loop
3. do-while() loop
19-Aug-2011
Franz Duran
100
//a variable.
//RB0 pin is configured as an output
//LED is initially off
while(1)
{
RB0 = 1;
for(i=0;i<50000;i++)
{
//empty body
}
//LED is ON
//delay
RB0 = 0;
for(i=0;i<50000;i++)
{
//empty body
}
//LED is OFF
//delay
}
}
19-Aug-2011
Franz Duran
101
i=0
i++
i<50000?
TRUE
FALSE
exit
//codes
19-Aug-2011
Franz Duran
102
34
OR
for(i=50000;i>0;i--)
{
//codes here
}
19-Aug-2011
Franz Duran
103
start
FALSE
condition?
TRUE
exit
//codes
19-Aug-2011
Franz Duran
104
EXERCISE:
Modify previous example to used while()
loop
19-Aug-2011
Franz Duran
105
35
start
FALSE
condition?
TRUE
19-Aug-2011
Franz Duran
exit
106
BASICS OF C: Loops
Franz Duran
107
BASICS OF C: Variables
Variables
program data that varies during run-time
temporary data
placed in volatile memory
General Purpose Registers (GPR)
PIC16F877A
368 Bytes GPR
19-Aug-2011
Franz Duran
108
36
BASICS OF C: Variables
unsigned char var1;
var1 = 100;
var1 = 500;
var1 = -10;
signed
var2 =
var2 =
var2 =
char var2;
100;
-1000;
150;
19-Aug-2011
Franz Duran
109
BASICS OF C: Variables
bit
(1-bit)
(0 1)
char
(8-bit)
(-128 127)
unsigned char
(8-bit)
(0 255)
short
(16-bit) (-32768 32767)
unsigned short
(16-bit) (0 65535)
int
(16-bit)
(-32768 32767)
unsigned int
(16-bit) (0 65535)
short long
(24-bit)
(-8388608 8388607)
unsigned short long (24-bit) (0 16777215)
long
(32-bit) (21474833648 2147483647)
unsigned long
(32-bit) (0 4294967295)
float
(24-bit)
(1.17549435e-38 - 3.40277e+38)
double
(24-bit)
(1.17549435e-38 - 3.40277e+38 )
double
(32-bit)
(1.17549435e-38 - 3.40282347e+38 )
19-Aug-2011
Franz Duran
110
BASICS OF C: Operators
1. = (Assignment operator)
2.
3.
4.
5.
Mathematical operators
Relational operators
Logical operators
Bitwise operators
19-Aug-2011
Franz Duran
111
37
BASICS OF C: Operators
= (Assignment operator)
x = y;
//assign the value of y
//to the variable x
//a + b is an expression
Franz Duran
112
BASICS OF C: Operators
Mathematical operators
1.
2.
3.
4.
5.
6.
7.
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
++ (increment)
-- (decrement)
19-Aug-2011
ex. x + y
ex. x - y
ex. x * y
ex. x / y
ex. x % y
ex. x++, ++x
ex. x--, --x
Franz Duran
113
BASICS OF C: Operators
Relational Operators
1. ==
2. >
3. >=
4. <
5. <=
6. !=
19-Aug-2011
(equal to)
ex. x==y
(greater than)
ex. x>y
ex. x>=y
(lesser than)
ex. x<y
ex. x<=y
ex. x!=y
Franz Duran
114
38
BASICS OF C: Operators
Logical Operators
1. && (Logical AND)
2. || (Logical OR)
3. ! (Logical NOT)
19-Aug-2011
Franz Duran
115
BASICS OF C: Operators
EXERCISE:
Turn on LED1 if BUTTON1 or BUTTON2
is pressed
Modify:
Turn on LED1 if BUTTON1 and BUTTON2
are pressed
19-Aug-2011
Franz Duran
116
BASICS OF C: Operators
Bitwise Operators
1.
2.
3.
4.
5.
6.
19-Aug-2011
Franz Duran
117
39
19-Aug-2011
A& B
Franz Duran
118
19-Aug-2011
//clear RB0
//clear RB0
Franz Duran
119
=
=
=
=
=
0;
0;
0;
0;
0;
Franz Duran
120
40
BASICS OF C: | Operator
OR (|) operator truth table:
A
A| B
19-Aug-2011
Franz Duran
121
BASICS OF C: | Operator
Bitwise-OR operator
Example:
RB0 = RB0 | 1;
Equivalent to:
RB0 = 1;
19-Aug-2011
//set RB0
//set RB0
Franz Duran
122
BASICS OF C: Operators
Example:
Configure PORTD<6:5> and PORTD<2:1> as
input:
Initial solution
TRISD6=1;
TRISD5=1;
TRISD2=1;
TRISD1=1;
Franz Duran
123
41
BASICS OF C: Operators
Masking technique
To clear a bit (or bits), AND this bit with 0
To set a bit (or bits), OR this bit with 1
Ex:
PORTB &= 0b10110000;
TRISD |= 0x66;
Mask values
19-Aug-2011
Franz Duran
124
BASICS OF C: ^ Operator
XOR (^) operator truth table:
A
A| B
19-Aug-2011
Franz Duran
125
BASICS OF C: ^ Operator
Bitwise-XOR operator
Toggle operator
Example:
RB0 = RB0 ^ 1;
//toggle RB0
Equivalent to:
RB0 = ~RB0;
19-Aug-2011
//toggle RB0
Franz Duran
126
42
BASICS OF C: ^ Operator
EXERCISE:
Create a LED blinker application using ^
operator
Toggle two LEDs alternately every 500ms
19-Aug-2011
Franz Duran
127
19-Aug-2011
Franz Duran
128
43