Microcontroller › 8051 › RTC DS1307 interfacing with 8051 in C using I2C…….
- This topic has 24 replies, 21 voices, and was last updated 9 years, 6 months ago by nagar.
-
AuthorPosts
-
January 25, 2013 at 2:00 pm #9047daneshParticipant
i have used the above code its works..but the clock is running very speed not according to time……….please do help me….
Thanks in Advance
March 3, 2013 at 8:49 am #9224Aniket PatelParticipantHi,
thanks to all !
I want to make a multiple alarm regarding to calander using ds1307
can any budy halp me to solve it ?
March 20, 2013 at 12:22 pm #9356nagalaxmiParticipanthi,
write_lcd((c/16)+48)
write_lcd((c%16)+48);
can any one pls explain me how is works ? am not getting logic behind it ………..
March 22, 2013 at 5:03 am #9361Arun SukumaranParticipanthi nagalaxmi…..,
try this code…. it is easy….
unsigned char x,y;//hourc=read_i2c(0xd0,0x02);x= c & 0x0F;x=x |’0′;y= c & 0xF0;y=y>>4;y=y | ‘0’;write_lcd(y);write_lcd(x);write_lcd(‘:’);March 23, 2013 at 4:20 am #9362lekhParticipanthey can anybody tell me which version of Proteus one can download and from where to download some link or so
June 26, 2013 at 6:21 am #10035AnonymousGuestRTC interfacing with 8051 using I2C
this is my code…….it displays time on lcd but the time doesnt change (my rtc also not changing)…….
Can someone please help me…… im stuck at this…..
******************main program**********************
#include<reg51.h>#include<string.h>#include “lcd.h”#include “i2c.h”#define SEC 0x00#define MIN 0x01#define HOUR 0x02#define DATE 0x04#define MONTH 0x05#define YEAR 0x06unsigned char read(unsigned char);void send2lcd(unsigned char );void send2lcd(unsigned char value){unsigned char buf = 0;buf = value & 0xF0; /* Filter for high byte */buf = (buf>>4)|(0x30); /* Convert to ascii code */lcd_data(buf); /* Show on LCD */buf = value & 0x0F; /* Filter for low byte */buf = buf | 0x30; /* Convert to ascii code */lcd_data(buf); /* Show on LCD */}unsigned char read(unsigned char addr){unsigned char ret;start();write_get(0xD0);write_get(addr);start();write_get(0xD1);ret = read_get();stop();return ret;}void main(){unsigned char sec, min, hour,date,month,year;while(1){lcd_init();lcd_cmd(0x80);lcd_string(“TIME-“);/* Get Time */lcd_cmd(0x85);hour = read(HOUR);if(hour >= 23){send2lcd(hour);}else{send2lcd(hour);}lcd_data(‘:’);min = read(MIN);send2lcd(min);lcd_data(‘:’);sec = read(SEC);if (sec <= 59){send2lcd(sec);}else{send2lcd(0x00);}lcd_cmd(0xC0);lcd_string(“DATE-“);lcd_cmd(0xC5);date = read(DATE);send2lcd(date);lcd_data(‘/’);month = read(MONTH);send2lcd(month);lcd_data(‘/’);year = read(YEAR);send2lcd(year);}}**************i2c.h*****************sbit SCL = P2^0;sbit SDA = P2^1;void start(void);void stop(void);unsigned char read_get(void);void write_get(unsigned char);void ack();void noack();void delay(unsigned int);void delay(unsigned int s){unsigned int i,j;for(i=0;i<;i++)for(j=0;j<=10;j++);}void start(){SDA = 1;SCL = 1;delay(10);SDA = 0;SCL = 0;}void write_get(unsigned char value){unsigned char i,write;for(i = 0;i < 8;i++){write = value&0x80;SDA = write;SCL = 1;delay(10);SCL = 0;value = value<<1;}SDA = 1;SCL = 1;ack();delay(10);SCL = 0;}unsigned char read_get(){unsigned char read = 0x00;unsigned char i;SDA = 1;for(i=0;i<8;i++){SCL = 0;delay(20);SCL = 1;read = read| SDA;if(i<7)read = read <<1;}noack();return read;}void stop(){SDA = 0;SCL = 1;delay(10);SDA = 1;SCL =0;}void ack(){SDA = 0; /* Clear SDA */delay(10);SCL = 1;delay(10);SCL = 0;SDA = 1; /* Set SDA */}void noack(){SDA = 1; /* Set SDA */delay(10);SCL = 1;delay(10);SCL = 0;SCL = 1; /* Set SCL */}**********************lcd.h**************sfr lcd = 0x90;sbit rs = P3^0;sbit rw = P3^1;sbit en = P3^2;void lcd_cmd(unsigned char);void lcd_init();void lcd_data(unsigned char);void delay1(unsigned int);void lcd_string(unsigned char a[]);void lcd_init(){lcd_cmd(0x01);lcd_cmd(0x38);lcd_cmd(0x06);lcd_cmd(0x0E);}void lcd_cmd(unsigned char x){lcd = x;rs = 0;rw = 0;en = 1;delay1(10);en = 0;}void lcd_data(unsigned char z){lcd = z;rs = 1;rw = 0;en = 1;delay1(10);en = 0;}void delay1(unsigned int y){unsigned int j;for(;y>0;y–)for(j=0;j<100;j++);}void lcd_string(unsigned char a[]){unsigned int i;for(i = 0;i < strlen(a);i++){lcd_data(a);;}}June 29, 2013 at 7:55 am #10057SivabalamuruganParticipanti need what is i2c concept
July 4, 2013 at 12:53 pm #10095AJISH ALFREDParticipantHi Sivabalamurugan,
Suggest you to go through this tutorial on the following link.
engineersgarage.com/tutorials/twi-i2c-interface
July 5, 2013 at 5:51 am #10102AnonymousGuestTHANK U Frnd…………………….
May 28, 2015 at 4:38 am #12943nagarParticipant/* '_nop_ ()' ( no operation function) is used for delay purpose'bit' is a data tye contain only 1 or 0 (any value other then 0 treated as 1)#include<intrins.h> is used to avoke from nop() function*/#include<reg51.h>#include<stdio.h>#include<string.h>#include<intrins.h>sbit rs=P2^4;sbit rw=P2^5;sbit en=P2^6;sbit SCL = P2^0; // connect to SCL pin (Clock)sbit SDA = P2^1; // connect to SDA pin (Data)unsigned char i;unsigned char Data;#define ACK 0#define NO_ACK 1void DelayMs(unsigned int count){// mSec Delay 11.0592 Mhzunsigned int i;while(count){i = 115;while(i>0) i–;count–;}}void cmd(unsigned char x){P1=x;rs=0;rw=0;en=1;DelayMs(20);en=0;}void dat(unsigned char x){P1=x;rs=1;rw=0;en=1;DelayMs(20);en=0;}void lcd_init(){cmd(0x01);DelayMs(50);cmd(0x80);DelayMs(50);cmd(0x0e);DelayMs(50);cmd(0x38);}void string(unsigned char *p){while(*p){dat(*p);p++;}}void start();void stop();void WriteI2C(unsigned char);unsigned char ReadI2C(bit); // bit is a data typeunsigned int s=0,s1=0,s2=0,h=0,h1=0,h2=0,m=0,m1=0,m2=0;unsigned char z[50];void main(){lcd_init();while(1){// seconds //start();WriteI2C(0xD0); // address of RTCWriteI2C(0x00); // address for secondsstart();WriteI2C(0xD1); // address of RTC to read values=ReadI2C(ACK); // after read data send acknowledgementstop();// minutes //start();WriteI2C(0xD0); // address of RTCWriteI2C(0x01); // address for minutessstart();WriteI2C(0xD1);m=ReadI2C(ACK);stop();// hour //start();WriteI2C(0xD0); // address of RTCWriteI2C(0x02); // address for hourssstart();WriteI2C(0xD1);h=ReadI2C(ACK);stop();s1=s&0x0f;s2=(s&0xf0)>>4;m1=m&0x0f;m2=(m&0xf0)>>4;h1=h&0x0f;h2=(h&0xf0)>>4;cmd(0x80);dat(h2+48);dat(h1+48);dat(':');dat(m2+48);dat(m1+48);dat(':');dat(s2+48);dat(s1+48);cmd(0xc0);// sprintf(z,"%d%d:%d%d:%d%d",h2,h1,m2,m1,s2,s1);// string(z);}}//
// start I2C//
void Start() // SDA pin is high to low _ when clock is high{SDA = 1;SCL = 1;_nop_();_nop_(); // for delaySDA = 0;_nop_();_nop_(); // for delaySCL = 0;_nop_();_nop_(); // for delay}//
// stop I2C//
void Stop() // SDA pin is low to high _/ when clock is high{SDA = 0;_nop_();_nop_(); // for delaySCL = 1;_nop_();_nop_(); // for delaySDA = 1;}//
// Write I2C//
void WriteI2C(unsigned char Data) // data bit is change when clock is change{for (i=0;i<8;i++){SDA = (Data & 0x80) ? 1:0;SCL=1;SCL=0;Data<<=1;}SCL = 1; // on this extra clock the ACK is receive by the master_nop_();_nop_();SCL = 0;_nop_();_nop_(); // for delay*}//
// Read I2C when slave sends the complete data it latches the SDA pin high as no ack for wait of another restart bit//
unsigned char ReadI2C(bit ACK_Bit){for (i=0;i<8;i++){SCL = 1;Data<<= 1;Data = (Data | SDA);SCL = 0;_nop_();}if (ACK_Bit == 1)SDA = 0; // Send ACKelseSDA = 1; // Send NO ACKSCL = 1; // on this extra clock the ACK is transmitted by the master_nop_();_nop_();SCL = 0;_nop_();_nop_(); // for delay*return Data;} -
AuthorPosts
- You must be logged in to reply to this topic.