Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Swordfish DS1307 Example

Status
Not open for further replies.

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)

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 Structure

Dim Time As TTime


Now you can access each instance like this;

Time.Second = 30
Time.Minute = 15
Time.Hour = 12

Handy.
 
do you have that for 8051, i've been on the net forth and back with no single clear code of how to get time from DS1307, i don't want code with serial connections, nor lcd with 4-bit interfacing, all i need is a simple assembly code that gets sec,min,hours,date,month,year.
that's all

thanks
 
I can appreciate the work you have done on porting the DS1307. Those little buggers provided no end of frustration on porting to a pic on another compiler. At one point, I thought I had toasted my only sample. Ordered more samples, and of course, the only thing that was toast was my coding.

If you were going to add a button to change the time, is polling or using an interrupt the best way?
 
nickelflippr said:
If you were going to add a button to change the time, is polling or using an interrupt the best way?

It really makes no difference, it's more dependent on your existing code - but, in my opinion, it's usually just wasting an interrupt for something which has no actual need for one.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top