from machine import Pin, Timer
import time
# Onboard LED
led = Pin("LED", Pin.OUT)
# Shared variables
tick_count = 0
one_second_flag = False
def timer_callback(timer):
global tick_count
global one_second_flag
tick_count += 1
# 10 × 100 ms = 1 second
if tick_count % 10 == 0:
one_second_flag = True
# Create hardware timer
timer = Timer()
# Fire every 100 ms
timer.init(
freq=10, # 10 Hz = 100 ms period
mode=Timer.PERIODIC,
callback=timer_callback
)
print("Timer Interrupt Demo")
print("Timer interrupt every 100 ms")
last_report = time.ticks_ms()
while True:
# Process timer events outside interrupt context
if one_second_flag:
one_second_flag = False
led.toggle()
print("One second elapsed")
print("Timer ticks:", tick_count)
# Simulate other work
if time.ticks_diff(time.ticks_ms(), last_report) > 250:
last_report = time.ticks_ms()
print("Main loop doing work...")
time.sleep_ms(10)