; Chapter 6 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 ;external signal to PT3/IC3 Time rmb 2 ;every 1 ms Init sei ;make atomic bclr TIOS,#$08 ;PT3=input capture bclr DDRT,#$08 ;PT3 is input movb #$80,TSCR1 ;enable TCNT movb #$01,TSCR2 ;500 ns clock bclr TCTL4,#$80 ;EDG3BA =01 bset TCTL4,#$40 ;on rise of IC3 bset TIE,#$08 ;Arm C3F movw #0,Time ;init global movb #$08,TFLG1 ;clear C3F cli ;enable rts IC3Han movb #$08,TFLG1 ;ack C3F [4] ldx Time [3] inx [1] stx Time [3] rti org $FFE8 ;timer channel 3 fdb IC3Han ;Program 6.1. Periodic interrupt using input capture and an external clock. ; MC9S12C32 ;external signal to PT1/IC1 Period rmb 2 ;units 500 ns First rmb 2 ;TCNT at first edge Done rmb 1 ;set each rising Init sei ;make atomic bclr TIOS,#$02 ;PT1=input capture bclr DDRT,#$02 ;PT1 is input movb #$80,TSCR1 ;enable TCNT movb #$01,TSCR2 ;500ns clk bclr TCTL4,#$08 ;EDG1BA =01 bset TCTL4,#$04 ;on rise of PT1 movw TCNT,First ;init global clr Done movb #$02,TFLG1 ;clear C1F bset TIE,#$02 ;Arm C1F cli ;enable rts IC1Han movb #$02,TFLG1 ;clear C1F [4] ldd TC1 [3] subd First [3] std Period [3] movw TC1,First [6] movb #$FF,Done [4] rti [8] org $FFEC ;timer channel 1 fdb IC1Han ; Program 6.3. Assembly language 16-bit period measurement. ; MC9S12C32 ; B=PB7, Q=PT2/IC2 Init bclr TIOS,#$04 ;PT2=input capture movb #$80,TSCR1 ;enable movb #$01,TSCR2 ;500 ns clock clr TIE ;gadfly, C2I=0 bset DDRB,#$80 rts ; return Reg D as R in Kohm Meas movb #$10,TCTL4 ;Rising edge movb #$04,TFLG1 ;C2F=0 bclr PORTB,#$80 ;PB7=0 bset PORTB,#$80 ;PB7=1 First brclr TFLG1,#$04,First ;Wait for first rising edge ldy TC2 ;TCNT at rising movb #$04,TFLG1 ;C2F=0 movb #$20,TCTL4 ;Falling edge pshy ;Save on stack Second brclr TFLG1,#$04,Second ;Wait for next falling edge ldd TC2 ;TCNT at falling subd 2,SP+ ;RegD=pulse width 1000 to 2000 cyc subd #1000 ;0<=R<=1000Kohm rts ; Program 6.6. Assembly language pulse-width measurement. ; MC9S12C32 Period rmb 2 ;units usec Init sei ;make atomic bset TIOS,#$08 ;OC3 bset DDRT,#$08 ;PT3 output movb #$80,TSCR1 ;enable movb #$01,TSCR2 ;500 ns clock bset TIE,#$08 ;Arm OC3 bset TCTL2,#$40 ;OL3=1, toggle bclr TCTL2,#$80 ;OM3=0 movb #$08,TFLG1 ;clear C3F ldd TCNT ;current time addd #50 ;first in 25us std TC3 cli ;enable rts OC3Han movb #$08,TFLG1 ;Ack [4] ldd TC3 [3] addd Period ;next [3] std TC3 [2] rti [8] org $FFE8 fdb OC3Han ; Program 6.10. Assembly language squarewave using output compare. ; MC9S12C32 High rmb 2 ;number of cycles high Low rmb 2 ;number of cycles low Init sei ;make atomic bset TIOS,#$08 ;OC3 bset DDRT,#$08 ;PT3 output movb #$80,TSCR1 ;enable movb #$01,TSCR2 ;500 ns clock bset TIE,#$08 ;Arm OC3 bset TCTL2,#$40 ;OL3=1, toggle bclr TCTL2,#$80 ;OM3=0 movb #$08,TFLG1 ;clear C3F ldd TCNT ;current time addd #50 ;first in 25us std TC3 cli ;enable rts OC3Han movb #$08,TFLG1 ;Ack [4][4] ldaa PTT ;rise/fall? [3][3] bita #$08 ;PT3 [1][1] beq zero [1][3] one ldd TC3 [3] addd High ;now PT3 is 1 [3] std TC3 [2] bra done [3] zero ldd TC3 [3] addd Low ;now PT3 is 0 [3] std TC3 [2] done rti [8][8] org $FFE8 fdb OC3Han ; Program 6.12. Assembly language pulse-width modulated squarewave using output compare. ;6812 measures 0 to 65535 Hz ;returns Reg D = freq in Hz Freq bclr DDRT,#$80 ;PT7 is input movb #$40,PACTL ;count falling movw #0,PACNT movb #$02,PAFLG ;clear PAOVF ldy #100 bsr Timer_Wait10ms ;Program 2.6 brclr PAFLG,#$02,ok ;check PAOVF bad ldd #65535 ;too big bra out ok ldd PACNT ;units in Hz out rts ; Program 6.18 Frequency measurement using the pulse accumulator. ;6812 measures 16us to 1.05s ;returns Reg D = pulse width in 16us Puls bclr DDRT,#$80 ;PT7 is input movb #$60,PACTL ;measure high movw #0,PACNT movb #$02,PAFLG ;clear PAOVF loop brclr PAFLG,#$01,loop brclr PAFLG,#$02,ok ;check PAOVF bad ldd #65535 ;too big bra out ok ldd PACNT ;units in 16us out rts ; Program 6.19 Pulse-width measurement using the pulse accumulator. ; Program 6.1. Initialization of a periodic interrupt using input capture and an external clock. ; MC68HC812A4 ;external signal to PT3/IC3 TIME rmb 2 ;every 1 ms Init sei ;make atomic bclr TIOS,$08 ;PT3=input capture clr DDRT ;PT3 is input movb #$80,TSCR ;enable TCNT movb #$32,TMSK2 ; 500 ns clk ldaa TCTL4 ;old value anda #$7F ;Clear EDG3B=0 oraa #$40 ;EDG3BA =01 staa TCTL4 ;on rise of IC3 bset TMSK1,$08 ;Arm IC3F movw #0,TIME ;init global movb #$08,TFLG1 ;clear IC3F cli ;enable rts ; Program 6.2. Periodic interrupt using input capture and an external clock. ; MC68HC812A4 IC3Han brset #$08,TFLG1,ClkHan [4] swi ClkHan movb #$08,TFLG1 ;clr IC3F [4] ldx TIME [3] inx [1] stx TIME [3] rti org $FFE8 ;timer channel 3 fdb IC3Han ; Program 6.4. Initialization for period measurement. ; MC68HC812A4 ;external signal to PT1/IC1 Period rmb 2 ;units 500 ns First rmb 2 ;TCNT at first edge Done rmb 1 ;set each rising Init sei ;make atomic bclr TIOS,$02 ;PT1=input capture bclr DDRT,$02 ;PT1 is input movb #$80,TSCR ;enable TCNT movb #$32,TMSK2 ;500ns clk ldaa TCTL4 ;old value anda #$F3 ;Clear EDG1B=0 oraa #$04 ;EDG1BA =01 staa TCTL4 ;on rise of IC1 movw TCNT,First ;init global clr Done movb #$02,TFLG1 ;clear IC1F bset TMSK1,$02 ;Arm IC1F cli ;enable rts ; Program 6.5. ISR for period measurement. ; MC68HC812A4 IC1Han movb #$08,TFLG1 ;clear C3F [4] ldd TC1 [3] subd First [3] std Period [3] movw TC1,First [6] movb #$FF,Done [4] rti [8] org $FFEC ;timer channel 1 fdb IC1Han ; Program 6.11. Ritual written in assembly language for the pulse width measurement. ; MC68HC812A4 ; B=PB7, Q=PT2/IC2 Init bclr TIOS,$04 ;PT2=input capt. movb #$90,TSCR ;enable, fast clr movb #$32,TMSK2 ;500 ns clk clr TMSK1 ;gadfly, C2I=0 bclr DDRB,$80 rts ; Program 6.12. Assembly language pulse width measurement. ; MC68HC812A4 ; return Reg D as R in Kohm Meas movb #$10,TCTL4 ;Rising edge movb #$04,TFLG1 ;C2F=0 bclr PORTB,$80 ;PB7=0 bset PORTB,$80 ;PB7=0 First brclr TFLG1,$04,First ;Wait for first rising edge ldy TC2 ;TCNT at rising, C2F=0 movb #$20,TCTL4 ;Falling edge pshy ;Save on stack Second brclr TFLG1,$04,Second ;Wait for next falling edge ldd TC2 ;TCNT at falling subd 2,SP+ ;RegD=pulse width 1000 to 2000 cyc subd #1000 ;0<=R<=1000Kohm rts ; Program 6.19. Assembly language ritual for periodic interrupt using output compare. ; MC68HC812A4 TIME rmb 2 ;inc every 1ms Init sei ;make atomic bset TIOS,$20 ;OC5 movb #$80,TSCR ;enable movb #$32,TMSK2 ;500 ns clk bset TMSK1,$20 ;Arm OC5 movw #0,TIME ;initialize movb #$20,TFLG1 ;clear C5F ldd TCNT ;current time addd #2000 ;first in 1 ms std TC5 cli ;enable rts ; Program 6.20. Assembly language ISR for periodic interrupt using output compare. ; MC68HC812A4 OC5HAN ldx TIME [3] inx [1] stx TIME [2] movb #$20,TFLG1 ;Acknowledge ldd TC5 addd #2000 ;next std TC5 rti org $FFE4 fdb OC5HAN ; Program 6.22. Ritual for the assembly language implementation of the squarewave using output compare. ; MC68HC812A4 Period rmb 2 ;units µsec Init sei ;make atomic bset TIOS,$08 ;OC3 bset DDRT,$08 ;PT3 output movb #$80,TSCR ;enable movb #$32,TMSK2 ;500 ns clk bset TMSK1,$08 ;Arm OC3 bset TCTL2,$40 ;OL3=1 bclr TCTL2,$80 ;OM3=0 movb #$08,TFLG1 ;clear C3F ldd TCNT ;current time addd #50 ;first in 25µs std TC3 cli ;enable rts ; Program 6.23. Assembly language squarewave using output compare. ; MC68HC812A4 OC3HAN movb #$08,TFLG1 ;Ack [4] ldd TC3 [3] addd Period ;next [3] std TC3 [2] rti [8] org $FFE8 fdb OC3HAN ; Program 6.25. Assembly language initialization pulse-width modulated squarewave using output compare. ; MC68HC812A4 High rmb 2 ;number of cycles high Low rmb 2 ;number of cycles low RITUAL sei ;make atomic bset TIOS,$08 ;OC3 bset DDRT,$08 ;PT3 output movb #$80,TSCR ;enable movb #$32,TMSK2 ;500 ns clk bset TMSK1,$08 ;Arm OC3 bset TCTL2,$C0 ;OL3=1,OM3=1 movb #$08,TFLG1 ;clear C3F ldd TCNT ;current time addd #50 ;first in 25µs std TC3 cli ;enable rts ; Program 6.26. Assembly language ISR for pulse-width modulated squarewave using output compare. ; MC68HC812A4 OC3HAN movb #$08,TFLG1 ;Ack [4] ldaa TCTL2 ;rise/fall? [3] bita #$40 [1] beq zero [1/3] one ldd TC3 [3] addd High ;now PT3 is 1 [3] std TC3 [2] bclr TCTL2,$40 [4] bra done [3] zero ldd TC3 [3] addd Low ;now PT3 is 0 [3] std TC3 [2] bset TCTL2,$40 [4] done rti [8] org $FFE8 fdb OC3HAN