; Chapter 8 9S12C32 assembly language programs ; Jonathan W. Valvano, 2/07/07 ; This software accompanies the book, ; Embedded Microcomputer Systems: Real Time Interfacing, Second Edition ; published by Thomson Engineering, 2006 ; ; MC9S12C32 ;loop until switch is pressed Key_WaitPress brset PTT,#$08,* ;PT3=0 if pressed ldy #1 bsr Timer_Wait10ms ;Program 2.6 rts ;loop until switch is released Key_WaitRelease brclr PTT,#$08,* ;PT3=1 if released ldy #1 bsr Timer_Wait10ms ;Program 2.6 rts Key_Init bsr Timer_Init bclr DDRT,#$08 ;PT3 is input rts ;Program 8.2. Switch debouncing using assembly software. ; MC9S12C32, PT3 is input ; RegB is the return value Key_Read ldd TCNT addd #20000 ;10ms delay std TC5 ;start timer movb #$20,TFLG1 ; clear flag ldab PTT ;0 if pressed andb #$08 ;B=old value same ldaa TFLG1 ;10ms bouncing anda #$20 ;C5F set? bne done ldaa PTT ;0 if pressed anda #$08 ;A=new value cba ;same as before beq same bra Key_Read ; different done rts Key_Init bset TIOS,#$20 ;enable OC5 movb #$80,TSCR1 ;enable movb #$01,TSCR2 ;500 ns clk bclr DDRT,#$08 ;PT3 is input rts ;Program 8.4. Another example of switch debouncing using assembly software. ; MC68HC812A4 KEY: ds 2 ;current value RITUAL: bset #$20,TIOS ;enable OC5 movb #$90,TSCR ;enable fast clr movb #$32,TMSK2 ;500 ns clk bclr #$20,TMSK1 ;disarm OC5 clr DDRH ;key inputs clr DDRJ ;key inputs movb #$FF,KWIEH ;arm key wakeup movb #$FF,KWIEJ ;arm key wakeup clr KPOLJ ;falling edge clr PUPEJ ;regular inputs bsr KeyBoard ;set 74LS374's cli ;Enable IRQ rts KeyBoard: movb #$FF,KWIFH ;clr flags movb #$FF,KWIFJ ;clear flags ldaa PORTJ ;Read MSB ldab PORTH ;Read LSB std KEY rts KeyHHan: clr KWIEH ;disarm key wakeup clr KWIEJ ;disarm key wakeup bset #$20,TMSK1 ;arm OC5 ldd TCNT ;TCNT addd #20000 ;OC5 10ms later std TC5 ;fast clear rti KeyJHan: clr KWIEH ;disarm key wakeup clr KWIEJ ;disarm key wakeup bset #$20,TMSK1 ;arm OC5 ldd TCNT ;TCNT addd #20000 ;OC5 10ms later std TC5 ;fast clear rti OC5Han: bclr #$20,TMSK1 ;disarm OC5 movb #$FF,KWIEH ;arm key wakeup movb #$FF,KWIEJ ;arm key wakeup bsr KeyBoard ;read keyboard rti ; Program 8.8. Assembly software interface of a direct connection keyboard. ; MC9S12C32 Key_Init clr DDRT ;PT3-PT0 inputs clr PTT ;PT7-PT4 oc output clr PPST ;pull-up on PT3-PT0 movb #$0F,PERT rts ScanTab fcb $80,"abcd" ;top row fcb $40,"efgh" fcb $20,"ijkl" fcb $10,"mnop" ;bottom row fcb 0 ; Returns RegA ASCII key pressed, ; RegY number of keys pressed ; Y=0 if no key pressed Key_Scan ldy #0 ;Number pressed ldx #ScanTab loop ldab 0,x ;row select beq done stab DDRT ;select row brset PTT,#$01,NotPT0 ldaa 4,x ;code for column 0 iny notPT0 brset PTT,#$02,NotPT1 ldaa 3,x ;code for column 1 iny NotPT1 brset PTT,#$04,notPT2 ldaa 2,x ;code for column 2 iny notPT2 brset PTT,#$08,notPT3 ldaa 1,x ;code for column 3 iny notPT3 leax 5,x ;Size of entry bra loop done rts ;Program 8.11. Assembly software interface of a matrix scanned keyboard. ; MC68HC812A4 Key: ds 1 ;current key PrevKey: ds 1 ;previous key Ritual: sei ldaa #$F0 ;PJ7-PJ4 outputs staa DDRJ ;PJ3-PJ0 inputs bsr KeyScan staa PrevKey staa Key bset #$20,TIOS ;activate OC5 bset #$20,TMSK1 ;arm OC5 ldaa #$32 staa TMSK2 ;500ns clock ldd TCNT addd #20000 std TC5 ; first in 10ms ldaa #$20 staa TFLG1 ; clear C5F cli rts ; returns RegA=code (0 for none) KeyScan: clra ;=0 means no clr PORTJ ;row=0 loop: ldab PORTJ ;read columns andb #$0F cmpb #$0F ;$0F means no beq none eorb #$0F ;code 1-9 tba ;found one none: ldab PORTJ addb #$10 ;next row stab PORTJ bne loop rts TOC5handler: bsr KeyScan cmpa PrevKey ;same as last? bne skip staa Key ;new value skip: staa PrevKey ldd TC5 addd #20000 std TC5 ;every 10ms ldaa #$08 staa TFLG1 ;ack OC5 rti ; Program 8.13. Assembly software interface of a multiplexed keyboard. ; 6811 or 6812 ;Linked list stored in EEPROM S10 fcb 10 ;Output pattern fdb S9 ;Next if CW fdb S6 ;Next if CCW S9 fcb 9 fdb S10 fdb S5 S5 fcb 5 fdb S9 fdb S6 S6 fcb 6 fdb S5 fdb S10 ;Global variables stored in RAM Pos ds 1 ;0<= Pos <=199 Pt ds 2 ;to current state ;Program 8.24. A double circular linked list used to control the stepper motor. ; 6811 or 6812 ;Move 1.8 degrees clockwise CW: ldx Pt ;current state ldx 1,x ;Next clockwise stx Pt ;Update pointer ldaa ,x ;Output pattern staa PORTB ;Set phase control ldaa Pos ;Update position inca ;clockwise cmpa #200 blo OK1 ;0<= Pos <=199 clra OK1: staa Pos rts ;Move 1.8 degrees counterclockwise CCW: ldx Pt ;current state ldx 3,x ;Next CCW stx Pt ;Update pointer ldaa ,x ;Output pattern staa PORTB ;Set phase control ldaa POS ;Update position deca ;CCW direction cmpa #255 bne OK2 ;0<= Pos <=199 ldaa #199 OK2: staa Pos rts Init: clr Pos ldx #S10 stx Pt movb #$FF,DDRB ;6812 only rts ;Program 8.25. Helper functions used to control the stepper motor. ;6811 or 6812 ;Reg B=desired 0<=RegB<=199 desired equ 0 ;desired state Seek pshb ;Save as local tsy subb Pos ;Go CW or CCW? beq done ;Skip if equal bhi high ;Desired>Pos? ;DesiredPos and Desired-Pos<100 goCCW bsr CCW ;RegA current cmpa desired,Y bne goCCW ;Pos=Desired? done pulb rts ;Program 8.26. High-level function to control the stepper motor. ; Program 8.2. Switch debouncing using assembly software. ; MC68HC812A4 WaitPress: ldaa PORTT ;PT3=0 if switch pressed anda #$08 bne WaitPress ;loop until switch is pressed ldd TCNT addd #20000 ;10ms delay std TC5 ; fast clear loopP: ldaa TFLG1 ;wait 10ms for switch to stop bouncing anda #$20 ;C5F set? beq loopP rts WaitRelease: ldaa PORTT ;PT3=1 if switch released anda #$08 beq WaitRelease ;loop until switch is released ldd TCNT addd #20000 ;10ms delay std TC5 ; fast clear loopR: ldaa TFLG1 ;wait 10ms for bouncing to stop anda #$20 ;C5F set? beq loopR rts Init: bset #$20,TIOS ;enable OC5 movb #$90,TSCR ;enable fast clr movb #$32,TMSK2 ;500 ns clk bclr #$08,DDRT ;PT3 is input rts ; Program 8.4. Another example of switch debouncing using assembly software. ; MC68HC812A4 * Reg B is the return value ReadPT3: ldd TCNT addd #20000 ;10ms delay std TC5 ldab PORTT ;0 if pressed andb #$08 ;B=old value Same: ldaa TFLG1 ;10ms bouncing anda #$20 ;C5F set? bne Done ldaa PORTT ;0 if pressed anda #$08 ;A=new value cba ;same as before beq Same bra ReadPT3; different Done: rts Init: bset #$20,TIOS ;enable OC5 movb #$90,TSCR ;enable fast clr movb #$32,TMSK2 ;500 ns clk bclr #$08,DDRT ;PT3 is input rts ; Program 8.11. Assembly software interface of a matrix scanned keyboard. ; MC68HC812A4 Ritual: clr DDRJ ;PJ3-PJ0 inputs rts ;PJ7-PJ0 oc outputs ScanTab: dc.b %01110000 ;PJ7 row data dc.b %10000000 ;PJ7 row DDRJ dc.b "abcd" ;characters dc.b %10110000 ;PJ6 row dc.b %01000000 ;PJ6 row DDRJ dc.b "efgh" ;characters dc.b %11010000 ;PJ5 row dc.b %00100000 ;PJ5 row DDRJ dc.b "ijkl" ;characters dc.b %11100000 ;PJ4 row dc.b %00010000 ;PJ4 row DDRJ dc.b "mnop" ;characters dc.b 0 ; Returns RegA ASCII key pressed, ; RegY number of keys pressed ; Y=0 if no key pressed Scan: ldy #0 ;Number pressed ldx #ScanTab Loop: ldab 0,x ;row select beq Done stab PORTJ ;select row ldab 1,x ;DDRJ value stab DDRJ brset PORTJ,#$01,NotPJ0 ldaa 5,x ;code for column 0 iny NotPJ0: brset PORTJ,#$02,NotPJ1 ldaa 4,x ;code for column 1 iny NotPJ1: brset PORTJ,#$04,NotPJ2 ldaa 3,x ;code for column 2 iny NotPJ2: brset PORTJ,#$08,NotPJ3 ldaa 2,x ;code for column 3 iny NotPJ3: leax 6,x ;Size of entry bra Loop Done: rts ; Program 8.24. A double circular linked list used to control the stepper motor. ; MC68HC11A8 or MC68HC812A4 ;Linked list stored in EEPROM S10: dc.b 10 ;Output pattern dc.w S9 ;Next if CW dc.w S6 ;Next if CCW S9: dc.b 9 dc.w S10 dc.w S5 S5: dc.b 5 dc.w S9 dc.w S6 S6: dc.b 6 dc.w S5 dc.w S10 ;Global variables stored in RAM POS: ds 1 ;0<=POS<=199 PT: ds 2 ;to current state ; Program 8.25. Helper functions used to control the stepper motor. ; MC68HC11A8 or MC68HC812 ;Reg B=desired 0<=RegB<=199 SEEK: pshb ;Save desired tsy subb POS ;Go CW or CCW? beq DONE ;Skip if equal bhi HIGH ;Desired>POS? ;DesiredPOS and Desired-POS<100 GOCCW: bsr CCW ;Reg A current cmpa ,Y bne GOCCW ;POS=Desired? DONE: pulb rts ;Return