'''
实验名称:ADC-电压测量
版本:v1.0
日期:20230824
作者:python-lingdu
说明:采集ADC数据,电压测量范围:0-1.3V,返回值:0-1300
实验平台:开发板
'''
from misc import ADC
from machine import Timer
from usr import st7789v
#构建LCD对象,长宽为240x240
lcd = st7789v.ST7789V(240, 240)
#定义字体颜色
fc = 0x0000 # 字体颜色 黑色 可根据需要修改
bc = 0xffff # 字体背景颜色 白色 可根据需要修改
#显示实验信息
lcd.lcd_show_ascii_str(10, 10, 16, 24, '01Studio', fc, bc)
lcd.lcd_show_ascii_str(10, 40, 16, 24, 'ADC', fc, bc)
#构建ADC对象
adc = ADC()
#共4个定时器,编号0~3
timer0 = Timer(Timer.Timer0)
adc_flag = 0
def fun(timer0):
global adc_flag
adc_flag = 1
#开启定时器,周期1秒
timer0.start(period=1000,mode=Timer.PERIODIC,callback=fun)
while True:
if adc_flag == 1:
value = adc.read(ADC.ADC0) #原始值
Voltage = '%.2f'%(value/1000) #电压值,单位V,小数2位
print(str(Voltage) + ' V')
#显示原始值
lcd.lcd_show_ascii_str(10, 100, 16, 24, str(value)+' ', fc, bc)
lcd.lcd_show_ascii_str(100, 100, 16, 24,'(1300)', fc, bc)
#显示电压值
lcd.lcd_show_ascii_str(10, 130, 16, 24, str(Voltage) + ' V', fc, bc)
adc_flag = 0 #清空标志位