| Swordfish Tutorial - DS1307 Date Time Chip (DTC) |
|
|
| Saturday, 04 April 2009 08:39 | |
|
The DS1307 is a great piece of kit, it provides real time date and clock values, and interfaces with the PIC micro via I2C. The values it holds for date/time are, Secs, Mins, Hours, Day, Date, Month and Year. I hear you asking, why use this when you could make a small program do the same with a PIC;
(LCD, 5v Regulator and Contrast Control not shown for ease of illustration)
If you wish to use a battery backup for the DS1307, then connect the positive of the battery to Pin 3, and the earth to Pin 4. When ever Vcc (Pin 8) is lower than Pin 3 (Batt), the device will enter backup mode and consume less than 500nA.
Device = 18F452 Clock = 20 // some LCD options... #option LCD_DATA = PORTD.4 #option LCD_RS = PORTD.2 #option LCD_EN = PORTD.3 // import LCD library... Include "LCD.bas" Include "utils.bas" Include "convert.bas" Include "I2C.bas" Structure TTime Second As Byte // Second (0..59) Minute As Byte // Minute (0..59) Hour As Byte // Hour (0..11 or 0..23) End Structure Structure TDate Day As Byte // Date (0..31) Month As Byte // Month (1..12) Year As Byte // Year (0..99) DayOfWeek As Byte // day of the week (1..7) End Structure Dim Time As TTime, Date As TDate, Seconds_Poll As Byte Sub SetTime(Hour, Minute, Second, DayOfWeek, Day, Month, Year As Byte) I2C.Start I2C.WriteByte(%11010000) // Send the RTC address, and put it in write mode I2C.WriteByte($00) // Move the pointer to first register I2C.WriteByte(DecToBCD(Second)) // Write each byte I2C.WriteByte(DecToBCD(Minute)) // I2C.WriteByte(DecToBCD(Hour)) // I2C.WriteByte(DecToBCD(DayOfWeek)) // I2C.WriteByte(DecToBCD(Day)) // I2C.WriteByte(DecToBCD(Month)) // I2C.WriteByte(DecToBCD(Year)) // I2C.WriteByte(0) // I2C.Stop End Sub Sub GetTime() I2C.Start I2C.WriteByte(%11010000) I2C.WriteByte($00) I2C.Restart I2C.WriteByte(%11010001) Time.Second = BCDToDec(I2C.ReadByte(I2C_ACKNOWLEDGE)) Time.Minute = BCDToDec(I2C.ReadByte(I2C_ACKNOWLEDGE)) Time.Hour = BCDToDec(I2C.ReadByte(I2C_ACKNOWLEDGE)) Date.DayOfWeek = BCDToDec(I2C.ReadByte(I2C_ACKNOWLEDGE)) Date.Day = BCDToDec(I2C.ReadByte(I2C_ACKNOWLEDGE)) Date.Month = BCDToDec(I2C.ReadByte(I2C_ACKNOWLEDGE)) Date.Year = BCDToDec(I2C.ReadByte(I2C_NOT_ACKNOWLEDGE)) I2C.Stop End Sub // Start Of Program... SetAllDigital // Make all pins digital I/O's I2C.Initialize() // Set up the I2C DelayMS(150) Cls SetTime(03,42,00,07,06,05,07) Main: GetTime If Time.Second = Seconds_Poll Then GoTo Main // If there is update in Secs, display time and Date EndIf // Display the new details on the LCD WriteAt(1,1,"Time: ", DecToStr(Time.Hour,2), ":", DecToStr(Time.Minute,2), ":", DecToStr(Time.Second,2)) WriteAt(2,1,"Date: ", DecToStr(Date.Day,2), "-", DecToStr(Date.Month,2), "-", DecToStr(Date.Year,2)) Seconds_Poll = Time.Second // Update the seconds polling register GoTo Main
|
|
| Last Updated ( Friday, 24 April 2009 09:23 ) |
Whos Online
- MrDEB
Forum Activity
simulation program - mrdeb Thursday, 11 March 2010 22:52 - [0 replies]
MARCHING ledS - mrdeb Thursday, 11 March 2010 19:13 - [3 replies]
REALISTIC LED candle flicker - mrdeb Wednesday, 10 March 2010 05:48 - [0 replies]
the LCD demo hello world - mrdeb Wednesday, 10 March 2010 05:41 - [29 replies]
USB problem - jon chandler Tuesday, 09 March 2010 11:31 - [3 replies]
Servo Module - andyo Tuesday, 09 March 2010 03:39 - [3 replies]
pic to ps2 communication - roshan Friday, 05 March 2010 01:30 - [7 replies]
Recent Comments
- 2010-03-11 21:03:32 Jon C...
If this wasn't a class project that had to be done on a PIC16F690 u...
- 2010-03-11 15:30:06 uuu
I am going to send two data from ADC to USART(async mode) to serial...
- 2010-03-05 13:24:23 mrpse...
I was thinking of battery with powered routers or powered for every...
- 2010-03-05 12:17:40 andyo
50 rooms - wow. The XBee modules are very easy to use but I'm not ...
- 2010-03-05 11:46:37 mrpse...
Thanks AndyO. What I have to do is design a device to monitor temp...
- 2010-03-05 03:08:58 andyo
The circuit used for the remote sensor can be found here: [url]http...
- 2010-03-04 07:19:06 renno
plz i need help do u know from where i could download it or how cou...
- 2010-03-04 07:16:05 renno
hello everyone i'm trying to finish my final project and i want to ...




Hey have a way to put the code in asm???
ty for all work!! :D