Academia.eduAcademia.edu

Major Project

Digital Clock using a Microcontroller EMBEDDED SYSTEMS: MAJOR PROJECT INTRAINZ EDUTECH Author: Pakki Vijaya Rama Mourya Shashank ee19037mourya@gmail.com Duration: December to January June 25, 2024 Contents Introduction 2 Circuit Design 2 Software Design 2 Source Code 5 1 Introduction The 8051 microcontroller is a popular microprocessor that has found widespread use in the embedded systems industry since its introduction by Intel in the 1980s. Known for its versatility, robustness, and cost-effectiveness, the 8051 has become a staple in many electronic devices. Its architecture provides an excellent foundation for various applications, including industrial automation, consumer electronics, automotive systems, and more. In this project, we will explore the implementation of a digital clock using the 8051 microcontroller. The digital clock will display the current time (hours, minutes, and seconds) on a 16x2 LCD. We will use Timer0 of the 8051 to generate interrupts at precise intervals to keep track of the time. The time will be displayed and updated continuously on the LCD. The 8051 microcontroller features multiple I/O ports, timers/counters, an interrupt system, and internal memory that make it well-suited for this application. By leveraging these features, we can create a reliable and accurate digital clock. Circuit Design The circuit design for the digital clock involves connecting the 8051 microcontroller to a 16x2 LCD and configuring the necessary connections for power supply and input/output operations. Here is a step-by-step guide to setting up the circuit: Power Supply Provide a stable 5V power supply to the 8051 microcontroller and the LCD. Ensure that the ground (GND) connections are common for all components. LCD Connections • Connect the RS (Register Select) pin of the LCD to P2^0 of the 8051. • Connect the EN (Enable) pin of the LCD to P2^1. • Connect the data pins D4, D5, D6, and D7 of the LCD to P2^2, P2^3, P2^4, and P2^5 of the 8051, respectively. • Connect the RW (Read/Write) pin of the LCD to GND to set it in write mode. • Connect the VCC and GND pins of the LCD to the power supply. • Connect a 10k ohm potentiometer to the VEE pin of the LCD to adjust the contrast. Microcontroller Configuration • Configure the I/O ports P2^0 to P2^5 as outputs for controlling the LCD. • Use Timer0 for generating interrupts at a 1-second interval. • Initialize the microcontroller and LCD at the start of the program. Additional Components • Use a 12MHz crystal oscillator and two 33pF capacitors for the clock circuit of the 8051. • Connect a reset button to the RST pin of the 8051 with a 10k ohm pull-up resistor. The circuit design ensures proper communication between the 8051 microcontroller and the LCD, allowing for accurate timekeeping and display. 2 Software Design The software design for the digital clock involves writing a program in C to control the 8051 microcontroller. The program will initialize the LCD, configure Timer0, handle interrupts, and update the time on the LCD. Here is a detailed explanation of the software design: Header File Inclusion: Include the 8051 microcontroller library 1 # include < reg51 .h > Pin Definitions: Define the pins connected to the LCD for easier reference in the code. 1 2 3 4 5 6 sbit sbit sbit sbit sbit sbit RS EN D4 D5 D6 D7 = = = = = = P2 ^0; P2 ^1; P2 ^2; P2 ^3; P2 ^4; P2 ^5; // // // // // // Register Select pin Enable pin Data pin 4 Data pin 5 Data pin 6 Data pin 7 Global Variables: Declare variables to keep track of the time (hours, minutes, seconds). 1 2 3 int seconds = 0; int minutes = 0; int hours = 0; Function Prototypes: Declare function prototypes for initializing the LCD, sending commands and data to the LCD, and updating the time display. 1 2 3 4 5 6 7 void void void void void void void delay ( int ms ) ; lcd_cmd ( unsigned char cmd ) ; lcd_data ( unsigned char data ) ; lcd_init () ; lcd_string ( char * str ) ; display_time ( int h , int m , int s ) ; timer0_ISR () interrupt 1; Timer0 ISR: Implement the Timer0 Interrupt Service Routine to update the time and display it on the LCD. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 void timer0_ISR () interrupt 1 { TH0 = 0 x4C ; // Load higher 8 - bit of timer0 TL0 = 0 x00 ; // Load lower 8 - bit of timer0 seconds ++; // Increment seconds if ( seconds == 60) { seconds = 0; minutes ++; if ( minutes == 60) { minutes = 0; hours ++; if ( hours == 24) { hours = 0; } } } display_time ( hours , minutes , seconds ) ; // Update the time display } Main Function: Initialize Timer0, the LCD, and enter an infinite loop. 1 2 3 4 5 6 7 void main () { TMOD = 0 x01 ; TH0 = 0 x4C ; TL0 = 0 x00 ; IE = 0 x82 ; TR0 = 1; // // // // // Timer0 mode1 (16 - bit timer mode ) Load higher 8 - bit of timer0 Load lower 8 - bit of timer0 Enable timer0 interrupt Start timer0 8 lcd_init () ; 9 // Initialize LCD 10 while (1) { // Main loop } 11 12 13 14 15 } Delay Function: Generate a delay in milliseconds using nested loops. void delay ( int ms ) { int i , j ; for ( i = 0; i < ms ; i ++) { for ( j = 0; j < 1275; j ++) ; } 1 2 3 4 5 6 7 8 } 3 LCD Command Function: Send commands to the LCD to configure it. void lcd_cmd ( unsigned char cmd ) { RS = 0; // Command mode D4 = ( cmd >> 4) & 0 x01 ; D5 = ( cmd >> 5) & 0 x01 ; D6 = ( cmd >> 6) & 0 x01 ; D7 = ( cmd >> 7) & 0 x01 ; EN = 1; delay (1) ; EN = 0; delay (1) ; D4 = cmd & 0 x01 ; D5 = ( cmd >> 1) & 0 x01 ; D6 = ( cmd >> 2) & 0 x01 ; D7 = ( cmd >> 3) & 0 x01 ; EN = 1; delay (1) ; EN = 0; delay (1) ; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 } LCD Data Function: Send data to the LCD to be displayed. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 void lcd_data ( unsigned RS = 1; // D4 = ( data >> 4) & D5 = ( data >> 5) & D6 = ( data >> 6) & D7 = ( data >> 7) & EN = 1; delay (1) ; EN = 0; delay (1) ; D4 = data & 0 x01 ; D5 = ( data >> 1) & D6 = ( data >> 2) & D7 = ( data >> 3) & EN = 1; delay (1) ; EN = 0; delay (1) ; } char data ) { Data mode 0 x01 ; 0 x01 ; 0 x01 ; 0 x01 ; 0 x01 ; 0 x01 ; 0 x01 ; LCD Initialization Function: Initialize the LCD by sending the required commands. 1 2 3 4 5 6 7 void lcd_init () lcd_cmd (0 x02 ) ; lcd_cmd (0 x28 ) ; lcd_cmd (0 x0C ) ; lcd_cmd (0 x06 ) ; lcd_cmd (0 x01 ) ; delay (2) ;} { // // // // // Initialize LCD in 4 - bit mode Configure the LCD in 2 lines and 5 x7 matrix Display on , cursor off Increment cursor ( shift cursor to right ) Clear display screen LCD String Function: Display a string on the LCD. 1 2 3 4 5 void lcd_string ( char * str ) { while (* str ) { lcd_data (* str ++) ; } } Display Time Function: Format the time and display it on the LCD.Displays the current time (hours, minutes, seconds) on the LCD 1 2 3 4 5 6 7 void display_time ( int h , int m , int s ) { char time_str [9]; sprintf ( time_str , " %02 d :%02 d :%02 d " , h , m , s ) ; lcd_cmd (0 x80 ) ; // Move to the beginning of the first line lcd_string ( time_str ) ; } 8 4 Source Code 1 2 3 4 5 6 7 # include < reg51 .h > sbit RS = P2 ^0; sbit EN = P2 ^1; sbit D4 = P2 ^2; sbit D5 = P2 ^3; sbit D6 = P2 ^4; sbit D7 = P2 ^5; 8 9 10 11 12 13 14 15 void void void void void void void delay ( int ms ) ; lcd_cmd ( unsigned char cmd ) ; lcd_data ( unsigned char data ) ; lcd_init () ; lcd_string ( char * str ) ; display_time ( int h , int m , int s ) ; timer0_ISR () interrupt 1; 16 17 18 19 int seconds = 0; int minutes = 0; int hours = 0; 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 void timer0_ISR () interrupt 1 { TH0 = 0 x4C ; TL0 = 0 x00 ; seconds ++; if ( seconds == 60) { seconds = 0; minutes ++; if ( minutes == 60) { minutes = 0; hours ++; if ( hours == 24) { hours = 0; } } } display_time ( hours , minutes , seconds ) ; } / 39 40 41 42 43 44 45 46 47 48 49 void main () { TMOD = 0 x01 ; TH0 = 0 x4C ; TL0 = 0 x00 ; IE = 0 x82 ; TR0 = 1; lcd_init () ; while (1) { 50 } 51 52 } 53 54 55 56 57 58 59 60 61 void delay ( int ms ) { int i , j ; for ( i = 0; i < ms ; i ++) { for ( j = 0; j < 1275; j ++) ; } } 62 63 64 65 66 67 68 69 70 71 72 73 74 void lcd_cmd ( unsigned { RS = 0; D4 = ( cmd >> 4) & D5 = ( cmd >> 5) & D6 = ( cmd >> 6) & D7 = ( cmd >> 7) & EN = 1; delay (1) ; EN = 0; delay (1) ; D4 = cmd & 0 x01 ; char cmd ) 0 x01 ; 0 x01 ; 0 x01 ; 0 x01 ; 5 D5 = ( cmd >> 1) & 0 x01 ; D6 = ( cmd >> 2) & 0 x01 ; D7 = ( cmd >> 3) & 0 x01 ; EN = 1; delay (1) ; EN = 0; delay (1) ; 75 76 77 78 79 80 81 82 } 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 void lcd_data ( unsigned { RS = 1; D4 = ( data >> 4) & D5 = ( data >> 5) & D6 = ( data >> 6) & D7 = ( data >> 7) & EN = 1; delay (1) ; EN = 0; delay (1) ; D4 = data & 0 x01 ; D5 = ( data >> 1) & D6 = ( data >> 2) & D7 = ( data >> 3) & EN = 1; delay (1) ; EN = 0; delay (1) ; } char data ) 0 x01 ; 0 x01 ; 0 x01 ; 0 x01 ; 0 x01 ; 0 x01 ; 0 x01 ; 104 105 106 107 108 109 110 111 112 113 void lcd_init () { lcd_cmd (0 x02 ) ; lcd_cmd (0 x28 ) ; lcd_cmd (0 x0C ) ; lcd_cmd (0 x06 ) ; lcd_cmd (0 x01 ) ; delay (2) ; } 114 115 116 117 118 119 120 121 void lcd_string ( char * str ) { while (* str ) { lcd_data (* str ++) ; } } 122 123 124 125 126 127 128 129 void display_time ( int h , int m , int s ) { char time_str [9]; sprintf ( time_str , " %02 d :%02 d :%02 d " , h , m , s ) ; lcd_cmd (0 x80 ) ; lcd_string ( time_str ) ; } 6