from machine import Pin, mem32, Timer
from time import sleep
SIO_BASE = 0xd0000000
GPIO_OUT = 0x010
GPIO_OUT_SET = 0x014
GPIO_OUT_CLR = 0x018
GPIO_OUT_XOR = 0x01c
def gpio_out(value):
mem32[SIO_BASE + GPIO_OUT] = value
def gpio_out_set(value):
mem32[SIO_BASE + GPIO_OUT_SET] = value
def gpio_out_clr(value):
mem32[SIO_BASE + GPIO_OUT_CLR] = value
def gpio_out_xor(value):
mem32[SIO_BASE + GPIO_OUT_XOR] = value
SEG_FIRST_GPIO = 0
DIG_FIRST_GPIO = 16
segs = [0b11000000,
0b11111001,
0b10100100,
0b10110000,
0b10011001,
0b10010010,
0b10000010,
0b11011000,
0b10000000,
0b10010000,
0b01111111]
segs = [i << SEG_FIRST_GPIO for i in segs]
digs = [1 << i + DIG_FIRST_GPIO for i in range(4)]
for i in range(len(digs)):
Pin(DIG_FIRST_GPIO + i, Pin.OUT)
for i in range(len(segs)):
Pin(SEG_FIRST_GPIO + i, Pin.OUT)
nums = [0, 0, 0, 0]
dig = 0
def set7seg(t):
global dig
gpio_out(segs[nums[dig]] | digs[dig])
dig = dig + 1 if dig < 3 else 0
def set_nums(n):
nums[0] = n // 1000
nums[1] = n // 100 % 10
nums[2] = n // 10 % 10
nums[3] = n % 10
timer = Timer(mode = Timer.PERIODIC, freq = 240, callback = set7seg)
for i in range(10000):
set_nums(i)
sleep(0.001)