RTC DS1307 INTERFACE WITH 8051 C PROGRAM
RTC DS1307 INTERFACE WITH 8051 C PROGRAM:
#include <reg51.h>
#include “ds1307.h”
#include “lcd.h”
void send2lcd(unsigned char value)
{
unsigned char buf = 0;
buf = value & 0xF0; /* Filter for high byte */
buf = (buf>>4)|(0×30); /* Convert to ascii code */
LCD_putc(buf); /* Show on LCD */
buf = value & 0×0F; /* Filter for low byte */
buf = buf | 0×30; /* Convert to ascii code */
LCD_putc(buf); /* Show on LCD */
}
/***************************** Main function *************************************/
void main(void)
{
unsigned char sec, min, hour, date, month, year;
LCD_init();
LCD_command(0×80);
LCD_puts(”Date:”);
LCD_command(0xc0);
LCD_puts(”Time:”);
DS1307_settime(0×10,0×51,0×00); /* Set Time (hh:mm:ss) */
DS1307_setdate(0×18,0×08,0×09); /* Set Date (dd/mm/yy) */
while(1)
{
/* Get Date & Time */
sec = DS1307_get(SEC);
min = DS1307_get(MIN);
hour = DS1307_get(HOUR);
date = DS1307_get(DATE);
month = DS1307_get(MONTH);
year = DS1307_get(YEAR);
/* Show Date in format dd/mm/yr */
LCD_command(0×86); /* Set LCD cursor at (1,6) */
send2lcd(date); /* Show date on LCD */
LCD_putc(’/');
send2lcd(month); /* Show month on LCD */
LCD_putc(’/');
send2lcd(year); /* Show year on LCD */
/* Show Time in format hr:min:sec */
LCD_command(0xC6); /* Set LCD cursor at (2,6) */
send2lcd(hour); /* Show hour on LCD */
LCD_putc(’:');
send2lcd(min); /* Show min on LCD */
LCD_putc(’:');
send2lcd(sec); /* Show sec on LCD */
//LCD_delay(255);
//LCD_clear();/* Set to origin LCD */
// LCD_delay(255);
}
} /* End main */
*///////////////////////////////////////////
Header files
*///////////////////////////////////////////
I2c header files:
sbit SDA=P2^7; /* Set P2.7 = SDA */
sbit SCL=P2^6;
#define I2C_DELAY 0×0F /* For delay i2c bus */
void I2C_delay(void)
{
unsigned char i;
for(i=0; i<I2C_DELAY; i++);
}
void I2C_clock(void)
{
I2C_delay();
SCL = 1; /* Start clock */
I2C_delay();
SCL = 0; /* Clear SCL */
}
void I2C_start(void)
{
if(SCL)
SCL = 0; /* Clear SCL */
SDA = 1; /* Set SDA */
SCL = 1; /* Set SCL */
I2C_delay();
SDA = 0; /* Clear SDA */
I2C_delay();
SCL = 0; /* Clear SCL */
}
void I2C_stop(void)
{
if(SCL)
SCL = 0; /* Clear SCL */
SDA = 0; /* Clear SDA */
I2C_delay();
SCL = 1; /* Set SCL */
I2C_delay();
SDA = 1; /* Set SDA */
}
bit I2C_write(unsigned char dat)
{
bit data_bit;
unsigned char i;
for(i=0;i<8;i++) /* For loop 8 time(send data 1 byte) */
{
data_bit = dat & 0×80; /* Filter MSB bit keep to data_bit */
SDA = data_bit; /* Send data_bit to SDA */
I2C_clock(); /* Call for send data to i2c bus */
dat = dat<<1;
}
SDA = 1; /* Set SDA */
I2C_delay();
SCL = 1; /* Set SCL */
I2C_delay();
data_bit = SDA; /* Check acknowledge */
SCL = 0; /* Clear SCL */
I2C_delay();
return data_bit; /* If send_bit = 0 i2c is valid */
}
unsigned char I2C_read(void)
{
bit rd_bit;
unsigned char i, dat;
dat = 0×00;
for(i=0;i<8;i++) /* For loop read data 1 byte */
{
I2C_delay();
SCL = 1; /* Set SCL */
I2C_delay();
rd_bit = SDA; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */
SCL = 0; /* Clear SCL */
}
return dat;
}
void I2C_ack()
{
SDA = 0; /* Clear SDA */
I2C_delay();
I2C_clock(); /* Call for send data to i2c bus */
SDA = 1; /* Set SDA */
}
void I2C_noack()
{
SDA = 1; /* Set SDA */
I2C_delay();
I2C_clock(); /* Call for send data to i2c bus */
SCL = 1; /* Set SCL */
}
*///////////////////////////////
*/////////////////////////
/*
* Filename : ds1307.h
* Hardware : Controller -> P89V51RD2
* XTAL -> 18.432 MHz
* Mode -> 6 Clock/MC
* I/O : SDA -> P2.7
* SCL -> P2.6
*/
#include “i2c.h” /* Need i2c bus */
#define DS1307_ID 0xD0
#define SEC 0×00
#define MIN 0×01
#define HOUR 0×02
#define DATE 0×04
#define MONTH 0×05
#define YEAR 0×06
unsigned char DS1307_get(unsigned char addr)
{
unsigned char ret;
I2C_start(); /* Start i2c bus */
I2C_write(DS1307_ID); /* Connect to DS1307 */
I2C_write(addr); /* Request RAM address on DS1307 */
I2C_start(); /* Start i2c bus */
I2C_write(DS1307_ID+1); /* Connect to DS1307 for Read */
ret = I2C_read(); /* Receive data */
I2C_stop(); /* Stop i2c bus */
return ret;
}
void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
I2C_start();
I2C_write(DS1307_ID); /* connect to DS1307 */
I2C_write(0×00); /* Request RAM address at 00H */
I2C_write(ss); /* Write sec on RAM address 00H */
I2C_write(mm); /* Write min on RAM address 01H */
I2C_write(hh); /* Write hour on RAM address 02H */
I2C_stop(); /* Stop i2c bus */
}
void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy)
{
I2C_start();
I2C_write(DS1307_ID); /* connect to DS1307 */
I2C_write(0×04); /* Request RAM address at 04H */
I2C_write(dd); /* Write date on RAM address 04H */
I2C_write(mm); /* Write month on RAM address 05H */
I2C_write(yy); /* Write year on RAM address 06H */
I2C_stop(); /* Stop i2c bus */
}
Related posts: