RTC updating problem
Discussion in "Project Doubts" started by kathirdot Mar 30, 2013.
Sat Mar 30 2013, 03:42 pm
Dear sir,
I am interfacing DS1307 with ATMEGA16.displaying the time in 7 segment display.7segment scanning time is 1 sec. Problem is rtc is not updating and i am using 32.768Khz crystal in DS1307.So kindly tell me the solutions for this problem.
Thanks in advance....
I am interfacing DS1307 with ATMEGA16.displaying the time in 7 segment display.7segment scanning time is 1 sec. Problem is rtc is not updating and i am using 32.768Khz crystal in DS1307.So kindly tell me the solutions for this problem.
Thanks in advance....
Sun Mar 31 2013, 01:37 pm
Dear sir,
thanks for your response.i am posting the code here.Pls let me know what are the problem in this code...
#define F_CPU 16000000UL
#include<avr/io.h>
#include<util/delay.h>
#include <util/twi.h>
#include <inttypes.h>
void TWI_init();
void TWI_start();
void TWI_stop();
void TWI_ack(unsigned char ackval);
void TWI_write(unsigned char data);
unsigned char TWI_read();
unsigned char readDS1307(unsigned char address);
void Get_rtc();
void Set_RTC();
void WriteDS1307(unsigned char address,unsigned char data);
unsigned char Time[15] ={"22:27:57\r"};
unsigned char Hour,Min,Sec;
int main()
{
DDRA=0xFF;
PORTA=0x00;
DDRC=0xFC;
PORTC=0xF0;
TWI_init();
WriteDS1307(0x07,0x10);
Set_RTC();
while(1)
{
Get_rtc();
}
}
void TWI_init()
{
TWSR=0x00;
TWBR=0x12;
TWCR=0x04;
}
void TWI_start()
{
TWCR = (1<<TWINT)|(1<<TWEA)|(1<<TWSTA)|(1<<TWEN);
while(!(TWCR &(1<<TWINT)));
}
void TWI_stop()
{
TWCR = (1<<TWINT)|(1<<TWEA)|(1<<TWSTO)|(1<<TWEN);
}
void TWI_ack(unsigned char ackval)
{
TWCR = (1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while(!(TWCR & (1<<TWINT)));
}
void TWI_write(unsigned char data)
{
TWDR=data;
TWCR=((1<<TWINT)|(1<<TWEN));
while (!(TWCR & (1<<TWINT)));
}
unsigned char TWI_read()
{
TWCR=((1<<TWINT)|(1<<TWEN));
while(!(TWCR & (1<<TWINT)));
return(TWDR);
}
void Get_rtc()
{
Sec=readDS1307(0x00);
Min=readDS1307(0x01);
Hour=readDS1307(0x02);
Time[7]=Sec&0x0F;
Time[6]=Sec&0xF0;
Time[6]>>=4;
Time[4]=Min&0x0F;
Time[3]=Min&0xF0;
Time[3]>>=4;
Time[1]=Hour&0x0F;
Time[0]=Hour&0xF0;
Time[0]>>=4;
To_7segrtc(Time);
}
unsigned char readDS1307(unsigned char address)
{
unsigned char rcvbyte;
TWI_start();
TWI_write(0xD0);
TWI_write(address & 0xFF);
TWI_start();
TWI_write(0xD1);
rcvbyte=TWI_read();
TWI_stop();
return(rcvbyte);
}
void Set_RTC()
{
WriteDS1307(0x02,0x12);
_delay_ms(100);
WriteDS1307 (0x01,0x16);
_delay_ms(100);
WriteDS1307 (0x00,0x32);
_delay_ms(100);
}
void WriteDS1307(unsigned char address,unsigned char dat)
{
TWI_start();
TWI_write(0xD0);
TWI_write(address);
TWI_write(dat);
TWI_stop();
}
thanks for your response.i am posting the code here.Pls let me know what are the problem in this code...
#define F_CPU 16000000UL
#include<avr/io.h>
#include<util/delay.h>
#include <util/twi.h>
#include <inttypes.h>
void TWI_init();
void TWI_start();
void TWI_stop();
void TWI_ack(unsigned char ackval);
void TWI_write(unsigned char data);
unsigned char TWI_read();
unsigned char readDS1307(unsigned char address);
void Get_rtc();
void Set_RTC();
void WriteDS1307(unsigned char address,unsigned char data);
unsigned char Time[15] ={"22:27:57\r"};
unsigned char Hour,Min,Sec;
int main()
{
DDRA=0xFF;
PORTA=0x00;
DDRC=0xFC;
PORTC=0xF0;
TWI_init();
WriteDS1307(0x07,0x10);
Set_RTC();
while(1)
{
Get_rtc();
}
}
void TWI_init()
{
TWSR=0x00;
TWBR=0x12;
TWCR=0x04;
}
void TWI_start()
{
TWCR = (1<<TWINT)|(1<<TWEA)|(1<<TWSTA)|(1<<TWEN);
while(!(TWCR &(1<<TWINT)));
}
void TWI_stop()
{
TWCR = (1<<TWINT)|(1<<TWEA)|(1<<TWSTO)|(1<<TWEN);
}
void TWI_ack(unsigned char ackval)
{
TWCR = (1<<TWINT)|(1<<TWEA)|(1<<TWEN);
while(!(TWCR & (1<<TWINT)));
}
void TWI_write(unsigned char data)
{
TWDR=data;
TWCR=((1<<TWINT)|(1<<TWEN));
while (!(TWCR & (1<<TWINT)));
}
unsigned char TWI_read()
{
TWCR=((1<<TWINT)|(1<<TWEN));
while(!(TWCR & (1<<TWINT)));
return(TWDR);
}
void Get_rtc()
{
Sec=readDS1307(0x00);
Min=readDS1307(0x01);
Hour=readDS1307(0x02);
Time[7]=Sec&0x0F;
Time[6]=Sec&0xF0;
Time[6]>>=4;
Time[4]=Min&0x0F;
Time[3]=Min&0xF0;
Time[3]>>=4;
Time[1]=Hour&0x0F;
Time[0]=Hour&0xF0;
Time[0]>>=4;
To_7segrtc(Time);
}
unsigned char readDS1307(unsigned char address)
{
unsigned char rcvbyte;
TWI_start();
TWI_write(0xD0);
TWI_write(address & 0xFF);
TWI_start();
TWI_write(0xD1);
rcvbyte=TWI_read();
TWI_stop();
return(rcvbyte);
}
void Set_RTC()
{
WriteDS1307(0x02,0x12);
_delay_ms(100);
WriteDS1307 (0x01,0x16);
_delay_ms(100);
WriteDS1307 (0x00,0x32);
_delay_ms(100);
}
void WriteDS1307(unsigned char address,unsigned char dat)
{
TWI_start();
TWI_write(0xD0);
TWI_write(address);
TWI_write(dat);
TWI_stop();
}
Tue Apr 02 2013, 04:35 pm
after every write or read operation there must be an ack or nak depending on what you want to do. I see TWI_ack() function calls missing from your code. please fix that.
Powered by e107 Forum System