Pages

Monday, December 16, 2013

A DS1307 library for AVR Atmega



The DS1307 serial real-time clock (RTC) is a low-power, full binary-coded decimal (BCD) clock/calendar.
This library set and get year, month, day, hour, minute and second from this RTC.


Setup parameters are stored in file ds1307.h

This library was developed on Eclipse, built with avr-gcc on Atmega8 @ 1MHz.


Code


Notes
  • read risk disclaimer
  • excuse my bad english

34 comments:

  1. In DS1307.c is a line that says: * initialize the accellerometer :-)

    ReplyDelete
    Replies
    1. thank you for your feedback :)
      you have find a copy and paste comment error :-/

      Delete
  2. Hi,
    how to use DS1307 with LCD16x2 library?

    ReplyDelete
    Replies
    1. Hello. Just load the uint8_t outputs from the ds1307_getdate function to you LCD library. You should use the sprintf or the itoa function to build the output buffer to send to the LCD library.

      Delete
  3. Hi,
    thank you for the great library. Why do you calculate the dayofweek? I think the register 0x03 is for the dayofweek (0-7)?
    Could you please explain, why you do this way, please?

    ReplyDelete
    Replies
    1. Hello, i get the day of week to set the date. You can find the ds1307_getdayofweek call in the ds1307_setdate function.

      Delete
    2. Hi,
      thank you for your answer, but I think it is a misunderstanding.
      I am a newbe, and I do not know, if my opinion is wrong.
      I think, the ds1307 calculates the day of week, because the datasheet says, that there is a register for it.

      Your code reads only year, month, day, hour, minute and second from the ds1307 via I2C. And the function getdayofweek() calculates with y, m, d the day of week.
      So why do you calculate the day of week in the microcontroller and do not use the ds1307 for it?

      Greetings

      Delete
    3. I write that register during the setdate because I suppose it was incremented by the DS1307, but not computed. As datasheet states: "The day-of-week register increments at midnight. Values that correspond to the day of week are user-defined but must be sequential".
      My guess is that ds1307 does not know what is the initial value. But now you make me suspect that the 0x03 register is written by the DS1307. Can you test if is written by DS1307 or is just incremented by an initial value set by user? I'm actually fully involved in other project and can not test this. Thank you for your constructive comment.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Hello, I want to ask why in my program there is an inscription Error: C: \ cvavreval \ inc \ ds1307.h (25), included from: jam.c: missing '('
    bisakan you please tell me?
    I am sorry Indonesian people, so too could not english

    ReplyDelete
  6. please send the code for interfacing GPS(NEO-6M-001) with
    ATMEGA328

    ReplyDelete
    Replies
    1. Hello, you can find the code for this library above on this blog post. It should works on ATmega328, just update the uart library with the newer P.Fleury one.

      Delete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Hello Davide,

    This is a really nice project!
    How do you do to set the time through uart (I mean sending uint8_t via RealTerm)?
    Actually, I would like to know how to get a string from uart instead of a single char.

    Thanks.

    ReplyDelete
    Replies
    1. Hello and thank you. The sample in this page does not implement an update time over UART protocol. You have to write your own protocol. I would use uart_getc and uart_gets. You can as example send a 0x55 character followed by 6 byte + 0x10 as ending, the 6 byte you send should be year, month, day, hour, minute, seconds. Reading that on your micro, parsing that out and setting time using ds1307_setdate function. Hope this helps.

      Delete
  9. how do you interface using 4x4 keypad as well as 16x2 lcd

    ReplyDelete
    Replies
    1. Hello, for the LCD library I suggest the P.Fleury one, for the keypad, you should build your own keypad matrix, I've a litte library for doing that, if needed i can post it.

      Delete
  10. esta biblioteca funciona en atmel studio?

    ReplyDelete
    Replies
    1. Hello, yes you can compile it with Atmel Studio too.

      Delete
  11. Please correct your library in file uart.c. Your missing in position 154.
    #elif defined(__AVR_ATmega168P__) || defined(__AVR_ATmega328P__)
    /* ATmega with one USART */
    #define ATMEGA_USART0
    #define UART0_RECEIVE_INTERRUPT USART_RX_vect
    #define UART0_TRANSMIT_INTERRUPT USART_UDRE_vect
    #define UART0_STATUS UCSR0A
    #define UART0_CONTROL UCSR0B
    #define UART0_DATA UDR0
    #define UART0_UDRIE UDRIE0

    ReplyDelete
    Replies
    1. Hello, thank you for sharing your solution, an alternative is to update the P.Fluery uart library with the new one.

      Delete
  12. i am not able to see the sch attached can you provide a photo instead

    ReplyDelete
    Replies
    1. Dear ano, you should also find the atmega8_ds1307_board_i2c_sample.png file that is the export image of the sch eagle schematic file.

      Delete