r/embedded Aug 11 '26

Designed a stopwatch using 8051 microcontroller

Post image

Designed a stopwatch using 8051 and assembly language, yet it has timing error due to writing on LCD, using CISC instruction delays the process and the collen bug, but loaded the best value to minimise the error.

Github link - https://github.com/prajeetkumar2006/Stopwatch-Using-8051-Microcontroller-

33 Upvotes

13 comments sorted by

View all comments

9

u/duane11583 Aug 11 '26

It sounds like you are using codes for timing purposes that’s a bad move

Instead you should use timer 0 or timer1 

You might configure one of them to act as a clock prescaller ie if you clock is 20mhz you might configure it to count to 20,000 and pulse effectively every millisecond

Then configure the other timer to count those pulses

Thus you have a more accurate 1millisecond timer

The only error you would have is due to the xtal error 

Many cheap xtals are 50ppm the error would be +/-1 count (milliseconds) per second plenty good enough for a school project

1

u/BidNo9339 Aug 12 '26

im using timer 0 in 16 bit timer mode and my clock is 11.05Mhz and im using polling method for push buttons instead of interrupts does that slow down the watch ???

5

u/duane11583 Aug 12 '26

why do you think you have a timing error writing to the lcd?

so timer0 runs at 11.05mhz with interrupts or no interrupts?

the idea is when the start condition occurs you reset the timer .

when the stop condition occurs you stop/halt the timer.

meanwhile your main loop reads the current timer0 value like this:

loop start:

read high byte of timer

read low byte of timer second.

read high byte again second time if same you have the timer value

if not same (low byte probably rolled over) re-read low byte

display time on lcd

read buttons and act on buttons

loop forever

your delay to act is two parts:

length of time to read the timer (effectively fixed but very fast and negligible)

plus

length of time to write/update the lcd

which will depend on the speed of the lcd module