from lcd_hd44780 import LcdHd44780
from machine import Timer
def stopwatch_100ms(t):
"""Interrupt handler of Timer executed every 100 millisecs"""
global tenths, seconds, minutes # Can update global variable here
tenths += 1# Modify tenths of seconds
tenths_str = str(tenths)
seconds_str = str(seconds)
minutes_str = str(minutes)
# Display tenths of seconds
lcd.move_to(2,3)
if minutes <= 9:
lcd.write("0")
lcd.write(minutes_str + ":")
if seconds <= 9:
lcd.write("0")
lcd.write(seconds_str+"."+tenths_str)
lcd = LcdHd44780(rs=26, e=25, d=[13, 12, 23, 27])
# Default screen
lcd.move_to(1,1)
lcd.write("Stopwatch:")
tim = Timer(0)
tim.init(period=100,
mode=Timer.PERIODIC,
callback=stopwatch_100ms)
tenths = 0
seconds = 0
minutes = 0
print("Start counting. Press `Ctrl+C` to stop")
try:
while True:
if tenths > 9:
seconds += 1
tenths = 0
if seconds > 59:
minutes += 1
seconds = 0
except KeyboardInterrupt:
print("Program stopped. Exiting...")
lcd.command(0x01)
tim.deinit()