; Chapter 3 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 ; PortT is DATA, PM0=GO Init movb #$FF,DDRT ;outputs movb #$01,DDRM bset PTM,#$01 ;GO=1 bsr Timer_Init ;Program 2.6 rts Out staa PTT ;set data bclr PTM,#$01 ;GO=0 bset PTM,#$01 ;GO=1 ldd #10000 ;10000us=10ms bsr Timer_Wait ;wait 10ms rts ;Program 3.3. Assembly language routines to initialize and output to a printer. ; MC9S12C32 ; PortT is DATA, PM0=GO Init clr DDRT ;data input bset DDRM,#$01 bclr PTM,#$01 ;GO=0 bsr Timer_Init ;Prog 2.6 rts In bset PTM,#$01 ;GO=1 bclr PTM,#$01 ;GO=0 ldd #10 bsr Timer_Wait ;10us ldaa PTT rts ;Program 3.5. Assembly language routines to initialize and read from an ADC. ; MC9S12C32 ; PT6-0 is DATA, PJ7=STROBE Init movb #$80,DDRT ;PT7 unused output bclr DDRJ,#$80 bset PPSJ,#$80 ;rise on PJ7 movb #$80,PIFJ ;clear flag7 clr PTT ;PT7=0 rts In brclr PIFJ,#$80,In movb #$80,PIFJ ;clear flag7 ldaa PTT rts ;Program 3.7. Assembly language routines to initialize and read from a keyboard. ; MC9S12C32 ; PortT=DATA PJ7=DONE PJ6=GO Init movb #$40,DDRJ ;PJ7 input,PJ6 output movb #$80,PPSJ ;rise on PJ7 clr DDRT ;PT7-0 inputs bclr PTJ,#$40 ;GO=0 rts In movb #$80,PIFJ ;clear flag7 bset PTJ,#$40 ;GO=1 bclr PTJ,#$40 ;GO=0 ;wait for rise of DONE loop brclr PIFJ,#$02,loop ldaa PTT rts ;Program 3.9. Assembly language routines to initialize and read from an ADC. ; MC9S12C32 ; PortT=DATA PJ7=READY PJ6=ACK Init movb #$40,DDRJ ;PJ7 in, PJ6 out movb #$80,PPSJ ;rise on PJ7 movb #$80,PIFJ ;clear flag7 clr DDRT ;PT7-0 inputs bset PTJ,#$40 ;ACK=1 rts In brclr PIFJ,#$80,In bclr PTJ,#$40 ;ACK=0 ldaa PTT ;read DATA movb #$80,PIFJ ;clear flag7 bset PTJ,#$40 ;ACK=1 rts ;Program 3.11. Handshaking assembly language routines to initialize and read from a sensor. ; MC9S12C32 ; PortT=DATA PJ7=READY PJ6=ACK Init movb #$40,DDRJ ;PJ7 in, PJ6 out movb #$80,PPSJ ;rise on PJ7 movb #$80,PIFJ ;clear flag7 movb #$FF,DDRT ;PT7-0 outputs bset PTJ,#$40 ;START=1 rts Out movb #$80,PIFJ ;clear flag7 bclr PTJ,#$40 ;START=0 staa PTT ;write DATA bset PTJ,#$40 ;ACK=1 wait brclr PIFJ,#$80,wait rts ;Program 3.14. Handshaking assembly language routines to initialize and write to a printer. ; 6812 ; PT7=RST PT6=CLK PT5=DQ DS_Init ldaa #$E0 ;PT7-5 output staa DDRT ldaa #$60 ;RST=0,CLK=1 staa PTT ;DQ=1 rts ;Program 3.16. Assembly language initialization of the DS1620 ; 6812 out8 ldab #8 ;8 bits clop bclr PTT,#$40 ;CLK=0 lsra ;lsb first bcc set0 bset PTT,#$20 ;DQ=1 bra next set0 bclr PTT,#$20 ;DQ=0 next bset PTT,#$40 ;CLK=1 decb bne clop rts DS_Start bset PTT,#$80 ;RST=1 ldaa #$EE bsr out8 bclr PTT,#$80 ;RST=0 rts DS_Stop bset PTT,#$80 ;RST=1 ldaa #$22 bsr out8 bclr PTT,#$80 ;RST=0 rts ;Program 3.18. Assembly language helper functions for the DS1620 ; 6812 DS_Config psha bset PTT,#$80 ;RST=1 ldaa #$0C bsr out8 pula bsr out8 bclr PTT,#$80 ;RST=0 rts ;Program 3.21. Assembly language functions to set the configuration register on the DS1620 ; 6812 out9 ldx #9 ;9 bits olop bclr PTT,#$40 ;CLK=0 lsrd ;lsb bcc oset0 bset PTT,#$20 ;DQ=1 bra onext oset0 bclr PTT,#$20 ;DQ=0 onext bset PTT,#$40 ;CLK=1 dex bne olop rts ;Program 3.22. Assembly language 9-bit output helper function for the DS1620 ; 6812 ;Reg D is temperature value DS_WriteTH pshd bset PTT,#$80 ;RST=1 ldaa #$01 bsr out8 puld bsr out9 bclr PTT,#$80 ;RST=0 rts ;Reg D is temperature value DS_WriteTL pshd bset PTT,#$80 ;RST=1 ldaa #$02 bsr out8 puld bsr out9 bclr PTT,#$80 ;RST=0 rts ;Program 3.23. Assembly language functions to set the threshold registers on the DS1620 ; 6812 ;input 8 bits, Reg A=data in8 ldx #8 ;8 bits ldaa DDRT anda #$DF ;DQ input staa DDRT ilop bclr PTT,#$40 ;CLK=0 lsra ;lsb brclr PTT,#$20,inext oraa #$80 ;DQ=1 inext bset PTT,#$40 ;CLK=1 dex bne ilop ldab DDRT orab #$20 ;DQ output stab DDRT rts ;Reg A is config value DS_ReadConfig bset PTT,#$80 ;RST=1 ldaa #$AC bsr out8 bsr in8 bclr PTT,#$80 ;RST=0 rts ;Program 3.25. Assembly language functions to read the configuration register on the DS1620 ; 6812 ;input 9 bits, Reg D=data in9 ldx #9 ;9 bits ldaa DDRT anda #$DF ;DQ input staa DDRT clra jlop bclr PTT,#$40 ;CLK=0 lsrd ;lsb brclr PTT,#$20,jnext oraa #$01 ;DQ=1 jnext bset PTT,#$40 ;CLK=1 dex bne jlop ldab DDRT orab #$20 ;DQ output stab DDRT rts ;Program 3.27. Assembly language 9-bit read helper function for the DS1620 ; 6812 ;Reg D returned as TH value DS_ReadTH bset PTT,#$80 ;RST=1 ldaa #$A1 bsr out8 bsr in9 bclr PTT,#$80 ;RST=0 rts ;Reg D returned as TL value DS_ReadTL bset PTT,#$80 ;RST=1 ldaa #$A2 bsr out8 bsr in9 bclr PTT,#$80 ;RST=0 rts ;Reg D returned as temperature DS_ReadT bset PTT,#$80 ;RST=1 ldaa #$AA bsr out8 bsr in9 bclr PTT,#$80 ;RST=0 rts ;Program 3.28. Assembly language functions to read the temperatures from the DS1620 ; Initalize 6812 SCI ; Inputs: none Outputs: none SCI_Init movb #$0c,SCICR2 ;enable SCI movw #13,SCIBD ;19200 bps rts ; Read 8 bits from serial port ; Return 8-bit byte in RegA RDRF equ $20 SCI_InChar ldaa SCISR1 ; serial status anda #RDRF ; available? beq SCI_InChar ; wait RDRF ldaa SCIDRL ; read ASCII rts ; Write 8 bits to serial port ; Input 8-bit byte in RegA TDRE equ $80 SCI_OutChar ldab SCISR1 ; serial status andb #TDRE ; output ready? beq SCI_OutChar ; wait TDRE staa SCIDRL ; write ASCII rts ;Program 3.31. Assembly functions that implement serial I/O. ; programs from first edition ; Program 3.3. Assembly language routines to initialize and output to a printer. ; MC68HC812A4 ; PortJ is DATA, PH0=GO Init ldaa #$FF staa DDRJ ;outputs ldaa #$01 staa DDRH staa PORTH ;GO=1 rts Out staa PORTJ ;set data ldaa #0 staa PORTH ;GO=0 ldaa #1 staa PORTH ;GO=1 ldd #16000 OLoop subd #1 ; [2] bne OLoop ; [3] rts ; Program 3.6. 6811 or 6812 assembly language routine to create an accurate time delay. ; 6811 or 6812 ; Reg D is the number of cycles to wait ; can range from 25 to 32767 Wait addd TCNT time at the end of delay Loop cpd TCNT wait for Endt-TCNT>0 bpl Loop rts ; Program 3.8. Assembly language routines to initialize and read from an A/D. ; MC68HC812A4 ; PortJ is DATA, PH0=GO Init clr DDRJ ldaa #$FF staa DDRH clr PORTH ;GO=0 rts In ldaa #1 staa PORTH ;GO=1 clr PORTH ;GO=0 ldaa #10 loop deca ;[1] bne loop ;[3] lda PORTJ rts ; Program 3.10. Assembly language routines to initialize and read from a keyboard. ; MC68HC812A4 ; PJ6-0 is DATA, PJ7=STROBE Init clr DDRJ ldaa #$80 ;rise on PJ7 staa KPOLJ ;key wakeup staa KWIFJ ;clear flag rts In brclr KWIFJ,$80,In ldaa #$80 staa KWIFJ ;clear flag ldaa PORTJ anda #$7F rts ; Program 3.12. Assembly language routines to initialize and read from an A/D. ; MC68HC812A4 ; PortH=DATA PJ1=DONE PJ0=GO Init ldaa #$01 ;PJ1 input staa DDRJ ;PJ0 output ldaa #$02 ;rise on PJ1 staa KPOLJ ;key wakeup clr DDRH ;PA inputs clr PORTJ ;GO=0 rts In ldaa #$02 staa KWIFJ ;clear flag ldaa #1 staa PORTJ ;GO=1 clr PORTJ ;GO=0 ;wait for rise of DONE Loop brclr KWIFJ,$02,Loop ldaa PORTH rts ; Program 3.14. Handshaking assembly language routines to initialize and read from a sensor. ; MC68HC812A4 ; PortH=DATA PJ1=READY PJ0=ACK Init ldaa #$01 ;PJ1 input staa DDRJ ;PJ0 output ldaa #$02 ;rise on PJ1 staa KPOLJ ;key wakeup ldaa #$02 staa KWIFJ ;clear flag1 clr DDRH ;PH inputs ldaa #$01 staa PORTJ ;ACK=1 rts In brclr KWIFJ,$02,In clr PORTJ ;ACK=0 ldaa PORTH ;read DATA ldab #$02 stab KWIFJ ;clear flag inc PORTJ ;ACK=1 rts ; Program 3.17. Handshaking assembly language routines to initialize and write to a printer. ; MC68HC812A4 ; PortH=DATA PJ1=READY PJ0=ACK Init ldaa #$01 ;PJ1 input staa DDRJ ;PJ0 output ldaa #$02 ;rise on PJ1 staa KPOLJ ;key wakeup ldaa #$02 staa KWIFJ ;clear flag ldaa #$FF staa DDRH ;PH outputs ldaa #$01 staa PORTJ ;START=1 rts Out ldab #$02 stab KWIFJ ;clear flag clr PORTJ ;START=0 staa PORTH ;write DATA inc PORTJ ;ACK=1 Wait brclr KWIFJ,$02,Wait rts ; Program 3.19. Assembly language initialization of the DS1620 ; MC68HC812A4/MC68HC912B32 ; PS7=RST PS6=CLK PS5=DQ Init ldaa #$E0 ;PD5-3 output staa DDRS ldaa #$60 ;RST=0,CLK=1 staa PORTS ;DQ=1 rts ; Program 3.21. Assembly language helper functions for the DS1620 ; MC68HC812A4/MC68HC912B32 out8 ldab #8 ;8 bits clop bclr PORTS,#$40 ;CLK=0 lsra ;lsb first bcc set0 bset PORTS,#$20 ;DQ=1 bra next set0 bclr PORTS,#$20 ;DQ=0 next bset PORTS,#$40 ;CLK=1 decb bne clop rts start bset PORTS,#$80 ;RST=1 ldaa #$EE bsr out8 bclr PORTS,#$80 ;RST=0 rts stop bset PORTS,#$80 ;RST=1 ldaa #$22 bsr out8 bclr PORTS,#$80 ;RST=0 rts ; Program 3.23. Assembly language functions to set the configuration register on the DS1620 ; MC68HC812A4/MC68HC912B32 config psha bset PORTS,#$80 ;RST=1 ldaa #$0C bsr out8 pula bsr out8 bclr PORTS,#$80 ;RST=0 rts ; Program 3.25. Assembly language 9-bit output helper function for the DS1620 ; MC68HC812A4/MC68HC912B32 out9 ldx #9 ;9 bits olop bclr PORTS,#$40 ;CLK=0 lsrd ;lsb bcc oset0 bset PORTS,#$20 ;DQ=1 bra next oset0 bclr PORTS,#$20 ;DQ=0 onext bset PORTS,#$40 ;CLK=1 dex bne olop rts ; Program 3.26. Assembly language functions to set the threshold registers on the DS1620 ; MC68HC812A4/MC68HC912B32 ;Reg D is temperature value WriteTH pshd bset PORTS,#$80 ;RST=1 ldaa #$01 bsr out8 puld bsr out9 bclr PORTS,#$80 ;RST=0 rts ;Reg D is temperature value WriteTL pshd bset PORTS,#$80 ;RST=1 ldaa #$02 bsr out8 puld bsr out9 bclr PORTS,#$80 ;RST=0 rts ; Program 3.28. Assembly language functions to read the configuration register on the DS1620 ; MC68HC812A4/MC68HC912B32 ;input 8 bits, Reg A=data in8 ldx #8 ;8 bits ldaa DDRD anda #$DF ;DQ input staa DDRD ilop bclr PORTS,#$40 ;CLK=0 lsra ;lsb brclr PORTS,#$20,inext oraa #$80 ;DQ=1 inext bset PORTS,#$40 ;CLK=1 dex bne ilop ldab DDRS orab #$20 ;DQ output stab DDRS rts ;Reg A is config value ReadConfig bset PORTS,#$80 ;RST=1 ldaa #$AC bsr out8 bsr in8 bclr PORTS,#$80 ;RST=0 rts ; Program 3.29. Assembly language 9-bit read helper function for the DS1620 ; MC68HC812A4/MC68HC912B32 ;input 9 bits, Reg D=data in9 ldx #9 ;9 bits ldaa DDRD anda #$DF ;DQ input staa DDRD clra jlop bclr PORTS,#$40 ;CLK=0 lsrd ;lsb brclr PORTS,#$20,jnext oraa #$01 ;DQ=1 jnext bset PORTS,#$40 ;CLK=1 dex bne ilop ldab DDRS orab #$20 ;DQ output stab DDRS rts ; Program 3.30. Assembly language functions to read the temperatures from the DS1620 ; MC68HC812A4/MC68HC912B32 ;Reg D returned as TH value ReadTH bset PORTS,#$80 ;RST=1 ldaa #$A1 bsr out8 bsr in9 bclr PORTS,#$80 ;RST=0 rts ;Reg D returned as TL value ReadTL bset PORTS,#$80 ;RST=1 ldaa #$A2 bsr out8 bsr in9 bclr PORTS,#$80 ;RST=0 rts ;Reg D returned as temperature ReadT bset PORTS,#$80 ;RST=1 ldaa #$AA bsr out8 bsr in9 bclr PORTS,#$80 ;RST=0 rts