///// main.c
#include<reg51.h>
#include<stdio.h>
#include<ds1307.h>
#include<serial.h>
#include<delay.h>
unsigned char RTC_ARR[7]; // Buffer for second,minute,.....,year
void SendByte1(unsigned char);
unsigned char p;
void WriteSerial(unsigned char*);
void SendByte(unsigned char s);
//---------------------------------------
// Main program
//---------------------------------------
void main(void)
{
InitSerial(); // Initialize serial port
//printf("%s",&Int2Day(1));
DelayMs(100);
//-----------------------------------
// Setup time and enable oscillator
//-----------------------------------
ReadRTC(&RTC_ARR[0]);
RTC_ARR[0] = RTC_ARR[0] & 0x7F; // enable oscillator (bit 7=0)
RTC_ARR[1] = 0x59; // minute = 59
RTC_ARR[2] = 0x23; // hour = 05 ,24-hour mode(bit 6=0)
RTC_ARR[3] = 0x04; // Day = 1 or sunday
RTC_ARR[4] = 0x31; // Date = 30
RTC_ARR[5] = 0x10; // month = August
RTC_ARR[6] = 0x12; // year = 05 or 2005
WriteRTC(&RTC_ARR[0]); // Set RTC
DelayMs(100);
while(1)
{
DelayMs(100);
ReadRTC(&RTC_ARR[0]);
DelayMs(100);
//WriteSerial("day");
//WriteSerial(Int2Day(RTC_ARR[3]));
//WriteSerial("\r\n");
WriteSerial("time");
SendByte1(RTC_ARR[2]); //DelayMs(100);
WriteSerial(":");
SendByte1(RTC_ARR[1]); //DelayMs(100);
WriteSerial(":");
SendByte1(RTC_ARR[0]); //DelayMs(100);
WriteSerial("\r\n");
WriteSerial("date");
SendByte1(RTC_ARR[4]); //DelayMs(100);
WriteSerial("/");
SendByte1(RTC_ARR[5]); // DelayMs(100);
//WriteSerial(Int2Month(RTC_ARR[5]));
WriteSerial("/");
SendByte1(RTC_ARR[6]); //DelayMs(100);
WriteSerial("\r\n");
WriteSerial("\r\n");
DelayMs(500);
}
}
void SendByte1(unsigned char value)
{
unsigned char buf = 0;
buf = value & 0xF0; /* Filter for high byte */
buf = (buf>>4)|(0x30); /* Convert to ascii code */
SendByte(buf);
buf = value & 0x0F; /* Filter for low byte */
buf = buf | 0x30; /* Convert to ascii code */
SendByte(buf);
}
/////////////ds1307.c
#include<reg51.h>
#include<delay.h>
#include<intrins.h>
#define ACK 1
#define NO_ACK 0
#define SLAVE 0xD0
#define WRITE 0x00
#define READ 0x01
#define ERR_ACK 0x01
unsigned char i;
const unsigned char * DayStr[7] = {{"Sun"},
{"Mon"},
{"Tue"},
{"Wen"},
{"The"},
{"Fri"},
{"Sat"}};
const unsigned char * MonthStr[12] ={{"Jan"},
{"Feb"},
{"Mar"},
{"Apr"},
{"May"},
{"Jun"},
{"Jul"},
{"Aug"},
{"Sep"},
{"Oct"},
{"Nov"},
{"Dec"}};
sbit SDA = P2^1; // connect to SDA pin (Data)
sbit SCL = P2^0; // connect to SCL pin (Clock)
//-------------------------------
// Convert BCD 1 byte to HEX 1 byte
//-------------------------------
unsigned char BCD2HEX(unsigned int bcd)
{
unsigned char temp;
temp=((bcd>>8)*100)|((bcd>>4)*10)|(bcd&0x0f);
return temp;
}
//-------------------------------
// start I2C
//-------------------------------
void Start(void)
{
SDA = 1;
SCL = 1;
_nop_();_nop_(); _nop_();_nop_();_nop_();_nop_();
SDA = 0;
_nop_();_nop_(); _nop_();_nop_(); _nop_();_nop_();
SCL = 0;
_nop_();_nop_(); _nop_();_nop_(); _nop_();_nop_();
}
//-------------------------------
// stop I2C
//-------------------------------
void Stop(void)
{
SDA = 0;
_nop_();_nop_();
SCL = 1;
_nop_();_nop_();
SDA = 1;
}
//-------------------------------
// Write I2C
//-------------------------------
void WriteI2C(unsigned char Data)
{
for (i=0;i<8;i++)
{
SDA = (Data & 0x80) ? 1:0;
SCL=1;_nop_();SCL=0;
Data<<=1;
}
SCL = 1;
_nop_();_nop_();
SCL = 0;
}
//-------------------------------
// Read I2C
//-------------------------------
unsigned char ReadI2C(bit ACK_Bit)
{
unsigned char Data=0;
SDA = 1;
for (i=0;i<8;i++)
{
SCL = 1;
Data<<= 1;
Data = (Data | SDA);
SCL = 0;
_nop_(); _nop_();
}
if (ACK_Bit == 1)
SDA = 0; // Send ACK
else
SDA = 1; // Send NO ACK
_nop_();_nop_();
SCL = 1;
_nop_();_nop_();
SCL = 0;
return Data;
}
//-------------------------------
// Read 1 byte form I2C
//-------------------------------
unsigned char ReadBYTE(unsigned char Addr)
{
unsigned char Data;
Start();
WriteI2C(0xD0);
WriteI2C(Addr);
Start();
WriteI2C(0xD1);
Data = ReadI2C(NO_ACK);
Stop();
return(Data);
}
//-------------------------------
// Write 1 byte to I2C
//-------------------------------
void WriteBYTE(unsigned char Addr,unsigned char Data)
{
Start();
WriteI2C(0xD0);
WriteI2C(Addr);
WriteI2C(Data);
Stop();
}
//-------------------------------
// Read RTC (all real time)
//-------------------------------
void ReadRTC(unsigned char * buff)
{
Start();
WriteI2C(0xD0);
WriteI2C(0x00);
Start();
WriteI2C(0xD1);
*(buff+0)=ReadI2C(ACK); // Second
*(buff+1)=ReadI2C(ACK); // Minute
*(buff+2)=ReadI2C(ACK); // hour
*(buff+3)=ReadI2C(ACK); // Day
*(buff+4)=ReadI2C(ACK); // date
*(buff+5)=ReadI2C(ACK); // month
*(buff+6)=ReadI2C(NO_ACK); // year
Stop();
}
//-------------------------------
// Write RTC
//-------------------------------
void WriteRTC(unsigned char *buff)
{
Start();
WriteI2C(0xD0);
WriteI2C(0x00);
WriteI2C(*(buff+0));
WriteI2C(*(buff+1));
WriteI2C(*(buff+2));
WriteI2C(*(buff+3));
WriteI2C(*(buff+4));
WriteI2C(*(buff+5));
WriteI2C(*(buff+6));
Stop();
}
char * Int2Day(unsigned char day)
{
return DayStr[day-1];
}
char * Int2Month(unsigned char month)
{
return MonthStr[BCD2HEX(month)-1];
}