Header Ads

Header ADS

PIC Microcontroller Based Digital LCD Clock

Circuit Diagram


Real Time Clock (RTC) using DS1307, Sensing and LCD display by Micro-Controller (PIC16f877A). Additional 4 push buttons to set both time and calender. Time Clock (12h AM/PM) + Calender (up-to 2050) with Day.

20150926_180201


You can download the Source code from this link Download



/*
Real Time Clock (RTC) Project
CODE LISTED BY S.Rajasekar
Real time showing in LCD with Actual Date and Day.
Value can set using (Set, ++, -- and Final) Push Buttons
Compiler MikroC Por 6.6.2
RTC - DS1307
Micro-Controller PIC16f877A at 8Mhz
Physical Simulation Done
*/
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
/* Code Listed by S.RAJASEKAR */
unsigned short read_ds1307(unsigned short address) // Data Read (Write Pointer, Then
Read)
{
unsigned short r_data;
I2C1_Start(); // issue I2C start signal
I2C1_Wr(0xD0); //Address 0x68 followed by direction bit (0 for write, 1 for
read)0x68 followed by 0 --> 0xD0
I2C1_Wr(address); //Send byte (address of DS1307 location)
I2C1_Repeated_Start(); //Issues repeated START signal.
I2C1_Wr(0xD1); //0x68 followed by 1 --> 0xD1
r_data=I2C1_Rd(0); //Read data and send not acknowledge signal
I2C1_Stop(); // issue I2C stop signal
return(r_data);
}
void write_ds1307(unsigned short address,unsigned short w_data) //Data Write
{
I2C1_Start(); // issue I2C start signal //address 0x68 followed by direction bit (0
for write, 1 for read)0x68 followed by Rajasekar --> 0xD0
I2C1_Wr(0xD0); // send byte via I2C (device address + W)
I2C1_Wr(address); // send byte (address of DS1307 location)
I2C1_Wr(w_data); // send data (data to be written)
I2C1_Stop(); // issue I2C stop signal
}
unsigned char BCD2UpperCh(unsigned char bcd)
{
return ((bcd >> 4) + '0'); // BCD 1st 4bit + '0' = Upper character of BCD
}
unsigned char BCD2LowerCh(unsigned char bcd)
{
return ((bcd & 0x0F) + '0'); // BCD last 4bit + '0' = Lower character of BCD
}
int second;
int minute;
int hour;
int hr;
int day;
int dday;
int month;
int year;
int ap;
unsigned short set_count = 0;
unsigned short int ed=0; //Editing enable disable bit
short set;
char time[] = "00:00:00 PM";
char date[] = "00-00-0000";
char *Dt = "OOO";
char txt1[] = "Digital Clock By";
char txt2[] = "S.RAJASEKAR DECE";
char i; // Loop variable
void Move_Delay() { // Function used for text moving
 Delay_ms(500); // You can change the moving speed here
 }
void main()
{
 Lcd_Init(); // Initialize LCD
 Lcd_Cmd(_LCD_CLEAR); // Clear display
 Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
 Lcd_Out(1,1,txt1); // Write text in first row
 Lcd_Out(2,1,txt2); // Write text in second row
 Delay_ms(5000);
 Lcd_Cmd(_LCD_CLEAR); // Clear display
I2C1_Init(32768); //DS1307 I2C is running at 32.768KHz
CMCON = 0x07; // To turn off comparators
ADCON1 = 0x06; // To turn off analog to digital converters
TRISA = 0x0F;
PORTA = 0x00;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_out(1, 1, "Time:");
Lcd_out(2, 1, "*");
Lcd_out(2, 16, "*");
do
{
set = 0;
if(PORTA.F0 == 0)
{
Delay_ms(50);
ed=1; //Editing enable
if(PORTA.F0 == 0)
{
set_count++;
if(set_count >= 9)
{
set_count = 0;
}
}
}
if (PORTA.F3==0) //Editing Controller
{
Delay_ms(50);
ed=0; //Editing disable
set_count=0;
}
if(set_count && ed ==1)
{
if(PORTA.F1 == 0)
{
Delay_ms(50);
if(PORTA.F1 == 0)
set = 1;
}
if(PORTA.F2 == 0)
{
Delay_ms(50);
if(PORTA.F2 == 0)
set = -1;
}
if(set_count && set)
{
switch(set_count)
{
case 1:
hour = BCD2Dec(hour);
hour = hour + set;
if(hour >= 24)
hour = 0;
else if(hour < 0)
hour = 23;
hour=Dec2BCD(hour);
hour = hour & 0x3F;
write_ds1307(2, hour); // Write hour
break;
case 2:
minute = BCD2Dec(minute);
minute = minute + set;
if(minute >= 60)
minute = 0;
else if(minute < 0)
minute = 59;
minute = Dec2BCD(minute);
write_ds1307(1, minute); // Write min
break;
case 3:
if(abs(set))
write_ds1307(0,0x00); // Reset second to 0 sec. and start Oscillator
break;
case 4:
hour = BCD2Dec(hour);
if (hour<12 else="" hour="" if="">=12)
hour = hour - 12;
hour =Dec2BCD(hour);
write_ds1307(2,hour); // Instant Flip AM/PM
break;
case 5:
day = BCD2Dec(day);
day = day + set;
if(day > 31)
day = 1;
else if(day <= 0)
day = 31;
day = Dec2BCD(day);
write_ds1307(4, day); // Write date
break;
case 6:
month = BCD2Dec(month);
month = month + set;
if(month > 12)
month = 1;
else if(month <= 0)
month = 12;
month = Dec2BCD(month);
write_ds1307(5,month); // Write month
break;
case 7:
year = BCD2Dec(year);
year = year + set;
if(year < 0)
year = 49;
else if(year >= 50)
year = 0;
year = Dec2BCD(year);
write_ds1307(6, year); // Write year
break;
case 8:
dday = BCD2Dec(dday);
dday = dday + set;
if(dday < 1)
dday = 7;
else if(dday > 7)
dday = 0;
dday = Dec2BCD(dday);
write_ds1307(3, dday); // Write day
break;
}
}
}
/* Code Listed by Sujjad O_O */
second = read_ds1307(0);
minute = read_ds1307(1);
hour = read_ds1307(2);
hr = hour;
//AM / PM Detection
if (hr >= 0x12) //AM detection
{
ap=1;
if (hr > 0x12)
{
hr = BCD2Dec(hr);
hr = hr -12;
hr = Dec2BCD(hr);
}
}
else //PM detection
{
ap=0;
hr = BCD2Dec(hr);
if (hr == 0)
{
hr = hr+12;
}
hr = Dec2BCD(hr);
}
//AM / PM silector (display in LCD)
if(ap>0)
time[9] = 'P';
else
time[9] = 'A';
dday = read_ds1307(3);
day = read_ds1307(4);
month = read_ds1307(5);
year = read_ds1307(6);

//Output
time[0] = BCD2UpperCh(hr);
time[1] = BCD2LowerCh(hr);
time[3] = BCD2UpperCh(minute);
time[4] = BCD2LowerCh(minute);
time[6] = BCD2UpperCh(second);
time[7] = BCD2LowerCh(second);
date[0] = BCD2UpperCh(day);
date[1] = BCD2LowerCh(day);
date[3] = BCD2UpperCh(month);
date[4] = BCD2LowerCh(month);
date[6] = '2';
date[7] = '0';
date[8] = BCD2UpperCh(year);
date[9] = BCD2LowerCh(year);
//Day silector
if (dday == 1)
Dt = "SUN";
else if (dday == 2)
Dt = "MON";
else if (dday == 3)
Dt = "TUE";
else if (dday == 4)
Dt = "WED";
else if (dday == 5)
Dt = "THU";
else if (dday == 6)
Dt = "FRI";
else if (dday == 7)
Dt = "SAT";
//LCD Display
Lcd_out(1, 6, time);
Lcd_out(2, 2, date);
Lcd_Out(2,13, Dt);
Delay_ms(100);
}while(1);
/* Code Listed by S.Rajasekar */
}

No comments

Powered by Blogger.