___________________________________________________________________________________________________
1. CIRCUIT DIAGRAM OF RTC IMPLEMENTATION USING I2C protocol OF ATMEGA 16 ________________________________________________________________________________________________ __________________________________________________________________________________ Content for the tab VIDEO
Click Here : Registers associated with I2C
Click Here : Depth understanding of I2C Protocol
1. PROGRAM OF I2C PROTCOL OF ATMEGA16 TO SHOW RTC DATA OF DS1307 ON LCD
/******************************************************
www.firmcodes.com
DEVELOPED BY:- FIRMWARE DEVELOPER
WHAT PROGRAM DO:-I2C PROTOCOL OF ATMEGA16 USED TO DISPLAY RTC OF DS1307 ON LCD
******************************************************/
#include<avr/io.h>
#include<util/delay.h>
#define lcd PORTA
void serial_ini();
void serial_tr(unsigned char);
void serial_tr_bcd(unsigned char);
void i2c_ini();
void i2c_start();
void i2c_wr(unsigned char);
unsigned char i2c_re(unsigned char);
void i2c_stop();
void rtc_ini();
void rtc_set_t(unsigned char,unsigned char,unsigned char);
void rtc_set_d(unsigned char,unsigned char,unsigned char);
void rtc_get_t(unsigned char *,unsigned char *,unsigned char *);
void rtc_get_d(unsigned char *,unsigned char *,unsigned char *);
void cmd(unsigned char x);
void lcd_display(unsigned char x);
void lcd_ini();
void lcd_str(unsigned char *str);
void lcd_pos(int line,int pos);
int main()
{
unsigned char i,j,k,p,q,r;
DDRA=0XFF;
DDRB=0XFF;
rtc_ini();
rtc_set_t(0x19,0x45,0x30);
rtc_set_d(0x09,0x01,0x10);
serial_ini();
lcd_ini();
lcd_str("TIME ");
lcd_pos(2,0);
lcd_str("DATE ");
while(1)
{
/* time
**************************************************************************/
lcd_pos(1,6);
rtc_get_t(&i,&j,&k);
lcd_display('0'+(i>>4));
lcd_display('0'+(i & 0x0f));
lcd_display(':');
lcd_display('0'+(j>>4));
lcd_display('0'+(j & 0x0f));
lcd_display(':');
lcd_display('0'+(k>>4));
lcd_display('0'+(k & 0x0f));
_delay_ms(500);
/* date
****************************************************************************/
lcd_pos(2,6);
rtc_get_d(&p,&q,&r);
lcd_display('0'+(r>>4));
lcd_display('0'+(r & 0x0f));
lcd_display(':');
lcd_display('0'+(q>>4));
lcd_display('0'+(q & 0x0f));
lcd_display(':');
lcd_display('0'+(p>>4));
lcd_display('0'+(p & 0x0f));
_delay_ms(500);
}
return 0;
}
void serial_ini()
{
UCSRB=(1<<TXEN);
UCSRC=(1<<UCSZ1)|(1<<UCSZ0)|(1<<URSEL);
UBRRL=0X33;
}
void serial_tr(unsigned char x)
{
while(!(UCSRA & (1<<UDRE)));
UDR= x;
while(TXC ==0);
}
void serial_tr_bcd(unsigned char x)
{
serial_tr('0'+(x>>4));
serial_tr('0'+(x & 0x0f));
}
void i2c_ini()
{
TWSR=0X00;
TWBR=0X47;
TWCR=0X04;
}
void i2c_start()
{
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while((TWCR &(1<<TWINT))==0);
}
void i2c_wr(unsigned char x)
{
TWDR=x;
TWCR=(1<<TWINT)|(1<<TWEN);
while((TWCR & (1<<TWINT))==0);
}
unsigned char i2c_re(unsigned char x)
{
TWCR=(1<<TWINT)|(1<<TWEN)|(x<<TWEA);
while((TWCR &(1<<TWINT))==0);
return TWDR;
}
void i2c_stop()
{
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
for(int i=0;i<200;i++);
}
void rtc_ini()
{
i2c_ini();
i2c_start();
i2c_wr(0xd0); // address DS1307 for write
i2c_wr(0x07); //set register pointer to 7
i2c_wr(0x00); //set value of location 7 to 0
i2c_stop(); // transmit stop condition
}
void rtc_set_t(unsigned char h,unsigned char m,unsigned char s)
{
i2c_start();
i2c_wr(0xd0); // address DS1307 for write
i2c_wr(0); //set register pointer to 0
i2c_wr(s);
i2c_wr(m);
i2c_wr(h);
i2c_stop();
}
void rtc_set_d(unsigned char y,unsigned char m,unsigned char d)
{
i2c_start();
i2c_wr(0xd0); // address DS1307 for write
i2c_wr(4); //set register pointer to 4
i2c_wr(d);
i2c_wr(m);
i2c_wr(y);
i2c_stop();
}
void rtc_get_t(unsigned char *h,unsigned char *m,unsigned char *s)
{
i2c_start();
i2c_wr(0xd0); // address DS1307 for write
i2c_wr(0); //set register pointer to 0
i2c_stop();
i2c_start();
i2c_wr(0xd1); // address DS1307 for read
*s=i2c_re(1); //read sec ,read ack
*m=i2c_re(1); //read min ,read ack
*h=i2c_re(0); //read hour ,read nack
i2c_stop();
}
void rtc_get_d(unsigned char *y,unsigned char *m,unsigned char *d)
{
i2c_start();
i2c_wr(0xd1); // address DS1307 for write
i2c_wr(0); //set register pointer to 4
i2c_stop();
i2c_start();
i2c_wr(0xd1); // address DS1307 for read
*d=i2c_re(1); //read day ,read ack
*m=i2c_re(1); //read month ,read ack
*y=i2c_re(0); //read year ,read nack
i2c_stop();
}
void cmd(unsigned char x)
{
lcd=x;
PORTB=(0<<0);
PORTB=(0<<1);
PORTB=(1<<2);
_delay_ms(10);
PORTB=(0<<2);
}
void lcd_display(unsigned char x)
{
lcd=x;
PORTB=(1<<0);
PORTB=(0<<1)|(1<<0);
PORTB=(1<<2)|(0<<1)|(1<<0);
_delay_ms(20);
PORTB=(0<<2)|(0<<1)|(1<<0);
}
void lcd_ini()
{
cmd(0x38);
cmd(0x0e);
cmd(0x01);
cmd(0x06);
cmd(0x80);
}
void lcd_str(unsigned char *str)
{
while(*str!='\0')
{
lcd_display(*str);
str++;
}
}
void lcd_pos(int line,int pos)
{
if(line==1)
cmd(0x80+pos);
else if(line==2)
cmd(0xc0+pos);
}
PROTEUS File for SIMULATION(Password Of RAR file is :-firmcodes.com)