PDA

View Full Version : Real time clock... what a headache!



Eng4444
- 1st June 2006, 09:15
i'm again searching for Real time clock but this time, not for DS1307 (check my previous threads please).

Could anyone provide me with any PicBasic Pro program for any real time clock IC used with any PIC?

i'll just buy both ICs and make my life easier... because i'm about to break my head with this DS1307 and PIC16F876A ...

Eng4444
- 1st June 2006, 21:17
i'm still waiting for some answers... thread is not closed yet :P

Charles Linquis
- 8th June 2006, 22:56
Simple code for use with DS1307 and 18F8722

-------------- RTC -----------------

RTC CON %11010000 ' RTC device address (byte addressing)
ScReg CON $00 ' seconds address (00 - 59) MSBit of SecReg must be set to a 0 to enable RTC
ContReg CON $07 ' control register
RAMstart CON $08
RTCflag CON 0 ' RTC flag (location 0 of internal EEPROM)
ClockControlByte CON %00010000 ' sets the SQW/OUT to 1Hz pulse, logic level low


' Clock Variables


Sc VAR BYTE
Mn VAR BYTE
Hr VAR BYTE
Da VAR BYTE
Dt VAR BYTE
Mo vAR BYTE
YR var BYTE


'---------------- END RTC assingment ---------------


Set up Seconds in TimBuff[5], TimBuff[6]
Minutes in TimBuff[3], TimBuff[4]
Hours in TimBuff[1],TimBuff[2]


Etc... ONE digit in each buffer element

Then GOSUB SetTime


To READ:

Gosub ShowTime
Current time is shown in the TimBuff[X] registers.







SetTime:

Number1 = TimBuff[5]
Number2 = TimBuff[6]

Gosub HexAscii2BCD

Sc = BCD

Number1 = TimBuff[3]
Number2 = TimBuff[4]


Gosub HexAscii2BCD

Mn = BCD

Number1 = TimBuff[1]
Number2 = TimBuff[2]

Gosub HexAscii2BCD

Hr = BCD

Number1 = TimBuff[7]
Number2 = TimBuff[8]

Gosub HexAscii2BCD

Mo = BCD

Number1 = TimBuff[9]
Number2 = TimBuff[10]

Gosub HexAscii2BCD

Da = 0 ' We don't care what day of the week it is
Dt = BCD

Number1 = TimBuff[11]
Number2 = TimBuff[12]

Gosub HexAscii2BCD

YR = BCD

Cntrl = 0 ' Must be zero to make clk run - NOT same as control reg!
Gosub ClockWrite
GOSUB ClockControl
Spare1 = 55 ' Doesn't have to be BCD
GOSUB ClockRamWrite
RETURN


ShowTime:

Gosub ClockRead
BCD = sc
GOSUB BCD2HexAscii

TimBuff[5] = Number1
TimBuff[6] = Number2

BCD = mn
GOSUB BCD2HexAscii
TimBuff[3] = Number1
TimBuff[4] = Number2

BCD = hr
GOSUB BCD2HexAscii
TimBuff[1] = Number1
TimBuff[2] = Number2

BCD = mo
GOSUB BCD2HexAscii
TimBuff[7] = Number1
TimBuff[8] = Number2

BCD = dt
GOSUB BCD2HexAscii
TimBuff[9] = Number1
TimBuff[10] = Number2

BCD = yr
GOSUB BCD2HexAscii
TimBuff[11] = Number1
TimBuff[12] = Number2



RETURN


HexASCII2BCD:
BCD = (Number1 - 48)<<4 + (Number2 - 48)
RETURN

BCD2HexASCII:
Number1 = (BCD >> 4) + 48
Number2 = (BCD & %00001111) + 48
RETURN





ClockWrite:
I2CWrite SDA,SCL,RTC,ScReg,[sc,mn,hr,da,dt,mo,yr,Cntrl]
Return

ClockControl:
I2CWRITE SDA,SCL,RTC,ContReg,[ClockControlByte]
RETURN

ClockRAMRead:
I2CRead SDA,SCL,RTC,RAMstart,[Spare1,Spare2,Spare3,Spare4,Spare5,Spare6]
RETURN
ClockRAMWrite:
I2CWRITE SDA,SCL,RTC,RAMstart,[Spare1,Spare2,Spare3,Spare4,Spare5,Spare6]
RETURN

ClockRead:
I2CRead SDA,SCL,RTC,ScReg,[sc,mn,hr,da,dt,mo,yr]
RETURN