#include "c:\miniIDE\hcs12.inc" org $1000 org $1500 lds #$1500 jsr openDS1631A swi openDS1631A ldaa #$92 jsr sendSlaveID brclr IBSR,RXAK,openOK0 ; did DS1631A acknowledge? ldab #$FF ; return error code -1 rts openOK0 movb #$AC,IBDR ; send the "Access Config" command brclr IBSR,IBIF,* movb #IBIF,IBSR ; clear the IBIF flag brclr IBSR,RXAK,openOK1 ; did DS1316A acknowledge? ldab #$FF rts openOK1 ldaa #$E0 ; configure DS1631A to perform conversion staa IBDR ; continuously, 12-bit resolution, TOUT active high brclr IBSR,IBIF,* ; wait until the byte has been shifted out movb #IBIF,IBSR ; clear the IBIF flag brclr IBSR,RXAK,openOK2 ; did DS1316A acknowledge? ldab #$FF rts openOK2 bclr IBCR,MSSL ; generate a stop condition ldab #0 ; normal return code rts ; *********************************************************************************** ; The following function generates a start condition and sends out the slave ID. The ; slave ID is passed in accumulator A. ; *********************************************************************************** sendSlaveID brset IBSR,IBB,* ; wait until I2C bus is free bset IBCR,TXRX+MSSL ; generate a start condition staa IBDR ; send out the slave address wait_I2C brclr IBSR,IBIF,* ; wait for address transmission to complete movb #IBIF,IBSR ; clear the IBIF flag rts ; *********************************************************************************** ; The following function starts the temperature conversion on the DS1631A. ; *********************************************************************************** startConv ldaa #$92 jsr sendSlaveID ; generate a start condition and send DS1631's ID brclr IBSR,RXAK,startOK0 ; did DS1631 acknowledge? ldab #$FF ; return error code -1 rts startOK0 movb #$51,IBDR ; send "Start Convert T" command brclr IBSR,IBIF,* ; wait until the byte is shifted out movb #IBIF,IBSR ; clear the IBIF flag brclr IBSR,RXAK,startOK1 ; did DS1631 acknowledge? ldab #$FF rts startOK1 bclr IBCR,MSSL ; generate a stop condition ldab #0 ; normal return code rts ; *********************************************************************************** ; The following function sets a new high temperature to the DS1631A. The high and ; lower half of TH are pushed into the stack. ; *********************************************************************************** THhi equ 2 THlo equ 3 setTH ldaa #$92 jsr sendSlaveID brclr IBSR,RXAK,setTHOK1 ; did DS1631A acknowledge? ldab #$FF ; return error code -1 rts setTHOK1 movb #$A1,IBDR ; send out access TH command */ brclr IBSR,IBIF,* ; wait until command is shifted out movb #IBIF,IBSR ; clear IBIF flag brclr IBSR,RXAK,setTHOK2 ; did DS1631A acknowledge? ldab #$FF rts setTHOK2 ldaa THhi,sp ; get the upper byte of TH from stack staa IBDR ; send out TH high byte brclr IBSR,IBIF,* movb #IBIF,IBSR ; clear the IBIF flag brclr IBSR,RXAK,setTHOK3 ; did DS1631A acknowledge? ldab #$FF rts setTHOK3 ldaa THlo,sp ; get the lower byte of TH from stack staa IBDR brclr IBSR,IBIF,* movb #IBIF,IBSR ; clear the IBIF flag brclr IBSR,RXAK,setTHOK4 ; did DS1631A acknowledge? ldab #$FF rts setTHOK4 bclr IBCR,MSSL ; generate the stop condition ldab #0 ; normal return code rts ; *********************************************************************************** ; The following function reads the Config register and returns the result in B. ; *********************************************************************************** readConf ldaa #$92 ; generate a start condition and sends out DS1631 jsr sendSlaveID ; ID brclr IBSR,RXAK,rdConfok1 ; did DS1631A acknowledge? ldab #$FF ; return error code -1 rts rdConfok1 movb #$AC,IBDR ; send out the Access Conf command brclr IBSR,IBIF,* ; wait until the command is shifted out movb #IBIF,IBSR ; clear the IBIF flag brclr IBSR,RXAK,rdConfok2 ; did DS1631A acknowledge? ldab #$FF ; return error code -1 rts rdConfok2 bset IBCR,RSTA ; generate a restart condition movb #$93,IBDR ; send out slave ID and set R/W bit to 1 brclr IBSR,IBIF,* ; wait until the command is shifted out movb #IBIF,IBSR ; clear the IBIF flag brclr IBSR,RXAK,rdConfok3 ; did DS1631A acknowledge? ldab #$FF ; return error code -1 rts rdConfok3 bclr IBCR,TXRX ; prepare to read bset IBCR,TXAK ; prepare to send NACK ldaa IBDR ; perform a dummy read to trigger 9 clock pulses brclr IBSR,IBIF,* ; wait until a byte has been shifted in movb #IBIF,IBSR ; clear the IBIF flag bclr IBCR,MSSL ; generate a stop condition ldab IBDR ; place the Conf register value in B rts ; *********************************************************************************** ; The following function reads the converted temperature and return the temperature ; in double accumulator D. A -1 is returned in X for any error. ; *********************************************************************************** readTemp ldx #0 ; initialize return error code rdLoop jsr readConf ; is temperature conversion done yet? cmpb #-1 ; is there any error? beq rdErr ; " andb #$80 ; check DONE bit bpl rdLoop ; conversion not done yet? ldaa #$92 ; generate a start condition and send out jsr sendSlaveID ; the DS1631A ID brclr IBSR,RXAK,rdTempok1 ; did DS1631A acknowledge? ldx #-1 rts rdTempok1 movb #$AA,IBDR ; sends "Read Temperature command" brclr IBSR,IBIF,* ; movb #IBIF,IBSR brclr IBSR,RXAK,rdTempok2 ; did DS1631A acknowledge? ldx #-1 rts rdTempok2 bset IBCR,RSTA ; generate a restart condition movb #$93,IBDR ; send DS1631A's ID with R/W set to 1 brclr IBSR,IBIF,* movb #IBIF,IBSR brclr IBSR,RXAK,rdTempok3 ; did DS1631A acknowledge? ldx #-1 rts rdTempok3 bclr IBCR,TXRX+TXAK ; prepare to receive and ACK ldaa IBDR ; perform a dummy read to initiate a read operation brclr IBSR,IBIF,* ; wait until the high byte of temperature to shift in movb #IBIF,IBSR bset IBCR,TXAK ; prepare send NACK for the last read ldaa IBDR ; place the high byte of Temperature in A brclr IBSR,IBIF,* ; wait for the low byte read to complete movb #IBIF,IBSR ; clear the IBIF flag bclr IBCR,MSSL ; generate a STOP condition ldab IBDR ; place the low byte of temperature in B ldx #0 ; normal return code 0 rts rdErr ldx #-1 rts end