# imports
import machine
import math
import time
from machine import Pin,Timer,ADC
#######################################
# Pin and constant definitions
#######################################
seven_segment_pins = [Pin(x,Pin.OUT)for x in range(7)]
seven_segment_dp_pin = Pin(7,Pin.OUT)
seven_segment_digit_pins = [Pin(x,Pin.OUT) for x in range(8,12)]
adc_pin = machine.ADC(26)
button_pin = machine.Pin(16,machine.Pin.IN, machine.Pin.PULL_UP)
#######################################
# Global variables
debounce = 0
interrupt_flag = 0
seven_segment_timer = machine.Timer(-1)
#######################################
display_value = 0
seven_segment_values = [
0b11000000,
0b01111001,
0b00100100,
0b00110000,
0b00011001,
0b00010010,
0b00000010,
0b01111000,
0b00000000,
0b00010000,
]
#######################################
# Function definitions
#######################################
# Function to read the ADC pin and
# to convert the digital value to a voltage level in the 0-3.3V range
# This function updates the value of the display_value global variable
def read_analogue_voltage(pin): # We can use the pin parameter if needed
global display_value
adc_value = adc_pin.read_u16()
voltage = adc_value * (3.3 / ((2**16) -1))
print("voltage",voltage)
display_value = voltage
# Function to disable timer that triggers scanning 7 segment displays
def disable_display_timer():
global seven_segment_timer
seven_segment_timer.deinit()
pass
# Function to enable timer that triggers scanning 7 segment displays
def enable_display_timer():
global seven_segment_timer
seven_segment_timer.init(mode=Timer.PERIODIC, period=30, callback=scan_display)
pass
# Function to handle scanning 7 segment displays
# Display the value stored in the display_value global variable
# on available 7-segment displays
def scan_display(timer_int):
value = display_value *1000
for i in range (4):
digit_value = value %10
dp_enable = i == 3
display_digit(digit_value,i,dp_enable)
value = value // 10
# Function display the given value on the display with the specified index
# dp_enable specifies if the decimal pooint should be on or off
def display_digit(digit_value, digit_index, dp_enable=False):
digit_value = int(digit_value)
for i in range(7):
seven_segment_pins[i].value(seven_segment_values[digit_value] >> i & 1)
if dp_enable:
seven_segment_dp_pin.value(0)
else: seven_segment_dp_pin.value(1)
seven_segment_digit_pins[digit_index].value(1)
time.sleep(0.001)
seven_segment_digit_pins[digit_index].value(0)
# Function to test avaiable 7-segment displays
def display_value_test():
global display_value
while True:
#display_value += 1
read_analogue_voltage(26)
time.sleep(1)
def interrupts(pin):
global debounce , interrupt_flag
button_press = time.ticks_ms() - debounce
if button_press >50:
interrupt_flag = 1
debounce = time.ticks_ms()
button_pin.irq(trigger=Pin.IRQ_FALLING, handler=interrupts)
# Function to setup GPIO/ADC pins, timers and interrupts
def setup():
enable_display_timer()
def main():
while True:
global interrupt_flag
if interrupt_flag == 1:
interrupt_flag = 0
read_analogue_voltage(26)
if __name__ == '__main__':
setup()
main()