Skip to main content

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

Popular posts from this blog

Digital Fan Regulator Circuit Diagram

This is the project of Digital Fan Regulator Circuit diagram. The circuit presented here can be used to control the speed of  fans using induction motor. The speed control is nonlinear, i.e. in steps. The current step number is displayed on a 7-segment display. Speed can be varied over a wide range because the circuit can alter the voltage applied to the fan motor from 130V to 230V RMS in a maximum of seven steps.  The triac used in the final stage is fired at different angles to get different voltage outputs by applying short-dura-tion current pulses at its gate. For this pur-pose a UJT relax-ation oscillator is used that outputs sawtooth waveform. This waveform is coupled to the gate of the triac through an optocoupler (MOC3011) that has a triac driver output stage.  Pedestal voltage control is used for varying the firing angle of the triac. The power supply for the relaxation oscillator is derived from the rectified mains via 10-kilo-ohm, 10W series dropping/limit-ing...

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

PWM is used in all sorts of power control and converter circuits. Some common examples include motor control, DC-DC converters, DC-AC inverters and lamp dimmers. There are numerous PWM controllers available that make the use and application of PWM quite easy. One of the most popular of such controllers is the versatile and ubiquitous SG3525 produced by multiple manufacturers – ST Microelectronics, Fairchild Semiconductors, On Semiconductors, to name a few. SG3525 is used extensively in DC-DC converters, DC-AC inverters, home UPS systems, solar inverters, power supplies, battery chargers and numerous other applications. With proper understanding, you can soon start using SG3525 yourself in such applications or any other application really that demands PWM control. Before going on to the description and application, let’s first take a look at the block diagram and the pin layout. Pins 1 (Inverting Input) and 2 (Non Inverting Input) are the inputs to the on-board error amplifier. If you a...

Automatic Intruder Alarm Circuit Diagram

Description: This is a simple single-zone burglar alarm circuit. Its features include automatic Exit and Entry delays and a timed Bell/Siren Cut-Off. It's designed to be used with the usual types of normally-closed input devices such as - magnetic reed contacts - micro switches - foil tape - and PIRs. But it can be Easily Modified to accept normally-open triggering devices - such as pressure mats. Circuit Diagram   Notes:   It's easy to use. First check that the building is secure and that the green LED is lit. Then move SW1 to the "set" position. The red LED will light. You now have about 30 seconds to leave the building. When you return and open the door - the Buzzer will sound. You then have about 30 seconds to move SW1 to the "off" position. If you fail to do so - the relay will energize and the Siren will sound. While at least one of the switches in the normally-closed loop remains open - the Siren will continue to sound. However, about 15-minutes aft...