Sign In

Trouble In Rtc Ds1307

Samreen Zahid samreen.zahid@gmail.com Pakistan

Hardware: Atmega16 Running On Proteus Software: C Project Using Avr Studio4

I am interfacing rtc ds1307 with atmega16 but the issue arising is that the clock is very slow. It is lacking in minutes with the original time. So, how can i make it synchronize with original time?

Images

Program

#include<avr/io.h>
#include<util/delay.h>

#define lcd_data PORTB        //LCD data port 
#define ctrl PORTD
#define en PD2        // enable signal
#define rw PD1        // read or write signal
void init_lcd(void)
{
    lcd_cmd(0x38);    // LCD initialization 
    //_delay_ms(1);
 
    lcd_cmd(0x01);    // clear LCD
   // _delay_ms(1);
 
    lcd_cmd(0x0C);    // cursor ON
   // _delay_ms(1);
 
    return;
}
  
void lcd_cmd(unsigned char cmd)
{    
    
    lcd_data=cmd;
    ctrl =(0<<rs)|(0<<rw)|(1<<en);    
    _delay_ms(1);
    ctrl =(0<<rs)|(0<<rw)|(0<<en);    
    _delay_ms(50);
    return 0;
}
 
void lcd_write(unsigned char data1)
{
    
    lcd_data= data1;
    ctrl = (1<<rs)|(0<<rw)|(1<<en);    
    _delay_ms(1);
    ctrl = (1<<rs)|(0<<rw)|(0<<en);    
    _delay_ms(50);                
    return 0 ;
}
 
void lcd_write_string(unsigned char *str)//store address value of the string in pointer *str
{
    int h=0;
    while(str[h]!='')// loop will go on till the NULL character in the string 
    {
        lcd_write(str[h]);// sending data on LCD byte by byte
        h++;
     }
    return;
}
void lcd_gotoxy(unsigned char x, unsigned char y)
{
	unsigned char firstCharAdr[]={0x80,0xC0,0x94,0xD4};
	lcd_cmd(firstCharAdr[y-1]+x-1);
	_delay_ms(50);
}

void usart_init(void)
{
	UCSRB=(1<<TXEN);
	UCSRC=(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);
	UBRRL=0x33;
}

void i2c_init(void)
{
	TWSR=0x00;
	TWBR=0x47;
	TWCR=0x04;
}

void i2c_start(void)
{
	TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT)));
}

void i2c_write(unsigned char data)
{
	TWDR=data;
	TWCR=(1<<TWINT)|(1<<TWEN);
	while(!(TWCR & (1<<TWINT)));
}

unsigned char i2c_read(unsigned char ackval)
{
	TWCR=(1<<TWINT)|(1<<TWEN)|(ackval<<TWEA);
	while(!(TWCR & (1<<TWINT)));
	return TWDR;
}

void i2c_stop()
{
	TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
	while(TWCR & (1<<TWINT)==0);
}

void rtc_init(void)
{
	i2c_init();
	i2c_start();
	i2c_write(0xD0);
	i2c_write(0x07);
	i2c_write(0x00);
	i2c_stop();
}



void rtc_getTime(unsigned char *h, unsigned char *m,unsigned char *s)
{

	
	i2c_start();
	i2c_write(0xD0);
	i2c_write(0);
	i2c_stop();

	i2c_start();
	i2c_write(0xD1);
	*s=i2c_read(1);
	*m=i2c_read(1);
	*h=i2c_read(0);
	i2c_stop();
	return  ;
	}

void rtc_setTime(unsigned char *h, unsigned char *m,unsigned char *s)
{

	
	i2c_start();
	i2c_write(0xD0);
	i2c_write(0);
	i2c_write(s);
	i2c_write(m);
	i2c_write(h);
	i2c_stop();
	return;
	}
int main(void)
{  
	DDRB=0xFF;
	DDRD=0x07;
	init_lcd();
	rtc_init();
	unsigned short i,j,k;
while(1){
        lcd_gotoxy(1,1);
        lcd_write_string("Time=");
        rtc_getTime(&i,&j,&k);
}
return 0;
}

Replies

R Avinash
2016-12-18 11:28:11
Post a video in you tube and link. And a schematic.

Post a reply to Samreen Zahid

Think you can help Samreen Zahid ? Then post your thoughts that might help Samreen Zahid. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.