Digital stopwatch using 8051 microcontroller mini project

In this mini-project we are going to make a simple Digital Stopwatch by using a Microcontroller from 8051 family. Digital Stopwatch allows you to record the exact number of hours and minutes you worked on a task/activity. It is a special type of watch with buttons that start, stop, and then zero the hands, used to time races.

Digital Stopwatch using 8051 Microcontroller

Components required:

  • 8051 Microcontroller (89C51)
  • Resistor – 10K
  • Capacitor – 10uF, 33pF (two)
  • Crystal oscillator – 12MHz
  • LCD display.

Circuit diagram:

Digital Stopwatch using 8051 Microcontroller
Digital stopwatch using 8051 microcontroller

Pin description of LCD display:

lcd pin diagram
lcd pin diagram
Pinnumber Name Description
1 Vss Ground
2 Vdd +5V
3 Vee Contrast Adjustment ‐2V to ‐5V
4 RS Register Select
5 RW 1 ‐Read , 0‐Write
6 E Enable Strobe
7 D0 Data Line
8 D1 Data Line
9 D2 Data Line
10 D3 Data Line
11 D4 Data Line
12 D5 Data Line
13 D6 Data Line
14 D7 Data Line
15 LED+ Backlit LED +V Vdd (Optional signal)
16 LED- Backlit LED +V Vdd (Optionalsignal)

C program:

// ************************************************
// Project:   Digital Stopwatch using 8051 microcontroller
// Author:    Mayuresh Joshi
// Blog: myclassbook.org
// ************************************************
#include<reg51.h>
//Function declarations
void cct_init(void);
void delay(int);
void lcdinit(void);
void WriteCommandToLCD(int);
void WriteDataToLCD(char);
void ClearLCDScreen(void);
void InitTimer0(void);
void UpdateTimeCounters(void);
void DisplayTimeToLCD(unsigned int,unsigned int,unsigned int);
//*******************
//Pin description
/*
P2.4 to P2.7 is data bus
P1.0 is RS
P1.1 is E
*/
//********************
// Defines Pins
sbit RS = P1^0;
sbit E  = P1^1;
// Define Clock variables
unsigned int usecCounter = 0;
unsigned int msCounter   = 0;
unsigned int secCounter  = 0;
unsigned int minCounter  = 0;
unsigned int hrCounter   = 0;
// ***********************************************************
// Main program
//
void main(void)
{
cct_init();             // Make all ports zero
lcdinit();              // Initilize LCD
InitTimer0();           // Start Timer0
while(1)
{
if( msCounter == 0 )                                       // msCounter becomes zero after exact one sec
{
DisplayTimeToLCD(hrCounter, minCounter, secCounter);   // Displays time in HH:MM:SS format
}
UpdateTimeCounters();                                      // Update sec, min, hours counters
}
}
void cct_init(void)
{
P0 = 0x00;   //not used
P1 = 0x00;   //not used
P2 = 0x00;   //used as data port
P3 = 0x00;   //used for generating E and RS
}
void InitTimer0(void)
{
TMOD &= 0xF0;    // Clear 4bit field for timer0
TMOD |= 0x02;    // Set timer0 in mode 2
TH0 = 0x05;      // 250 usec reloading time
TL0 = 0x05;      // First time value
ET0 = 1;         // Enable Timer0 interrupts
EA  = 1;         // Global interrupt enable
TR0 = 1;         // Start Timer 0
}
void Timer0_ISR (void) interrupt 1     // It is called after every 250usec
{
usecCounter = usecCounter + 250;   // Count 250 usec
if(usecCounter==1000)              // 1000 usec means 1msec
{
msCounter++;
usecCounter = 0;
}
TF0 = 0;     // Clear the interrupt flag
}
void UpdateTimeCounters(void)
{
if (msCounter==1000)
{
secCounter++;
msCounter=0;
}
if(secCounter==60)
{
minCounter++;
secCounter=0;
}
if(minCounter==60)
{
hrCounter++;
minCounter=0;
}
if(hrCounter==24)
{
hrCounter = 0;
}
}
void DisplayTimeToLCD( unsigned int h, unsigned int m, unsigned int s )   // Displays time in HH:MM:SS format
{
ClearLCDScreen();      // Move cursor to zero location and clear screen
// Display Hour
WriteDataToLCD( (h/10)+0x30 );
WriteDataToLCD( (h%10)+0x30 );
//Display ':'
WriteDataToLCD(':');
//Display Minutes
WriteDataToLCD( (m/10)+0x30 );
WriteDataToLCD( (m%10)+0x30 );
//Display ':'
WriteDataToLCD(':');
//Display Seconds
WriteDataToLCD( (s/10)+0x30 );
WriteDataToLCD( (s%10)+0x30 );
}
void delay(int a)
{
int i;
for(i=0;i<a;i++);   //null statement
}
void WriteDataToLCD(char t)
{
RS = 1;             // This is data
P2 &= 0x0F;                         // Make P2.4 to P2.7 zero
P2 |= (t&0xF0);     // Write Upper nibble of data
E  = 1;             // => E = 1
delay(150);
E  = 0;             // => E = 0
delay(150);
P2 &= 0x0F;                         // Make P2.4 to P2.7 zero
P2 |= ((t<<4)&0xF0);// Write Lower nibble of data
E  = 1;             // => E = 1
delay(150);
E  = 0;             // => E = 0
delay(150);
}
void WriteCommandToLCD(int z)
{
RS = 0;             // This is command
P2 &= 0x0F;                         // Make P2.4 to P2.7 zero
P2 |= (z&0xF0);     // Write Upper nibble of data
E  = 1;             // => E = 1
delay(150);
E  = 0;             // => E = 0
delay(150);
P2 &= 0x0F;                         // Make P2.4 to P2.7 zero
P2 |= ((z<<4)&0xF0);// Write Lower nibble of data
E  = 1;             // => E = 1
delay(150);
E  = 0;             // => E = 0
delay(150);
}
void lcdinit(void)
{
///////////// Reset process from datasheet /////////
delay(15000);
P2 &= 0x0F;                           // Make P2.4 to P2.7 zero
P2 |= (0x30&0xF0);    // Write 0x3
E  = 1;               // => E = 1
delay(150);
E  = 0;               // => E = 0
delay(150);
delay(4500);
P2 &= 0x0F;                           // Make P2.4 to P2.7 zero
P2 |= (0x30&0xF0);    // Write 0x3
E  = 1;               // => E = 1
delay(150);
E  = 0;               // => E = 0
delay(150);
delay(300);
P2 &= 0x0F;                           // Make P2.4 to P2.7 zero
P2 |= (0x30&0xF0);    // Write 0x3
E  = 1;               // => E = 1
delay(150);
E  = 0;               // => E = 0
delay(150);
delay(650);
P2 &= 0x0F;                           // Make P2.4 to P2.7 zero
P2 |= (0x20&0xF0);    // Write 0x2
E  = 1;               // => E = 1
delay(150);
E  = 0;               // => E = 0
delay(150);
delay(650);
/////////////////////////////////////////////////////
WriteCommandToLCD(0x28);    //function set
WriteCommandToLCD(0x0c);    //display on,cursor off,blink off
WriteCommandToLCD(0x01);    //clear display
WriteCommandToLCD(0x06);    //entry mode, set increment
}
void ClearLCDScreen(void)
{
WriteCommandToLCD(0x01);   // Clear screen command
delay(1000);
}

Download C program, Proteus simulation file, HEX file, etc using below link.

Also read:  How to make PCB layout on Eagle software

Download ButtonYou may also like:

I hope you liked this Digital Stopwatch mini project. If you have any queries  feel free to ask in comment section below. Subscribe to our newsletter and like our facebook page for future updates. Have a nice day!

 

Published by

Mayuresh Joshi

Mayuresh Joshi a web geek and founder of MyClassBook, a popular educational blog since 2012. He has completed his B.Tech (Instrumentation Engg.) from SGGSIE&T, Nanded. He loves to write on electronic projects, SharePoint and other current technologies.

4 thoughts on “Digital stopwatch using 8051 microcontroller mini project”

  1. i think there is some mistake in reset point which is connecting to the power.in simulation this circuit works but with real components didn’t.

Leave a Reply

Your email address will not be published. Required fields are marked *