#include "c:\miniIDE\hcs12.inc" period equ $2E ; ASCII code of period character degree equ 223 ; ASCII code of degree character org $1000 quo ds.b 1 rem ds.b 1 sign ds.b 1 fract ds.b 1 buf ds.b 8 org $1500 lds #$1500 ; set up stack pointer ldy #2 ; wait for LCD to complete jsr delayby100ms ; internal configuratin jsr openlcd ; configure LCD ldaa #$80 ; set LCD cursor to upper jsr lcd_cmd ; left corner ldx #msg1 ; output "Temperature = " jsr puts2lcd ; " jsr openAD0 ; configure ATD0 module forever movb #$20,buf ; initialize the buffer to 0.0 movb #$20,buf+1 ; " movb #$30,buf+2 ; " movb #period,buf+3 ; " movb #$30,buf+4 ; " movb #degree,buf+5 ; degree character movb #$43,buf+6 ; letter 'C' movb #0,buf+7 ; null character movb #$87,ATD0CTL5 ; start an ATD conversion sequence movb #0,sign ; initialize sign to positive movb #$30,fract ; initialize fractional digit to 0 brclr ATD0STAT0,SCF,* ; wait for the conversion to complete ldd ATD0DR0 ; read a conversion result ldy #10 ; compute result x 10 / 62 emul ; " ldx #62 ; " ediv ; " stab rem ; save the remainder tfr y,d ; transfer quotient to B subb #40 ; subtract temperature offset bhs save_quo ; if non-negative, don't touch remainder negb ; compute 2's complement of quotient stab quo movb #1,sign ; temperature is negative ldab rem ; if remainder is 0, skip a few instruction beq convert ; ldab #62 ; compute 62 - rem subb rem ; " stab rem ; " bra cal_fract save_quo stab quo ; save updated quotient cal_fract ldab rem beq convert ; come here when positive ldaa #10 ; compute fractional digit mul ; " ldx #62 ; " idiv ; " cmpb #31 ; round off fractional digit blt no_round ; " inx ; " cpx #10 ; " bne no_round ; " inc quo ; " bra convert ; prepare to separate integer digits no_round tfr x,d ; convert fractional digit to ASCII code addb #$30 ; " stab fract ; " convert ldab quo clra ; " ldx #10 ; use repeated divide by 10 to separate idiv ; integral digits addb #$30 ; " stab buf+2 ; save the one's digit tfr x,d ; " tstb ; is quo zero? beq add_fra ; if integral part is 0 ldx #10 ; separate the ten's digit idiv addb #$30 ; convert and store the ten's digit stab buf+1 ; " tfr x,d ; test hundred's digit tstb ; is quotient 0? beq add_fra movb #$31,buf ; hundreds digit, if any, is 1 only add_fra movb fract,buf+4 ; insert fraction digit ldaa sign ; check the sign beq out_it movb #$2D,buf ; when minus, add minus character out_it ldaa #$C0 ; set cursor to 2nd row jsr lcd_cmd ; " ldx #spaces ; clear the 2nd row of the LCD jsr puts2lcd ; " ldaa #$C5 ; set LCD cursor position jsr lcd_cmd ; " ldx #buf ; output the temperature string jsr puts2lcd ; " ldy #2 ; wait for 200 ms jsr delayby100ms ; " jmp forever ; continue ; ********************************************************************************* ; The following function perform the AD0 configuration. ; ********************************************************************************* openAD0 movb #$E0,ATD0CTL2 ; enable AD0, fast ATD flag clear, power down ATD in wait mode jsr wait20us ; wait until AD0 is stabilized movb #$0A,ATD0CTL3 ; perform one A/D conversion, finish current conversion than freeze movb #$25,ATD0CTL4 ; select 4 cycles of sample time, set prescaler divisor to 12 rts ; ********************************************************************************* ; The following function uses OC0 to create 20 us time delay. ; ********************************************************************************* wait20us movb #$90,TSCR1 ; enable TCNT, fast timer flag clear movb #0,TSCR2 ; set TCNT presclaer to 1 bset TIOS,OC0 ; enable OC0 ldd TCNT ; wait for 20 us addd #480 ; " std TC0 ; " lp_ad2 brclr TFLG1,C0F,lp_ad2 ; " rts #include "C:\miniIDE\lcd_util_dragon12.asm" #include "c:\miniIDE\delay.asm" msg1 fcc "Temperature = " dc.b 0 spaces fcc " " dc.b 0 end