Sign In

Trouble In Displaying Day ,time And Date Using Ds1307 On Lcd

pratishtha pratishthasharma2795@gmail.com

Hardware: Atmega16 With Inbuilt 8MHZ Software: C Project Using Atmel Studio 6.1

i am an engineering student of EC and trying to learn embedded system, i am using DS1307 with atmega16 and i am beginner in this field of embedded system.

i am using DS1307 with atmega 16 controller and displaying day, time, and date on lcd in 8 bit mode, it is working on proteus only when i attach i2c debugging tool to it otherwise not but when i implemented it on hardware it is giving some default value like.. sunday for day ,00:00:00 for time and 01/01/2000 for date and not incrementing values and i think there is something missing in my code and need your help in this.

Images

Program

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


 char seconds,minutes,hours=0X00,day,date,month,year,hr,min,sec,t1='2',t2='0';
char *months[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
char *days[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
char *meridian[]={"AM","PM","24 Hours"};
 char *set_datail[]={"Set day","Set Hour","Set Minute","Set Second","Set Meridian","Set date","Set Month","Set Year"};
short int v1=0,v2=0,v3=0,v4=0,v5=0,v6=0,i=0,address=1,data=0;
int x=05;
int main(void)
{
	lcd_init();
	rtc_init();
	set_cursor(0,0);
	lcd_writestring("AUTOMATIC DRIP");
	lcd_command(0XC0);
	lcd_writestring("IRRIGATION");
	_delay_ms(500);
	lcd_command(0X01);
		
	while(1)
	{
		
		
		rtc_getTime(&hours,&minutes,&seconds);
		rtc_get_day(&day);
		show_day_time();
		rtc_getDate(&date,&month,&year);
		show_date();
		
		
		
	}
}

void i2c_init()
{
	
	TWSR=0X00;
	TWBR=0X47;
	TWCR=0X04;
}
void i2c_start()
{
	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 ackvalue)
{
	TWCR=(1<<TWINT)|(1<<TWEN)|(ackvalue<<TWEA);
	while(!(TWCR&(1<<TWINT)));
	return TWDR;
}
void i2c_stop()
{
	TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
	_delay_ms(100);
}
void rtc_init(void)
{
	i2c_init();
	i2c_start();
	i2c_write(0XD0);
	i2c_write(0X07);
	i2c_write(0X00);
	i2c_stop();
}
void rtc_Set_Time(unsigned char hours,unsigned char minutes,unsigned char seconds)
{
	i2c_start();
	i2c_write(0XD0);
	i2c_write(0X00);
	i2c_write(seconds);
	i2c_write(minutes);
	i2c_write(hours);
	i2c_stop();
	
}
void rtc_Set_date(unsigned char date,unsigned char month,unsigned char year)
{
	i2c_start();
	i2c_write(0XD0);
	i2c_write(0X04);
	i2c_write(date);
	i2c_write(month);
	i2c_write(year);
	i2c_stop();
	
}

void rtc_getTime(unsigned char *hours,unsigned char *minutes,unsigned char *seconds)

{
	i2c_start();
	i2c_write(0XD0);
	i2c_write(0X00);
	i2c_stop();
	
	i2c_start();
	i2c_write(0XD1);
	*seconds=i2c_read(1);
	*minutes=i2c_read(1);
	*hours=i2c_read(0);
	//return *seconds;
	//return *minutes;
	//return *hours;
	i2c_stop();
	
}
void rtc_getDate(unsigned char *date,unsigned char *month,unsigned char *year)
{
	i2c_start();
	i2c_write(0XD0);
	i2c_write(0X04);
	i2c_stop();
	
	i2c_start();
	i2c_write(0XD1);
	*date=i2c_read(1);
	
	*month=i2c_read(1);
	*year=i2c_read(0);
	//return *date;
	//return *month;
	//return *year;
	i2c_stop();
	
}
void rtc_get_day(unsigned char *day)
{
	i2c_start();
	i2c_write(0XD0);
	i2c_write(0X03);
	i2c_stop();
	
	i2c_start();
	i2c_write(0XD1);
	*day=i2c_read(0);
	//return *day;
	i2c_stop();
	
}

void rtc_Set_day(unsigned char day)
{
	i2c_start();
	i2c_write(0XD0);
	i2c_write(0X03);
	i2c_write(day);
	i2c_stop();
	
}
void lcd_data(unsigned char ch)
{
	PORTD|=0X01;
	PORTB = ch;
	PORTD|=0X02;
	_delay_ms(4);
	PORTD&=~0X02;
}
void lcd_command(unsigned char ch)
{
	
	PORTD&=~0X01;
	PORTB= ch;
	PORTD|=0X02;
	_delay_ms(4);
	PORTD&=~0X02;
}
void lcd_init()
{
	
	DDRB=0XFF;
	DDRD=0XFF;
	lcd_command(0X38);
	lcd_command(0X0E);
	lcd_command(0X0C);
	
}

void display_2d(unsigned char **address)
{
	
	char *p;
	p=**address;
	while(*p)
	{
		
		lcd_data(p);
		p++;
	}
	
}
void lcd_writestring(const char *msg)
{
	
	
	while (*msg!=0)
	{
		
		lcd_data(*msg);
		msg++;
	}
}


void set_cursor(unsigned char line ,unsigned char position)
{
	
	char i;
	if (line==0)
	lcd_command(0X80);
	else
	lcd_command(0XC0);
	for(i=0;i<position;i++)
	{
		lcd_command(0X14);
		
	}
}
void show_day_time()
{
	
	set_cursor(0,0);

	lcd_writestring(days[(day-1)]);
	set_cursor(0,5);
	if((hours&0X40)==0X00)
	display_digit(((hours&0X30)>>4)*10+(hours&0X0F));
	else
	display_digit(((hours&0X10)>>4)*10+(hours&0X0F));
	lcd_data(':');
	display_digit(((minutes&0XF0)>>4)*10+(minutes&0X0F));
	lcd_data(':');
	display_digit(((seconds&0XF0)>>4)*10+(seconds&0X0F));
	//lcd_data(':');
	set_cursor(0,14);
	if((hours&0X60)==0X40)
	{
		display_2d(meridian);
	}
	
}
void display_digit(unsigned int n)
{
	int x;
	if(n==0)
	{
		
		lcd_data('0');
		lcd_data('0');
	}
	else if(n<10&&n>0)
	{
		lcd_data('0');
		lcd_data('0'+n);
		
	}
	else
	{
		
		x=n/10;
		lcd_data('0'+x);
		n%=10;
		lcd_data('0'+n);
		
	}
}
void show_date()
{
	
	set_cursor(1,0);
	
	display_digit((((date&0X30)>>4)*10+(date&0X0F)));
	lcd_data('/');
	//display_digit((t1-48)*10+(t2-48));
	display_digit(((((month&0x10)>>4)*10+(month&0X0F))));
	lcd_data('/');
	display_digit((t1-48)*10+(t2-48));
	display_digit(((year&0X30)>>4)*10+(year&0X0F));
	
}

Replies

R Avinash Gupta
2017-05-02 01:57:38
Are you clearing the CLOCK HALT (CH) bit of DS1307? Refer datasheet ...
R pratishtha
2017-05-02 03:15:26
i think i have not set it........do i need to set it at the time of rtc initialization?
R Avinash Gupta
2017-05-03 04:51:35
Haven't you read the datasheet of this chip?
R pratishtha
2017-05-07 09:00:48
yes sir i have read the datasheet and also cleared the CH Bit but is still no updating.
R Avinash Gupta
2017-05-08 06:36:59
Point out the line number where you are clearing the CH bit ...

also post your schematic and photo of hardware to check for any faults ...
R pratishtha sharma
2017-05-08 14:26:37
after line no. 78 in the samefunctionof rtc_init() after i2c start i going to location 00 and writing 00 at the location 00and then i2c stop.

Post a reply to pratishtha

Think you can help pratishtha ? Then post your thoughts that might help pratishtha. 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.