Circuit Designing & Firmware Development
Pages
Home
Datasheets
Tutorials
brock
Domain Check Availabilty Widget 1
Thursday, August 2, 2012
Reading RTC DS1307 with 8051
This application describes the general hardware and software information about DS1307 Real Time Clock.
Here I am giving the hardware interfacing and c code also.
CODE
main.c
# include <8052.h> # include "LcdV2.h" # include "Ds1307.h" # define DATE_INDEX 0 # define MONTH_INDEX 2 # define YEAR_INDEX 4 # define HOUR_INDEX 6 # define MIN_INDEX 8 # define SEC_INDEX 10 unsigned char gucTime[12]; unsigned char gucSec = 0, gucMin = 0, gucHour = 0, gucDate = 0, gucMonth = 0, gucYear = 0; void ReadDateTime(void); void DisplayDateTime(void); void main(void) { unsigned int i = 0; unsigned int j = 0; unsigned char ucButton = 0; unsigned char ucAscii[6]; LcdInit(); LcdGotoXy(0,0); LcdPuts("IIC-RTC Reader"); LcdCmd(0x0c); while(1) { ReadDateTime(); DisplayDateTime(); for(i = 0; i < 40000; i++); } } void DisplayDateTime(void) { LcdGotoXy(1,0); LcdPutc(gucTime[DATE_INDEX]); LcdPutc(gucTime[DATE_INDEX + 1]); LcdPutc(gucTime[MONTH_INDEX]); LcdPutc(gucTime[MONTH_INDEX + 1]); LcdPutc(gucTime[YEAR_INDEX]); LcdPutc(gucTime[YEAR_INDEX + 1]); LcdPuts(" "); LcdPutc(gucTime[HOUR_INDEX]); LcdPutc(gucTime[HOUR_INDEX + 1]); LcdPutc(':'); LcdPutc(gucTime[MIN_INDEX]); LcdPutc(gucTime[MIN_INDEX + 1]); LcdPutc(':'); LcdPutc(gucTime[SEC_INDEX]); LcdPutc(gucTime[SEC_INDEX + 1]); } void ReadDateTime(void) { unsigned char ucSec, ucMin, ucHour, ucDate, ucMonth, ucYear; unsigned char ucTemp; //unsigned char ucAscii[6]; ucSec = Ds1307Get(SEC); ucMin = Ds1307Get(MIN); ucHour = Ds1307Get(HOUR); ucDate = Ds1307Get(DATE); ucMonth = Ds1307Get(MONTH); ucYear = Ds1307Get(YEAR); ucTemp = ucSec; ucTemp = (ucTemp >> 4); ucTemp = (ucTemp + '0'); /* Seconds Index */ gucTime[SEC_INDEX] = ucTemp; ucTemp = ucSec; ucTemp = (ucTemp & 0x0f); ucTemp = (ucTemp + '0'); gucTime[SEC_INDEX + 1] = ucTemp; /* Minuts Index*/ ucTemp = ucMin; ucTemp = (ucTemp >> 4); ucTemp = (ucTemp + '0'); gucTime[MIN_INDEX] = ucTemp; ucTemp = ucMin; ucTemp = (ucTemp & 0x0f); ucTemp = (ucTemp + '0'); gucTime[MIN_INDEX + 1] = ucTemp; /* Hour Index*/ ucTemp = ucHour; ucTemp = (ucTemp >> 4); ucTemp = (ucTemp + '0'); gucTime[HOUR_INDEX] = ucTemp; ucTemp = ucHour; ucTemp = (ucTemp & 0x0f); ucTemp = (ucTemp + '0'); gucTime[HOUR_INDEX + 1] = ucTemp; /* Date Index*/ ucTemp = ucDate; ucTemp = (ucTemp >> 4); ucTemp = (ucTemp + '0'); gucTime[DATE_INDEX] = ucTemp; ucTemp = ucDate; ucTemp = (ucTemp & 0x0f); ucTemp = (ucTemp + '0'); gucTime[DATE_INDEX + 1] = ucTemp; /* Month Index*/ ucTemp = ucMonth; ucTemp = (ucTemp >> 4); ucTemp = (ucTemp + '0'); gucTime[MONTH_INDEX] = ucTemp; ucTemp = ucMonth; ucTemp = (ucTemp & 0x0f); ucTemp = (ucTemp + '0'); gucTime[MONTH_INDEX + 1] = ucTemp; /* Year Index*/ ucTemp = ucYear; ucTemp = (ucTemp >> 4); ucTemp = (ucTemp + '0'); gucTime[YEAR_INDEX] = ucTemp; ucTemp = ucYear; ucTemp = (ucTemp & 0x0f); ucTemp = (ucTemp + '0'); gucTime[YEAR_INDEX + 1] = ucTemp; if(ucHour >= 0x10) { if(ucHour >= 0x20) ucHour = ucHour - 12; //ucHour - 6; else ucHour = ucHour - 6; } if(ucMin >= 0x10) if(ucMin >= 0x20) if(ucMin >= 0x30) if(ucMin >= 0x40) if(ucMin >= 0x50) ucMin = (ucMin - 30); else ucMin = (ucMin - 24); else ucMin = (ucMin - 18); else ucMin = (ucMin - 12); else ucMin = (ucMin - 6); gucSec = ucSec; gucMin = ucMin; gucHour = ucHour; gucDate = ucDate; gucMonth = ucMonth; gucYear = ucYear; }
ds1307.h
/************************************************************************************ Description: This module is to interface Rtc(1307) to the microcontroller over I2c. ************************************************************************************/ # ifndef __DS1307_V1__ # define __DS1307_V1__ # include "I2cRtc.h" # include "LcdV2.h" /* *************************************************************************** Macro Definitions */ # define SEC 0x00 # define MIN 0x01 # define HOUR 0x02 # define DATE 0x04 # define MONTH 0x05 # define YEAR 0x06 # define DS1307_ID 0xD0 /* *************************************************************************************** */ unsigned char Ds1307Get(unsigned char ucAddr) { unsigned char ucRet; I2cStart(); I2cWrite(DS1307_ID + 1); ucRet = I2cRead(); return ucRet; } /* *************************************************************************************** */ void Ds1307SetTime(unsigned char ucHour, unsigned char ucMin, unsigned char ucSec) { I2cStart(); I2cWrite(ucSec); I2cWrite(ucMin); I2cWrite(ucHour); I2cStop(); } /* *************************************************************************************** */ void Ds1307SetDate(unsigned char ucDate, unsigned char ucMonth, unsigned char ucYear) { I2cStart(); I2cWrite(ucDate); I2cWrite(ucMonth); I2cWrite(ucYear); I2cStop(); } /* *************************************************************************************** */ void RtcDisplay(unsigned char ucValue) { unsigned char ucTemp = 0; ucTemp = (ucValue & 0xF0); ucTemp = ((ucTemp >> 4) | (0x30)); LcdPutc(ucTemp); ucTemp = ucValue & 0x0F; ucTemp = ucTemp | 0x30; LcdPutc(ucTemp); } /* *************************************************************************************** */ # endif
i2crtc.h
/*********************************************************************************** Description: This module is to interface I2c Bus. ************************************************************************************/ # ifndef __I2C_V1__ # define __I2C_V1__ /* *************************************************************************** Macro Definitions */ #define SDA P3_7 #define SCL P3_6 #define I2C_DELAY 0x0F /* *********************************************************************************** Prototypes */ void I2cClock(void); void I2cStart(void); void I2cStop(void); bit I2cWrite(unsigned char ucData); unsigned char I2cRead(void); void I2cAck(void); void I2cNoAck(void); void I2cDelay(void); /* *********************************************************************************************** */ void I2cClock(void) { I2cDelay(); SCL = 1; I2cDelay(); SCL = 0; } /* *********************************************************************************************** */ void I2cStart(void) { SDA = 0; I2cDelay(); SCL = 0; } /* *********************************************************************************************** */ void I2cStop(void) { SCL = 1; I2cDelay(); SDA = 1; } /* *********************************************************************************************** */ bit I2cWrite(unsigned char ucData) { bit bDataBit; unsigned char i; for(i=0;i<8;i++) { bDataBit = ucData & 0x80; SDA = bDataBit; ucData = ucData << 1; } SDA = 1; I2cDelay(); return bDataBit; } /* *********************************************************************************************** */ unsigned char I2cRead(void) { bit bRdBit; unsigned char i, ucData; ucData = 0x00; for(i = 0; i < 8; i++) { SCL = 1; I2cDelay(); bRdBit = SDA; ucData = (ucData << 1); ucData = (ucData | bRdBit); } return ucData; } /* *********************************************************************************************** */ void I2cAck() { SDA = 0; I2cDelay(); I2cClock(); SDA = 1; } /* *********************************************************************************************** */ void I2cNoAck(void) { SDA = 1; I2cDelay(); I2cClock(); SCL = 1; } /* *********************************************************************************************** */ void I2cDelay(void) { unsigned char i; for( i = 0; i < I2C_DELAY; i++); } # endif
Newer Post
Older Post
Home
View mobile version