查看: 2031|回复: 0

DAC的使用用法

[复制链接]
  • TA的每日心情
    慵懒
    2018-1-6 09:01
  • 签到天数: 7 天

    连续签到: 1 天

    [LV.3]偶尔看看II

    发表于 2018-5-23 09:28:39 | 显示全部楼层 |阅读模式
    分享到:
    本帖最后由 酥小小酥 于 2018-5-23 10:04 编辑

    基本用法
    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

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则



    手机版|小黑屋|与非网

    GMT+8, 2024-4-24 03:15 , Processed in 0.110018 second(s), 15 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.