|
|
View previous topic :: View next topic |
Author |
Message |
mojsa2000
Joined: 18 May 2010 Posts: 78
|
problem with DS1307 |
Posted: Tue Jun 15, 2010 10:06 am |
|
|
hi
I want to use a DS1307. I have a problem that I can't undersatnd it. I have loaded time and date in ds1307 and when I read the time and date the first value that is read is true and the other values are not true.
Please help me.
Code: |
#include <16f877.h>
#use delay(clock=4000000)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#bit ack_check=0x91.6
#bit ack_dt=0x91.5
#include <lcd.c>
#define use_lcd_portd true //set port D to use LCD
unsigned int8 sec,min,hour,day,date,month,year;
unsigned int8 sec_bcd,min_bcd,hour_bcd,day_bcd,date_bcd,month_bcd,year_bcd;
unsigned int8 high_nibble,low_nibble;
read_sec()
{
i2c_start();
i2c_write(0xd0);
if(ack_check==0)
i2c_write(0x00);//set the address
if(ack_check==0)
i2c_start();
i2c_write(0xd1);
if(ack_check==0)
sec=i2c_read();
i2c_stop();
high_nibble=(sec&0xf0)>>4;
low_nibble=sec&0x0f;
sec_bcd=high_nibble*10+low_nibble;
printf("\r\%u",sec_bcd);
printf(lcd_putc,"%u",sec_bcd);
return sec_bcd;
}
/////////////////////////////////////////////////////
read_min()
{
i2c_start();
i2c_write(0xd0);
if(ack_check==0)
i2c_write(0x01);//set the address
if(ack_check==0)
i2c_start();
i2c_write(0xd1);
if(ack_check==0)
min=i2c_read();
i2c_stop();
high_nibble=(min&0xf0)>>4;
low_nibble=(min&0x0f);
min_bcd=high_nibble*10+low_nibble;
printf("\r\%u",min_bcd);
printf(lcd_putc,"%u",min_bcd);
return min_bcd;
}
////////////////////////////////////////////////////
read_hour()
{
i2c_start();
i2c_write(0xd0);
if(ack_check==0)
i2c_write(0x02);//set the address
if(ack_check==0)
i2c_start();
i2c_write(0xd1);
if(ack_check==0)
hour=i2c_read();
i2c_stop();
high_nibble=(hour&0xf0)>>4;
low_nibble=hour&0x0f;
hour_bcd=high_nibble*10+low_nibble;
printf("\r\%u",hour_bcd);
return hour_bcd;
}
////////////////////////////////////////////////////
read_day_of_week()
{
i2c_start();
i2c_write(0xd0);
if(ack_check==0)
i2c_write(0x03);//set the address
if(ack_check==0)
i2c_start();
i2c_write(0xd1);
if(ack_check==0)
day=i2c_read();
i2c_stop();
high_nibble=(day&0xf0)>>4;
low_nibble=day&0x0f;
day_bcd=high_nibble*10+low_nibble;
printf("\r\%u",day_bcd);
return day_bcd;
}
////////////////////////////////////////////////////
void main()
{
lcd_init();
i2c_start();
i2c_write(0xd0);//write operation activates
if(ack_check==0)//check ack flag
i2c_write(0x00);//set the address
if(ack_check==0)
i2c_write(0x45);//write second as 45
if(ack_check==0)
i2c_write(0x32);//write min as 32
if(ack_check==0)
i2c_write(0x18);//set hour as 18
if(ack_check==0)
i2c_write(0x01);
if(ack_check==0)
i2c_write(0x28);
if(ack_check==0)
i2c_write(0x08);
if(ack_check==0)
i2c_write(0x10);
if(ack_check==0)
i2c_stop();
read_sec();
read_min();
read_hour();
read_day_of_week();
} |
|
|
|
P51D
Joined: 25 Jan 2010 Posts: 36
|
Re: problem with DS1307 |
Posted: Tue Jun 15, 2010 10:46 am |
|
|
mojsa2000 wrote: | hi
I read the time and date the first value that is read is true and the other values are not true.
|
which other values?
Did you realized that the values are in BCD code?
For year, minutes and seconds the lower nibble is for the values 0-9 and the higher nibble for 10,20,30...-90
I've wrote the code for an ATmega32 and the DS1307. This are the read and write functions with start and stop...
maybe it could help you
Code: |
void write_ds1307(int address, int data){ // write byte to DS1307
i2c_start();
i2c_write(RTC_Adresse|Write);
i2c_write(address);
i2c_write(data);
i2c_stop();
}
int read_ds1307(int address){ // read byte of DS1307
int data;
i2c_start();
i2c_write(RTC_Adresse|Write);
i2c_write(address);
i2c_start();
i2c_write(RTC_Adresse|Read);
data = i2c_read(NACK);
i2c_stop();
return(data);
}
void start_ds1307(){ //RTC starten
int sec;
if (ds1307_started) return;
sec = (read_ds1307(0) & (~0x80)); //CH = 0 -> start.
write_ds1307(0,sec);
ds1307_started = 1;
}
void stop_ds1307(){ //RTC stop
int sec;
sec = (read_ds1307(0) | 0x80); //CH = 1 -> stop.
write_ds1307(0,sec);
ds1307_started = 0;
}
|
best wishes
P51D |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jun 15, 2010 11:05 am |
|
|
See this thread in the CCS code library for a ds1307 driver and a test
program. Use this driver instead of your code:
http://www.ccsinfo.com/forum/viewtopic.php?t=23255
Read all the posts in the thread for help with ds1307 problems. |
|
|
|
|
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
|