#include "c:\miniIDE\hcs12.inc" org $1000 buf ds.b 7 tready ds.b 1 ; flag to indicate a new time info. is ready org $1500 lds #$1500 ; set up stack pointer movw #PJ_ISR,UserPortJ ; set up Port J interrupt vector ldaa #$1F ; value to set I2C baud rate to 100 KHz ldaa #$FE ; the slave address for the I2C module jsr openI2C ; initialize I2C module ldab #$90 ; control byte for DS1307 jsr openDS1307 ; initialize DS1307 jsr openLCD ; configure LCD jsr getTime ; read time and calendar information and store in buf[] sei ; disable interrupt ldaa #$D0 ; pass DS1307 ID in A ldaa #$0 ; pass second address in B ldx #buf ; pass pointer to buf in X jsr sendTime ; send time information to DS1307 swi ; **************************************************************************** ; The following function prompt the user to enter time and calendar ; information using the DIP switches. After setting the time or calendar ; information, he/she press the button connected to the PJ0 pin. ; **************************************************************************** getTime pshx pshy ldy #buf ; Y is the pointer to the buffer movb #$FF,ATD1DIEN ; enable Port AD1 for digital inputs bclr DDRJ,BIT0 ; enable PJ0 pin for input bset PERJ,BIT0 ; enable pullup or pulldown on PJ0 pin bclr PPSJ,BIT0 ; enable pulldown so that interrupt is rising edge triggered bset PIEJ,BIT0 ; enable PJ0 interrupt cli ; " ldaa #$80 ; set LCD cursor to the upper left corner jsr cmd2lcd ; " ldx #prompty ; output the prompt "Enter year:" jsr puts2lcd ; " waity tst tready ; is new year info. ready? beq waity ; " movb PTAD1,1,y+ ; save year info. in buffer movb #0,tready ldaa #$80 ; set LCD cursor to the upper left corner jsr cmd2lcd ; " ldx #promptm ; output the prompt "Enter month:" jsr puts2lcd ; " waitm tst tready ; is new month info. ready? beq waitm ; " movb PTAD1,1,y+ ; save month info. in buffer movb #0,tready ; clear the ready flag ldaa #$80 ; set LCD cursor to the upper left corner jsr cmd2lcd ; " ldx #prompte ; output the prompt "Enter date:" jsr puts2lcd ; " waite tst tready ; is new date info. ready? beq waite ; " movb PTAD1,1,y+ ; save date info. in buffer movb #0,tready ; clear the ready flag ldaa #$80 ; set LCD cursor to the upper left corner jsr cmd2lcd ; " ldx #promptd ; output the prompt "Enter day:" jsr puts2lcd ; " waitd tst tready ; is new day info. ready? beq waitd ; " movb PTAD1,1,y+ ; save day info. in buffer movb #0,tready ldaa #$80 ; set LCD cursor to the upper left corner jsr cmd2lcd ; " ldx #prompth ; output the prompt "Enter hours:" jsr puts2lcd ; " waith tst tready ; is new hour info. ready? beq waith ; " movb PTAD1,1,y+ ; save hour info. in buffer movb #0,tready ldaa #$80 ; set LCD cursor to the upper left corner jsr cmd2lcd ; " ldx #promptmi ; output the prompt "Enter minutes:" jsr puts2lcd ; " waitmi tst tready ; is new minute info. ready? beq waitmi ; " movb PTAD1,1,y+ ; save hour info. in buffer movb #0,tready ldaa #$80 ; set LCD cursor to the upper left corner jsr cmd2lcd ; " ldx #prompts ; output the prompt "Enter seconds:" jsr puts2lcd ; " waits tst tready ; is new second info. ready? beq waits ; " movb PTAD1,1,y+ ; save second info. in buffer movb #0,tready puly pulx rts ; **************************************************************************** ; The following function will send the time and calendar information pointed ; to by X to the DS1307. The device ID and the starting register address are ; passed in A and B, respectively. ; **************************************************************************** sendTime jsr sendSlaveID ; send out device ID of the DS1307 brclr IBSR,RXAK,sndTimeOK1 ; did DS1307 acknowledge? ldab #$FF ; return error code -1 if not acknowledged rts sndTimeOK1 stab IBDR ; send out register address for seconds brclr IBSR,IBIF,* ; wait until seconds' address has been shifted out movb #IBIF,IBSR ; clear the IBIF flag brclr IBSR,RXAK,sndTimeOK2 ; did DS1307 acknowledge? ldab #$FF ; return error code -1 if not acknowledged rts sndTimeOK2 ldy #7 ; byte count tfr X,D ; set X to point to second’s value addd #6 ; “ tfr D,X ; “ sndloop movb 1,x-,IBDR ; send out one byte brclr IBSR,IBIF,* movb #IBIF,IBSR brclr IBSR,RXAK,sndTimeOK3 ; did DS1307 acknowledge? ldab #$FF ; return error code -1 if not acknowledged rts sndTimeOK3 dbne y,sndloop ; continue until all bytes have been sent out bclr IBCR,MSSL ; generate a stop condition. ldab #0 ; return normal return code 0 rts ; **************************************************************************** ; The following function has two incoming parameters passed in accumulator A ; and B to set up baud rate and slave address respectively. ; **************************************************************************** openI2C bset IBCR,IBEN ; enable I2C module staa IBFD ; establish SCL frequency stab IBAD ; establish I2C module slave address bclr IBCR,IBIE ; disable I2C interrupt bset IBCR,IBSWAI ; disable I2C in wait mode rts ; **************************************************************************** ; The following function generates a start condition and sends out the slave ; ID to the I2C bus. ; **************************************************************************** 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 sends out the DS1307 ID, control register address, and the ; control byte to configure the DS1307. The DS1307 ID is passed in A. The ; control register address is passed in B. The control byte is passed in X. ; **************************************************************************** openDS1307 ldaa #$D0 ; slave ID of DS1307 jsr sendSlaveID brclr IBSR,RXAK,sndRegAdr ldab #$FF ; return error code -1 rts sndRegAdr movb #$07,IBDR ; send out the control register address brclr IBSR,IBIF,* ; wait util the register address is shifted out movb #IBIF,IBSR ; clear the IBIF flag stab IBDR ; send out control byte brclr IBSR,IBIF,* ; wait until the control byte is shifted out movb #IBIF,IBSR bclr IBCR,MSSL ; generate stop condition ldab #0 ; send back normal return code 0 rts ; ***************************************************************************** ; The following function is the port J interrupt service routine. ; ***************************************************************************** PJ_ISR movb #1,tready movb #1,PIFJ ; clear the PIFJ0 flag rti #include "c:\miniIDE\lcd_util_dragon12.asm" #include "c:\miniIDE\delay.asm" prompts fcc "Enter seconds:" dc.b 0 promptmi fcc "Enter minutes:" dc.b 0 prompth fcc "Enter hours:" dc.b 0 promptd fcc "Enter day:" dc.b 0 prompte fcc "Enter date:" dc.b 0 promptm fcc "Enter month:" dc.b 0 prompty fcc "Enter year:" dc.b 0 end