6.1.3. 模拟IO¶
侦侦拍AI相机有2个ADC,可以用于模拟输入。可以测量的电压在0-3.3V
import pyb
adc = pyb.ADC(pyb.Pin('P6'))
def loop():
pyb.delay(10) # wait 10 ms
print("%f volts" % (((adc.read() * 3.3) + 2047.5) / 4095)) # read value, 0-4095
The ADC has 12-bits of resolution so it will output a value between 0 and 4095 for 0 to 3.3 volts. Finally, note that while the pin is in ADC mode it is not 5V tolerant anymore.
Next, to generate an analog output voltage you can put the I/O pin in DAC mode:
import pyb, math
dac = pyb.DAC(pyb.Pin('P6'))
counter = 0
def loop():
pyb.delay(10) # wait 10 ms
dac.write(int(math.sin(math.radians(counter % 360)) * 255))
counter += 1
The above code generates a sin wave on the I/O pin which will go from 0 to 255 for 0 to 3.3 volts. Again, note that while the pin is in DAC mode it is not 5V tolerant anymore.