Microcontroller › 8051 › RTC DS1307 interfacing with 8051 in C using I2C……. › /* ‘_nop_ ()’ ( no
May 28, 2015 at 4:38 am
#12943
nagar
Participant
/* '_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 1
void DelayMs(unsigned int count)
{
// mSec Delay 11.0592 Mhz
unsigned 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 type
unsigned 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 RTC
WriteI2C(0x00); // address for seconds
start();
WriteI2C(0xD1); // address of RTC to read value
s=ReadI2C(ACK); // after read data send acknowledgement
stop();
// minutes //
start();
WriteI2C(0xD0); // address of RTC
WriteI2C(0x01); // address for minutess
start();
WriteI2C(0xD1);
m=ReadI2C(ACK);
stop();
// hour //
start();
WriteI2C(0xD0); // address of RTC
WriteI2C(0x02); // address for hourss
start();
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 delay
SDA = 0;
_nop_();_nop_(); // for delay
SCL = 0;
_nop_();_nop_(); // for delay
}
//
// stop I2C
//
void Stop() // SDA pin is low to high _/ when clock is high
{
SDA = 0;
_nop_();_nop_(); // for delay
SCL = 1;
_nop_();_nop_(); // for delay
SDA = 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 ACK
else
SDA = 1; // Send NO ACK
SCL = 1; // on this extra clock the ACK is transmitted by the master
_nop_();_nop_();
SCL = 0;
_nop_();_nop_(); // for delay*
return Data;
}