#include <p18f4520.h>
#include <i2c.h>
#include <Delays.h>
char minutes,seconds,day,hrs,date,month,year;
void main()
{
OpenI2C(MASTER,SLEW_OFF);
StartI2C();
WriteI2C(0xD0);
WriteI2C(0x00);
WriteI2C(0x80);
WriteI2C(0x10); //minutes
WriteI2C(0x10); // hours
WriteI2C(0x2); //day
WriteI2C(0x22); //date
WriteI2C(0x4); //month
WriteI2C(0x13); //year
StopI2C();
//Start Rtc Oscillator
StartI2C();
WriteI2C(0xD0); //address of rtc device
WriteI2C(0x00); //0th memory location
WriteI2C(0x00); // EOSC-bar: start osciallator of rtc
Delay1KTCYx(25);
while(1)
{
// Read RTC Data
StartI2C();
WriteI2C(0xD0);//address of rtc device
WriteI2C(0x00);//0th memory location
RestartI2C();
WriteI2C(0xD1); //address of rtc device
seconds = ReadI2C();
AckI2C();
minutes = ReadI2C();
AckI2C();
hrs =ReadI2C();
AckI2C();
day =ReadI2C();
AckI2C();
date=ReadI2C();
AckI2C();
year=ReadI2C();
NotAckI2C();
StopI2C();
//while ( !DataRdyI2C() );
seconds = ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F); // Transform seconds
minutes = ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F); // Transform months
hrs = ((hrs & 0xF0) >> 4)*10 + (hrs & 0x0F); // Transform seconds
day = ((day & 0xF0) >> 4)*10 + (day & 0x0F); // Transform months
date = ((seconds & 0xF0) >> 4)*10 + (date & 0x0F);
month = ((seconds & 0xF0) >> 4)*10 + (month & 0x0F);
year = ((seconds & 0xF0) >> 4)*10 + (year & 0x0F);
Delay1KTCYx(25);
while(1);
}
}
Code C - [expand] 1 while(1);
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 WriteI2C(0x25); // write 25 to date word (REG4) WriteI2C(0x09); // write 9 (September) to month word (REG5) WriteI2C(0x08); // write 08 to year word (REG6) StopI2C(); // issue stop signal Delay10KTCYx(25); StartI2C(); // issue start signal WriteI2C(0xD0); // address DS1307 WriteI2C(0); // start from word at address 0 WriteI2C(0); // write 0 to REG0 (enable counting + 0 sec)
Code C - [expand] 1 while ( !DataRdyI2C() );
Code C - [expand] 1 2 3 4 5 6 7 seconds = ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F); // Transform seconds minutes = ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F); // Transform months hrs = ((hrs & 0xF0) >> 4)*10 + (hrs & 0x0F); // Transform seconds day = ((day & 0xF0) >> 4)*10 + (day & 0x0F); // Transform months date = ((seconds & 0xF0) >> 4)*10 + (date & 0x0F); month = ((seconds & 0xF0) >> 4)*10 + (month & 0x0F); year = ((seconds & 0xF0) >> 4)*10 + (year & 0x0F);
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?