;**************************************************************************** ;TITLE: Program to store and recover data from RTC, depending on choice ; of user. ; ;Format of instruction : c sc mn hr dd mm dy yy ; where, c : read RTC (R) or write to RTC (W) ; sc : seconds (00-59) ; mn : minutes (00-59) ; hr : hour (00-23) ; dd : date (00-31) ; mm : month (00-12) ; dy : day (00-07) ; yy : year (00-99) ; ;Crystal frequency : 3.58 MHz. ;Baud rate : 2400 ;To Read the RTC, Press 'R' on the PC terminal ;To write to the RTC, press 'W' and all the data in the above format with ;separated by a space. ; ;FUNCTIONS ;********* ;receive : receive 2 digits from comp & pack ;transmit : transmit the 2 packed digits to comp ;hextoasc : converts hex to ascii ;asctohex : converts ascii digit to hex ;rxcomp : receive ascii digit from comp ;txcomp : transmit ascii digit to comp ;rtcwrite : write data to RTC, in burst mode (whole routine) ;rxrtc : receive a byte from RTC ;txrtc : transmit a byte to RTC ;rtcread : receive data from RTC, in burst read (whole routine) ;**************************************************************************** .include "2313def.inc" .def rtemp = r16 .def treg = r17 ; data (in ASCII) to be transmitted to computer .def rreg = r18 ; data (in ASCII) received from computer .def cnt = r19 ; counter 0 .def datbyt = r20 ; data byte (packed-hex) Rx from or Tx to comp .def rtcdat = r21 ; data byte Rx from or Tx to RTC .def choice = r22 .def cnt1 = r23 ; counter 1 .def rtemp1 = r24 .cseg .org 0 rjmp RESET RESET: ldi rtemp, $a0 ; initialize stack out SPL, rtemp ldi rtemp, $5A ; Setting baud rate to 2400 at crystal out UBRR,rtemp ; frequency of 3.58 MHz rcall rxcomp ; get choice -- whether to read from or write mov choice,rreg ; to RTC rcall rxcomp ; wait for key press cpi choice,$52 ; if choice = R brne nxt rcall rtcread ; then receive data from RTC nxt: cpi choice,$57 ; else if choice = W brne nxt1 rcall rtcwrite ; then write data to clock nxt1: ldi treg,$0a ; leave a line after a command rcall txcomp rjmp RESET ; loop again ;***************************************************************************** ;'receive' ;Aim ; Function to receive 8 bits (2 digits) from comp ;Registers used ; rreg : contains hex data from user ; datbyt : contains packed data byte which is ; transferred to calling function ;Functions called ; rxcomp ; asctohex ;***************************************************************************** receive:rcall rxcomp ; get one digit from comp rcall asctohex ; convert received data to hex swap rreg ; swap nibbles mov datbyt,rreg ; copy rreg to data byte register rcall rxcomp ; get next digit from comp rcall asctohex add datbyt,rreg ret ; go to calling function ;***************************************************************************** ;'transmit' ;Aim ; Transmit data to computer - 8 bits ;Registers used ; treg : contains data to be transmitted ; datbyt : contains packed hex data from calling function ;Functions called ; hextoasc ; txcomp ;***************************************************************************** transmit: mov treg,datbyt swap treg ; swap nibbles rcall hextoasc ; convert one digit to ASCII rcall txcomp ; and transmit mov treg,datbyt rcall hextoasc ; convert next digit to ASCII rcall txcomp ; and transmit ret ; return if complete data transmitted ;***************************************************************************** ;'hextoasc' ;Aim ; Convert hex to ASCII. Hex value is passed from & ASCII value is passed ; back to calling function in register treg. ;Registers used ; rtemp temporary register ; treg register containing data ;Functions called ; none ;***************************************************************************** hextoasc: andi treg,$0f cpi treg,$0a brsh henxt1 ldi rtemp,$30 rjmp henxt2 henxt1: ldi rtemp,$37 henxt2: add treg,rtemp ret ;***************************************************************************** ;'asctohex' ;Aim ; Convert ASCII to hex. ASCII value to be converted is passed from & hex ; value is passed back to calling function in register rreg. ;Registers used ; rreg register containing data ;Functions called ; none ;***************************************************************************** asctohex: cpi rreg,$40 brsh asnxt subi rreg,$30 rjmp asnxt1 asnxt: subi rreg,$37 asnxt1: ret ;***************************************************************************** ;'rxcomp' ;Aim ; Function to receive data (1 digit) from computer. Value received is ; passed to calling function through register rreg. ;Registers used ; rtemp temporary register ; rreg register containing data received ;Functions called ; none ;***************************************************************************** rxcomp: ldi rtemp, $10 ; Enable receiver out UCR,rtemp rxpoll: sbis USR,7 ; Poll to check if char received rjmp rxpoll in rreg,UDR ; Put received data in rreg ret ;***************************************************************************** ;'txcomp' ;Aim ; Function to transmit data (1 digit) to computer. The value to be ; transmitted is passed from calling function into register treg. ;Registers used ; rtemp temporary register ; treg register containing data to be transmitted ;Functions called ; none ;***************************************************************************** txcomp: ldi rtemp, $08 ; Enable transmitter out UCR,rtemp out UDR,treg txpoll: sbis USR,5 ; Poll to check end of transmission rjmp txpoll ret ;****************************************************************************** ;'rtcwrite' ;Aim ; Complete function to write a data into rtc ;Registers used ; rtcdat register in which data to be transmitted is stored ; rtemp temporary register ; datbyt value passed from function receive ;Functions called ; txrtc ; rxcomp ; receive ;***************************************************************************** rtcwrite:ldi rtemp, 0b11111111 ; configure B as output port out DDRB,rtemp cbi PORTB,PINB3 ; clear reset cbi PORTB,PINB4 ; clear CLK sbi PORTB,PINB3 ; set reset ldi rtcdat,0b10001110 ; transmit address of control register rcall txrtc ldi rtcdat,0b00000000 ; transmit data to control register rcall txrtc sbi PORTB,PINB4 ; set clk cbi PORTB,PINB3 ; clear reset cbi PORTB,PINB4 ; clear clk sbi PORTB,PINB3 ; set reset ldi rtcdat, 0b10111110 ; initialise RTC in burst mode rcall txrtc ; transmit to RTC rcall receive ; receive seconds from comp mov rtemp1,datbyt ori datbyt,$80 ; set CH bit in seconds register mov rtcdat,datbyt rcall txrtc rcall rxcomp ; wait for key press rcall receive ; receive minutes from comp andi datbyt,$7F mov rtcdat,datbyt rcall txrtc rcall rxcomp ; wait for key press rcall receive ; receive hours from comp andi datbyt,$3F mov rtcdat,datbyt rcall txrtc rcall rxcomp ; wait for key press rcall receive ; receive date from comp andi datbyt,$3F mov rtcdat,datbyt rcall txrtc rcall rxcomp ; wait for key press rcall receive ; receive month from comp andi datbyt,$1F mov rtcdat,datbyt rcall txrtc rcall rxcomp ; wait for key press rcall receive ; receive day from comp andi datbyt,$07 mov rtcdat,datbyt rcall txrtc rcall rxcomp ; wait for key press rcall receive ; receive year from comp mov rtcdat,datbyt rcall txrtc ldi rtcdat,$00 ; write to control register rcall txrtc sbi PORTB,PINB4 ; set clk cbi PORTB,PINB3 ; clear reset cbi PORTB,PINB4 ; clear clk sbi PORTB,PINB3 ; set reset ldi rtcdat,$80 ; transmit addr of sec reg. rcall txrtc mov rtcdat,rtemp1 ; enable CH bit of sec reg andi rtcdat,$7F rcall txrtc sbi PORTB,PINB4 ; set clk cbi PORTB,PINB3 ; clear reset cbi PORTB,PINB4 ; clear clk sbi PORTB,PINB3 ; set reset ldi rtcdat,$8E rcall txrtc ldi rtcdat,$80 ; disable write to RTC - enable write rcall txrtc ; protect cbi PORTB,PINB3 ; clear reset to inhibit further operations ret ;**************************************************************************** ;'txrtc' ;Aim ; Function to write a byte to rtc. Value to be written is passed from ; calling function in register rtcdat. ;Registers used ; cnt to store no of bits ; rtcdat to store the data ;Functions called ; none ;***************************************************************************** txrtc: ldi cnt, $08 ; load counter txbck: ror rtcdat ; rotate data right through carry cbi PORTB,PINB4 ; clear SCLK brcc txnxt ; if carry set sbi PORTB,PINB2 ; set I/O rjmp txnxt1 ; else txnxt: cbi PORTB,PINB2 ; clear I/O txnxt1: sbi PORTB,PINB4 ; set SCLK dec cnt ; decrement counter brne txbck ; if counter not zero, go back nop cbi PORTB,PINB4 ; clear SCLK ret ;****************************************************************************** ;'rxrtc' ;Aim ; Function to read a byte from rtc. Value read is returned in register ; rtcdat ;Registers used ; cnt to store no of bits ; rtcdat to store the data read ;Functions called ; none ;***************************************************************************** rxrtc: ldi cnt,$08 ; load counter ldi rtcdat,0 rxbck: sbi PORTB,PINB4 ; set SCLK sbic PINB,PINB2 ; if I/O set then sec ; set carry sbis PINB,PINB2 ; else clc ; clear carry ror rtcdat cbi PORTB,PINB4 ; clear SCLK dec cnt ; decrement counter brne rxbck ; if counter equals zero, goto calling function ret ; else jump back ;****************************************************************************** ;'rtcread' ;Aim ; Complete function to read data from rtc ;Registers used ; rtcdat register in which data to be transmitted is stored ; rtemp temporary register ; cnt counter ;Functions called ; txrtc ; rxrtc ; txcomp ;***************************************************************************** rtcread: ldi rtemp, 0b11111111 ; configure B as output port out DDRB,rtemp cbi PORTB,PINB3 cbi PORTB,PINB4 ; clear SCLK sbi PORTB,PINB3 ; set RST ldi rtcdat,0b10111111 ; initialise RTC in burst read rcall txrtc ldi cnt1,$08 ldi rtemp,$0b11111011 ; configure pin B2 as input out DDRB,rtemp rtbck: rcall rxrtc ; get data from RTC and transmit it in mov datbyt,rtcdat ; following order : rcall transmit ; sec min hour date month day year ldi treg,$20 ; leave a space after each data byte rcall txcomp ; is read dec cnt1 ; decrement counter brne rtbck ; jump back if all data bytes not transmitted cbi PORTB,PINB3 ; clear RST to inhibit further operations ret ;*****************************************************************************