• No results found

Chapter 9 Intro to C(1)

N/A
N/A
Protected

Academic year: 2021

Share "Chapter 9 Intro to C(1)"

Copied!
30
0
0

Loading.... (view fulltext now)

Full text

(1)

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

(2)

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

(3)

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

(4)

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

(5)

+ame Program Coded in C

+ame Program Coded in C

%emory )sage *auge %emory )sage *auge

(6)

'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 

(7)

'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 )##*#

(8)

'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!

(9)

'ata %emory .llocation in C18

'ata %emory .llocation in C18

(10)

'ata %emory .llocation in C18

'ata %emory .llocation in C18

• i 6 d3177773 6 7x417 int5 4 bytes#i 6 d3177773 6 7x417 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!

(11)

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

(12)

'ata %emory allocation in C18

'ata %emory allocation in C18

(13)

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 used5

used5

rom const int i = 10000; rom const int i = 10000;

rom const char mnum!" = 01$%&5'()*+ rom const char mnum!" = 01$%&5'()*+

(14)

Program %emory .llocation in C18

Program %emory .llocation in C18

(15)

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 mdata = 0x$00 #pragma romdata mdata = 0x$00

rom const char mdata!" = 01$%&5'()*+ rom const char mdata!" = 01$%&5'()*+

(16)

Putting

Putting DataData in +pecific data memoryin +pecific data memory

.ddress .ddress

(17)

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.

/ 

/ 

(18)

Writing code in a specific program memory Writing code in a specific program memory

address address

(19)

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#!

(20)

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 8FC; ,isable 8nterrupt rioritiesRB2C 8FC; ,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

(21)

Interrupt Example 0evisited

Interrupt Example 0evisited

2R3 0H0$00 2R3 0H0$00 60789R: 60789R:  btss 8C6B2C 6MR08D btss 8C6B2C 6MR08D  bra FH86  bra FH86  bc 8C6B2C 6MR08D  bc 8C6B2C 6MR08D

 btg 2R6 5; toggle ort5 to create sI ?ave  btg 2R6 5; toggle ort5 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: retieretie FC, FC,

(22)

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$0h>  #include <p1)D&5$0h> 

#pragma conig 29B = 9 K,6 = 2DD EL = 2DD #pragma conig 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. /

/

6R89bitsR5 = 0;

6R89bitsR5 = 0; 44set R5 output44set R5 output 2R6bitsR5 = 0;

2R6bitsR5 = 0; 60B2C = 0x0);

60B2C = 0x0); 44 6imer0 1'@bit no prescale internal c44 6imer0 1'@bit no prescale internal c 6MR0 = 0xDD;

6MR0 = 0xDD; 6MR0E = 0xD$; 6MR0E = 0xD$;

RB2Cbits8FC = 0;

RB2Cbits8FC = 0; 44disable priorit levels44disable priorit levels 8C6B2Cbits6MR08D = 0; 8C6B2Cbits6MR08D = 0; 8C6B2Cbits6MR08F = 1; 8C6B2Cbits6MR08F = 1; 8C6B2Cbits38F = 1; 8C6B2Cbits38F = 1; 60B2Cbits6MR02C = 1;

(23)

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 -8C6B2Cbits6MR08D. i -8C6B2Cbits6MR08D. / / 8C6B2Cbits6MR08D=0; 8C6B2Cbits6MR08D=0; 2R6bitsR5 = 2R6bitsR5;44toggle 2R6bitsR5 = 2R6bitsR5;44toggle ort5 to create sI ?ave

ort5 to create sI ?ave 6MR0 = 0xDD; 6MR0 = 0xDD; 6MR0E = 0xD$; 6MR0E = 0xD$;    

(24)

)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! ,elaEoop:

,elaEoop: decsJdecsJ ,FEAN7E,FEAN7E bra ,elaEoop  bra ,elaEoop decsJ ,FEAN7 decsJ ,FEAN7 bra ,elaEoop  bra ,elaEoop

(25)

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

(26)

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 theyte 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

(27)

IB> Programming in C

IB> Programming in C

(28)

IB> Programming in C

IB> Programming in C

• it-addressable IB> programming5 Changeit-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

2R6bitsR5 = 0

2R6bitsR5 = 0 in Cin C

• e!g!"e!g!" bc 6R89 5 bc 6R89 5 is expressed asis expressed as

6R89bits6R895 = 0

(29)

(ogic >perations in C

(ogic >perations in C

it-$ise operators5 it-$ise operators5 1! 1!.A' #.A' #

 + Extract lo$er nibble5 P>0,  7x7Extract lo$er nibble5 P>0,  7x7

4!

4!>0 D#>0 D#

 + e!g!" +PIF.()E 6 7xG7 D +PI:Ie!g!" +PIF.()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#

(30)

+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!

References

Related documents

Although many open questions remain regarding the mechanistic details and connections of the different pathways, DRG1 was discovered to be involved in chromatin

To maximise the use of scientific evidence in health and public health policy, researchers should recognise the tendency of policymakers to base judgements on their beliefs,

Builds connecting walkway between mobile unit or portable showers and work area, using hand tools, lumber, nails, plastic sheeting, and duct tape.. Positions portable

Dissemination is rated as present but not adequate because of the following: the last time that an annual summary of health service statistics was published with

But before the full implementation of OBE and finalization of the program educational objectives, student outcomes, curriculum content and intended learning outcomes, the College

Though all data are from the same strip (RSP422) and carefully calibrated and intercalibrated (between the years 2007, 2008 and 2009) and, as a result, the forest levels seem to

As with Moore, Prichard takes it to follow from Kant’s account of the human cognitive mind that the objects of perception and knowledge are constituted by a subject’s