class DAC – 数字信号转换为模拟信号

DAC模块用于输出模拟信号(输出特定的电压值)

资源有限, 只可以在P6上输出. 生成电压范围0到3.3V

Usage

from pyb import DAC

dac = DAC('P6')          # create DAC on pin P6
                         # 在P6创建一个DAC对象
dac.write(128)           # write a value to the DAC (makes P6 1.65V)
                         # P6输出一个电压值 128, 转化为实际电压值
dac = DAC('P6', bits=12) # use 12 bit resolution
                         # 使用12位的分辨率
dac.write(4095)          # output maximum value, 3.3V
                         # 输出最大值 3.3v

输出一个连续的sin波形

import math
from pyb import DAC

# create a buffer containing a sine-wave
# 首先创建一个buffer用于存储sin波形,在不同相位的值
buf = bytearray(100)
for i in range(len(buf)):
    buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf)))

# output the sine-wave at 400Hz
# 使用定时器, 周期的从buffer中读取相位值.
dac = DAC('P6')
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)

输出一个连续的sin波形, 使用12-bit的分辨率

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('P6', bits=12)
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)

Constructors

class pyb.DAC(port, bits=8)

创建一个新的DAC对象 默认分辨率为8-bits

port Pin对象 / 引脚名称

DAC('P6')

DAC(Pin('P6'))

bits 整数, 代表DAC输出电压的分辨率, 指的是二进制的位数. 可选范围为8或者12 对应的电压值的取值范围为 0 至 2^bits -1

bits 最小值 最大值
8 0 2^8 - 1
12 0 2^12 - 1

Methods

dac.init(bits=8)

重新初始化DAC, 可以修改分辨率. bits 为8或者12

dac.noise(freq)

生成伪随机信号.

freq 按照给定频率输出

dac.triangle(freq)

生成锯齿波(三角波)

生成的电压值, 随机给定的频率变化.

the frequency of the repeating triangle wave itself is 2048 times smaller.

dac.write(value)

DAC输出数值, 取值范围是0至2^bits-1

dac.write_timed(data, freq, *, mode=DAC.NORMAL)

使用定时器, 阶段性输出特定的值

其中data是一个数组, write_timed 就按照顺序输出data中的值

模式(mode)分为两种DAC.NORMALDAC.CIRCULAR

其中Normal是过一遍, Circular是循环输出.

Initiates a burst of RAM to DAC using a DMA transfer. The input data is treated as an array of bytes in 8-bit mode, and an array of unsigned half-words (array typecode ‘H’) in 12-bit mode.freq can be an integer specifying the frequency to write the DAC samples at, using Timer(6). Or it can be an already-initialized Timer object which is used to trigger the DAC sample. Valid timers are 2, 4, 5, 6, 7 and 8.mode can be DAC.NORMAL or DAC.CIRCULAR.

TODO

各种波形, 结合示波器, 给出图片.

Copyright 杭州云江科技有限公司 2017 all right reserved,powered by Gitbook该文件修订时间: 2018-04-02 09:53:12

results matching ""

    No results matching ""