| 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
- andyo
- Jon Chandler
- MrDEB
Forum Activity
LCD / 18F1320 - mrdeb Saturday, 20 March 2010 06:21 - [3 replies]
A 'throw-away" PIC board - mrdeb Thursday, 18 March 2010 06:19 - [32 replies]
Credit Where Credit's Due - jon chandler Tuesday, 16 March 2010 12:01 - [9 replies]
Marching LEDs - mrdeb Saturday, 13 March 2010 22:54 - [7 replies]
USB 8 Channel Servo Controller - andyo Saturday, 13 March 2010 01:19 - [2 replies]
Dedicated Servo Controller - graham Friday, 12 March 2010 17:58 - [0 replies]
Must have....delta temperature glowies! - graham Friday, 12 March 2010 17:31 - [3 replies]
Recent Comments
- 2010-03-20 07:58:45 andyo
Nice. The TAP-28 boards are looking good. When I've had a buzzi...
- 2010-03-19 22:05:12 ozbec...
The author (Hop) suggests to "set the target PIC's configuratio...
- 2010-03-17 22:58:18 Jon C...
Graham, Thanks for fixing the spacing on the array. This is how i...
- 2010-03-17 21:14:34 ASDne...
Thanks Graham, It is a problem with Flowcode because the demo wa...
- 2010-03-17 19:50:32 Graha...
Looks like you've pretty much solved every minor issue that was enc...
- 2010-03-17 11:42:03 Jon C...
The low-cost servo does have one other feature. The origina...
- 2010-03-17 10:56:44 Graha...
I am not familiar with flowcode, though did you try powering the bo...
- 2010-03-15 21:47:55 ASDne...
Hello there, I am still trying to get the LCD to work but I am pro...




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