manarose
Newbie level 4
- Joined
- Dec 4, 2007
- Messages
- 6
- Helped
- 2
- Reputation
- 4
- Reaction score
- 1
- Trophy points
- 1,283
- Location
- Amman- Jordan
- Activity points
- 1,347
#include "REG52.H"
#include "REG52.H"
sbit SDA = P1^0;
sbit SCL = P1^1;
//declare function
void delay(void);
void SCL_high(void);
void SCL_low(void);
void I2C_Start(void);
void I2C_Stop(void);
bit I2C_Write(unsigned char dat);
unsigned char I2C_Read(bit ack);
//RTC DS1307
void rtc_write(unsigned char add , unsigned char dataa);
unsigned char rtc_read(unsigned char add);
//--------------------ham chinh----------------------------
void main(void)
{
unsigned char hour, minute, second, date, month, year;
rtc_write(0x07, 10); // set control register 00010000; 1Hz
//......................
// ghi thoi gian vao RTC
rtc_write(0x00, second);
rtc_write(0x01, minute);
rtc_write(0x02, hour);
// rtc_write(0x03, day_of_week);
rtc_write(0x04, date);
rtc_write(0x05, month);
rtc_write(0x06, year);
while(1)
{
// doc thoi gian RTC
second = rtc_read(0x00);
minute = rtc_read(0x01);
hour = rtc_read(0x02);
date = rtc_read(0x04);
month = rtc_read(0x05);
year = rtc_read(0x06);
P2=255-second;
}
}
//===============cac function================
//===============delay=======================
void delay(void)
{
unsigned char i;
for (i = 0; i < 20; i++)
{;}
}
//---------------SCL high--------------------------
void SCL_high(void)
{
SCL = 1;
delay();
}
//---------------SCL low---------------------------
void SCL_low(void)
{
SCL = 0;
delay();
}
//-----------------START Condition-----------------------
void I2C_Start(void)
{
SDA = 1;
SCL = 1;
SDA = 0;
delay();
SCL = 0;
SDA = 1;
}
//------------------STOP Condition--------------------------
void I2C_Stop(void)
{
SDA = 0;
SCL_high();
SDA = 1;
}
//-------------------I2C Write---------------------------
bit I2C_Write(unsigned char dat)
{
unsigned char i;
bit outbit;
for (i = 1; i <= 8; i++)
{
outbit=dat&0x80;
SDA = outbit;
dat = dat << 1;
SCL_high();
SCL_low();
}
SDA = 1; // set SDA to receive Acknowledge
SCL_high();
outbit = SDA; // check busy or not
SCL_low();
return(outbit); // if outbit=1 ,A=1: error // if outbit=0 ,A=0: ok
}
//------------------I2C Read----------------------------------
unsigned char I2C_Read(bit ack)
{
unsigned char i, dat;
bit inbit;
dat = 0;
for(i=1;i<=8;i++) {
SCL_high();
inbit = SDA;
dat = dat << 1;
dat = dat | inbit;
SCL_low();
}
if (ack) SDA = 0; // set SDA = 0 (ACK)
else SDA = 1; // set Non ACK
SCL_high();
SCL = 0;
SDA = 1; // Set SDA = 1 for next read
delay();
return(dat);
}
//-------------------------------------------------------------
// ghi data vao cac thang ghi co dia chi add cua RTC
//---------------------RTC wtie----------------------------------------
void rtc_write(unsigned char add , unsigned char dataa)
{
I2C_Start();
I2C_Write(0xd0); // dia chi cua DS1307 o che do ghi
I2C_Write(add); // dia chi thanh ghi
I2C_Write(((dataa/10)<<4)|(dataa%10)); // chuyen tahnh so thap phan
I2C_Stop();
}
//---------------------------------------------------------------
// doc du lieu tai thanh ghi cho dia chi add cua RTC
//---------------------------------------------------------------
unsigned char rtc_read(unsigned char add)
{
unsigned char dataa ;
I2C_Start();
I2C_Write(0xd0); // dia chi cua DS1307 o che do ghi
I2C_Write(add); // dia chi thanh ghi can doc
I2C_Start(); // restart
I2C_Write(0xd1); // dia chi DS1307 o che do doc
dataa = I2C_Read(0);
I2C_Stop();
dataa = (dataa & 0x0f) + (dataa>>4)*10; // chuyenthanh so thap phan
return (dataa);
}
thank you very much i realy pretuiate your help but aunestly i did not understand this code very well so i tried to simulate it but i get stucked in an infinite loop in the iiportcin routine.ravimarcus said:Attached is the assembly code routines for 16F877 to interface with DS1307.
Cheers
Ravi
It will not work as is. You will have to implement it in your code. I have just given you all the routines individually.manarose said:thank you very much i realy pretuiate your help but aunestly i did not understand this code very well so i tried to simulate it but i get stucked in an infinite loop in the iiportcin routine.
ravimarcus said:Use any 2 I/O line to interface to DS1307. You do not have to use the internal IIC engine to communicate with DS1307.
Ravi
manarose said:thank you for supporting me in my project work, but i did not understand how i can use any I/O lines to communicate with ds1307, if so how can i set the time for first time and how can i reseive the clock and the date data all the time to display them on a LCD? can i use interrupt in this case?
a VAR byte[7] 'an array variable
'I2CWRITE DataPin,ClockPin,Control,{Address,}[Value{,Value...}]{,Label}
a[0] = 34 'seconds
a[1] = 16 'minutes
a[2] = 14 'hours
a[3] = 4 'day of the week 1 to 7
a[4] = 13 'date
a[5] = 12 'month
a[6] = 07 'year
I2CWRITE PORTC.4,PORTC.3,$a0,07,["the control byte as in data sheet"] 'set the control register
I2CWRITE PORTC.4,PORTC.3,$a0,0,[STR a\7] 'set the time on the ds1307
I2CWRITE PORTC.4,PORTC.3,$a0,$07,["the control byte as in data sheet"] 'reset the control register
Main:
I2CREAD PORTC.4,PORTC.3,$a0,0,[STR a\7]
LCDOUT $FE, $C0,STR a\7 'Clear the display and print the time and date
PAUSE 100
GOTO Main
end
Hello
See this site
Real Time clock ith Ds1307 , PIC16F877A and CCS C
**broken link removed**
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?