Autor Tema: Duda con driver ds1307  (Leído 4351 veces)

0 Usuarios y 1 Visitante están viendo este tema.

Desconectado soymoe

  • PIC18
  • ****
  • Mensajes: 456
    • El blog de Moe
Duda con driver ds1307
« en: 04 de Enero de 2013, 19:39:03 »
Estuve viendo esta libreria para el manejo del ds1307, pero tengo dudas de como ingresar la hora y configurar para 12 o 24 hs, me ayudan?
Código: [Seleccionar]
////////////////////////////////////////////////////////////////////////////////
///                               DS1307.C                                   ///
///                     Driver for Real Time Clock                           ///
///                                                                          ///
/// ds1307_init() - Enable oscillator without clearing the seconds register -///
///                 used when PIC loses power and DS1307 run from 3V BAT     ///
///               - Disable squarewave output                                ///
///                                                                          ///
/// ds1307_set_date_time(day,mth,year,dow,hour,min,sec)  Set the date/time   ///
///                                                                          ///
/// ds1307_get_date(day,mth,year,dow)               Get the date             ///
///                                                                          ///
/// ds1307_get_time(hr,min,sec)                     Get the time             ///
///                                                                          ///
////////////////////////////////////////////////////////////////////////////////

#define RTC_SDA  PIN_C4
#define RTC_SCL  PIN_C3

#use i2c(master, sda=RTC_SDA, scl=RTC_SCL, SLOW)

BYTE bin2bcd(BYTE binary_value);
BYTE bcd2bin(BYTE bcd_value);

void ds1307_init(void)
{
   BYTE seconds = 0;

   i2c_start();
   i2c_write(0xD0);      // SELECCIONA RTC CON  ORDEN WR
   i2c_write(0x00);      // REG 0
   i2c_start();
   i2c_write(0xD1);      // RD from RTC
   seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1307
   i2c_stop();
   seconds = bin2bcd(seconds & 0x7F);

   delay_us(3);

   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x00);      // REG 0
   i2c_write(seconds);     // Start oscillator with current "seconds value
   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x07);      // Control Register
   i2c_write(0x10);     // Enable squarewave output pin
   i2c_stop();

}

void ds1307_OUT(void){
   i2c_start();
   i2c_write(0xD0);      // WR to RTC
   i2c_write(0x07);      // WR to RTC
   i2c_write(0x10);     // Enable squarewave output pin
   i2c_stop();
}

void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
  sec &= 0x7F;
  hr &= 0x3F;

  i2c_start();
  i2c_write(0xD0);            // I2C write address
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_write(bin2bcd(sec));      // REG 0
  i2c_write(bin2bcd(min));      // REG 1
  i2c_write(bin2bcd(hr));      // REG 2
  i2c_write(bin2bcd(dow));      // REG 3
  i2c_write(bin2bcd(day));      // REG 4
  i2c_write(bin2bcd(mth));      // REG 5
  i2c_write(bin2bcd(year));      // REG 6
  //i2c_write(0x80);            // REG 7 - Disable squarewave output pin
   i2c_write(0x10);     // Enable squarewave output pin
  i2c_stop();
}

void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x03);            // Start at REG 3 - Day of week
  i2c_start();
  i2c_write(0xD1);
  dow  = bcd2bin(i2c_read() & 0x7f);   // REG 3
  day  = bcd2bin(i2c_read() & 0x3f);   // REG 4
  mth  = bcd2bin(i2c_read() & 0x1f);   // REG 5
  year = bcd2bin(i2c_read(0));         // REG 6
  i2c_stop();
}

void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
  i2c_start();
  i2c_write(0xD0);
  i2c_write(0x00);            // Start at REG 0 - Seconds
  i2c_start();
  i2c_write(0xD1);
  sec = bcd2bin(i2c_read() & 0x7f);
  min = bcd2bin(i2c_read() & 0x7f);
  hr  = bcd2bin(i2c_read(0) & 0x3f);
  i2c_stop();

}

BYTE bin2bcd(BYTE binary_value)
{
  BYTE temp;
  BYTE retval;

  temp = binary_value;
  retval = 0;

  while(1)
  {
    // Get the tens digit by doing multiple subtraction
    // of 10 from the binary value.
    if(temp >= 10)
    {
      temp -= 10;
      retval += 0x10;
    }
    else // Get the ones digit by adding the remainder.
    {
      retval += temp;
      break;
    }
  }

  return(retval);
}


// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
  BYTE temp;

  temp = bcd_value;
  // Shifting upper digit right by 1 is same as multiplying by 8.
  temp >>= 1;
  // Isolate the bits for the upper digit.
  temp &= 0x78;

  // Now return: (Tens * 8) + (Tens * 2) + Ones

  return(temp + (temp >> 2) + (bcd_value & 0x0f));
}


Desconectado Miquel_S

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1251
Re: Duda con driver ds1307
« Respuesta #1 en: 05 de Enero de 2013, 07:31:00 »
Hola soymoe, te paso algo que hice con el ds1307 donde contesta tus dudas, cualquier cosa pregunta.

http://www.ucontrol.com.ar/forosmf/proyectos-con-pic/reloj-alarmas-para-escuelas/

Saludos!
Todos somos muy ignorantes. Lo que ocurre es que no todos ignoramos las mismas cosas.

Desconectado soymoe

  • PIC18
  • ****
  • Mensajes: 456
    • El blog de Moe
Re: Duda con driver ds1307
« Respuesta #2 en: 06 de Enero de 2013, 19:02:06 »
No entendi como se configura la hora porque tenes que determinar si queres 24  o 12 hs. Otra pregunta, como hago para saber cuando tengo que actualizar los minutos y horas en un display?

Desconectado Arkaedus

  • PIC10
  • *
  • Mensajes: 25
Re: Duda con driver ds1307
« Respuesta #3 en: 18 de Junio de 2015, 14:49:48 »
Hola soymoe, te paso algo que hice con el ds1307 donde contesta tus dudas, cualquier cosa pregunta.

http://www.ucontrol.com.ar/forosmf/proyectos-con-pic/reloj-alarmas-para-escuelas/

Saludos!

Me podrias pasar tu driver de _ds1307.c

Es que en el PIC C Compiler IDE no me viene

Desconectado Miquel_S

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1251
Re: Duda con driver ds1307
« Respuesta #4 en: 19 de Junio de 2015, 17:42:50 »
Hola Arkaedus este es el driver que use para dicho proyecto:
Código: C
  1. //////////////////////////////////////////////////////////////////////////////////////
  2. ///                               DS1307.C                                           ///
  3. ///                     Driver for Real Time Clock                                   ///
  4. ///                     modified by Redpic 08/2006                                   ///
  5. ///                  http://picmania.garcia-cuervo.com                               ///
  6. ///                                                                                  ///
  7. /// void ds1307_init(val)                                                            ///
  8. ///                  - Enable oscillator without clearing the seconds register       ///
  9. ///                    used when PIC loses power and DS1307 run from 3V BAT          ///
  10. ///                  - Config Control Register with next parameters:                 ///
  11. ///                     DS1307_ALL_DISABLED          All disabled                    ///
  12. ///                     DS1307_OUT_ON_DISABLED_HIHG  Out to Hight on Disable Out     ///
  13. ///                     DS1307_OUT_ENABLED           Out Enabled                     ///
  14. ///                     DS1307_OUT_1_HZ              Freq. Out to 1 Hz               ///
  15. ///                     DS1307_OUT_4_KHZ             Freq. Out to 4.096 Khz          ///
  16. ///                     DS1307_OUT_8_KHZ             Freq. Out to 8.192 Khz          ///
  17. ///                     DS1307_OUT_32_KHZ            Freq. Out to 32.768 Khz         ///
  18. ///                                                                                  ///
  19. ///                     Example init:                                                ///
  20. ///                     ds1307_init(DS1307_ALL_DISABLED);                            ///
  21. ///                     ds1307_init(DS1307_OUT_ENABLED | DS1307_OUT_1_HZ);           ///
  22. ///                                                                                  ///
  23. /// void ds1307_set_date_time(day,mth,year,dow,hour,min,sec) - Set the date/time     ///
  24. ///                                                                                  ///
  25. /// void ds1307_get_date(day,mth,year,dow)                   - Get the date          ///
  26. ///                                                                                  ///
  27. /// void ds1307_get_time(hr,min,sec)                         - Get the time          ///
  28. ///                                                                                  ///
  29. /// char ds1307_read_nvram_byte(char addr)                   - Read byte in address  ///
  30. ///                                                                                  ///
  31. /// void ds1307_write_nvram_byte(char addr, char value)      - Write byte in address ///
  32. ///                                                                                  ///
  33. /// void ds1307_get_day_of_week(char* ptr)                   - Get string Day Of Week///
  34. ///                                                                                  ///
  35. /// If defined USE_INTERRUPTS all functions disable Global Interrupts on starts and  ///
  36. ///                           enable Global on ends else usar can do it hiself       ///
  37. ///                                                                                  ///
  38. ///////////////////////////////////////////////////////////////////////////////////////
  39.  
  40. #ifndef RTC_SDA
  41. #define RTC_SDA  PIN_B0
  42. #define RTC_SCL  PIN_B1
  43. #endif
  44.  
  45. #use i2c(master, sda=RTC_SDA, scl=RTC_SCL)
  46.  
  47. #define DS1307_ALL_DISABLED         0b00000000 // All disabled
  48. #define DS1307_OUT_ON_DISABLED_HIHG 0b10000000 // Out to Hight on Disable Out
  49. #define DS1307_OUT_ENABLED          0b00010000 // Out Enabled
  50. #define DS1307_OUT_1_HZ             0b00000000 // Freq. Out to 1 Hz
  51. #define DS1307_OUT_4_KHZ            0b00000001 // Freq. Out to 4.096 Khz
  52. #define DS1307_OUT_8_KHZ            0b00000010 // Freq. Out to 8.192 Khz
  53. #define DS1307_OUT_32_KHZ           0b00000011 // Freq. Out to 32.768 Khz
  54.  
  55. #define Start_user_address_nvram    0x08
  56. #define End_user_address_nvram      0x3f
  57.  
  58. char days_of_week[7][11]={"Lunes\0","Martes\0","Miércoles\0","Jueves\0","Viernes\0","Sábado\0","Domingo\0"};
  59.  
  60. byte ds1307_bin2bcd(byte binary_value);
  61. byte ds1307_bcd2bin(byte bcd_value);
  62.  
  63. void ds1307_init(int val){
  64.  
  65.    byte seconds = 0;
  66.  
  67. #ifndef USE_INTERRUPTS
  68.    disable_interrupts(global);
  69. #endif
  70.  
  71.    i2c_start();
  72.    i2c_write(0xD0);
  73.    i2c_write(0x00);
  74.    i2c_start();
  75.    i2c_write(0xD1);
  76.    seconds = ds1307_bcd2bin(i2c_read(0));
  77.    i2c_stop();
  78.    seconds &= 0x7F;
  79.  
  80.    delay_us(3);
  81.  
  82.    i2c_start();
  83.    i2c_write(0xD0);
  84.    i2c_write(0x00);
  85.    i2c_write(ds1307_bin2bcd(seconds));
  86.    i2c_start();
  87.    i2c_write(0xD0);
  88.    i2c_write(0x07);
  89.    i2c_write(val);
  90.    i2c_stop();
  91.  
  92. #ifndef USE_INTERRUPTS
  93.    enable_interrupts(global);
  94. #endif
  95.  
  96. }
  97.  
  98. void ds1307_set_date_time(byte day, byte mth, byte year, byte dow, byte hr, byte min, byte sec){
  99.  
  100. #ifndef USE_INTERRUPTS
  101.    disable_interrupts(global);
  102. #endif
  103.  
  104.   sec &= 0x7F;
  105.   hr &= 0x3F;
  106.  
  107.   i2c_start();
  108.   i2c_write(0xD0);
  109.   i2c_write(0x00);
  110.   i2c_write(ds1307_bin2bcd(sec));
  111.   i2c_write(ds1307_bin2bcd(min));
  112.   i2c_write(ds1307_bin2bcd(hr));
  113.   i2c_write(ds1307_bin2bcd(dow));
  114.   i2c_write(ds1307_bin2bcd(day));
  115.   i2c_write(ds1307_bin2bcd(mth));
  116.   i2c_write(ds1307_bin2bcd(year));
  117.   i2c_stop();
  118.  
  119. #ifndef USE_INTERRUPTS
  120.    enable_interrupts(global);
  121. #endif
  122.  
  123. }
  124.  
  125. void ds1307_get_date(byte &day, byte &mth, byte &year, byte &dow){
  126.  
  127. #ifndef USE_INTERRUPTS
  128.    disable_interrupts(global);
  129. #endif
  130.  
  131.   i2c_start();
  132.   i2c_write(0xD0);
  133.   i2c_write(0x03);
  134.   i2c_start();
  135.   i2c_write(0xD1);
  136.   dow  = ds1307_bcd2bin(i2c_read() & 0x7f);
  137.   day  = ds1307_bcd2bin(i2c_read() & 0x3f);
  138.   mth  = ds1307_bcd2bin(i2c_read() & 0x1f);
  139.   year = ds1307_bcd2bin(i2c_read(0));
  140.   i2c_stop();
  141.  
  142. #ifndef USE_INTERRUPTS
  143.    enable_interrupts(global);
  144. #endif
  145.  
  146. }
  147.  
  148. void ds1307_get_time(byte &hr, byte &min, byte &sec){
  149.  
  150. #ifndef USE_INTERRUPTS
  151.    disable_interrupts(global);
  152. #endif
  153.  
  154.   i2c_start();
  155.   i2c_write(0xD0);
  156.   i2c_write(0x00);
  157.   i2c_start();
  158.   i2c_write(0xD1);
  159.   sec = ds1307_bcd2bin(i2c_read() & 0x7f);
  160.   min = ds1307_bcd2bin(i2c_read() & 0x7f);
  161.   hr  = ds1307_bcd2bin(i2c_read(0) & 0x3f);
  162.   i2c_stop();
  163.  
  164. #ifndef USE_INTERRUPTS
  165.    enable_interrupts(global);
  166. #endif
  167.  
  168. }
  169.  
  170.  
  171. char ds1307_read_nvram_byte(char addr){
  172.  
  173.    char retval;
  174.  
  175. #ifndef USE_INTERRUPTS
  176.    disable_interrupts(global);
  177. #endif
  178.  
  179.    i2c_start();
  180.    i2c_write(0xD0);
  181.    i2c_write(addr);
  182.  
  183.    i2c_start();
  184.    i2c_write(0xD1);
  185.    retval = i2c_read(0);
  186.    i2c_stop();
  187.  
  188.    return(retval);
  189.  
  190. #ifndef USE_INTERRUPTS
  191.    enable_interrupts(global);
  192. #endif
  193.  
  194. }
  195.  
  196. void ds1307_write_nvram_byte(char addr, char value){
  197.  
  198. #ifndef USE_INTERRUPTS
  199.    disable_interrupts(global);
  200. #endif
  201.  
  202.    i2c_start();
  203.    i2c_write(0xD0);
  204.    i2c_write(addr);
  205.    i2c_write(value);
  206.    i2c_stop();
  207.  
  208. #ifndef USE_INTERRUPTS
  209.    enable_interrupts(global);
  210. #endif
  211.  
  212. }
  213.  
  214. void ds1307_get_day_of_week(char* ptr){
  215.  
  216.    byte lday;
  217.    byte lmonth;
  218.    byte lyr;
  219.    byte ldow;
  220.    ds1307_get_date(lday,lmonth,lyr,ldow);
  221.    sprintf(ptr,"%s",days_of_week[ldow]);
  222. }
  223.  
  224. ///////////////////////////////////////////////////////////////////////////////
  225.  
  226. byte ds1307_bin2bcd(byte binary_value){
  227.  
  228.   byte temp;
  229.   byte retval;
  230.  
  231.   temp = binary_value;
  232.   retval = 0;
  233.   while(1){
  234.     if(temp >= 10){
  235.       temp -= 10;
  236.       retval += 0x10;
  237.     }else{
  238.       retval += temp;
  239.       break;
  240.     }
  241.   }
  242.   return(retval);
  243. }
  244.  
  245. byte ds1307_bcd2bin(byte bcd_value){
  246.  
  247.   byte temp;
  248.  
  249.   temp = bcd_value;
  250.   temp >>= 1;
  251.   temp &= 0x78;
  252.   return(temp + (temp >> 2) + (bcd_value & 0x0f));
  253. }
  254.  
  255. ///////////////////////////////////////////////////////////////////////////////
Lo cogí prestado del compañero RedPic
http://picmania.garcia-cuervo.net/proyectos_aux_rtc.php

Saludos!
Todos somos muy ignorantes. Lo que ocurre es que no todos ignoramos las mismas cosas.

Desconectado Arkaedus

  • PIC10
  • *
  • Mensajes: 25
Re: Duda con driver ds1307
« Respuesta #5 en: 20 de Junio de 2015, 14:10:03 »
Muchas gracias es que estoy teniendo un problema con un proyecto que he hehco y no encuentro la solución

No sé si me pudieras echar un vistazo a ver si a simple vista ves algun defecto de programacion.

Este es el topic donde trato el problema.

http://www.todopic.com.ar/foros/index.php?topic=44694.0


Muchas gracias  por el driver

Desconectado Arkaedus

  • PIC10
  • *
  • Mensajes: 25
Re: Duda con driver ds1307
« Respuesta #6 en: 20 de Junio de 2015, 16:41:09 »
en se proyecto esta tambien un driverflex_lcd.c y ds1621.c prodrias psarlos para poder compilar y ver como funciona tu proyecto?

Gracias :)