|
|
View previous topic :: View next topic |
Author |
Message |
robo_z
Joined: 11 Dec 2014 Posts: 5
|
[Solved] DS3231 and PIC16f628A |
Posted: Wed Jun 14, 2023 1:28 pm |
|
|
Hello i want show the time and date from a RTC DS3231, but i have problems for show correctly.
this is my code:
Code: |
#include <16F628A.h>
#FUSES NOWDT
#FUSES PUT //Power Up Timer
#FUSES NOMCLR //Master Clear pin enabled
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES CPD //Data EEPROM Code Protected
#FUSES PROTECT //Code protected from reads
#use delay(internal=4MHz)
#use fast_io(A)
#use fast_io(B)
#define DS3231_INT PIN_B0
#define DS3231_SDA PIN_B1
#define DS3231_SCL PIN_B2
#define PIN_TXD PIN_B7
#use rs232(baud=9600, parity=N, xmit=PIN_TXD, rcv=0, bits=8, DISABLE_INTS, FORCE_SW, stream=SERIE)
#use i2c(MASTER, SDA=DS3231_SDA, SCL=DS3231_SCL, FAST=100000, STREAM=BUS_I2C)
const unsigned int DirDS3231 = 0xD0;
unsigned int Segundo, Minuto, Hora, Dia, Mes, Anio, Alarma1_Min, Alarma1_Hora, Alarma2_Min, Alarma2_Hora, Temperatura;
unsigned int Bcd2Dec(unsigned int MiBCD)
{
return ( (MiBCD >> 4) * 10 + (MiBCD & 0x0F) );
}
unsigned int Dec2Bcd(unsigned int MiDEC)
{
return ( ((MiDEC / 10) << 4) + (MiDEC % 10) );
}
void LeerDS3231(unsigned int DirReloj)
{
i2c_start(BUS_I2C); // Inicia la Comunicaion I2C
i2c_write(BUS_I2C, DirReloj); // Escribimos la Direccion del DS3231
i2c_write(BUS_I2C, 0x00); // Send register address (Registro Segundos)
i2c_stop(BUS_I2C); // Detiene la Comunicaion I2C
i2c_start(BUS_I2C); // Reiniciamos la Comunicacion I2C
i2c_write(BUS_I2C, DirReloj | 0x01); // Iniciamos la Lectura
Segundo = Bcd2Dec(i2c_read(BUS_I2C, 1)); // Leemos Segundos Del Registro 0
Minuto = Bcd2Dec(i2c_read(BUS_I2C, 1)); // Leemos Minutos Del Registro 1
Hora = Bcd2Dec(i2c_read(BUS_I2C, 1)); // Leemos Hora Del Registro 2
i2c_read(BUS_I2C, 1); // Saltamos el Registro 3 (Dia de la Semana)
Dia = Bcd2Dec(i2c_read(BUS_I2C, 1)); // Leemos Fecha Del Registro 4
Mes = Bcd2Dec(i2c_read(BUS_I2C, 1)); // Leemos Mes Del Registro 5
Anio = Bcd2Dec(i2c_read(BUS_I2C, 1)); // Leemos Anio Del Registro 6
i2c_read(BUS_I2C, 1); // Saltamos el Registro 7 (Alarma 1 Segundos)
Alarma1_Min = Bcd2Dec(i2c_read(BUS_I2C, 1)); // Leemos Alarma 1 Minutos Del Registro 8
Alarma1_Hora = Bcd2Dec(i2c_read(BUS_I2C, 1)); // Leemos Alarma 1 Horas Del Registro 9
i2c_read(BUS_I2C, 1); // Saltamos el Registro 10 (Alarma 1 Fecha)
Alarma2_Min = Bcd2Dec(i2c_read(BUS_I2C, 1)); // Leemos Alarma 2 Minutos Del Registro 11
Alarma2_Hora = Bcd2Dec(i2c_read(BUS_I2C, 1)); // Leemos Alarma 2 Horas Del Registro 12
i2c_read(BUS_I2C, 1); // Saltamos el Registro 13 (Alarma 2 Fecha)
i2c_read(BUS_I2C, 1); // Saltamos el Registro 14 (Control)
i2c_read(BUS_I2C, 1); // Saltamos el Registro 15 (Status)
i2c_read(BUS_I2C, 1); // Saltamos el Registro 16 (Anging Offset)
Temperatura = i2c_read(BUS_I2C, 1); // Leemos Temperatura MSB Del Registro 17
i2c_stop(BUS_I2C); // Detiene la Comunicaion I2C
}
void main()
{
setup_comparator(NC_NC_NC_NC);
port_b_pullups(0x0F);
set_tris_a(0x30);
set_tris_b(0x09);
while(TRUE)
{
LeerDS3231(DirDS3231);
fprintf(SERIE, "Fecha:%02u/%02u/%02u Hora:%02u:%02u:%02u Temperatura:%02u\r", Dia, Mes, Anio, Hora, Minuto, Segundo, Temperatura);
}
}
|
but i get irregular data:
Code: |
Fecha:14/06/23 Hora:14:22:33 Temperatura:00
Fecha:165/165/165 Hora:165:165:03 Temperatura:255
Fecha:14/06/23 Hora:14:22:33 Temperatura:00
Fecha:165/165/165 Hora:165:165:03 Temperatura:255
Fecha:14/06/23 Hora:14:22:33 Temperatura:32
Fecha:165/165/165 Hora:165:165:03 Temperatura:255
Fecha:14/06/23 Hora:14:22:33 Temperatura:32
Fecha:165/165/165 Hora:165:165:03 Temperatura:255
Fecha:14/06/23 Hora:14:22:33 Temperatura:32
...
|
how to solve this problem.
thanks for you help.
Last edited by robo_z on Wed Jun 14, 2023 5:02 pm; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Wed Jun 14, 2023 4:34 pm |
|
|
hmm...what is the value of the I2C bus pullup resistors ?
do you have battery attached to the DS3231 ?
also add a 1 second delay in your loop..helps us humans to see better... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Jun 14, 2023 5:00 pm |
|
|
You need to do a NACK on the last i2c_read operation.
You are doing an ACK.
ACK = 1
NACK = 0 |
|
|
robo_z
Joined: 11 Dec 2014 Posts: 5
|
|
Posted: Wed Jun 14, 2023 5:01 pm |
|
|
temtronic wrote: | hmm...what is the value of the I2C bus pullup resistors ?
do you have battery attached to the DS3231 ?
also add a 1 second delay in your loop..helps us humans to see better... |
thanks for you help
i solve add a not acknoledge at final of read.
Code: |
i2c_read(BUS_I2C, 1); // Saltamos el Registro 15 (Status)
i2c_read(BUS_I2C, 1); // Saltamos el Registro 16 (Aging Offset)
Temperatura = i2c_read(BUS_I2C, 1); // Leemos Temperatura MSB Del Registro 17
i2c_read(BUS_I2C, 0); // Saltamos el Registro 18 (Temperatura LSB) y Cerramos con NOT ACK
i2c_stop(BUS_I2C); // Detiene la Comunicaion I2C
|
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|