PDA

View Full Version : Setting RTC by Serial input



jamie_s
- 26th November 2015, 13:29
Hi all,
Heres a code example that i have used to set an RTC by a serial input, its probably not the most efficient code, but it works and could help some of you out
It converts the Decimal input to BCD for the DS1302 DS1307 RTC

all the variables are BYTES




'------ SET RTC -------------------------------------------------------------------------------------------

SetRTC:

HSEROUT ["Enter Day" , 13,10] ' Output
HSERIN [DEC2 r_day]

RTCday=r_day DIG 1
RTCday=RTCday <<4
RTCday=RTCday+(r_day DIG 0)

HSEROUT ["Enter Month" , 13,10] ' Output
HSERIN [DEC2 r_month]

RTCmonth=r_month DIG 1
RTCmonth=RTCmonth<<4
RTCmonth=RTCmonth+(r_month DIG 0)

HSEROUT ["Enter Year 20xx" , 13,10] ' Output
HSERIN [DEC2 r_year]

RTCyear=r_year DIG 1
RTCyear=RTCyear<<4
RTCyear=RTCyear+(r_year DIG 0)

HSEROUT ["Enter Hour" , 13,10] ' Output
HSERIN [DEC2 r_hour]

RTChour=r_hour DIG 1
RTChour=RTChour<<4
RTChour=RTChour+(r_hour DIG 0)

HSEROUT ["Enter Minute" , 13,10] ' Output
HSERIN [DEC2 r_min]

RTCmin=r_min DIG 1
RTCmin=RTCmin<<4
RTCmin=RTCmin+(r_min DIG 0)

r_sec=$00

pause 100
HSEROUT ["Writing to RTC" , 13,10] ' Output

I2CWRITE SDA,SCL,$D0,$00,[r_sec,RTCmin,RTChour,$7,RTCday,RTCmonth,RTCyear,$9 0] ' Write to DS1307

pause 500
goto setup



'================================================= ================================================== ==========================

mackrackit
- 26th November 2015, 13:57
Thank you for sharing. However you may want to add more explanation for people new to this. I could see this becoming a tutorial...