Chapter 9 Introduction to
Chapter 9 Introduction to
Chapter 9 Introduction to
Chapter 9 Introduction to
PIC18 C
PIC18 C
PIC18 C
PIC18 C
Programming
Programming
Programming
Why program the PIC18 in C?
Why program the PIC18 in C?
Why program the PIC18 in C?
Why program the PIC18 in C?
•
• C programming is easier and less timeC programming is easier and less timeC programming is easier and less timeC programming is easier and less time
consuming consuming consuming
consuming
•
• C is easier to modify and updateC is easier to modify and updateC is easier to modify and updateC is easier to modify and update
•
• Existing libraries available e!g!" delays#Existing libraries available e!g!" delays#Existing libraries available e!g!" delays#Existing libraries available e!g!" delays#
•
• C code is poC code is poC code is portable to otherC code is portable to otherrtable to otherrtable to other
microcontroller $ith little or no microcontroller $ith little or no microcontroller $ith little or no
microcontroller $ith little or no
modification modification modification
modification
•
Why program the PIC18 in C?
Why program the PIC18 in C?
Why program the PIC18 in C?
Why program the PIC18 in C?
•
• C programming is easier and less timeC programming is easier and less timeC programming is easier and less timeC programming is easier and less time
consuming consuming consuming
consuming
•
• C is easier to modify and updateC is easier to modify and updateC is easier to modify and updateC is easier to modify and update
•
• Existing libraries available e!g!" delays#Existing libraries available e!g!" delays#Existing libraries available e!g!" delays#Existing libraries available e!g!" delays#
•
• C code is poC code is poC code is portable to otherC code is portable to otherrtable to otherrtable to other
microcontroller $ith little or no microcontroller $ith little or no microcontroller $ith little or no
microcontroller $ith little or no
modification modification modification
modification
•
Example & 'elay Program in (ab 1
Example & 'elay Program in (ab 1
Example & 'elay Program in (ab 1
Example & 'elay Program in (ab 1
%emory )sage *auge %emory )sage *auge %emory )sage *auge
+ame Program Coded in C
+ame Program Coded in C
%emory )sage *auge %emory )sage *auge
'isadvantages of C
'isadvantages of C
• ,he code produced is less space-efficient,he code produced is less space-efficient and runs more slo$ly than native assembly and runs more slo$ly than native assembly
code! code!
• . compiler is much more expensive than an. compiler is much more expensive than an assembler!
assembler!
Image courtesy of S. Katzen, The essential PIC18 Microcontroller, Springer
'ata ,ypes" +i/es and 0anges
'ata ,ypes" +i/es and 0anges
'ata ,ype +i/e in its 'ata 0ange unsigne char 8 ! to "##
$signe% char 8 &1"8 to '1"( unsigne int 1) ! to )##*#
'ata ,ypes" +i/es and 0anges
'ata ,ypes" +i/es and 0anges
char char
•ecause PIC18 is an 8-bit microcontroller"ecause PIC18 is an 8-bit microcontroller"
char
char data type is most commonly used!data type is most commonly used!
•C compliers use the signed type as defaultC compliers use the signed type as default unless the 2ualifier unsigned is put in front! unless the 2ualifier unsigned is put in front!
int int
•intint variables are stored in t$o 8-bitvariables are stored in t$o 8-bit
registers! registers!
•'on3t use'on3t use intint unless $e have to! If one 8-unless $e have to! If one 8-bit register is enough to store a variable" $e bit register is enough to store a variable" $e
don3t $ant to use 4 registers! don3t $ant to use 4 registers!
'ata %emory .llocation in C18
'ata %emory .llocation in C18
'ata %emory .llocation in C18
'ata %emory .llocation in C18
• i 6 d3177773 6 7x417 int5 4 bytes#i 6 d3177773 6 7x417 int5 4 bytes#
• i located in data memory address 78.578!i located in data memory address 78.578! 78 and 78. store the high and lo$ bytes 78 and 78. store the high and lo$ bytes
respectively! respectively!
• ,he array mynum is stored in 78C578,he array mynum is stored in 78C578 • :ere" the complier controls $here to put:ere" the complier controls $here to put
the variables! the variables!
'ata %emory allocation in C18
'ata %emory allocation in C18
• . programmer can control $here to put. programmer can control $here to put variables!
variables! • )se)se
#pragma idata <variable> = <RAM addr> #pragma idata <variable> = <RAM addr>
before the declaration! before the declaration! • e!g!"e!g!"
#pragma idata i = 0x150
#pragma idata i = 0x150
unsigned int i = 10000;
unsigned int i = 10000;
'ata %emory allocation in C18
'ata %emory allocation in C18
Program %emory .llocation in C18
Program %emory .llocation in C18
• ,o $rite fixed constant# data to,o $rite fixed constant# data to program memory" the ;ey$ord
program memory" the ;ey$ord
rom
rom
isis used5used5
rom const int i = 10000; rom const int i = 10000;
rom const char mnum!" = 01$%&5'()*+ rom const char mnum!" = 01$%&5'()*+
Program %emory .llocation in C18
Program %emory .llocation in C18
Putting
Putting DataData in +pecific data memoryin +pecific data memory
.ddress .ddress
• ,o place,o place datadata in a specific programin a specific program
memory address" use5 memory address" use5
#pragma romdata mdata = 0x$00 #pragma romdata mdata = 0x$00
rom const char mdata!" = 01$%&5'()*+ rom const char mdata!" = 01$%&5'()*+
Putting
Putting DataData in +pecific data memoryin +pecific data memory
.ddress .ddress
Writing code in a specific program memory Writing code in a specific program memory
address address
• ,o place,o place codecode in a specific programin a specific program
memory address" use5 memory address" use5
#pragma code ,ela = 0x%00
#pragma code ,ela = 0x%00
void ,ela-int cnt.
void ,ela-int cnt.
/
/
Writing code in a specific program memory Writing code in a specific program memory
address address
Well" $hy do I $ant to $rite code in a Well" $hy do I $ant to $rite code in a
specific program memory location? specific program memory location?
• 0emember this?0emember this?
• We have used polling to create a s2uareWe have used polling to create a s2uare $ave of <7= duty cycle on P>0,<@ $ave of <7= duty cycle on P>0,<@
bit! bit!
• In this example" $e implement the sameIn this example" $e implement the same thing using interrupt $ith interrupt
thing using interrupt $ith interrupt priorities disabled IPEA 6 7#!
Interrupt Example 0evisited
Interrupt Example 0evisited
2R3 0x0000 2R3 0x0000 goto Main goto Main
2R3 0x000) 44 igh priorit interrupt starts at 0x0) 2R3 0x000) 44 igh priorit interrupt starts at 0x0) goto 60789R goto 60789R 2R3 0x0100 2R3 0x0100 Main: Main: bc 6R89 5 bc 6R89 5 bc 2R6 5 bc 2R6 5
movl? 0x0); 6imer0 1'@bit no prescale internal c movl? 0x0); 6imer0 1'@bit no prescale internal c mov? 60B2C mov? 60B2C movl? 0xDD movl? 0xDD mov? 6MR0 mov? 6MR0 movl? 0xD$ movl? 0xD$
mov? 6MR0E; load DDD$ to 6MR0 mov? 6MR0E; load DDD$ to 6MR0 bc
bc RB2C 8FC; ,isable 8nterrupt rioritiesRB2C 8FC; ,isable 8nterrupt riorities bc
bc 8C6B2C 6MR08D; Blear interrupt lag bit8C6B2C 6MR08D; Blear interrupt lag bit bs
bs 8C6B2C 6MR08F; enable 6imer0 interrupt8C6B2C 6MR08F; enable 6imer0 interrupt bs
Interrupt Example 0evisited
Interrupt Example 0evisited
2R3 0H0$00 2R3 0H0$00 60789R: 60789R: btss 8C6B2C 6MR08D btss 8C6B2C 6MR08D bra FH86 bra FH86 bc 8C6B2C 6MR08D bc 8C6B2C 6MR08D
btg 2R6 5; toggle ort5 to create sI ?ave btg 2R6 5; toggle ort5 to create sI ?ave movl? 0xDD movl? 0xDD mov? 6MR0 mov? 6MR0 movl? 0xD$ movl? 0xD$
mov? 6MR0E; ReinitialiJe 6MR0 to DDD$ mov? 6MR0E; ReinitialiJe 6MR0 to DDD$ FH86:
FH86: retieretie FC, FC,
We have to $rite I+0 starting 7x78! :o$ We have to $rite I+0 starting 7x78! :o$
did $e do it in C? did $e do it in C?
#include <p1)D&5$0h> #include <p1)D&5$0h>
#pragma conig 29B = 9 K,6 = 2DD EL = 2DD #pragma conig 29B = 9 K,6 = 2DD EL = 2DD void timer7isr7internal-void.;
void timer7isr7internal-void.;
44@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 44@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ #pragma code timer7isr = 0x0) 44 9tore the belo? code at address 0x0) #pragma code timer7isr = 0x0) 44 9tore the belo? code at address 0x0) void timer7isr-void.
void timer7isr-void. /
/
7asm 3262 timer7isr7internal 7endasm 7asm 3262 timer7isr7internal 7endasm 44@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 44@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ #pragma code #pragma code void main -void. void main -void. /
/
6R89bitsR5 = 0;
6R89bitsR5 = 0; 44set R5 output44set R5 output 2R6bitsR5 = 0;
2R6bitsR5 = 0; 60B2C = 0x0);
60B2C = 0x0); 44 6imer0 1'@bit no prescale internal c44 6imer0 1'@bit no prescale internal c 6MR0 = 0xDD;
6MR0 = 0xDD; 6MR0E = 0xD$; 6MR0E = 0xD$;
RB2Cbits8FC = 0;
RB2Cbits8FC = 0; 44disable priorit levels44disable priorit levels 8C6B2Cbits6MR08D = 0; 8C6B2Cbits6MR08D = 0; 8C6B2Cbits6MR08F = 1; 8C6B2Cbits6MR08F = 1; 8C6B2Cbits38F = 1; 8C6B2Cbits38F = 1; 60B2Cbits6MR02C = 1;
We have to $rite I+0 starting 7x78!
We have to $rite I+0 starting 7x78!
:o$ did $e do it in C?
:o$ did $e do it in C?
#pragma interrupt timer7isr7internal #pragma interrupt timer7isr7internal void timer7isr7internal -void.
void timer7isr7internal -void. / / i -8C6B2Cbits6MR08D. i -8C6B2Cbits6MR08D. / / 8C6B2Cbits6MR08D=0; 8C6B2Cbits6MR08D=0; 2R6bitsR5 = 2R6bitsR5;44toggle 2R6bitsR5 = 2R6bitsR5;44toggle ort5 to create sI ?ave
ort5 to create sI ?ave 6MR0 = 0xDD; 6MR0 = 0xDD; 6MR0E = 0xD$; 6MR0E = 0xD$;
)se of C to generate time delay
)se of C to generate time delay
• )sing assembly language" $e can)sing assembly language" $e can
control the exact instructions executed control the exact instructions executed
in a time delay subroutine and thus be in a time delay subroutine and thus be
able to control the exact time delay! able to control the exact time delay! ,elaEoop:
,elaEoop: decsJdecsJ ,FEAN7E,FEAN7E bra ,elaEoop bra ,elaEoop decsJ ,FEAN7 decsJ ,FEAN7 bra ,elaEoop bra ,elaEoop
)se of C to generate time delay
)se of C to generate time delay
• C compliers convert C statements to assemblyC compliers convert C statements to assembly instructions!
instructions!
• 'ifferent compliers produce assembly code of'ifferent compliers produce assembly code of different
different lengthlength!!
• ,he actual time delay generated by the follo$ing,he actual time delay generated by the follo$ing function depends on the complier used!
function depends on the complier used!
• Aeed to measure the exact time delay usingAeed to measure the exact time delay using
void ,ela /
unsigned int i;
or-i=0; i<10000; iOO.; return;
IB> Programming in C
IB> Programming in C
• 0ecall5 IB> Programming involves0ecall5 IB> Programming involves P>0,x and ,0I+x registers!
P>0,x and ,0I+x registers!
• yte IB> Programming5 Change theyte IB> Programming5 Change the
$hole byte stored in P>0,x or ,0I+x! $hole byte stored in P>0,x or ,0I+x! • e!g!"e!g!"
2R6 = 0x1)
2R6 = 0x1)
6R89 = 0H$0
6R89 = 0H$0
IB> Programming in C
IB> Programming in C
IB> Programming in C
IB> Programming in C
• it-addressable IB> programming5 Changeit-addressable IB> programming5 Change a single bit $ithout disturbing the rest of a single bit $ithout disturbing the rest of
the P>0,x or ,0I+x registers! the P>0,x or ,0I+x registers!
• P>0,bits!0 6 P>0,bits!0 6 thth bit of P>0,bit of P>0,
• ,0I+bits!,0I+ 6 ,0I+bits!,0I+ 6 thth bit of ,0I+bit of ,0I+
• +ame function as bcf or bsf in assembly+ame function as bcf or bsf in assembly language
language
• e!g!"e!g!" bc 2R6 5 bc 2R6 5 is expressed asis expressed as
2R6bitsR5 = 0
2R6bitsR5 = 0 in Cin C
• e!g!"e!g!" bc 6R89 5 bc 6R89 5 is expressed asis expressed as
6R89bits6R895 = 0
(ogic >perations in C
(ogic >perations in C
it-$ise operators5 it-$ise operators5 1! 1!.A' #.A' #+ Extract lo$er nibble5 P>0, 7x7Extract lo$er nibble5 P>0, 7x7
4!
4!>0 D#>0 D#
+ e!g!" +PIF.()E 6 7xG7 D +PI:Ie!g!" +PIF.()E 6 7xG7 D +PI:I
G!
G!Exclusive >0 H#5 1H1 6 7Exclusive >0 H#5 1H1 6 7 !
!Inverter J#Inverter J#
+implicity of C5 .n Example
+implicity of C5 .n Example
(ab 4 ,as; 4 (ab 4 ,as; 4
,as;5 'isplay your group number of the ,as;5 'isplay your group number of the
-digit -segment (E'! digit -segment (E'!
0emember ho$ much 0emember ho$ much
$or; you have done to $or; you have done to
ma;e it $or; in ma;e it $or; in
assembly? assembly?
Fery simple coding in C! Fery simple coding in C!