Autor Tema: Mudando codigo del DS1307 al DS3231  (Leído 3071 veces)

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

Desconectado esperanzachat

  • PIC10
  • *
  • Mensajes: 39
Mudando codigo del DS1307 al DS3231
« en: 23 de Mayo de 2019, 14:19:17 »
Hola a toidos, tengo hecho un codigo que usa el DS1307, pero como tiene adelantos de la hora, para solucinarlo, es usar el DS3231 en vez del DS1307, la idea es que cuando le mando por bluetooth el comando "hora", el sistema muestra la hora, repito que el sistema funciona bien con el DS1307, pero quiero hacer lo mismo usando el DS3231. Aqui les dejo el codigo simplificado, no se porque no muestra la hora. Uso una libreria para el DS3231.
Código: [Seleccionar]
#include <16F88.h>
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES PUT                      //Power Up Timer
#FUSES PROTECT                  //Code protected from reads
#FUSES NOBROWNOUT               //No brownout reset
#FUSES MCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#use delay(int=4000000)
#use rs232(uart1, baud=9600)//usart1 -->ajuste de XMIT y RCV para la USART 1
#use I2C(MASTER, I2C1, SLOW = 100000, STREAM = DS3231_STREAM)
#include <stdlib.h>////para poder hacer las conversiones a entero
#include <DS3231.c>  // include DS1307 driver source file
// DS3231 library variable declaration
RTC_Time *mytime;
int8 DMinutos=0,UMinutos=0,DHora=0,UHora=0,TActivacion=10,hora=0,minutos=0,segundos=0;
int8 t1,t2,t3,t4,t5;
int8 m1,m2,m3,m4,m5;
// CONSTANTES /////////////////////////////////////////////////////////////////
int const lenbuff=32;                  // Longitud de buffer, Ajustar
                                       // a lo que desees (o te sea posible)
                                       
                                       // VARIABLES EN RAM ///////////////////////////////////////////////////////////
int  xbuff=0x00;                       // Índice: siguiente char en cbuff
char cbuff[lenbuff];                   // Buffer
char rcvchar=0x00;                     // último caracter recibido
char strHoras[3];
char strMinutos[3];
char strTa[3];
int1 flagcommand=0,EsNumero=0;                    // Flag para indicar comando disponible
int8 CantidadNumeros=0;
int8 i=0;
int1 Activar=0;//me dice si tengo que activar la salida

// Declaración de Funciones ///////////////////////////////////////////////////

void inicbuff(void);                   // Borra buffer
int  addcbuff(char c);                 // añade caracter recibido al buffer
void echos(char c);                    // Eco selectivo sobre RS232


// Desarrollo de Funciones ////////////////////////////////////////////////////

void inicbuff(void){                   // Inicia a  cbuff -------------------
   int i;

   for(i=0;i<lenbuff;i++){             // Bucle que pone a 0 todos los
      cbuff[ i ]=0x00;                   // caracteres en el buffer
   }
   xbuff=0x00;                         // Inicializo el indice de siguiente
                                       // caracter
}

int addcbuff(char c)
{                  // Añade a cbuff -----------------------   
   cbuff[xbuff++]=c;          // Añade caracter recibido al Buffer
}

// INTERRUPCIONES /////////////////////////////////////////////////////////////

#int_rda
void serial_isr() {                    // Interrupción recepción serie USART

   ///restart_wdt();
   rcvchar=0x00;                       // Inicializo caracter recibido
   if(kbhit()){                        // Si hay algo pendiente de recibir ...
      rcvchar=getc();                  // lo descargo y ...
      addcbuff(rcvchar);               // lo añado al buffer y ...
      ///echos(rcvchar);                  // hago eco (si procede).
   }
}
ImprimirPlantillaReloj(void)
{
   //printf("TIME: %02u:%02u:%02u", mytime->hours, mytime->minutes, mytime->seconds);
   printf("%u",DHora);
   printf("%u",UHora);
   printf(":");
   printf("%u",DMinutos);
   printf("%u\r\n",UMinutos);//salto de linea
}
MostrarHoraActual(void)

   DHora=mytime->hours/10;///descompongo hora actual
   UHora=mytime->hours%10;
   DMinutos=mytime->minutes/10;
   UMinutos=mytime->minutes%10;
   
   printf("%u",DHora);
   printf("%u",UHora);
   printf(":");
   printf("%u",DMinutos);
   printf("%u\r\n",UMinutos);//salto de linea
}

AjustarHoraActual(void)
{
  mytime->hours   = hora;
  mytime->minutes = minutos;
  mytime->seconds = 0;
  mytime->dow     = THURSDAY;
  mytime->day     = 3;
  mytime->month   = JANUARY;
  mytime->year    = 19;
  // write time and date to the RTC chip
  RTC_Set(mytime);
  printf("Hora ajustada...\r\n");
}
   
ObtenerHoraActual(void)
{
   // read current time and date from the RTC chip
  mytime = RTC_Get();///llamo a la funcion que me da la hora
}

void main() {
  setup_ccp1(CCP_OFF);
  setup_comparator(NC_NC_NC_NC);
  enable_interrupts(int_rda);
  enable_interrupts(global);
  set_tris_b(0x04);  ///0000 0111       
  set_tris_a(0xFF);
  IntSqw_Set(OUT_1Hz);// enable SQW output with frequency of 1Hz
  inicbuff();    // Borra buffer al inicio
  output_Low(PIN_B3);///salida rele en bajo
  while (TRUE)
      {
      flagcommand==0;///todavia no recibi un comando
      inicbuff();    // Borra buffer al inicio
      delay_ms(1000);///espera que pase el tiempo
      if((cbuff[0]=='h')&&(cbuff[1]=='o')&&(cbuff[2]=='r')&&(cbuff[3]=='a'))///comando para ver la hora
        {
        flagcommand=1; ///me dice que recibio un comando
        printf("%s\r\n",cbuff);//salto de linea
        printf("La Hora Actual es: ");
        ObtenerHoraActual();////me da la hora actual
        MostrarHoraActual();///muestra la hora
        inicbuff(); ///borra buffer
        }
      if(flagcommand==0) inicbuff(); ///borra buffer   
      }
 }
adjunto la libreria y la página original:
https://simple-circuit.com/ds3231-rtc-driver-ccs-c-compiler/
Ojalá puedan ayudarme, gracias.

Desconectado esperanzachat

  • PIC10
  • *
  • Mensajes: 39
Re:Mudando codigo del DS1307 al DS3231
« Respuesta #1 en: 26 de Mayo de 2019, 21:51:11 »
Hola de nuevo, sigo tratando de mudar el codigo pero estoy fracazando. Al mandar el comando "hora" para que me muestre la hora, el sistema responde, pero no imprime la hora. aqui el codigo.
Código: [Seleccionar]
#include <16F88.h>
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES PUT                      //Power Up Timer
#FUSES PROTECT                  //Code protected from reads
#FUSES NOBROWNOUT               //No brownout reset
#FUSES MCLR                   //Master Clear pin used for I/O
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#use delay(int=4000000)
#use rs232(uart1, baud=9600)//usart1 -->ajuste de XMIT y RCV para la USART 1
#use I2C(MASTER, I2C1, SLOW = 100000, STREAM = DS3231_STREAM)
#include <stdlib.h>////para poder hacer las conversiones a entero
#include <DS3231.c>  // include DS1307 driver source file
// DS3231 library variable declaration
RTC_Time *mytime;
int8 DMinutos=0,UMinutos=0,DHora=0,UHora=0,TActivacion=10,hora=0,minutos=0,segundos=0;
int8 t1,t2,t3,t4,t5;
int8 m1,m2,m3,m4,m5;
// CONSTANTES /////////////////////////////////////////////////////////////////
int const lenbuff=32;                  // Longitud de buffer, Ajustar
                                       // a lo que desees (o te sea posible)
                                       
                                       // VARIABLES EN RAM ///////////////////////////////////////////////////////////
int  xbuff=0x00;                       // Índice: siguiente char en cbuff
char cbuff[lenbuff];                   // Buffer
char rcvchar=0x00;                     // último caracter recibido
char strHoras[3];
char strMinutos[3];
char strTa[3];
int1 flagcommand=0,EsNumero=0;                    // Flag para indicar comando disponible
int8 CantidadNumeros=0;
int8 i=0;
int1 Activar=0;//me dice si tengo que activar la salida

// Declaración de Funciones ///////////////////////////////////////////////////

void inicbuff(void);                   // Borra buffer
int  addcbuff(char c);                 // añade caracter recibido al buffer
void echos(char c);                    // Eco selectivo sobre RS232


// Desarrollo de Funciones ////////////////////////////////////////////////////

void inicbuff(void){                   // Inicia a  cbuff -------------------
   int i;

   for(i=0;i<lenbuff;i++){             // Bucle que pone a 0 todos los
      cbuff[ i ]=0x00;                   // caracteres en el buffer
   }
   xbuff=0x00;                         // Inicializo el indice de siguiente
                                       // caracter
}

int addcbuff(char c)
{                  // Añade a cbuff -----------------------   
   cbuff[xbuff++]=c;          // Añade caracter recibido al Buffer
}

// INTERRUPCIONES /////////////////////////////////////////////////////////////

#int_rda
void serial_isr() {                    // Interrupción recepción serie USART

   ///restart_wdt();
   rcvchar=0x00;                       // Inicializo caracter recibido
   if(kbhit()){                        // Si hay algo pendiente de recibir ...
      rcvchar=getc();                  // lo descargo y ...
      addcbuff(rcvchar);               // lo añado al buffer y ...
      ///echos(rcvchar);                  // hago eco (si procede).
   }
}
MostrarHoraActual(void)

   DHora=1;//mytime->hours)/10;///descompongo hora actual
   UHora=2;//(mytime->hours)%10;
   DMinutos=3;//(mytime->minutes)/10;
   UMinutos=5;//(mytime->minutes)%10;
   
   printf("%u",DHora);
   printf("%u",UHora);
   printf(":");
   printf("%u",DMinutos);
   printf("%u\r\n",UMinutos);//salto de linea
}

AjustarHoraActual(void)
{
  mytime->hours   = hora;
  mytime->minutes = minutos;
  mytime->seconds = 0;
  mytime->dow     = THURSDAY;
  mytime->day     = 3;
  mytime->month   = JANUARY;
  mytime->year    = 19;
  // write time and date to the RTC chip
  RTC_Set(mytime);
  printf("Hora ajustada...\r\n");
}
   
ObtenerHoraActual(void)
{
   // read current time and date from the RTC chip
 mytime = RTC_Get();///llamo a la funcion que me da la hora
}

void main() {
  setup_ccp1(CCP_OFF);
  setup_comparator(NC_NC_NC_NC);
  enable_interrupts(int_rda);
  enable_interrupts(global);
  set_tris_b(0x04);  ///0000 0111       
  set_tris_a(0xFF);
  //IntSqw_Set(OUT_1Hz);// enable SQW output with frequency of 1Hz
  inicbuff();    // Borra buffer al inicio
  output_Low(PIN_B3);///salida rele en bajo
  while (TRUE)
      {
      flagcommand==0;///todavia no recibi un comando
      inicbuff();    // Borra buffer al inicio
      delay_ms(1000);///espera que pase el tiempo
      if((cbuff[0]=='h')&&(cbuff[1]=='o')&&(cbuff[2]=='r')&&(cbuff[3]=='a'))///comando para ver la hora
        {
        flagcommand=1; ///me dice que recibio un comando
        printf("%s\r\n",cbuff);//salto de linea
        printf("La Hora Actual es: ");
        ObtenerHoraActual();////me da la hora actual
        MostrarHoraActual();///muestra la hora
        inicbuff(); ///borra buffer
        }
      if(flagcommand==0) inicbuff(); ///borra buffer   
      }
 }
}
aqui la libreria ds3231:
Código: [Seleccionar]
///////////////////////////////////////////////////////////////////////////
////                                                                   ////
////                             DS3231.c                              ////
////                                                                   ////
////                      Driver for CCS C compiler                    ////
////                                                                   ////
////     Driver for Maxim DS3231 serial I2C real-time clock (RTC).     ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////
////                                                                   ////
////                     https://simple-circuit.com/                   ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////


#if defined DS3231_I2C_NO_STREAM
  #define RTC_I2C_START()   i2c_start()
  #define RTC_I2C_STOP()    i2c_stop()
  #define RTC_I2C_WRITE(x)  i2c_write(x)
  #define RTC_I2C_READ(x)   i2c_read(x)
#elif defined DS3231_I2C_STREAM
  #define RTC_I2C_START()   i2c_start(DS3231_I2C_STREAM)
  #define RTC_I2C_STOP()    i2c_stop(DS3231_I2C_STREAM)
  #define RTC_I2C_WRITE(x)  i2c_write(DS3231_I2C_STREAM, x)
  #define RTC_I2C_READ(x)   i2c_read(DS3231_I2C_STREAM, x)
#else
  #define RTC_I2C_START()   i2c_start(DS3231_STREAM)
  #define RTC_I2C_STOP()    i2c_stop(DS3231_STREAM)
  #define RTC_I2C_WRITE(x)  i2c_write(DS3231_STREAM, x)
  #define RTC_I2C_READ(x)   i2c_read(DS3231_STREAM, x)
#endif

#include <stdint.h>

#define DS3231_ADDRESS       0xD0
#define DS3231_REG_SECONDS   0x00
#define DS3231_REG_AL1_SEC   0x07
#define DS3231_REG_AL2_MIN   0x0B
#define DS3231_REG_CONTROL   0x0E
#define DS3231_REG_STATUS    0x0F
#define DS3231_REG_TEMP_MSB  0x11

typedef enum
{
  SUNDAY = 1,
  MONDAY,
  TUESDAY,
  WEDNESDAY,
  THURSDAY,
  FRIDAY,
  SATURDAY
} RTC_DOW;

typedef enum
{
  JANUARY = 1,
  FEBRUARY,
  MARCH,
  APRIL,
  MAY,
  JUNE,
  JULY,
  AUGUST,
  SEPTEMBER,
  OCTOBER,
  NOVEMBER,
  DECEMBER
} RTC_Month;

typedef struct rtc_tm
{
  uint8_t seconds;
  uint8_t minutes;
  uint8_t hours;
  RTC_DOW dow;
  uint8_t day;
  RTC_Month month;
  uint8_t year;
} RTC_Time;

typedef enum
{
  ONCE_PER_SECOND = 0x0F,
  SECONDS_MATCH = 0x0E,
  MINUTES_SECONDS_MATCH = 0x0C,
  HOURS_MINUTES_SECONDS_MATCH = 0x08,
  DATE_HOURS_MINUTES_SECONDS_MATCH = 0x0,
  DAY_HOURS_MINUTES_SECONDS_MATCH = 0x10
} al1;

typedef enum
{
  ONCE_PER_MINUTE = 0x0E,
  MINUTES_MATCH = 0x0C,
  HOURS_MINUTES_MATCH = 0x08,
  DATE_HOURS_MINUTES_MATCH = 0x0,
  DAY_HOURS_MINUTES_MATCH = 0x10
} al2;

typedef enum
{
  OUT_OFF = 0x00,
  OUT_INT = 0x04,
  OUT_1Hz = 0x40,
  OUT_1024Hz = 0x48,
  OUT_4096Hz = 0x50,
  OUT_8192Hz = 0x58
} INT_SQW;

RTC_Time c_time, c_alarm1, c_alarm2;

///////////////////////// All Functions /////////////////////////
                                                               //
uint8_t bcd_to_decimal(uint8_t number);                        //
uint8_t decimal_to_bcd(uint8_t number);                        //
void RTC_Set(RTC_Time *time_t);                                //
RTC_Time *RTC_Get();                                           //
void Alarm1_Set(RTC_Time *time_t, al1 _config);                //
RTC_Time *Alarm1_Get();                                        //
void Alarm1_Enable();                                          //
void Alarm1_Disable();                                         //
int1 Alarm1_IF_Check();                                        //
void Alarm1_IF_Reset();                                        //
int1 Alarm1_Status();                                          //
void Alarm2_Set(RTC_Time *time_t, al2 _config);                //
RTC_Time *Alarm2_Get();                                        //
void Alarm2_Enable();                                          //
void Alarm2_Disable();                                         //
int1 Alarm2_IF_Check();                                        //
void Alarm2_IF_Reset();                                        //
int1 Alarm2_Status();                                          //
void IntSqw_Set(INT_SQW _config);                              //
void Enable_32kHZ();                                           //
void Disable_32kHZ();                                          //
void OSC_Start();                                              //
void OSC_Stop();                                               //
int16_t Get_Temperature();                                     //
uint8_t RTC_Read_Reg(uint8_t reg_address);                     //
void RTC_Write_Reg(uint8_t reg_address, uint8_t reg_value);    //
                                                               //
/////////////////////////////////////////////////////////////////

// converts BCD to decimal
uint8_t bcd_to_decimal(uint8_t number)
{
  return ( (number >> 4) * 10 + (number & 0x0F) );
}

// converts decimal to BCD
uint8_t decimal_to_bcd(uint8_t number)
{
  return ( ((number / 10) << 4) + (number % 10) );
}

// sets time and date
void RTC_Set(RTC_Time *time_t)
{
  // convert decimal to BCD
  time_t->day     = decimal_to_bcd(time_t->day);
  time_t->month   = decimal_to_bcd(time_t->month);
  time_t->year    = decimal_to_bcd(time_t->year);
  time_t->hours   = decimal_to_bcd(time_t->hours);
  time_t->minutes = decimal_to_bcd(time_t->minutes);
  time_t->seconds = decimal_to_bcd(time_t->seconds);
  // end conversion

  // write data to the RTC chip
  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS);
  RTC_I2C_WRITE(DS3231_REG_SECONDS);
  RTC_I2C_WRITE(time_t->seconds);
  RTC_I2C_WRITE(time_t->minutes);
  RTC_I2C_WRITE(time_t->hours);
  RTC_I2C_WRITE(time_t->dow);
  RTC_I2C_WRITE(time_t->day);
  RTC_I2C_WRITE(time_t->month);
  RTC_I2C_WRITE(time_t->year);
  RTC_I2C_STOP();
}

// reads time and date
RTC_Time *RTC_Get()
{
  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS);
  RTC_I2C_WRITE(DS3231_REG_SECONDS);
  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS | 0x01);
  c_time.seconds = RTC_I2C_READ(1);
  c_time.minutes = RTC_I2C_READ(1);
  c_time.hours   = RTC_I2C_READ(1);
  c_time.dow   = RTC_I2C_READ(1);
  c_time.day   = RTC_I2C_READ(1);
  c_time.month = RTC_I2C_READ(1);
  c_time.year  = RTC_I2C_READ(0);
  RTC_I2C_STOP();

  // convert BCD to decimal
  c_time.seconds = bcd_to_decimal(c_time.seconds);
  c_time.minutes = bcd_to_decimal(c_time.minutes);
  c_time.hours   = bcd_to_decimal(c_time.hours);
  c_time.day     = bcd_to_decimal(c_time.day);
  c_time.month   = bcd_to_decimal(c_time.month);
  c_time.year    = bcd_to_decimal(c_time.year);
  // end conversion

  return &c_time;
}

// sets alarm1 details
void Alarm1_Set(RTC_Time *time_t, al1 _config)
{
  // convert decimal to BCD
  time_t->day     = decimal_to_bcd(time_t->day);
  time_t->hours   = decimal_to_bcd(time_t->hours);
  time_t->minutes = decimal_to_bcd(time_t->minutes);
  time_t->seconds = decimal_to_bcd(time_t->seconds);
  // end conversion

  // write data to the RTC chip
  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS);
  RTC_I2C_WRITE(DS3231_REG_AL1_SEC);
  RTC_I2C_WRITE( (time_t->seconds) | (bit_test(_config, 0) << 7) );
  RTC_I2C_WRITE( (time_t->minutes) | (bit_test(_config, 1) << 7) );
  RTC_I2C_WRITE( (time_t->hours) | (bit_test(_config, 2) << 7) );
  if ( bit_test(_config, 4) )
    RTC_I2C_WRITE( (time_t->dow) | 0x40 | (bit_test(_config, 3) << 7) );
  else
    RTC_I2C_WRITE( (time_t->day) | (bit_test(_config, 3) << 7) );
  RTC_I2C_STOP();
}

// reads alarm1 details
RTC_Time *Alarm1_Get()
{
  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS);
  RTC_I2C_WRITE(DS3231_REG_AL1_SEC);
  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS | 0x01);
  c_alarm1.seconds = RTC_I2C_READ(1) & 0x7F;
  c_alarm1.minutes = RTC_I2C_READ(1) & 0x7F;
  c_alarm1.hours   = RTC_I2C_READ(1) & 0x3F;
  c_alarm1.dow = c_alarm1.day = RTC_I2C_READ(0) & 0x3F;
  RTC_I2C_STOP();

  // convert BCD to decimal
  c_alarm1.seconds = bcd_to_decimal(c_alarm1.seconds);
  c_alarm1.minutes = bcd_to_decimal(c_alarm1.minutes);
  c_alarm1.hours   = bcd_to_decimal(c_alarm1.hours);
  c_alarm1.day     = bcd_to_decimal(c_alarm1.day);
  // end conversion

  return &c_alarm1;
}

// enables alarm1
void Alarm1_Enable()
{
  uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
  ctrl_reg |= 0x01;
  RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}

// disables alarm1
void Alarm1_Disable()
{
  uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
  ctrl_reg &= 0xFE;
  RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}

// checks if alarm1 occurred, returns 1 if yes and 0 if no
int1 Alarm1_IF_Check()
{
  uint8_t stat_reg = RTC_Read_Reg(DS3231_REG_STATUS);
  return bit_test(stat_reg, 0);
}

// resets alarm1 flag bit
void Alarm1_IF_Reset()
{
  uint8_t stat_reg = RTC_Read_Reg(DS3231_REG_STATUS);
  stat_reg &= 0xFE;
  RTC_Write_Reg(DS3231_REG_STATUS, stat_reg);
}

// returns TRUE (1) if alarm1 is enabled and FALSE (0) if disabled
int1 Alarm1_Status()
{
  uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
  if(ctrl_reg & 0x01)
    return 1;
  else
    return 0;
}

// sets alarm2 details
void Alarm2_Set(RTC_Time *time_t, al2 _config)
{
  // convert decimal to BCD
  time_t->day     = decimal_to_bcd(time_t->day);
  time_t->hours   = decimal_to_bcd(time_t->hours);
  time_t->minutes = decimal_to_bcd(time_t->minutes);
  // end conversion

  // write data to the RTC chip
  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS);
  RTC_I2C_WRITE(DS3231_REG_AL2_MIN);
  RTC_I2C_WRITE( (time_t->minutes) | (bit_test(_config, 1) << 7) );
  RTC_I2C_WRITE( (time_t->hours) | (bit_test(_config, 2) << 7) );
  if ( bit_test(_config, 4) )
    RTC_I2C_WRITE( (time_t->dow) | 0x40 | (bit_test(_config, 3) << 7) );
  else
    RTC_I2C_WRITE( (time_t->day) | (bit_test(_config, 3) << 7) );
  RTC_I2C_STOP();
}

// reads alarm2 details
RTC_Time *Alarm2_Get()
{
  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS);
  RTC_I2C_WRITE(DS3231_REG_AL2_MIN);
  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS | 0x01);
  c_alarm2.minutes = RTC_I2C_READ(1) & 0x7F;
  c_alarm2.hours   = RTC_I2C_READ(1) & 0x3F;
  c_alarm2.dow = c_alarm2.day = RTC_I2C_READ(0) & 0x3F;
  RTC_I2C_STOP();

  // convert BCD to decimal
  c_alarm2.minutes = bcd_to_decimal(c_alarm2.minutes);
  c_alarm2.hours   = bcd_to_decimal(c_alarm2.hours);
  c_alarm2.day     = bcd_to_decimal(c_alarm2.day);
  // end conversion

  return &c_alarm2;
}

// enables alarm2
void Alarm2_Enable()
{
  uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
  ctrl_reg |= 0x02;
  RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}

//disables alarm2
void Alarm2_Disable()
{
  uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
  ctrl_reg &= 0xFD;
  RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}

// checks if alarm2 occurred, returns 1 if yes and 0 if no
int1 Alarm2_IF_Check()
{
  uint8_t stat_reg = RTC_Read_Reg(DS3231_REG_STATUS);
  return bit_test(stat_reg, 1);
}

// resets alarm2 flag bit
void Alarm2_IF_Reset()
{
  uint8_t stat_reg = RTC_Read_Reg(DS3231_REG_STATUS);
  stat_reg &= 0xFD;
  RTC_Write_Reg(DS3231_REG_STATUS, stat_reg);
}

// returns TRUE (1) if alarm2 is enabled and FALSE (0) if disabled
int1 Alarm2_Status()
{
  uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
  if(ctrl_reg & 0x02)
    return 1;
  else
    return 0;
}

// writes 'reg_value' to register of address 'reg_address'
void RTC_Write_Reg(uint8_t reg_address, uint8_t reg_value)
{
  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS);
  RTC_I2C_WRITE(reg_address);
  RTC_I2C_WRITE(reg_value);
  RTC_I2C_STOP();
}

// returns the value stored in register of address 'reg_address'
uint8_t RTC_Read_Reg(uint8_t reg_address)
{
  uint8_t reg_data;

  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS);
  RTC_I2C_WRITE(reg_address);
  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS | 0x01);
  reg_data = RTC_I2C_READ(0);
  RTC_I2C_STOP();

  return reg_data;
}

// sets INT/SQW pin configuration
void IntSqw_Set(INT_SQW _config)
{
  uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
  ctrl_reg &= 0xA3;
  ctrl_reg |= _config;
  RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}

// enables 32kHz (pin 32kHz)
void Enable_32kHZ()
{
  uint8_t stat_reg = RTC_Read_Reg(DS3231_REG_STATUS);
  stat_reg |= 0x08;
  RTC_Write_Reg(DS3231_REG_STATUS, stat_reg);
}

// disables 32kHz (pin 32kHz)
void Disable_32kHZ()
{
  uint8_t stat_reg = RTC_Read_Reg(DS3231_REG_STATUS);
  stat_reg &= 0xF7;
  RTC_Write_Reg(DS3231_REG_STATUS, stat_reg);
}

// starts RTC oscillator
void OSC_Start()
{
  uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
  ctrl_reg &= 0x7F;
  RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}

// stops RTC oscillator
void OSC_Stop()
{
  uint8_t ctrl_reg = RTC_Read_Reg(DS3231_REG_CONTROL);
  ctrl_reg |= 0x80;
  RTC_Write_Reg(DS3231_REG_CONTROL, ctrl_reg);
}

// returns chip temperature
// Temperature is stored in hundredths C (output value of "3125" equals 31.25 °C).
int16_t Get_Temperature()
{
  uint8_t t_msb, t_lsb;
  uint16_t c_temp;
  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS);
  RTC_I2C_WRITE(DS3231_REG_TEMP_MSB);
  RTC_I2C_START();
  RTC_I2C_WRITE(DS3231_ADDRESS | 0x01);
  t_msb = RTC_I2C_READ(1);
  t_lsb = RTC_I2C_READ(0);
  RTC_I2C_STOP();

  c_temp = (uint16_t)t_msb << 2 | t_lsb >> 6;

  if(t_msb & 0x80)
    c_temp |= 0xFC00;

  return c_temp * 25;
}
Espero me puedan ayudar.gracias.

Desconectado KILLERJC

  • Colaborador
  • DsPIC33
  • *****
  • Mensajes: 8242
Re:Mudando codigo del DS1307 al DS3231
« Respuesta #2 en: 26 de Mayo de 2019, 22:14:24 »
Entonces tenes un problema en tu libreria.... Comparala con la del DS1307 y fijate que diferencias tiene y porque.

Desconectado esperanzachat

  • PIC10
  • *
  • Mensajes: 39
Re:Mudando codigo del DS1307 al DS3231
« Respuesta #3 en: 27 de Mayo de 2019, 15:12:54 »
Gracias.Voy a tratar emular primero el ejemplo de la pagina fuente a ver si funciona. Despues les cuento.


 

anything