gramo
New Member
After a few headaches, I finally finsished the DS1307 Real Time Chip conversion to Swordfish.
Might be someone else out there who could use it, its a little simpler than the version available from the Swordfsih wiki;
**broken link removed**
**broken link removed** (LCD PSU and Contrast not shown for ease of illustration)
I like the way you can use structures in SF;
Structure TTime
Dim Time As TTime
Now you can access each instance like this;
Time.Second = 30
Time.Minute = 15
Time.Hour = 12
Handy.
Might be someone else out there who could use it, its a little simpler than the version available from the Swordfsih wiki;
**broken link removed**
**broken link removed** (LCD PSU and Contrast not shown for ease of illustration)
Code:
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
// program start...
SetAllDigital // Make all pins digital I/O's
I2C.Initialize() // Set up the I2C
DelayMS(150)
Cls
SetTime(3,42,0,7,6,5,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
I like the way you can use structures in SF;
Structure TTime
Second As Byte // Second (0..59)
Minute As Byte // Minute (0..59)
Hour As Byte // Hour (0..11 or 0..23)
End StructureMinute As Byte // Minute (0..59)
Hour As Byte // Hour (0..11 or 0..23)
Dim Time As TTime
Now you can access each instance like this;
Time.Second = 30
Time.Minute = 15
Time.Hour = 12
Handy.