Digital Watch - Clock Circuit using LCD 16x2 and PIC 16F877a

Today I'll introduce you how to make a simple digital watch with LCD 16x2 and PIC 16F877a.
It's very easy to implement this clock and you can make this as a gift clocks for kids.
I'll also provide you the source code of the clock so you can easily customize to make your own countdown clock, count up timer or something that more useful than an analog clock.

OK. Below is the circuit diagram of the digital watch.
Online Digital Clock Circuit Diagram
Online Digital Clock Circuit Diagram
SOURCE CODE:
Tags: digital clock online, 555 timer, count up timer, a timer, 555 timer projects, digital wall clock, 
Note: If you have any problem with running this code, feel free to comment below.
This project has 2 file as described below. Copy it to your workspace, compile and run.

File: LCD4bit.h

#define lcd_RS PIN_D1
#define lcd_RW PIN_D2
#define lcd_E PIN_D3
#define lcd_B1 PIN_D4
#define lcd_B2 PIN_D5
#define lcd_B3 PIN_D6
#define lcd_B4 PIN_D7
#define dong_1 0x80
#define dong_2 0xC0
#define Xoa_lcd 0x01
//====================LCD Library Functions==============
#separate void LCD_Init();
#separate void LCD_Position(int x);
#separate void LCD_Char(int x);
#separate void LCD_Cmd(int x);
#separate void LCD_Pulse();
#separate void LCD_CGRAM();
//==========================Main program of Digital Watch with LCD========================
#separate void LCD_Data (int x)
{
output_bit(lcd_B1, x & 0x01);
output_bit(lcd_B2, x & 0x02);
output_bit(lcd_B3, x & 0x04);
output_bit(lcd_B4, x & 0x08);
}
#separate void LCD_Pulse()
{
output_high(lcd_E);
delay_us ( 3); // was 10
output_low(lcd_E);
delay_us ( 150); // was 5
}
#separate void LCD_Position(int x)
{
LCD_Data(Swap(x) | 0x08);
LCD_Pulse();
LCD_Data(Swap(x));
LCD_Pulse();
}
#separate void LCD_Cmd (int x)
{
LCD_Data(Swap(x));
LCD_Pulse();
LCD_Data(Swap(x));
LCD_Pulse();
if (x == 0x01) delay_us(2000);
}
#separate void LCD_Char (int x)
{
output_high(lcd_RS);
LCD_Data(Swap(x));
LCD_Pulse();
LCD_Data(Swap(x));
LCD_Pulse();
output_low(lcd_RS);
}
#separate void LCD_Init ()
{
// output_high(lcd_open);
LCD_Data(0x00);
delay_ms(20); //cho cho VDD len >> 15ms
output_low (lcd_RS); //Default LCD RS
output_low (lcd_RW); //Default LCD WR
LCD_Data(0x03); //4 bit mode
LCD_Pulse();
LCD_Pulse();
LCD_Pulse();
LCD_Data(0x02); //Clear the LCD screen
LCD_Pulse();
LCD_Cmd(0x2c);
LCD_Cmd(0x0c); //Enable display the LCD cursor
LCD_Cmd(0x06); //Advance the cursor of LCD
LCD_Cmd(0x01); //Clear LCD display screen
}

File: digital_clock.c (main code)
#include<16f877a.h>
#device *=16 adc=10
#include <math.h>
#include <string.h>
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT,NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#include <LCD4bit.h>
#use i2c(Master,Slow,sda=PIN_B1,scl=PIN_B0)
int set=0;
int mod=0;
byte giay,phut,gio, ngay,thang, nam;
byte BCD2DEC(byte bcd)// Function converts BCD to Decimal
{
byte h;
byte l;
byte dec;
h = bcd / 16;
l = bcd % 16;
dec = (h * 10) + l;
return dec;
}
byte DEC2BCD(byte dec)//Function converts Decimal to BCD
{
byte h;
byte l;
byte bcd;
h = dec / 10;
l = dec % 10;
bcd = (h << 4) + l;
return bcd;
}
void hienthi(int8 dec)// Function displays a number on LCD
{
LCD_Char(dec/10 + 48);
LCD_Char(dec%10 + 48);
}
void ghidl(byte address, BYTE data)// Function writes data to DS1307
{
i2c_start();
i2c_write(0xd0);// Send the address
i2c_write(address);// Pointer to address
i2c_write(data);// Write data to the address
i2c_stop();
}
BYTE docdl(byte address)
{
BYTE data;
i2c_start();
i2c_write(0xd0);//Send the address
i2c_write(address);// Pointer to addr
i2c_stop();
i2c_start();
i2c_write(0xd1);// Send read data command
data=i2c_read(0);
i2c_stop();
return(data);
}
void update_Ds1307()
{
ghidl(0, 0); // update second
delay_ms(5);
ghidl(1, DEC2BCD(phut));// update minute
delay_ms(5);
ghidl(2, DEC2BCD(gio));// update hour
delay_ms(5);
ghidl(4, DEC2BCD(ngay));// update day
delay_ms(5);
ghidl(5, DEC2BCD(thang)); // update month
delay_ms(5);
ghidl(6, DEC2BCD(nam));// update year
delay_ms(5);
}
#int_RB
void chinh_gio()
{
if (input(PIN_B4)==0)// adjust time
{
if (set==0)// start adjusting time
{
set=1;
mod=1;
}
else // update DS1307
{
set=0;
mod=0;
}
}
else if (input(PIN_B5)==0)// set required time
{
mod++;
if (mod==7) mod=1;
}
else if (input(PIN_B6)==0)// increase
{
if (set==1)
switch(mod)
{
case 1:
gio++;
if (gio==24) gio=0;
break;
case 2:
phut++;
if (phut==60) phut=0;
break;
case 3:
ngay++;
if((ngay==30)&(thang==4)||(thang==6)||(thang==9)||(thang==11))
ngay=1;
else if((thang==2)&&(nam%4==0)&&(ngay==30)) ngay=1;
else if ((thang==2)&&(nam%4!=0)&&(ngay==29)) ngay=1;
else if (ngay==32) ngay=1;
break;
case 4:
thang++;
if (thang==13) thang=1;
break;
case 5:
nam++;
if (nam==100) nam=0;
break;
}
}
else if (input(PIN_B7)==0)// giam
{
switch(mod)
{
case 1:
if (gio==0) gio=24;
gio--;
break;
case 2:
if (phut==0) phut=60;
phut--;
break;
case 3:
ngay--;
if((ngay==0)&(thang==4)||(thang==6)||(thang==9)||(thang==11))
ngay=30;
else if((thang==2)&&(nam%4==0)&&(ngay==0)) ngay=29;
else if ((thang==2)&&(nam%4!=0)&&(ngay==0)) ngay=28;
else if (ngay==0) ngay=31;
break;
case 4:
thang--;
if (thang==0) thang=12;
break;
case 5:
nam--;
if (nam==0) nam=99;
break;
}
}
else return;
}
void main()
{
enable_interrupts(INT_RB);
enable_interrupts(global);
LCD_init();// khoi tao LCD
LCD_Position(0x84);
LCD_Char("LE MINH HA");
LCD_Position(0xC5);
LCD_char("TC2052");
delay_ms(200);
LCD_cmd(0x01);// xoa man hinh
LCD_Position(0x80);
LCd_char("Time");
LCD_Position(0xc0);
LCD_char("Date");
While (TRUE)
{
if (set == 0)// lay thoi gian
{
giay=BCD2DEC(docdl(0));
delay_us(100);
phut=BCD2DEC(docdl(1));
delay_us(100);
gio =BCD2DEC(docdl(2));
delay_us(100);
ngay=BCD2DEC(docdl(4));
delay_us(100);
thang=BCD2DEC(docdl(5));
delay_us(100);
nam=BCD2DEC(docdl(6));
delay_us(100);
}
if (set==1)
update_ds1307();
// hien thi
LCD_Position(0x85);
hienthi(gio);
LCD_char(":");
hienthi(phut);
LCD_char(":");
hienthi(giay);
LCD_Position(0xc5);
hienthi(ngay);
LCD_char(":");
hienthi(thang);
LCD_char(":");
hienthi(nam);
// twinkling while adjust time
switch(mod)
{
case 1: // config hour
LCD_Position(0x85);
LCD_char(" ");
delay_ms(30);
break;
case 2: // configure minute
LCD_Position(0x88);
LCD_Char(" ");
delay_ms(30);
break;
case 3: // configure year
LCD_Position(0xc5);
LCD_char(" ");
delay_ms(30);
break;
case 4: // configure month
LCD_Position(0xc8);
LCD_char(" ");
delay_ms(30);
break;
case 5: // configure day
LCD_Position(0xcb);
LCD_char(" ");
delay_ms(30);
break;
}
}
}

Comments

  1. Thank you Ali :)
    Hope to see your more feedback about electronics project :)

    ReplyDelete
  2. Emplotime is an online project management application that bundles time tracking, task management and invoicing. This means that you can keep track of the entire lifetime of a project (from creation until getting paid) without having to use and pay for several apps.

    ReplyDelete
  3. A very good and informative article. Thanks for sharing with us.

    led ticker

    ReplyDelete
  4. Hello Sir
    A very nice project can u send me a code in hex Thnkyou email yasiralikhi2@gmail.com

    ReplyDelete
  5. You are a extremely clever person!
    Digital signage

    ReplyDelete

Post a Comment

Popular posts from this blog

N-Channel MOSFET High-Side Drive: When, Why and How?

Using the high-low side driver IR2110 - explanation and plenty of example circuits

Using the SG3525 PWM Controller - Explanation and Example: Circuit Diagram / Schematic of Push-Pull Converter