Digital Clock [Real Time] Using DS1307 and PIC Microcontroller [step by step ] (Updated)
This tutorial contains description about microcontroller based digital clock and DS1307 .We know microcontroller is a computer and specific time required to complete each task .So we cannot get real time and that's why we need a real time clock IC .We will use DS1307 IC in this tutorial .
Digital Clock [Real Time] Using DS1307 and PIC Microcontroller [step by step ]
DS1307 is a real time clock IC which basically works with low power and returns BCD(Binary Coded Decimal) number. We will describe later about BCD .
Through this IC we can get year, month, date, hour, minute and second .The months , days , second can be incremented automatically and It can operate either in 24-hour format or 12-hour format with AM/PM .DS1307 has built in power sensing circuit which can backup the IC automatically during the failure of power supply.Crystal (1000KHz) is required to operate this IC and don't need any external capacitor connection .
At this stage , we have got basic information about DS1307 .In this tutorial we need to use I2C communication with Microcontroller . Description about I2C communication are given below .
I2C Communication with Microcontroller using MikroC
The I2C(I Squared C) communication means " Inter-Integrated Circuit Communication" .A microcontroller can communicate with low power peripherals through I2C .To make a successful I2C communication we just need two ends or pins[ SCL and SDA ].Here SCL for clock selection and SDA for data . Both SCL and SDA are Open Drain drives that means the chip can drive it's low output and cannot drive the high output. It 's required to connect two pull up resistors in such way that one end of both resistors with +5v and others end with SCL , SDA respectively so that the higher output can be driven .The pull up resistors value will be according to the I2C bus frequency .We are using 2k Ohm pull up resistor in this Tutorial .
(adsbygoogle = window.adsbygoogle || []).push({});
void I2C1_Init(const unsigned long clock); this function is needed to initialize and returns nothing .We have to provide frequency as it's arguments . Example : I2C1_Init(100000);
unsigned short I2C1_Start(void) ; this function is needed to start communication . Example : I2C1_Start();
void I2C1_Repeated_Start(void); for starting again .
unsigned short I2C1_Is_Idle(void); Returns 1 if I²C bus is free, otherwise returns 0.
(adsbygoogle = window.adsbygoogle || []).push({});
unsigned short I2C1_Rd(unsigned short ack); Returns one byte from the slave .
unsigned short I2C1_Wr(unsigned short data_); Sends data byte (parameter data) via I²C bus.
void I2C1_Stop(void); To stop the signal .
The Time Keeper Register of DS1307:
The Time Keeper Register of DS1307
Here we can see the address of each function ,data bits and data range .First we have to initialize the I2C using 10kHz frequency and start the communication using start function .Now if we like to write , it's required to send out 0xD0 [D0=0x68+0]through write function so that the IC can be enabled for writing to us.Instructions given below :
I2C1_Wr(0xD0); [D0=0x68+0]
I2C1_Wr(address);
During reading the data comes from DS1307 , we need to send out 0xD1 [D1=0x68+1]through write functiono that the IC can be enabled for reading to us .Instructions given in below :
I2C1_Wr(0xD1);[D1=0x68+1]
Here in I2C1_Rd(0); To read data we will use Readdata(adrs) function and it is user defined function.
In "Readdata(0);" function we will write 0 as argument and the 0 means address of Seconds Register.This function returns short type data in BCD . I this way we can read data from each address .An important thing needed to notice that bit 6 of hour register is defined as the 24-hour or 12-hour mode selection bit.If we select 1 the bit 5 of hour used for representing AM/PM and if we select 0 the bit 5 used for representing hour .
BCD number means Binary Coded Decimal number where each binary number is in 4bit .If we consider a decimal number 21 ,the BCD conversion will be 00100001 .Here blue represents the 2 and red represents 1. In that way we get BCD .Now the question is how can we get character from BCD ?Cause if we like to show in LCD , we need character .Look at the code given below :
(adsbygoogle = window.adsbygoogle || []).push({});
char firstcharofbcd(char bcddata)
{
char tireturn;
tireturn= (bcddata >> 4) +0x30; Here we are shifting 4 bits to right hand side .
return tireturn;
}
If we consider 21 we can get 2 in character format .Because in short format data we added 0x30 , which is character '0' .So the sum of '0'+0010 = '2' .
char secondcharofbcd(char bcddata)
{
char toret;
toret=(bcddata & 0x0F) + 0x30;
return toret;
} .
0x0F means '00001111' .We know in an AND gate if any input is low or 0 and the gate output is 0.If all input is 1 and we can get output 1.So by applying AND operation between 0x0F and BCD , we can make first 0000 bit as 0 .Look at the example given below :
00100001
00001111 &
-------------
00000001= 1
I think we get all required basic information .Now lets create a project in Proteus .
Please follow the instructions given below :
Pick all the required parts and Complete the circuit as given below :
Note : In practical , you have to connect 12MHz crystal between pin9 and pin10 . Also remember that 1 & 20 will be connected with VDD and 8 & 19 will be connected with GND .
Digital Clock [Real Time] Using DS1307 and PIC Microcontroller [step by step ]
Now we need the .hex file .So lets Create a project in MikroC .Please follow the steps given below :
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Load the source code on microcontroller and just run the project now .
(adsbygoogle = window.adsbygoogle || []).push({});
sbit LCD_RS at RB7_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D7 at RB2_bit;
sbit LCD_RS_Direction at TRISB7_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
short readdata(short adrs){
short getinp;
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(adrs);
I2C1_Repeated_Start();
I2C1_Wr(0xD1);
getinp=I2C1_Rd(0);
I2C1_Stop();
return getinp;
}
void writedata(unsigned short adrs,short writedat)
{
I2C1_Start();
I2C1_Wr(0xD0);
I2C1_Wr(adrs);
I2C1_Wr(writedat);
I2C1_Stop();
}
char firstcharofbcd(char bcddata)
{
char tireturn;
tireturn= (bcddata >> 4) +0x30;
return tireturn;
}
char secondcharofbcd(char bcddata)
{
char toret;
toret=(bcddata & 0x0F) + 0x30;
return toret;
}
char time[] = " : : ";
char date[] = " / / ";
short inc,in;
short chk;
short sec;
short mint;
short hr;
short h, ht;
short day;
short mon;
short yr;
short wk;
char wkd;
void main() {
I2C1_Init(100000); //DS1307 I2C is running at 100KHz
inc=0;
CMCON = 0x07; // To turn off comparators
ADCON1 = 0x0F; // To turn off analog to digital converters
TRISA = 0x3F;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_out(1,1,"Time:");
while(1){
sec = readdata(0);
mint = readdata(1);
hr = readdata(2);
h=hr; // for 12 hour format
// here means 00100000 in binary and we can get a signal in change of AM/PM
/* I dont know why 12hour format is not working .I worked hard but it didnot work .
Finally i make it manually , you can see here .Thank You !!
*/
///////////////////////////////////////
h= Bcd2Dec(h);
if(h-12>=0){
if(h-12==0) {
ht=12 ;
chk=1; }
if(h-12==1) {
ht=1 ;
chk=1; }
if(h-12==2) {
ht=2 ;
chk=1; }
if(h-12==3) {
ht=3 ;
chk=1; }
if(h-12==4) {
ht=4 ;
chk=1; }
if(h-12==5) {
ht=5 ;
chk=1; }
if(h-12==6) {
ht=6;
chk=1; }
if(h-12==7) {
ht=7;
chk=1; }
if(h-12==8) {
ht=8;
chk=1; }
if(h-12==9) {
ht=9 ;
chk=1; }
if(h-12==10) {
ht=10;
chk=1; }
if(h-12==11) {
ht=11 ;
chk=1; }
} else{
if(h==0){
ht=12;
chk=0;
}
else{
ht=h;
chk=0;
}
}
h=Dec2Bcd(ht);
///////////////////////////////////////////////////////////////////
wk=readdata(3);
day = readdata(4);
mon = readdata(5);
yr = readdata(6);
time[0] = firstcharofbcd(h);
time[1] =secondcharofbcd(h);
time[3] = firstcharofbcd(mint);
time[4] = secondcharofbcd(mint);
time[6] = firstcharofbcd(sec);
time[7] = secondcharofbcd(sec);
date[0] = firstcharofbcd(day);
date[1] = secondcharofbcd(day);
date[3] = firstcharofbcd(mon);
date[4] = secondcharofbcd(mon);
date[6] = firstcharofbcd(yr);
date[7] = secondcharofbcd(yr);
wkd=secondcharofbcd(wk);
switch(wkd){
case '1':
date[9] ='S';
date[10] ='u';
date[11] ='n';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '2':
date[9] ='M';
date[10] ='o';
date[11] ='n';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '3':
date[9] ='T';
date[10] ='u';
date[11] ='e';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '4':
date[9] ='W';
date[10] ='e';
date[11] ='d';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '5':
date[9] ='T';
date[10] ='h';
date[11] ='u';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '6':
date[9] ='F';
date[10] ='r';
date[11] ='i';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
case '7':
date[9] ='S';
date[10] ='a';
date[11] ='t';
date[12] ='d';
date[13] ='a';
date[14] ='y';
break;
}
if(chk)
{
time[9] = 'P';
time[10] = 'M';
}
else
{
time[9] = 'A';
time[10] = 'M';
}
Lcd_out(1, 6, time);
Lcd_out(2, 1, date);
if(PORTA.F0==0){
inc++;
delay_ms(300);
mint=Bcd2Dec(mint);
mint=mint+inc;
if(mint>59){
mint=0;
}
mint=Dec2Bcd(mint);
writedata(1,mint);
}
if(PORTA.F1==0){
delay_ms(300);
h=readdata(2);
inc=Bcd2Dec(h);
inc++;
if(inc>23){
inc=0;
}
h=Dec2Bcd(inc);
writedata(2,h);
}
inc=0;
if(PORTA.F2==0){
delay_ms(300);
//////
wk= readdata(3);
in=Bcd2Dec(wk);
in++;
if(in>7){
in=1;
}
wk= Dec2Bcd(in);
writedata(3,wk);
///////
h=readdata(4);
inc=Bcd2Dec(h);
inc++;
yr=readdata(6);
yr=Bcd2Dec(yr);
if(yr%4==0){
mon=readdata(5);
mon=Bcd2Dec(mon);
if(mon==2){
if(inc>29){
inc=1;
}
} else if(mon==1 | mon==3 | mon==5 | mon==7 | mon==8 | mon==10| mon==12){
if(inc>31){
inc=1;
}
}else{
if(inc>30){
inc=1;
}
}
}else{
mon=readdata(5);
mon=Bcd2Dec(mon);
if(mon==2) {
if(inc>28){
inc=1;
}
}
else if(mon==1 | mon==3 | mon==5 | mon==7 | mon==8 | mon==10| mon==12){
if(inc>31){
inc=1;
}
}else{
if(inc>30){
inc=1;
}
}
}
day=Dec2Bcd(inc);
writedata(4,day);
}
inc=0;
if(PORTA.F3==0){
inc++;
delay_ms(300);
if(inc>12){
inc=0;
}
mon=Bcd2Dec(mon);
mon=mon+inc;
if(mon>12){
mon=1;
}
mon=Dec2Bcd(mon);
writedata(5,mon);
}
inc=0;
if(PORTA.F4==0){
inc++;
delay_ms(300);
if(inc>59){
inc=0;
}
yr=Bcd2Dec(yr);
yr=yr+inc;
if(yr>59){
yr=1;
}
yr=Dec2Bcd(yr);
writedata(6,yr);
}
if(PORTA.F5==0){
delay_ms(300);
//////
wk= readdata(3);
in=Bcd2Dec(wk);
in++;
if(in>7){
in=1;
}
wk= Dec2Bcd(in);
writedata(3,wk);
///////
}
}
}
Now , the go to the proteus circuit and double click on Microcontroller .Please the steps :
http://feeds.feedburner.com/microcontroller_for_beginners