Autor Tema: Dudas sobre reloj DS1302  (Leído 8486 veces)

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

Desconectado hqv

  • PIC12
  • **
  • Mensajes: 70
Dudas sobre reloj DS1302
« en: 06 de Agosto de 2005, 14:55:00 »
Hola amigos del foro:

Mis dudas son las siguientes:

Tengo un reloj DS1302 y no se si es SPI o I2c ...yo creo que es I2c ya que ocupa una línea bidireccional para los datos y otro del pin del clok..pero no estoy seguro...

otra duda es que existe un driver en el compilador ccs para ocupar dicho reloj...pero tiene uno que modificar ese archivo ya que ocupa otros pines....

solo tendría que incluir deicho archivo...#include <ds1302.c> y nada más
o hay que incluir algo si es que fuese I2C o spi.....

estoy medio confundido por lo que ven...por eso espero de su ayuda desinteresada

gracias

hq

Desconectado pachopic

  • Colaborador
  • PIC16
  • *****
  • Mensajes: 209
RE: Dudas sobre reloj DS1302
« Respuesta #1 en: 06 de Agosto de 2005, 16:53:00 »
hols:

simplemente colocas el include. no necesitas nada mas, si quieres cambiar alguno de los pines sclk  rst o I/O  abre la libreria, y la modificas, aqui un bosquejo, lo modifique a partir de uno que trae el ccs, busca en los ejemplos.......



Codigo:
////////////////////////////////////////////////////////////////////////////////
//
//  MUESTRA LA HORA EN TIEMPO REAL USANDO UN RTC  DS1302
//  CON TECLADO MATRICIAL DE 4X4
//CCS COMPILER V3.227   fjrd07@yahoo.es                      10072005
///////////////////////////////////////////////////////////////////////////////////////
#include <16F877A.h>    // DISPOSITIVO APROGRAMAR
#fuses XT,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT//PALABRA DECONFIGURACION
#use delay(clock=4000000)//FRECUENCIA EN MHZ DEL CRISTAL
#byte portb=6           // DIRECION DE MEMORIA DE PORTB
#byte portd=8           // DIRECION DE MEMORIA DE PORTD
#bit RBIF=11.0          //BANDERA DE CAMBIO DE ESTADO PINES RB4-RB7
#include <GET_KEY.h>//libreria que permite manejar un teclado de 4X4
#include <lcd.c>   // libreria que maneja la LCD
#include <ds1302.c>// permite usar un reloj en tiempo real (RTC) ds1302
#define use_portd_lcd TRUE //  puerto donde voy a conectar la lcd

byte day,mth,year,dow,hour,min,sec,hr;
pausa(long  x)
{
delay_ms(x);
}
////////////////////////////////////////////////////////////////////////////////
//
//     CONVESION BIN  A BCD PARA PODER PROGRAMAR EL DS1302
//
////////////////////////////////////////////////////////////////////////////////
BYTE get_bcd() {
  char primero,segundo;// caracteres donde se retorna el num en bcd

  do {
  while (!RBIF);
  pausa(15); // Espera para evitar rebotes.
  RBIF=0; // Prepara el flag para la siguiente vez.
  uno=GET_KEY(); // obtiene caracter de teclado.
  while (!RBIF);
  pausa(15); // Espera para evitar rebotes.
  RBIF=0;
  } while ((primero<"0") || (primero>"9"));
  lcd_putc(primero);// muestra en lcd primer caracter
  primero-="0";

  do {
    while (!RBIF);
    pausa(15); // Espera para evitar rebotes.
    RBIF=0; // Prepara el flag para la siguiente vez.
    segundo=GET_KEY();//obtiene caracter del teclado
    while (!RBIF);
    pausa(15); // Espera para evitar rebotes.
    RBIF=0;
  } while (((segundo<"0") || (segundo>"9")) && (segundo!=""));// valida datos
  lcd_putc(segundo);//muestra en lcd segundo

  if(segundo=="")//retorna con caracteres en bcd en primero y segundo
    return(primero);
  else
    return((primero<<4)|(segundo-"0"));
}
////////////////////////////////////////////////////////////////////////////////
// FUNCION QUE SETEA EL RELOJ RTC DS1302
//
////////////////////////////////////////////////////////////////////////////////

void set_clock(){//setea los parametros de hora y fecha del rtc
   BYTE day, mth, year, dow, hour, min;
   lcd_init();
   printf(lcd_putc," AJUSTE FECHA: "Giño;
   printf(lcd_putc,"
ANO 20: "Giño;
   year=get_bcd();
   pausa(2000);
   lcd_init();
   printf(lcd_putc," AJUSTE MES 0-12: "Giño;
   printf(lcd_putc,"
MES: "Giño;
   mth=get_bcd();
   pausa(2000);
   lcd_init();
   printf(lcd_putc," AJUSTE DIA: 0-30 "Giño;
   printf(lcd_putc,"
DIA: "Giño;
   day=get_bcd();
   pausa(2000);
   lcd_init();
   printf(lcd_putc," AJUSTE DIA SEM: "Giño;
   printf(lcd_putc,"
DIA SEMANA 1-7: "Giño;
   dow=get_bcd();
   pausa(2000);
   lcd_init();
   printf(lcd_putc," AJUSTE HORA: "Giño;
   printf(lcd_putc,"
HORA: "Giño;
   hour=get_bcd();
   pausa(2000);
   lcd_init();
   printf(lcd_putc," AJUSTE HORA: "Giño;
   printf(lcd_putc,"
MiN: "Giño;
   min=get_bcd();
   pausa(2000);
   rtc_set_datetime(day,mth,year,dow,hour,min);
}


//////////////////////////////////////////////////////////////
//
//                  PROGRAMA PRINCIPAl
//
/////////////////////////////////////////////////////////////
 main()
{

   enable_interrupts(GLOBAL);     // Activamos interrupciones globales (teclado)
   enable_interrupts(INT_RB); // ... y la interrupcion por cambioen rb4-rb7
   rtc_init();        // inicializo RTC
   rtc_set_datetime(0,0,0,0,0,0); // A CEROS TODOS LOS DATOS (no es necesario)
   get_key_init();    // INICIALIZO TECLADO
   lcd_init();        // inicializo LCD
   while (TRUE)//bucle infinito escanea el teclado y muestra su valor
   {
        lcd_init();        // inicializo LCD
        for(i=0;i>-44;i--)
               {
        lcd_gotoxy(1,2);// ME UBICO EN LA FILA 1 CARACTER 3
        printf(lcd_putc,"H %X:%X:%X D%X/%X/%X",hr,min,sec,day,mth,year);
        lcd_gotoxy(i,1);// ME UBICO EN LA FILA 2 CARACTER i
        lcd_putc(" RELOJ CALENDARIO RTC DS1302     "Giño;
        rtc_get_time(hr,min,sec);//  lectura de la hora desde el rtc
        rtc_get_date(day,mth,year,dow);// lectura de la fecha desde el rtc
        PAUSA(300);
                }
     }
 }
//******************************************************************************
//                  CABECERA DE INTERRUPCIONES
//          SI SE ACTIVA EL TECLADO HAY INTERRUPCION
//******************************************************************************
 #INT_RB // Rutina de servicio de la interrupcion por cambio en rb0-rb7
rb_handler()
{
              disable_interrupts(GLOBAL); //desabilito interrupciones mientras atiendo
              rbif=0;
              lcd_init();        // inicializo LCD
              lcd_putc("SET CLOCK"Giño;
              delay_ms(2000);
              get_key();
              if(get_key()=="D")set_clock();CONTINUE;}
 }



saludosSonrisa GiganteSonrisa GiganteSonrisa GiganteSonrisa GiganteSonrisa GiganteSonrisa GiganteSonrisa GiganteSonrisa GiganteSonrisa GiganteSonrisa GiganteSonrisa Gigante

Desconectado hqv

  • PIC12
  • **
  • Mensajes: 70
RE: Dudas sobre reloj DS1302
« Respuesta #2 en: 07 de Agosto de 2005, 12:34:00 »
gracias po la ayuda..pero ahora tengo un problema con que el reloj ds1302 me manda hoiras que no existen...los segundos llegan hasta 100 y los minutos también...

Qué puedo hacer para solucionjar ese desajuste?????


Gracias

hq

Desconectado laloavr

  • PIC10
  • *
  • Mensajes: 34
RE: Dudas sobre reloj DS1302
« Respuesta #3 en: 22 de Agosto de 2005, 17:03:00 »

Postea parte de tu codigo de control del RTC para Ayudarte

Desconectado alejosro

  • PIC10
  • *
  • Mensajes: 3
Re: Dudas sobre reloj DS1302
« Respuesta #4 en: 27 de Noviembre de 2009, 10:03:36 »
Buena alguien me podriafacitlitar la librerai ds1302??? gracias

Desconectado AND_8901

  • PIC10
  • *
  • Mensajes: 1
Re: Dudas sobre reloj DS1302
« Respuesta #5 en: 15 de Abril de 2010, 00:00:52 »
como estan?
espero me puedan ayudar estoy usando el ds1302 con un pic 18f452, el reloj me funsiona lo unico que me falta es como poner la hora como am o pm no se como escribir en el bit 7 y 5 para que me cambien el formato espero me puedan ayudar, gracias

Desconectado Miquel_S

  • Colaborador
  • PIC24H
  • *****
  • Mensajes: 1251
Re: Dudas sobre reloj DS1302
« Respuesta #6 en: 15 de Abril de 2010, 03:38:59 »
Hola alejosro aqui la libreria ds1302.
Código: CSS
  1. //////////////////////////////////////////////////////////////////////////
  2. ////                               DS1302.C                           ////
  3. ////                     Driver for Real Time Clock                   ////
  4. ////                                                                  ////
  5. ////  rtc_init()                                   Call after power up////
  6. ////                                                                  ////
  7. ////  rtc_set_datetime(day,mth,year,dow,hour,min)  Set the date/time  ////
  8. ////                                                                  ////
  9. ////  rtc_get_date(day,mth,year,dow)               Get the date       ////
  10. ////                                                                  ////
  11. ////  rtc_get_time(hr,min,sec)                     Get the time       ////
  12. ////                                                                  ////
  13. ////  rtc_write_nvr(address,data)                  Write to NVR       ////
  14. ////                                                                  ////
  15. ////  data = rtc_read_nvr(address)                 Read from NVR      ////
  16. ////                                                                  ////
  17. ////  get_bcd(data)                              Convert data to BCD  ////
  18. ////                                                                  ////
  19. ////  rm_bcd(data)                               Convert data to int  ////
  20. ////                                                                  ////
  21. //////////////////////////////////////////////////////////////////////////
  22. ////        (C) Copyright 1996,2003 Custom Computer Services          ////
  23. //// This source code may only be used by licensed users of the CCS C ////
  24. //// compiler.  This source code may only be distributed to other     ////
  25. //// licensed users of the CCS C compiler.  No other use, reproduction////
  26. //// or distribution is permitted without written permission.         ////
  27. //// Derivative programs created using this software in object code   ////
  28. //// form are not restricted in any way.                              ////
  29. //////////////////////////////////////////////////////////////////////////
  30.  
  31. #ifndef RTC_SCLK
  32.  
  33. #define RTC_SCLK PIN_B1
  34. #define RTC_IO   PIN_B3
  35. #define RTC_RST  PIN_B2
  36.  
  37. #endif
  38.  
  39. void write_ds1302_byte(BYTE cmd) {
  40.    BYTE i;
  41.  
  42.    for(i=0;i<=7;++i) {
  43.       output_bit(RTC_IO, shift_right(&cmd,1,0) );
  44.       output_high(RTC_SCLK);
  45.       output_low(RTC_SCLK);
  46.    }
  47. }
  48.  
  49. void write_ds1302(BYTE cmd, BYTE data) {
  50.  
  51.    output_high(RTC_RST);
  52.    write_ds1302_byte(cmd);
  53.    write_ds1302_byte(data);
  54.    output_low(RTC_RST);
  55. }
  56.  
  57. BYTE read_ds1302(BYTE cmd) {
  58.    BYTE i,data;
  59.  
  60.    output_high(RTC_RST);
  61.    write_ds1302_byte(cmd);
  62.  
  63.    for(i=0;i<=7;++i) {
  64.       shift_right(&data,1,input(RTC_IO));
  65.       output_high(RTC_SCLK);
  66.       delay_us(2);
  67.       output_low(RTC_SCLK);
  68.       delay_us(2);
  69.    }
  70.    output_low(RTC_RST);
  71.  
  72.    return(data);
  73. }
  74.  
  75. void rtc_init() {
  76.    BYTE x;
  77.    output_low(RTC_RST);
  78.    delay_us(2);
  79.    output_low(RTC_SCLK);
  80.    write_ds1302(0x8e,0);
  81.    write_ds1302(0x90,0xa4);
  82.    x=read_ds1302(0x81);
  83.    if((x & 0x80)!=0)
  84.      write_ds1302(0x80,0);
  85. }
  86.  
  87. int get_bcd(BYTE data)
  88. {
  89.    int nibh;
  90.    int nibl;
  91.  
  92.    nibh=data/10;
  93.    nibl=data-(nibh*10);
  94.  
  95.    return((nibh<<4)|nibl);
  96. }
  97.  
  98. int rm_bcd(BYTE data)
  99. {
  100.    int i;
  101.  
  102.    i=data;
  103.    data=(i>>4)*10;
  104.    data=data+(i<<4>>4);
  105.  
  106.    return data;
  107. }
  108.  
  109. void rtc_set_datetime(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min) {
  110.  
  111.    write_ds1302(0x86,get_bcd(day));
  112.    write_ds1302(0x88,get_bcd(mth));
  113.    write_ds1302(0x8c,get_bcd(year));
  114.    write_ds1302(0x8a,get_bcd(dow));
  115.    write_ds1302(0x84,get_bcd(hr));
  116.    write_ds1302(0x82,get_bcd(min));
  117.    write_ds1302(0x80,get_bcd(0));
  118. }
  119.  
  120. void rtc_get_date(BYTE& day, BYTE& mth, BYTE& year, BYTE& dow) {
  121.    day = rm_bcd(read_ds1302(0x87));
  122.    mth = rm_bcd(read_ds1302(0x89));
  123.    year = rm_bcd(read_ds1302(0x8d));
  124.    dow = rm_bcd(read_ds1302(0x8b));
  125. }
  126.  
  127. void rtc_get_time(BYTE& hr, BYTE& min, BYTE& sec) {
  128.    hr = rm_bcd(read_ds1302(0x85));
  129.    min = rm_bcd(read_ds1302(0x83));
  130.    sec = rm_bcd(read_ds1302(0x81));
  131. }
  132.  
  133. void rtc_write_nvr(BYTE address, BYTE data) {
  134.    write_ds1302(address|0xc0,data);
  135. }
  136.  
  137. BYTE rtc_read_nvr(BYTE address) {
  138.     return(read_ds1302(address|0xc1));
  139. }

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


 

anything