Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
11 views

Digital Clock Using 8051 Microcontroller

Digital Clock using 8051 Microcontroller

Uploaded by

MG Shivanand
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Digital Clock Using 8051 Microcontroller

Digital Clock using 8051 Microcontroller

Uploaded by

MG Shivanand
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Digital Clock using 8051 Microcontroller

Description for Digital Clock Using 8051 Microcontroller


A digital clock implemented using the 8051 microcontroller is a simple and
efficient project to display real-time hours, minutes, and seconds on a 16x2
LCD display. The clock runs continuously, tracking time based on a 1-second
interval generated by the microcontroller’s internal timer. This project
demonstrates how the microcontroller can handle timing operations, interact
with peripheral devices like an LCD, and update time in real-time.
Components Used:
1. 8051 Microcontroller (AT89C51):
o This microcontroller serves as the core of the digital clock system.
It handles timekeeping through its internal Timer0 and controls the
communication with the LCD display.
2. 16x2 LCD Display:
o The 16x2 LCD display is used to show the current time in the
format of hours, minutes, and seconds (HH:MM
). It operates in 4-bit mode, reducing the number of pins needed for
communication.
3. Push Buttons (Optional):
o Push buttons can be included to manually adjust the clock’s time or
reset it. These buttons can be used for setting the hours or minutes.
4. Crystal Oscillator (11.0592 MHz):
o The crystal oscillator provides an accurate clock signal to the
microcontroller, ensuring precise timing for the clock’s operation.
5. Power Supply:
o A 5V power supply is used to power the 8051 microcontroller and
the LCD display.
Working Principle:
1. Timekeeping Using Timer0:
o The 8051 microcontroller’s internal Timer0 is configured in 16-bit
mode to create a 1-second delay. Timer0 counts the internal clock
cycles and overflows every second, triggering an interrupt.
o Each time the timer overflows, the clock increments the seconds.
Once the seconds reach 60, they reset to zero, and the minutes
increment. Similarly, when the minutes reach 60, they reset, and
the hours increment. The hours reset after reaching 24, keeping the
time format in a 24-hour cycle.
2. LCD Display Control:
o The LCD is operated in 4-bit mode to save I/O pins of the
microcontroller. Commands and data are sent to the LCD in two 4-
bit nibbles, reducing the number of data lines required from 8 to 4.
o The LCD is initialized with commands to configure it for 2-line
display, with the cursor hidden. The time is displayed on the first
line of the LCD.
3. Interrupt-Driven Time Update:
o Timer0 uses an interrupt service routine (ISR) that automatically
updates the time whenever the timer overflows. This ensures the
microcontroller can perform other tasks without blocking the
timing process.
o After each interrupt, the new time is calculated, and the display is
updated with the current hours, minutes, and seconds.
Functions and Features:
1. Real-Time Time Display:
o The clock shows real-time updates of hours, minutes, and seconds
on the LCD display.
2. Time Adjustment (Optional):
o Push buttons can be included to manually set or adjust the time in
case of power loss or to sync with a known time.
3. Power Efficiency:
o The use of 4-bit mode for the LCD reduces the number of pins
required, saving hardware resources and simplifying the design.
Applications:
• This digital clock project can be used in embedded systems applications
where timekeeping is crucial, such as in home appliances, wall clocks,
industrial equipment, and alarm systems.
Advantages:
1. Accurate Timekeeping: The use of the 8051’s timer and crystal
oscillator ensures the clock keeps accurate time.
2. Efficient Use of I/O Pins: Operating the LCD in 4-bit mode minimizes
pin usage on the microcontroller.
3. Interrupt-Driven: The timekeeping is managed using interrupts,
allowing the microcontroller to handle other tasks simultaneously without
affecting clock accuracy.
In conclusion, the digital clock using the 8051 microcontroller is a practical
project demonstrating how microcontrollers handle real-time tasks, interfacing
with external displays, and using interrupts to ensure smooth operation.

Source code :
#include <reg51.h> // Header file for 8051
#include <stdio.h>

// Define LCD pins


sbit LCD_RS = P2^0; // LCD Register Select pin
sbit LCD_RW = P2^1; // LCD Read/Write pin
sbit LCD_EN = P2^2; // LCD Enable pin
sbit LCD_D4 = P2^4; // Data pins for 4-bit mode
sbit LCD_D5 = P2^5;
sbit LCD_D6 = P2^6;
sbit LCD_D7 = P2^7;

// Time variables
unsigned char hours = 0, minutes = 0, seconds = 0;

// Delay function to create a delay in milliseconds


void delay(unsigned int time_in_ms) {
unsigned int x, y;
for (x = 0; x < time_in_ms; x++)
for (y = 0; y < 1275; y++);
}

// Send a command to the LCD


void LCD_command(unsigned char command_value) {
LCD_RS = 0; // Command mode
LCD_RW = 0; // Write mode
P2 = (command_value & 0xF0); // Send upper nibble
LCD_EN = 1; delay(1); LCD_EN = 0;
P2 = ((command_value << 4) & 0xF0); // Send lower nibble
LCD_EN = 1; delay(1); LCD_EN = 0;
}

// Send data to the LCD


void LCD_data(unsigned char data_value) {
LCD_RS = 1; // Data mode
LCD_RW = 0; // Write mode
P2 = (data_value & 0xF0); // Send upper nibble
LCD_EN = 1; delay(1); LCD_EN = 0;
P2 = ((data_value << 4) & 0xF0); // Send lower nibble
LCD_EN = 1; delay(1); LCD_EN = 0;
}

// LCD Initialization
void LCD_init() {
LCD_command(0x02); // Initialize to 4-bit mode
LCD_command(0x28); // 2 line, 5x7 matrix
LCD_command(0x0C); // Display ON, cursor OFF
LCD_command(0x06); // Auto increment cursor
LCD_command(0x01); // Clear screen
delay(5);
}

// Function to display a string on the LCD


void LCD_print(char *message) {
while (*message) {
LCD_data(*message++);
}
}

// Function to display the time in HH:MM:SS format on the LCD


void display_time() {
char time_str[16];
sprintf(time_str, "%02d:%02d:%02d", hours, minutes, seconds);
LCD_command(0x80); // Move cursor to first line
LCD_print("Time: ");
LCD_print(time_str);
}

// Timer0 Interrupt Service Routine (ISR)


void timer0_ISR() interrupt 1 {
TH0 = 0x3C; // Reload Timer0 for 1 second delay
TL0 = 0xB0;

seconds++;
if (seconds == 60) {
seconds = 0;
minutes++;
if (minutes == 60) {
minutes = 0;
hours++;
if (hours == 24) {
hours = 0;
}
}
}
display_time(); // Refresh time display on LCD
}

// Main function
void main() {
TMOD = 0x01; // Timer0 Mode 1 (16-bit mode)
TH0 = 0x3C; // Load high byte for 1 second delay
TL0 = 0xB0; // Load low byte for 1 second delay
IE = 0x82; // Enable Timer0 interrupt
TR0 = 1; // Start Timer0

LCD_init(); // Initialize the LCD


display_time(); // Display initial time

while (1); // Infinite loop


}

You might also like