基本用法
from pyb import DAC
dac = DAC(1) # create DAC 1 on pin X5
dac.write(128) # write a value to the DAC (makes X5 1.65V)
dac = DAC(1, bits=12) # use 12 bit resolution
dac.write(4095) # output maximum value, 3.3V
输出正弦波
import math
from pyb import DAC
# create a buffer containing a sine-wave
buf = bytearray(100)
for i in range(len(buf)):
buf = 128 + int(127 * math.sin(2 * math.pi * i / len(buf)))
# output the sine-wave at 400Hz
dac = DAC(1)
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
输出12位精度正弦波
import math
from array import array
from pyb import DAC
# create a buffer containing a sine-wave, using half-word samples
buf = array('H', 2048 + int(2047 * math.sin(2 * math.pi * i / 128)) for i in range(128))
# output the sine-wave at 400Hz
dac = DAC(1, bits=12)
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
class pyb.DAC(port, bits=8)
定义DAC
port,1或2,对应X5(PA4)/X6(PA5)
bits,输出精度,可以是8或12
dac.init(bits=8)
初始化DAC
dac.noise(freq)
以指定频率,产生伪随机噪声信号
dac.triangle(freq)
以指定频率产生三角波
dac.write(value)
写入参数。在8bits时,参数范围[0-255];在12bits时,参数范围[0..4095]
dac.write_timed(data, freq, *, mode=DAC.NORMAL)
使用DMA方式周期写入数据
data,缓冲区数组
freq,默认使用Timer(6),用指定频率更新。也可以指定另外的定时器,有效的定时器是[2, 4, 5, 6, 7, 8]
mode,DAC.NORMAL or DAC.CIRCULAR |