Sie sind auf Seite 1von 41

Peter Nauth

Microcontroller Technology

Version 240512

Peter Nauth

Microcontroller Technology

Basics in Digital Technology

Signed and unsigned numbers

Unsigned Numbers cover positive values,only Signed Numbers: Negative values are chracteri ed by an MS! " 1 #$amples unsigned : 1%%%1%%1b " 1&' *i++erence: 1,, ( ,' !it-ise *i++erence 1%%11%11 ( %%111%%1 %11%%%1% Sum o+ Second./omplement 1,, ( ,' )1 1%%11%11 0 11%%%111 1 %11%%%1% signed: ( %111%111b " ( 11)

Seite 2 von 41

Peter Nauth

Microcontroller Technology

Charac Hex NU2 S67 ST: #:T #6T #N> 9/@ !#2 !S 79T 2B KT BB /M S6 SG *2# */1 */2 */& */4 N9@ SQN #T! /9N #M SU! #S/ BS DS MS US % 1 2 & 4 , 4 ' 1 ) 9 ! / * # B 1% 11 12 1& 14 1, 14 1' 11 1) 19 1! 1/ 1* 1# 1B

Dec % 1 2 & 4 , 4 ' 1 ) 1% 11 12 1& 14 1, 14 1' 11 1) 2% 21 22 2& 24 2, 24 2' 21 2) &% &1

Charac Hex 2% 8 ; < = ? A C E F H 0 , . N O % 1 2 & 4 , 4 ' 1 ) : T W " \ _ 21 22 2& 24 2, 24 2' 21 2) 29 2! 2/ 2* 2# 2B &% &1 &2 && &4 &, &4 &' &1 &) &9 &! &/ &* &# &B

Dec &2 && &4 &, &4 &' &1 &) 4% 41 42 4& 44 4, 44 4' 41 4) ,% ,1 ,2 ,& ,4 ,, ,4 ,' ,1 ,) 4% 41 42 4&

Charac Hex 3 9 ! / * # B D 7 G I @ 2 M N 6 P > M S T U K R : Q S U X Z ] ` 4% 41 42 4& 44 4, 44 4' 41 4) 49 4! 4/ 4* 4# 4B ,% ,1 ,2 ,& ,4 ,, ,4 ,' ,1 ,) ,9 ,! ,/ ,* ,# ,B

Dec 44 4, 44 4' 41 4) '% '1 '2 '& '4 ', '4 '' '1 ') 1% 11 12 1& 14 1, 14 1' 11 1) )% )1 )2 )& )4 ),

Charac Hex 5 a b c d e + g h i J L l m n o p P r s t u v $ y V Y [ ^ 4% 41 42 4& 44 4, 44 4' 41 4) 49 4! 4/ 4* 4# 4B '% '1 '2 '& '4 ', '4 '' '1 ') '9 '! '/ '* '# 'B

Dec )4 )' )1 )) 1%% 1%1 1%2 1%& 1%4 1%, 1%4 1%' 1%1 1%) 11% 111 112 11& 114 11, 114 11' 111 11) 12% 121 122 12& 124 12, 124 12'

9S/GG./ode

/onversion o+ !inary Numbers to 7e$ Numbers by the rule: 4 binary digits correspond to 1 he$adecimal digit #$ample: %1111111b " Bh

Seite & von 41

Peter Nauth

Microcontroller Technology

Basics in Microcontroller Technology

9nalog andO or digital electronic circuit

*ata Gnput

*ata Processing according to a +i$ed -ired procedure

*ata 6utput

*ata processing -ith electronic circuits cannot be adapted to ne- procedures -ithout changing the circuit

Program

Microcontroller

*ata Gnput

*ata Processing according to a +le$ible and adptable program

*ata 6utput

*ata processing -ith microcontroller can easily be adapted by changing the program -hich contains the data processing procedure

7igh 2evel 2anguage Source /ode

/ompiler

9ssembly /ode

9ssembly /ompiler

6bJect code Econsisting o+ instruction bytes: operators and operandsF is stored in the controller to be e$ecuted

Program code levels

Seite 4 von 41

Peter Nauth

Microcontroller Technology

/locL

*ata Gnput O 6utput

aP E/PUF

Memory
*ata !us 9dress !us /ontrol !us

GO6

System !us

!locL *iagram o+ a MicrocomputerN The Microprocessor EaP, /PUF reads the Gnstruction !ytes o+ the he$code Emachine codeF +rom the MemoryN 9 Microcomputer on a single chip is called a Microcontroller

6perator !yte

/ontrol Unit
Control Bus

6perand !yte

6ther Megisters 92U 9LLu

00 Programm /ounter 9ddress o+ ne$t Gnstruction !yte Data Bus A ress Bus

/omponents o+ a Microprocessor EaP, /PUF

Seite , von 41

Peter Nauth

Microcontroller Technology

Microprocessor selects address o+ ne$t instruction byte and puts it on address bus

Microprocessor activates the control signal PS#N -hich is connected -ith the control input 6# Eoutput enableF o+ the program memory

Program memory puts instruction byte -hich is stored on the selected address to the data bus

Microprocessor reads instruction byte +rom data bus

Microprocesser decodes instruction byte and e$ecutes it

Gnstruction Processing

!nstruction rate Gp UMGPSZ "


1 T p [ s ]

1%,&,:

MGPS

E12

M7

/locLF

:/111: 12 MGPS E24 M7 /locLF indicates, ho- many 10 6 9ssembly Gnstructions the processor can e$ecute in 1 s ETp: Machine /ycle EM/F TimeFN C!"C : #!"C : /omple$ Gnstruction Set /omputer EeNgN 1%,&,, :/111F Meduced Gnstruction Set /omputer
Seite 4 von 41

Peter Nauth

Microcontroller Technology

Design o$ Microcontroller "yste%s &ith the "AB '0515( 5)5

Pin connections o+ the Micro./ontroller 1%,1,O ,&,

*erivate o+ the 1%,1 Micro./ontroller +amily 1.!it /ontroller, /locL ma$N 14 M7 , Gp " 1 MGPS at12 M7 ' Ports available +or GO6 o+ *igital *ata EP% ( P4F or 9nalog signals EP4F & Timer O /ounter ' e$ternal Gnterrupt Gnputs Ethrough the pins o+ Ports P1 und P&F 1 RorLing Megister EM% ( M'F at 4 Megister banLs Gnternal *ata Memory: 2,4 !yte, optional: #$ternal *ata Memory: 44 L!yte #$ternal Program Memory: 44 L!yte
Seite ' von 41

Peter Nauth

Microcontroller Technology

Connection o$ external %e%ory to '05)5 an *C''' Controller


Microcontroller

Microprocessor aP

Gnternal *ata Memory 2,4 !yte

P& RM M*

PS#N

P%

92#

P2

#$ternal Program Memory 44 L!yte

6# *! *ata !us 9*

2 a t c h 9ddress !us

R# 6# #$ternal *ata Memory 44 L!yte *!

9*

Seite 1 von 41

Peter Nauth

Microcontroller Technology

Ti%ing Diagra% $or external Progra% Me%ory Access +'05)5,


Machine /ycle Maschinenzyklus

States Phasen

S1 S2 S3 S4 S5 S6 S1 S2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2 P1 P2

ALE

PSEN

P0

Adr L !"

Adr L !"

Adr L !"

P2

Adr #i$h"

Adr #i$h"

Timing Diagramm

E:" a/ reads instruction byte via P%F

!nternal Data Me%ory 9ddress


BBh

*ata
"0ecial12unction1#egister +"2#,/ . 9., !.Megister . Ports P% NNN P,

StacL: Stores re.entry address and data 2GB6, de+ault adress is %1h .\ StacLpointer SP points at adrN %'h Rrite: Gncrement Mead: *ecrement /hange *e+ault: M6K SP, <ne-`adr

1%h

. PSR, *PTM Not use -y controller

2%h %%h

#egister -an.s/ 4 $ M% NNN M'

M% b M' W.\ addresses %%h b %'h Ede+aultF

Gnternal *ata Memory

Seite ) von 41

Peter Nauth

Microcontroller Technology

#egister P, P,`*GM ! P4 P4`*GM 9// P4 *9PM 9**9T 9*/6N PSR T72 T22 /M/7 /M/2 T2/6N //7& //2& //7& //22 //71 //21 //#N GM/6N GP1 G#N1 P& GP% G#N% P2 S!UB S/6N P1 T71 T7% T21 T2% TM6* T/6N P/6N *P7 *P2 SP P%

A resse E:/111: )&F B% E:/111: /)F #% *! *9 *) *1 *% /* // /! /9 /1 /' /4 /, /4 /& /2 /1 /% !) !1 !% 9) 91 9% )) )1 )% 1* 1/ 1! 19 1) 11 1' 1& 12 11 1%

Bita r3 Port , !.Megister Port 4 9.Megister Port 4 9*/ 9*/ *ata Qes Qes

2unction

De$ault BB %% BB %% BB %% %% %% %% %% %% %% %% %% %% %% %% %% %% %% %% %% %% %% BB %% %% BB Unde+iniert %% BB %% %% %% %% %% %% %% %% %% %' BB

B1 E:/111: )2F Qes Qes

#1 E:/111: /1F Qes Qes

9*/ /ontrol Programm Status Rort Timer 2 7igh.!yte Timer 2 2o-.!yte /M/ 7igh.!yte /M/ 2o-.!yte

Qes

Timer 2 /ontrol /ompareO/apture 7igh.!yte /ompareO/apture 2o-.!yte /ompareO/apture 7igh.!yte /ompareO/apture 2o-.!yte /ompareO/apture 7igh.!yte /ompareO/apture 2o-.!yte /ompareO/apture #nable.!yte

Qes Qes Qes Qes Qes Qes Qes

Gnterrupt.MePuest./ontrol Gnterrupt.Priority.Megister 1 Gnterrupt.#nable.Megister 1 Port & Gnterrupt.Priority.Megister % Gnterrupt.#nable.Megister % Port 2 Serieller Port !u++er Serieller Port /ontrol Port 1 Timer 1 7igh.!yte Timer % 7igh.!yte Timer 1 2o-.!yte Timer % 2o-.!yte Timer #instellung

Qes

Timer /ontrol Po-er /ontrol *ata Pointer E*PTMF 7igh.!yte *ata Pointer E*PTMF 2o-.!yte StacL Pointer

Qes

Port %

Special Bunction Megister o+ 1%,&,


Seite 1% von 41

Peter Nauth

Microcontroller Technology

P"4 +Progra% "tatus 4ort,


!itnumber Kalue !it ' 2' !it 4 !it , !it 4 !it & !it 2 !it 1 !it % 24 9/ 2, B% 24 MS1 2& MS% 22 6K 21 B1 2% P

SymbN !itadrN /Q

PSRN' PSRN4 PSRN, PSRN4 PSRN& PSRN2 PSRN1 PSRN% PhysN !itadrN %*'h %*4h %*,h %*4h %*&h %*2h %*1h %*%h C5.!it The /arry.Blag indicates an over+lo- +or unsigned numbersN Gt is set Eto 1F, i+ the calculation o+ a sum or a di++erence e$ceeds the limit % % 2,, E%%h % BBhF has been e$ceededN 9/.!it The 9u$illary./arry.Blag is set i+ the calculation o+ a sum or di++erence an over+lo+rom !it & and !it 4 or vice versa occurredN Gt is used by the instruction *9 9 +or a !/* correctionN B6 % % 1 1 % 1 % 1 The Blag % can be used by the programmerN Megister !anL % activated, adresses %%h . %'h o+ internal *ata Memory Megister !anL 1 activated, adresses %1h . %Bh o+ internal *ata Memory Megister !anL 2 activated, adresses 1%h . 1'h o+ internal *ata Memory Megister !anL & activated, adresses 11h . 1Bh o+ internal *ata Memory #"1 #"0 Megister !anL Select /ontrol !its +or addresses o+ M% ( M'

6K The 6ver+lo-.Blag indicates an over+lo- +or signed numbersN Gt is set Eto 1F, i+ the calculation o+ a sum or a di++erence e$ceeds the limit 12' % .121 E'Bh%1%hF has been e$ceededN B1 P The Blag % can be used by the programmerN The Parity.Blag is set Eto 1F i+ the sum o+ the bits % . ' o+ the 9.Megister is odd

Seite 11 von 41

Peter Nauth

Microcontroller Technology

"2#
P"4

Bit 6 Bit 7 Bit 5 Bit 4 Bit ) Bit 2 Bit 1 Bit 0


/2@ 9/ G&BM TB2 SR*T RM R*T SM1 /2@6 /OT TM1 P*S 9*#: B% G2BM G#:4 #:4 T1 #T2 SM2 T2#: M1 TB% G*2S !SQ MS1 T2M1 G#:, #:, T% #S M#N GNT2 M% TM% . 9*M MS% T2M% G#:& #:4 GNT1 #T1 T!1 GNT4 D9T# G#1 DB1 M:2 6K T2/M G#:& #:1 GNT% #:1 M!1 GNT, /OT GT1 DB% M:1 B1 T2G1 G#:2 #:2 T$* #T% TG GNT4 M1 G#% P*# M:% P T2G% G9*/ #9*/ M$* #:% MG GNT& M% GT% G*2# /Q

ADC8N !*

T2C8N T2PS !#C8N !9N1 P) !9N0 "C8N P1 TM8D TC8N PC8N #:B2 #:#N2 M* #92 SM% T2 D9T# TB1 SM6*

The most important Special.Bunction.Megister and their symbolic !it 9dresses

L'(& Anzei$e 2/*


P5 P4 P4

Anal $& @eybd tastatur

Microcontroller Mini& M dul 515


P6 P1 P3

LE( Schal& S-itch ter "uchsen /onnectrs "uchsen /onnectrs "uchsen /onnectrs

Ein&) Aus$a*e *igital GO6 di$italer +erte

Gnterrupts ,nterru-ts 9nalog 2*ernah/e GO6 .i/er$ate Timer Date anal $er +erte 01hler /ounter

Sens r

/ompnents o+ an Microcontroller System


Seite 12 von 41

Peter Nauth

Microcontroller Technology

Asse%-ly Progra%%ing o$ "AB '0515(5)5 an *C''' "tructure o$ an asse%-ly instruction Label: 6perator 6perand1, 6perand2, 6perand& ;comment *ata +lo- is +rom right to le+t iNeN +rom operand 2 and & to operand 1 Co0y an arith%etic instructions /opy Gnstructions: M6K 6perand1, 6perand2 eNgN: M6K 9, <2/h 9rithmetic Gnstructions: 9** 6perand1, 6perand2 SU!! 6perand1, 6perand2 eNgN 9** 9, <2*h eNgN /2M / SU!! 9, <2*h T9dd 6perand1 and 6perand2 and copy Tthe sum into 6perand1 TSubstract 6perand2 Eand the /arry.BlagF T+rom 6perand1 T9dd 2*h to the 9.Megister T/lear /`Megister E" /arry.BlagF TSubstract 2*h and the /arry.Blag +rom the 9.Megister T /opy data +rom 6perand2 to 6perand1 T/opy the number 2/h into the 9.Megister

Data !(8 &ith 0orts 1N Set data direction by setting the register P$`*GM: Gnput " %, 6utput " 1 eNgN M6K P4`*GM, <%BBh TP4 output enabled M6K P,`*GM, <%%h TP, input enabled 2N /opy data +rom or to ports eNgN M6K P4, 9

T/opy output o+ 9.register to P4

MemarL: in order to address the ports -ith their symbolic addresses, the register library REGXC888_lite.INC (see cha ter REGXC888_lite.INC! m"st #e incl"$e$ in the r%gram (see cha ter E$it& c%m ile an$ r"n a r%gram !.

Seite 1& von 41

Peter Nauth

Microcontroller Technology

9ddres %% %% %% %1 %% %2 %% %& %% %4 %% %, %% %4 %% %' %% %1 %% %) %% %9 %% %! %% %/ %% %*

*ata ', /) BB ', %1 %& '! %4 #, #1 2' B, B1 NNN

/omments 6p./ode o+ M6K dadr, <L1 1N 6perand: address o+ P4`*GM 2N 6perand <%BBh 6p./ode o+ M6K dadr, <L1 1N 6perand %1 2N 6perand <%& 6p./ode o+ M6K Mr, <L1 mit r"& 2N 6perand <%4 6p./ode o+ M6K 9, dadr 9ddress o+ P4 in internal *ata Memory E8F 6p./ode o+ 9** 9, 3Mi mit i"1 6p./ode o+ M6K dadr, 9 9ddress o+ P, in internal *ata Memory E8F

Gnstruction M6K P4`*GM, <%BBh

< bytes & !yte

M6K %1, <%&

& !yte

M6K M&, <%4

2 !yte

M6K 9, P4

2 !yte

9** 9, 3M1 M6K P,, 9

1 !yte 2 !yte

7e$code Estored in Program MemoryF o+ an e$ample programN The start address is supposed to be %%%%hN Bor P4 and P, the addresses Einternal data memoryF +or the 1%,&, controller are used

Megister

PhysicalN 9dress %% %1 %2 %& %4 %, %4 %' %1

*ata a+ter pro. gram e$ecuton

M6K %1, <%&

M% M1 M2 M& M4 M, M4 M'

%& M6K M& <%4 %4

%&

%4

9** 9, 3M1

NNNN 9// P4 ! P,

NNNN #% #1 B% B1 B1 0 %4 " B, B1 %% B, M6K 9, P4 M6K P,, 9

Gnternal data memory a+ter program e$ecution

Seite 14 von 41

Peter Nauth

Microcontroller Technology

Unconditional Jump Instructions


'"m t% target a$$ress (ith%"t e)"m ti%ns* +',- a$r16 '"m t% the target a$$ress a$r16 l+a#el %r h.sical a$ress!. In the instr"cti%n the % erat%r a$r16 can #e (ritten as the la#el (s.m#%lic a$$ress! %r the h.sical a$$ress %/ the target 0',- rel '"m #. the relati1e a$ress rel. In the instr"cti%n the % erat%r rel can #e (ritten as the la#el (s.m#%lic a$$ress! %/ the target %r the $i//erence %/ the target a$$ress an$ the a$$ress a/ter the 0',- instr"cti%n. The a$$ress $i//erence m"st n%t e)cee$ 213845136.

Conditional Jump Instructions


'"m & i/ a c%n$iti%n has #een met. 7ther(ise c%ntin"e (ith the instr"cti%n /%ll%(ing the '"m instr"cti%n '8 rel '"m #. the relati1e a$$ress rel& i/ 9 :: 0 'N8 rel '"m #. the relati1e a$$ress rel& i/ 9 ;: 0 C'NE 9& $a$r& rel '"m #. the relati1e a$$ress rel& i/ 9 ;: $a$r D'N8 Rr& rel Decrement Rr an$ <"m #. the relati1e a$ress rel i/ Rr ;:0 Simple Loop ,7= R0& >n ?n"m#er %/ l%% r"ns is n +77-* 444.. D'N8 R0& +77Stacked Loop ,7= R1& ># ?n"m#er %/ l%% r"ns is a@# +77-A* ,7= R0& >a +77-9* 444.. D'N8 R0& +77-9 D'N8 R1& +77-A
Seite 1, von 41

2oop instructions

Peter Nauth

Microcontroller Technology

Loop Program: Write 0-9 to Adresses 20-29


RM: M6K M%, <1%T M6K M1, <2%T M6K 9, <%%T 266P: M6K 3M1, 9T GN/ 9 T GN/ M1T *INS M%, 266PT #N*: 2IMP #N*T 1 M/ EMachine /ycle :/111F " 1&N& ns 1 M/ EMachine /ycle :/111F " 1&N& ns 1 M/ EMachine /ycle :/111F " 1&N& ns 1 M/ EMachine /ycle :/111F " 1&N& ns 1 M/ EMachine /ycle :/111F " 1&N& ns 1 M/ EMachine /ycle :/111F " 1&N& ns 2 M/ EMachine /ycle :/111F " 144N4 ns 2 M/ EMachine /ycle :/111F " 144N4 ns

9ddress

*ata

/omment

9ssembly GnstrN

!yte

%% %% %% %1 %% %2 %% %& %% %4 %% %, %% %4 %% %' %% %1 %% %) %% %9

'1 %9 ') 14 '4 %% B' %4 %) *1 B!

6p./ode o+ M6K Mr, <L1 -ith r"% 2N 6perand <1%d 6p./ode o+ M6K Mr, <L1 -ith r"1 2N 6perand <2%d 6p./ode o+ M6K 9, <L1 2N 6perand <%% 6p./ode o+ M6K 3Mi, 9 mit i" 1 6p./ode o+ GN/ 9 6p./ode o+ GN/ Mr -ith r" 1 6p./ode o+ *INS Mr, rel -ith r" % Iump address 266P is shi+ted by B!h " ., E%4h. %!hF relatively to the address +ollo-ing the Jump instruction *INS 6p./ode o+ 2IMP adr14 7igh !yte o+ absolute Jump addres %% %! 2o- !yte o+ absolute Jump addres %% %!

M6K M%, <1%

2 !yte

M6K M1, <2%

2 !yte

M6K 9, <%%

2 !yte

M6K 3M1, 9 GN/ 9 GN/ M1

1 !yte 1 !yte 1 !yte

*INS M%, 266P 2 !yte

%% %! %% %/ %% %* %% %#

%2 %% %! NNN

2IMP #N*

& !yte

/ontent o+ e$ternal program memory a+ter compilation o+ the loop program RM

un !ime o" W #$it%out endless loop &'(): T : B @ 8B.Bns 5 10 @ C 8B.BnsD


Seite 14 von 41

Peter Nauth

Microcontroller Technology

!nterru0ts
2N Gnterrupt o+ 27 9nter*rechun$ running program des Pr $ra//s andund Jump to zur S-run$ Adresse address &h 3h

r$ 0003h l3/- ,S40

&N 37Iump S-run$ to zuGSM% ,S40

/S#D 9T lJmp GSM%

%$%%%&

1N 2o-.signal at 17 L !& Si$nal Pinan &N2 triggers Pin 372 l8st Gnterrupt ,nterru-t aus

Pin 372

set* EAL set* EX0 -5 6677 6677 6677 l3/- l -

4:cks-run$ 4N47 !acL Jump and und ; rtsetzun$ continuation o+ des Pr $ra//s program

,S405 6677 6677 reti

Mechanism o+ e$ternal<:r interrupt % A*lau</echanis/en den e=ternen ,nterru-t 0

Gnterrupt #$ternal GM % #$ternal GM 1 #$ternal GM 2 #$ternal GM & #$ternal GM 4 #$ternal GM , #$ternal GM 4 Timer % Timer 1 Timer 2 Serial Gnter+ace 9*./onverter Gnterrupt Parameter

/onnection EPinF P &N2 P &N& P 1N4 P 1N% P 1N1 P 1N2 P 1N&

Gnterrupt Kector Gnterrupt MePuest %&h 1&h 4!h ,&h ,!h 4&h 4!h 2o- 2evel 2o- 2evel Negative Slope Negative Slope Positive Slope Positive Slope Positive Slope

Priority % % % % % % %

Number % 2 ' 1 ) 1% 11 1 & , 4 4

Selection #:% #:1 #:2 #:& #:4 #:, #:4 #T% #T1 #T2 #S

Trg: P&N2, /nt:P&N4 %!h Trg: P&N&, /nt:P&N, 1!h

6ver+lo- TimMeg % 6ver+lo- TimMeg % %

M$: P&N%, T$: P&N1 P4

2&h

/har received 9*/ +inished

% %

Seite 1' von 41

Peter Nauth

Microcontroller Technology

Ti%er Mo e 1
14 bit Timer Timer register is T76:T2% E7igh byte in T7%, 2o- !yte in T2%F 14 bit reload value TM2 must be reloaded in T7% and T2% by program GSM is called in an interval o+ TGSM " E4,,&4. TM2FH1&N&ns

Ti%er Mo e 2
1 bit Timer Timer register is T2% 1 bit reload value TM2 is stored in T7% Meload is per+ormed automatically at over+loGSM is called in an interval o+ TGSM " E2,,. T7%FH1&N&ns
T2% incremented each 1&N& ns E1M/ +or :/111F .L0 !ird 3ede 1Es inkre/entiert '). > 0 @1 4el ad

? 255

.L0

> 255

,nterru-t SerAicer utine ,S4 <:r .i/er 0 6ver+loE2,,.T7%FH1&N& ns .\ Alle B255each C .#0D Es !ird die ,S4 Meload B<alls value aktiAiertD loaded und automatically $estartet .LE /it +rom T7% to T2% and GM> is de/ 4el ad!ert aus .#0an $eladen generated i+ GM is activated

.#0

,/ .i/er/ dus B'). > 0D !ird .L0 3ede 1Es inkre/entiert BPr zess rtakt > 12 M#zD7

Setting o+ Timer /on+iguration done -ith SBM TM6* Set Timer % to Mode 2: M6K TM6*, <%2h Set Timer 1 to Mode 2: M6K TM6*, <2%h TSet !it M1 o+ Timer % TSet !it M1 o+ Timer 1

Set Timer % to Mode2 and enable /ounter: M6K TM6*, <%4h TSet M1 and /OT

Seite 11 von 41

Peter Nauth

Microcontroller Technology

Progra%%ing o$ Delays &ith Ti%er in Mo e 2 -y %eans o$ !"#1signals


=GN/2U*# EM#D:/111`liteNGN/F

TPM6D S#DM#NT /6*#T TPU!2G/ Timer`*elayT

T only i+ called by e$ternal program T only i+ called by e$ternal program

/S#D 2IMP TMS#D

9T %$%%%! GSM PM6D

T 9ddress GnterruptveLtor E" 6MD %%%!h +or 1%,&, /ompilerF T Iump to GSMT 2IMP GSM is stored at address %$%%%! T only i+ called by e$ternal program

THHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH Timer`*elay: M6K M6K M6K S#T! S#T! S#T! P4`*GM, <%BBh TM6*, T7%, #T% #9 TM% <%2h T P4 *irection au+ 6utput T Set Timer % to Modus2

<rld TMeload Kallue +or setting Gnterrupt Service Gntervall T`isr"E2,,.rldFHT`m T Timer % Gnterrupt aLtivate T Melease all Gnterrupts T Start Timer

loop: -ait:

M6K IN/ /2M *INS

M%, -ait / M%,

<m

T Set loop counter to m T Rait until /Q is set by GSM, then the Time T`isr has passed T /lear /.Blag

-ait

T m H T`isr has passed T Gnstructions to be e$ecuted a+ter the time mHT`GSM

9ction: Stop: M#T

Tonly necessary i+ Program Timer`*elay Renn is called +rom Ee$ternalF program

THHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH GSM: S#T! / GSM`Stop: M#TG T GSM is called each T`GSM"E2,,.M2*FHT`MS T Set /QT is checLed in Main Program Timer`*elay,in order to measure T`isr

THHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH end

Seite 1) von 41

Peter Nauth

Microcontroller Technology

Melo y Progra%

SAB 80515/ 535

P473

P473

'PL P473

'PL P473

.)2

Signal +or the generation o+ a tone -ith a speaLer connected to P4N&N Si$nalAerlau< <:r die . nerzeu$un$ /it eine/ an P473 The speaLerssenen signal must be complemented t-ice during period an$eschl Lauts-recher7 ,n einer Peri de /ussadas
Lauts-rechersi$nal z!ei/al inAertiert !erden

Tone Nu%-er TH: / /is * bbb /C bbb TO2 " T7S H 1)s E" M' H 1)s, i+ T7S is stored in M'F + " 241,4& 7 , T7S " 1%% " ,% H
313 F 13

+ " 1&%,11 7 , + " 1&1,,4 7 , + " 144,1& 7

T7S " 2%% " 1%% H T7S " 11) " 1%% H

313 F 13 311 F 13

T7S " 1'1 " 1%% H 310 F 13

Seite 2% von 41

Peter Nauth

Microcontroller Technology

Start

4F " > .#0 M' T7S 41 >. M1 " nl1n$e T2S

46 > 131

0eitAerz8$erun$ *elay (E' 4F

2ength o+ loop Schlei<endaer is 1)1G asEsec N T

4F >> 0

'PL P57F

(E' 46

4F > .#0

46 >> 0

T
(E' 41

N
41 >> 0

T
Ende . Melody n-r $ra// zur Entk --lun$ A n . nh8he und . ndauer B.#0 > program -ith decoupling o+ tone +rePuency and . tone nh8henzahlD length

Ton Number: BrePuency: Ton 2ength number: Ton 2ength

T7S .\ M' + " 1 O ET7S H &1asF T2S .\ M1 T2 " T2S H M4 H 1)as " T2S H 2,, ms Ei+ M4 " 1&1F

Seite 21 von 41

Peter Nauth

Microcontroller Technology

"u-routines
/all o+ a subroutine: Structure o+ a subroutine : srname: bbN M#T T!egin o+ subroutine srname T#nd o+ subroutine srname 2/922 srname

EMe.entry address +or bacL Jump to calling program is stored in the stacLF

Ta-le Calculation/ E#$ample: read &rd data o+ table @#NN2F


@#NN2: *! M6K M6K M6K/ M6K bbN %1, %4, %), 14, 2, *PTM, <@#NN2 9, <%2 9, 39 0 *PTM M%, 9 TTable declaration TSet !ase Pointer at table address TSet Gnde$ Pointer to &rd table data TTable access TStore table data in M% TNe$t instructions

9pplication +or Melody program Erough structureF M#26: *! T2S1, T7S1, T2S2, T7S2, bNT2Sn, T7Sn, % P29Q: M6K M6K/ IS M6K GN/ M6K M6K/ M6K *PTM, <M#26 9, <%% 9, 39 0 *PTM ST6P M1, 9 *PTM 9, <%% 9, 39 0 *PTM M2, 9 TSet !ase Pointer at table address TSet Gnde$ Pointer TTable access TStore T2S in M1 TSet !ase Pointer to ne$t address TSet Gnde$ Pointer TTable access TStore T7S in M2 TPlay tone TIump to ST6P i+ 9 is %Eend o+ tableF M#P#9T: M6K

2/922 UPT6N# GN/ *PTM 2IMP M#P#9T ST6P: 2IMP ST6P

Seite 22 von 41

Peter Nauth

Microcontroller Technology

/ontrol -ord 9S/GG char

*!: MS: #:

9S/GG. character or control -ord /ontrol mode EMS " %F or *isplay mode EMS " 1F Negative Slope e$ecutes display o+ character or control +unction, -ait 1'%%as EcontrolF or 4%as EdisplayF -ait or until !B""%N *! " %1h .\ /lear display, /ursor to1st column le+t *! " 1%h 0 n .\ /ursor to position n in 1st ro*! " /%h 0 n .\ /ursor to position n in 2nd ro-

MOR: Rrite mode EMOR " %F or Mead mode EMOR " 1F

/ontrol -ords:

Seite 2& von 41

Peter Nauth

Microcontroller Technology

Connection 0lan *C''' 9x0eri%ental Boar

Seite 24 von 41

Peter Nauth

Microcontroller Technology

Seite 2, von 41

Peter Nauth

Microcontroller Technology

!ntegrate De;elo0%ent 9n;iron%ent +!D9, <Vision 4 Create a ne$ pro*ect 1. Create a ne( /%l$er /%r .%"r la# e)ercises 3. 0tart E=isi%n B. Create a ne( r%<ect an$ st%re it in .%"r /%l$er (Data T. e* -r%<ect Giles @."1 r%<!

H. C%ntr%ller In/ine%n XC888C+,I8GG a"s(Jhlen

Seite 24 von 41

Peter Nauth

Microcontroller Technology

C. In %r$er t% create a sim"lati%n an$ a har$(are (%rDs ace& clicD %n Kset" /ile e)tensi%n 4.L

an$ create 3 (%rDs aces* 0im"lati%n an$ ,CTI0.stem

Seite 2' von 41

Peter Nauth

Microcontroller Technology

6. 7 en K7 ti%ns /%r Target MTarget 1ML /%r 0im"lati%n /irst an$ then& a/ter ha1ing er/%rme$ the s"#seN"ent ste s& /%r ,CTI0.stem an$ re eat the s"#seN"ent ste s

6. 0et Target as /%ll%(s

Seite 21 von 41

Peter Nauth

Microcontroller Technology

8. 0et De#"g as /%ll%(s

;G%r 0im"lati%n O%rDs ace* 9cti1ate KPse 0im"lat%rL ;G%r ,CTI0.stem O%rDs ace* 9cti1ate KIn/ine%n XC800 P+INQ Dri1erL

Seite 2) von 41

Peter Nauth

Microcontroller Technology

R. 0et Ptilities as G%ll%(s

&dit+ compile and run a program 1. 7 en the relate$ r%<ect 3. 7 en a ne( /ile* Gile SS Ne( B. 0a1e the /ile* Gile SS 0a1e as E)tensi%n /%r CI r%gram* @.c E)tensi%n /%r 9ssem#l.I r%gram* @.src H. Incl"$e the r%gram int% the r%<ect* Right m%"se clicD %n K0%"rce Gr%" 1L SS +e/t m%"se clicD %n K9$$ Giles t% 0%"rce M0%"rce Gr%" 1ML C. 7 en the /ile again& e$it the r%gram an$ sa1e it. The r%gram scelett%n %/ an assem#l. r%gram is* TN7,7DC1 TINC+PDE (REGXC888_lite.INC! 44 assem#l. c%$e 44 en$ 6. C% . REGXC888_lite.INC int% .%"r /%l$er (i/ n%t a1aila#le* cancel the instr"cti%n TINC+PDE (REGXC888_lite.INC!

Seite &% von 41

Peter Nauth

Microcontroller Technology

6. C%m ile the r%gram an$ start the $e#"gger m%$"s

8. 9cti1ate eri herials

R. R"n the r%gram

Seite &1 von 41

Peter Nauth

Microcontroller Technology

,eader "ile &-.C///0lite ?IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII ? Erstellt 300R 1%n O.Gr%te ? GUIGranD/"rt ? XC888 -r%cess%r ?IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII T09=E TN7+I0T ? ? AVTE Register -H_DIR D9T9 0)CR -H_D9T9 D9T9 0)C8 -C_DIR D9T9 0)RB -C_D9T9 D9T9 0)R3 -H -C TU0 TU1 T+0 T+1 T,7D C,C7N FF -%rt H -H_0 -H_1 -H_3 -H_B -H_H -H_C -H_6 -H_6 AIT 0)C8? AIT 0)CR? AIT 0)C9? AIT 0)CA? AIT 0)CC? AIT 0)CD? AIT 0)CE? AIT 0)CG? D9T9 0)C8 D9T9 0)R3 D9T9 D9T9 D9T9 D9T9 D9T9 D9T9 0)8C 0)8D 0)89 0)8A 0)8R 0)A9

TR0 ET0 E9

AIT 0)8C? AIT 0)9R? AIT 0)9G?

TRE0T7RE
Seite &2 von 41

Peter Nauth

Microcontroller Technology

'0511!nstruction =ist Co0y !nstructions


1nemonic M8V A>#r M8V A> a r M8V A>?#i M8V A>@.onst' M8V #r>A M8V #r> a r M8V #r>@.onst' M8V a r>A M8V a r>#r M8V a r> a r M8V a r>?#i 156 dadr+7konst/ M8V ?#i>A M8V ?#i> a r M8V ?#i>@.onst' M8V DPT#>@.onst17 M8VC A>?AADPT# M8VC A>?AAPC M8V* A>?#i M8V* A>?DPT# M8V* ?#i>A M8V* ?DPT#>A PB"H a r P8P a r *CH A>#r *CH A> a r *CH A>?#i *CHD A>?#i "4AP A N8P 23te 1 3 1 3 1 3 3 3 3 B 3 B 1 3 3 B 1 1 1 1 1 1 3 3 1 3 1 1 1 1 1C 1 1 1 1 1 3 1 1 3 3 3 3 1 3 1 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 4lags I I I I I I I I I I I I I I I I I I I 5p E82EG EC E6&E6 6H G82GG 9829G 6826G GC 8828G 8C 86&86 6C G6&G6 96&96 66&66 R0 RB 8B E3&EB E0 G3&GB G0 C0 D0 C82CG CC C6&C6 D6&D6 CH 00

Seite && von 41

Peter Nauth

Microcontroller Technology

Arith%eti. !nstructions
1nemonic ADD A>#r ADD A> a r ADD A>?#i ADD A>@.onst' ADDC A>#r ADDC A> a r ADDC A> ?#i ADDC A> @.onst' "BBB A>#r "BBB A> a r "BBB A>?#i "BBB A>@.onst' !NC A !NC #r !NC a r !NC ?#i !NC DPT# D9C A D9C #r D9C a r D9C ?#i MB= AB D!V AB DA A 23tes 1 3 1 3 1 3 1 3 1 3 1 3 1 1 3 1 1 1 1 3 1 1 1 1 1C 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 H H 1 4lags CV&9C& 7=&CV&9C& 7=&CV&9C& 7=&CV&9C& 7=&CV&9C& 7=&CV&9C& 7=&CV&9C& 7=&CV&9C& 7=&CV&9C& 7=&CV&9C& 7=&CV&9C& 7=&CV&9C& 7=&I I I I I I I CV&7=&CV&7=&CV 5p 38I3G 3C 36&36 3H B8IBG BC B6&B6 BH R82RG RC R6&R6 RH 0H 0820G 0C 06&06 9B 1H 1821G 1C 16&16 9H 8H DH

Seite &4 von 41

Peter Nauth

Microcontroller Technology

=ogic !nstructions
1nemonic AN= A>#r AN= A> a r AN= A>?#i AN= A>@.onst' AN= a r>A AN= a r>@.onst' 8#= A>#r 8#= A> a r 8#= A>?#i 8#= A>@.onst' 8#= a r>A 8#= a r>@.onst' . L A+ r *#= A> a r *#= A>?#i *#= A>@.onst' *#= a r>A *#= a r>@.onst' C=# A CP= A 23tes 1 3 1 3 3 B 1 3 1 3 3 B 1 3 1 3 3 B 1 1 18 1 1 1 1 1 3 1 1 1 1 1 3 1 1 1 1 1 3 1 1 4lags I I I I I I 5p C82CG CC C6&C6 CH C3 CB H82HG HC H6&H6 HH H3 HB 6826G 6C 66&66 6H 63 6B EH GH

Bit Mani0ulation !nstructions


1nemonic C=# C C=# -a r "9TB C "9TB -a r CP= C CP= -a r AN= C>-a r AN= C>(-a r 8#= C>-a r 8#= C>(-a r M8V C>-a r 156 9adr+C 23tes 1 3 1 3 1 3 3 3 3 3 3 3 18 1 1 1 1 1 1 3 3 3 3 3 3 4lags CV I CV I CV I CV CV CV CV CV I 5p CB C3 DB D3 AB A3 83 80 63 90 93 R3

"hi$t !nstructions
1nemonic #= A #=C A ## A ##C A 23tes 1 1 1 1 18 1 1 1 1 4lags I CV&I CV&5p 3B BB 0B 1B

Seite &, von 41

Peter Nauth

Microcontroller Technology

"u-routine !nstructions
1nemonic ACA== a r11 =CA== a r17 #9T #9T! 23tes 3 B 1 1 18 3 3 3 3 4lags I I I I 5p 11 13 33 B3

Cu%0 !nstructions
1nemonic ACMP a r11 =CMP a r17 "CMP rel CMP ?AADPT# C: rel CN: rel CC rel CNC rel CB -a r> rel CNB -a r>rel CBC -a r>rel CJ'& A+dadr+rel CCN9 A>@.onst'>rel CCN9 #r>@.onst'>rel CCN9 ?#i>@.onst'>rel DCN: #r>rel DCN: a r>rel 23tes 3 B 3 1 3 3 3 3 B B B B B B B 3 B 18 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4lags I I I I I I I I I I I CV CV CV CV I I 5p 01 03 80 6B 60 60 H0 C0 30 B0 10 AC AH A82AG A6&A6 D82DG DC

A--ri;iations
9 9/ adr11 adr14 ! badr /Q dadr 6K P rel Mi Mr ? <Lonst1 <Lonst14 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 9ccumulator 9u$iliary./arry.Blag 11.!it.9dress -ithin a 2.L!yte.Seite 14.!it.9dress Megister ! !it.9dress /arry.Blag 9ddress o+ the internal data memory 6ver+lo-.Blag Parity.Blag relativ 1.!it.6++set.9dress E(121 bis 012'F Megister EM% or M1F Megister EM% . M'F 2abel +or indirect addressing 1.!it./onstant 14.!it./onstant
Seite &4 von 41

Peter Nauth

Microcontroller Technology

C- Programming
OOHHHHHHHHH /alculate P4 0 P, and -rite the sum to P&HHHHHHHHHHHHOO <include W:/111/2MNh\ <include WstdioNh\ OO <include cU9MTN7c OO <include WstringNh\ int i,J,LT int arrayU14ZT int sumEvoidFT void mainEvoidF V P4`*GM " %$%%T P,`*GM " %$%%T P&`*GM " %$BBT OO Set Port *irection to Gnput OO Set Port *irection to Gnput OO Set Port *irection to 6utput OO Dlobal Kariables OO Dlobal 9rrays OO Bunction prototype

-hileE1F OO #ndless loop V i" P4T OO Gnput o+ P4 value J" P,T OO Gnput o+ P, value L" sumEFT P&" LT [ [ int sumEvoidF V int aT a " i 0 JT return aT [ OO HHHHHHHHHHHH#$ample o+ an GSMHHHHHHHHHHHHHHOO void isr EF interrupt 1 V bbNNT [ OO instructions o+ GSM OO GSM o+ Timer % ETimer % GSM has number 1, see pN1': numberF OO 6utput o+ result to P&

OO 2ocal variable

Seite &' von 41

Peter Nauth

Microcontroller Technology

C-5perators Arith%etic 80erators 0 . O H ? .. 00 Sum *i++erence *ivision Multiplication Modulo *ecrement Epost and preF Gncrement Epost and preF

Bit 80erators A Y WW \\ ^ 8 9N* 6M 2e+t shi+t Might shi+t /omplement Negation Eresult is 1, i+ 6perand is % and vice verseF

Co%0arison +use e3g3 as con itions in control structures, "" 8" \ W \" W" YY ePual not ePual larger than smaller larger or ePual smaller or ePual 6M

AA 9N*

Seite &1 von 41

Peter Nauth

Microcontroller Technology

Assign%ents " H" O" ?" 0" ." *irect assignment 9ssignment o+ multiplication 9ssignment o+ *ivision 9ssignment o+ ModuloN 9ssignment o+ 9dditionN 9ssignment o+ SubtractionN

WW" 9ssignment o+ 2e+t Shi+tN \\" 9ssignment o+ Might Shi+t A" Y" ]" A 9ssignment o+ 9N* 9ssignment o+ 6M 9ssignment o+ :6M

ress 80erators A H 9ddress o+ Pointer to

Seite &) von 41

Peter Nauth

Microcontroller Technology

Data Ty0es char unsigned char int unsigned int character or signed numbers E1 !yteF unsigned numbers E1 !yteF signed numbers E2 !yteF unsigned numbers E2 !yteF

Control "tructures -hileEconditionF V OO instructions e$ecuted continously as long as condition is True [ +orEi"%T iWm T i00F OOloop runs m times V OO instructions e$ecuted m times [ i+ EconditionF V OO instructions e$ecuted once i+ condition is True [

Seite 4% von 41

Peter Nauth

Microcontroller Technology

=iterature
Microcontrollers in Practice Springer Series in 9dvanced Microelectronics, KolN 11 Susnea, Goan, Mitescu, Marian Springer Kerlag, 2%%, GS!N: )'1.&.,4%.2,&%1.&

9x0loring C $or Microcontrollers 9 7ands on 9pproach Parab, IN, ShelaLe, KNDN, @amat, MN@N, NaiL, DNMN Springer Kerlag, 2%%' GS!N )'1.1.4%2%.4%44.&

Seite 41 von 41

Das könnte Ihnen auch gefallen