Why program the PIC18 in C? • C programming is easier and less time consuming • C is easier to modify and update • Existing libraries available e!g!" delays# • C code is portable po rtable to other microcontroller $ith little or no modification • We $ill be using %icrochip C18 compiler 2
Why program the PIC18 in C? • C programming is easier and less time consuming • C is easier to modify and update • Existing libraries available e!g!" delays# • C code is portable po rtable to other microcontroller $ith little or no modification • We $ill be using %icrochip C18 compiler 2
Example & 'elay Program in (ab 1
%emory )sage *auge
3
+ame Program Coded in C
%emory )sage *auge
4
'isadvantages of C • ,he code produced is less space-efficient and runs more slo$ly than native assembly code! • . compiler is much more expensive than an assembler!
Image courtesy of S. Katzen, The essential PIC18 Microcontroller, Springer
5
'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 )##*#
$signe% int
1)
&*"()8 to *"()(
6
'ata ,ypes" +i/es and 0anges char •ecause PIC18 is an 8-bit microcontroller" char data data type is most commonly used! •C compliers use the signed type as default unless the 2ualifier unsigned is put in front!
int int variables variables are stored in t$o 8-bit •
registers! •'on3t use int unless $e have to! If one 8bit register is enough to store a variable" $e don3t $ant to use 4 registers! 7
'ata %emory .llocation in C18 • i 6 d3177773 6 7x417 int5 4 bytes# • i located in data memory address 78.578! 78 and 78. store the high and lo$ bytes respectively! • ,he array mynum is stored in 78C578 • :ere" the complier controls $here to put the variables!
9
'ata %emory allocation in C18 • . programmer can control $here to put variables! • )se #pragma idata =
before the declaration! • e!g!"
#pragma idata i = 0x150 unsigned int i = 10000; 10
'ata %emory allocation in C18
11
Program %emory .llocation in C18 • ,o $rite fixed constant# data to program memory" the ;ey$ord rom is used5 rom const int i = 10000; rom const char mnum!" = 01$%&5'()*+
12
Program %emory .llocation in C18
i mynum 13
Putting Data in +pecific data memory .ddress • ,o place data in in a specific program memory address" use5 #pragma romdata mdata = 0x$00 rom const char mdata!" = 01$%&5'()*+
14
Putting Data in in +pecific data memory .ddress
15
Writing code in a specific program memory address • ,o place code in in a specific program memory address" use5
Well" $hy do I $ant to $rite code in a specific program memory location? • 0emember this? • We have used polling to create a s2uare $ave of 0,0, 7x7
+implicity of C5 .n Example (ab 4 ,as; 4 ,as;5 'isplay your group number of the digit -segment (E'! 0emember ho$ much $or; you have done to ma;e it $or; in assembly? Fery simple coding in C! 29
Thank you for interesting in our services. We are a non-profit group that run this website to share documents. We need your help to maintenance this website.