Make Digital Clock use PIC16F877A Microcontroller and mikorC compiler - part 3
This is our 3rd tutorial for Digital Clock. In this tutorial I implement Calendar with leap year. Here I am use PIC16F877A Microcontroller and Mikro C compiler for code editing.
You can watch the video or read the written tutorial below.
The Digital Clock show three parameter. They are second, minute and hour. The three parameter depend on each other. The hour depend on minute and minute depend on second. The second is change per second and than it value 60 the second is reset and increase the minute. The minute change per 60 second and when minute reached 60 than minute is reset and increase the our value.
The Calendar show three parameter. They are day, month and year. In this project I am implement 2015 to 2030 years and with leap year. Leap year can be easily calculate by simple formula.
simple formula is:
year % 4 = 0 , leap year
year % 4 = value , General year
Here , "%" is modulus operator.
Digital Clock part 2
Circuit Diagram
Project Code
// Lcd module connection
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
// End Lcd module connection
char text(int a){
switch(a){
case 0:
return '0';
case 1:
return '1';
case 2:
return '2';
case 3:
return '3';
case 4:
return '4';
case 5:
return '5';
case 6:
return '6';
case 7:
return '7';
case 8:
return '8';
case 9:
return '9';
}
}
int second= 0;
int minute = 0;
int hour = 0;
int day = 0;
int month =0 ;
int year =0;
int leap=0;
unsigned short set;
unsigned short set_state=0;
int i = 0; // i variable for AM,PM
int j = 0; // j variable for hour to day change
char *time = "00:00:00";
char *date = "00-00-00";
void main() {
TRISC = 0x0F ;
PORTC = 0X00;
Lcd_Init(); // Initialize Lcd module
Lcd_Cmd(_LCD_CLEAR); // Clear Lcd display
Lcd_Cmd(_LCD_CURSOR_OFF); // Lcd cursor off
Lcd_Out(1,3,"Developed By");
Lcd_Out(2,1,"MINA TECHNOLOGY");
delay_ms(2000); // 2second delay
Lcd_Cmd(_LCD_CLEAR); // Clear Lcd display
hour = 1;
//set_state = 0;
year = 15;
month =1;
day = 1;
while(1){
set = 0;
if(PORTC.F0==1){
delay_ms(100);
if(PORTC.F0==1){
set_state++; // Increase set_state
if(set_state>=7) set_state = 0; // Clear set_state
}
}
if(set_state){
if(PORTC.F1==1){
set = 1;
}
if(PORTC.F2==1){
set = -1;
}
if(set_state && set){
switch(set_state){
case 1:
hour = hour + set;
break;
case 2:
minute = minute + set;
break;
case 3:
second = 0;
break;
case 4:
day = day + set;
break;
case 5 :
month = month + set;
break;
case 6 :
year = year + set;
break;
}
}
}
second++;
if(second>=60){
minute++;
second = 0;
}
if(minute>=60){
hour++; // Increase the hour value
minute = 0; // Clear minute
}
if(hour>=13) {
hour = 1; // Reset hour
i = ~i;
j++ ;
if(j==2){
day++; // day increase
}
if(j>=3)j=1;
}
if(month==1){ // January month
if(day>=32){
day = 1; // 31 days of January month
month++;
}
}
else if(month==2){
leap =year % 4 ; // For leap year
if(leap==0){
if(day>=30){
day = 1; // 29 days of February month
month++;
}
}
else if(leap){
if(day>=29){
day=1; // 28 days of February month
month++; // Increase month
}
}
}
else if(month==3){ // March month
if(day>=32){
day =1; // 31 days of March month
month++; // Increase month
}
}
else if(month==4){ // April month
if(day>=31) {
day =1; // 30 days of April month
month++; // Increase month
}
}
else if(month==5){ // May month
if(day>=32) {
day =1; // 31 days of May month
month++; // Increase month
}
}
else if(month==6){ // June month
if(day>=31){
day = 1; // 30 days June month
month++; // Increase month
}
}
else if(month==7){ // July month
if(day>=32) {
day =1; // 31 days July month
month++; // Increase month
}
}
else if(month==8){ // Auguast month
if(day>=32){
day =1; // 31 days of August
month++; // Increase month
}
}
else if(month==9){ // September month
if(day>=31) {
day =1; // 30 days of September
month++; // Increase month
}
}
else if(month==10){ // October month
if(day>=32) {
day =1; // 31 days of October month
month++; // Increase month
}
}
else if(month==11){ // November month
if(day>=31) {
day =1; // 30 days of November month
month++; // Increase month
}
}
else if(month==12){ // December month
if(day>=32) {
day =1; // 31 days of December month
month++; // Increase month
}
}
if(month>=13){
year++; // Increase the year value
month = 1;
}
if(year>=31){ // Inplement 2030
year = 15; // Initial year 2015
}
time[0] =text(hour / 10); // Left digit of hour
time[1] =text(hour % 10); // Right digit of hour
time[3] = text(minute / 10); // Left digit of minute
time[4] = text(minute % 10 ); // Right digit of minute
time[6] = text(second / 10); // Left digit of second
time[7] = text(second % 10); // Right digit of second
date[0] =text(year /10); // Left digit of year
date[1] = text(year % 10); // Right digit of year
date[3]= text( month / 10); // Left digit of month
date[4] = text(month % 10); // Right digit of month
date[6] = text(day /10); // Left digit of day
date[7] = text(day %10 ); // Right digiy of day
Lcd_Out(2,4,date);
Lcd_Out(1,4,time);
if(i==0) Lcd_Out(1,14,"AM"); // Lcd out AM
if(i)Lcd_Out(1,14,"PM"); // show time value
delay_us(999970); // 999.970 ms delay
}
}
Download free source code
Post Comments