Chapter 9 Intro to C(1)

Share Embed Donate


Short Description

2004...

Description

Chapter 9 Introduction to PIC18 C Programming

1

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 • 'eclared t$o variables5 i" mynum!

8

'ata %emory .llocation in C18 • i 6 d3177773 6 7x417 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 mnum!" = 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 mdata = 0x$00 rom const char mdata!" = 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

#pragma code ,ela = 0x%00 void ,ela-int cnt. /   16

Writing code in a specific program memory address

17

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

4!>0 D#  + e!g!" +PIF.()E 6 7xG7 D +PI:I

G!Exclusive >0 H#5 1H1 6 7 !Inverter J#  + e!g!" ,oggle P>0,5 2R6 = 2R6 28

+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

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF