;------------------------------------------------------------------------------- ; ; Real Time Clock interface code ; ; by Andrew D. Vassallo ; email: snurple@hotmail.com ; ; These routines were written specifically for the NJU6355 E or G version RTC. ; They may be usable for other clocks, but have not been tested. ; ;------------------------------------------------------------------------------- ;------ All variables in Bank 0 CBLOCK Year Month Date Day Hours Minutes Seconds Time_Temp Time_Count ENDC #define CLK PORTB, 0 #define DATA PORTB, 1 #define IO PORTB, 2 ; input/output select #define CE PORTB, 3 ; chip enable bit ;--------------------------------------------- Subroutines Follow ---------------------------------------------- ;------ This routine sets the time for the RTC. Note that upon power-up, the RTC has NO TIME SET. You must ;------ define the time and set it in the RTC before attempting to read it. Otherwise, no values will be returned. ;------ Note: the Get_Time routine reads the variables from the RTC and stores them in BCD format, not Hex. If ;------ you want to "reset" the time with the Set_Time routine, then you don't have to use the Hex2BCD conversion. ;------ In fact, if the original variables are defined in BCD format, you don't need to convert, either. But, since ;------ Hex is easier to work with and necessary for table lookup, it might be worthwhile to convert back and forth ;------ if you need to manipulate the variables or use a table. Set_Time bcf CLK ; need CLK low for CE toggle nop bsf IO ; see timing diagram from NJU6355 datasheet nop bsf CE ; Send 44 bits total (Year[8],Month[8],Date[8],Day[4],Hours[8],Minutes[8]) movf Year, 0 call Hex2BCD ; must convert each variable to BCD before sending to RTC movwf Time_Temp call Write_RTC movf Month, 0 call Hex2BCD movwf Time_Temp call Write_RTC movf Date, 0 call Hex2BCD movwf Time_Temp call Write_RTC movf Day, 0 call Hex2BCD movwf Time_Temp call Write_RTC_4bits ; Day input is only 4 bits movf Hours, 0 call Hex2BCD movwf Time_Temp call Write_RTC movf Minutes, 0 call Hex2BCD movwf Time_Temp call Write_RTC ; Ignore Seconds - RTC does not accept "seconds" input bcf CLK ; finally pull the clock low for CE toggle nop bcf CE ; send the final write command to the RTC nop bcf IO ; leave the RTC in output mode return ;------ This routine grabs the current time from the RTC and STORES IT IN ;------ BCD FORMAT in the registers Year, Month, Date, Day, Hours, Minutes, Seconds ;------ This is worth noting again: THE VARIABLES ARE STORED IN BCD FORMAT, NOT HEX Get_Time bcf CLK nop bcf IO ; see timing diagram from NJU6355 datasheet nop bsf CE ; Read all 52 bits (Year[8],Month[8],Date[8],Day[4],Hours[8],Minutes[8],Seconds[8]) call Read_RTC movwf Year call Read_RTC movwf Month call Read_RTC movwf Date call Read_RTC_4bits ; Day value is only 4 bits movwf Day call Read_RTC movwf Hours call Read_RTC movwf Minutes call Read_RTC movwf Seconds ; Turn off RTC output bcf CE return ;------ This just clocks the next RTC function into Time_Temp and returns it in W. ; Call with: nothing required for call ; Returns with: time function byte in W ; Uses: Time_Temp as temporary storage and Time_Count for bit counter Read_RTC_4bits movlw 0x04 goto $+2 Read_RTC movlw 0x08 movwf Time_Count Read_Loop bsf CLK ; data valid after CLK->high bsf STATUS, C ; default is "1" output from RTC btfss DATA bcf STATUS, C bcf CLK rrf Time_Temp, 1 ; LSB output first decfsz Time_Count, 1 goto Read_Loop movf Time_Temp, 0 ; move into W for return return ;------ This just clocks whatever is in the W register out to the RTC. ; Use "Write_RTC" to send 8-bit values. Use "Write_RTC_4bits" for 4-bit values. ; Call with: number to be sent in Time_Temp ; Returns with: nothing returned ; Uses: Time_Temp as temporary storage Write_RTC_4bits movlw 0x04 goto $+2 Write_RTC movlw 0x08 movwf Time_Count Write_Loop bcf CLK nop bsf DATA ; default is "1" output from RTC btfss Time_Temp, 0 ; send LSB first each time bcf DATA nop bsf CLK ; data valid after CLK->high rrf Time_Temp, 1 decfsz Time_Count, 1 goto Write_Loop return ;------ Must convert from Hex to BCD for output to RTC ; Note that this BCD conversion is limited to input values less than 99 decimal (0x63 hex) ; Call with: number to be converted in W ; Returns with: converted BCD number in W ; Uses: Time_Temp and Time_Count to hold value during calculations Hex2BCD movwf Time_Count ; use as temporary holding register for conversion clrw btfsc Time_Count, 4 addlw 0x16 btfsc Time_Count, 5 addlw 0x32 addlw 0x06 skpdc addlw -0x06 btfsc Time_Count, 6 addlw 0x64 addlw 0x06 skpdc addlw -0x06 movwf Time_Temp movf Time_Count, 0 andlw 0x0F addwf Time_Temp, 0 skpndc addlw 0x06 addlw 0x06 skpdc addlw -0x06 return
Comments:
Questions:
file: /Techref/microchip/nju6355rtc-dv.htm, 5KB, , updated: 2009/3/19 09:55, local time: 2024/12/14 12:20,
owner: ADV--A41,
18.97.14.83:LOG IN
|
©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? <A HREF="http://massmind.org/Techref/microchip/nju6355rtc-dv.htm"> NJU6355 E or G version Real Time Clock interface code</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to massmind.org! |
.