Micropython TPYBoard ADC的使用方法

Posted 小五义

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Micropython TPYBoard ADC的使用方法相关的知识,希望对你有一定的参考价值。

基本用法

import pyb
adc = pyb.ADC(Pin(‘Y11‘)) # create an analog object from a pin
adc = pyb.ADC(pyb.Pin.board.Y11)
val = adc.read() # read an analog value
adc = pyb.ADCAll(resolution) # creale an ADCAll object
val = adc.read_channel(channel) # read the given channel
val = adc.read_core_temp() # read MCU temperature
val = adc.read_core_vbat() # read MCU VBAT
val = adc.read_core_vref() # read MCU VREF
pyb.ADC(pin)

通过GPIO定义一个ADC
pyb.ADCAll(resolution)

定义ADC的分辨率,可以设置为8/10/12
adc.read()
读取adc的值,返回值与adc分辨率有关,8位最大255,10位最大1023,12位最大4095

adc.read_channel(channel)
读取指定adc通道的值

adc.read_core_temp()
读取内部温度传感器

adc.read_core_vbat()
读取vbat电压
vback = adc.read_core_vbat() * 1.21 / adc.read_core_vref()

adc.read_core_vref()
读取vref电压(1.21V参考)
3V3 = 3.3 * 1.21 / adc.read_core_vref()

adc.read_timed(buf, timer)
以指定频率读取adc参数到buf
buf,缓冲区
timer,频率(Hz)

使用这个函数会将ADC的结果限制到8位
adc = pyb.ADC(pyb.Pin.board.X19) # create an ADC on pin X19
buf = bytearray(100) # create a buffer of 100 bytes
adc.read_timed(buf, 10) # read analog values into buf at 10Hz
#this will take 10 seconds to finish
for val in buf: # loop over all values
print(val) # print the value out

以上是关于Micropython TPYBoard ADC的使用方法的主要内容,如果未能解决你的问题,请参考以下文章

TPYBoard获取MicroPython提示

[TPYBoard - Micropython] 五分种学会用TPYBoard - GPS 制作短信群发机

[Micropython]TPYBoard v10x拼插编程实验 点亮心形点阵

MicroPython实例之TPYBoardv102自动浇花系统

[TPYBoard - Micropython 之会python就能做硬件 9] 五分种学会用TPYBoard V102 制作避障小车(升级版)

Micropython TPYBoard v102 自动浇花实验