some changes compared to the code example on : http://micropython.org/doc/module/pyb/DAC
Code: Select all
from pyb import DAC
from pyb import LED
import math
dac = DAC(1)
led = LED(4)
led.on()
buf = bytearray(100)
for i in range(len(buf)) :
buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf)))
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
I've added a LED to see the board is running.
result : The sinewave is clipped probably because the full DAC range isn't available.
Lowering the range to 100digits and taking a higher time resolution gives better results :
Code: Select all
buf = bytearray(1000)
for i in range(len(buf)) :
buf[i] = 128 + int(100 * math.sin(2 * math.pi * i / len(buf)))
dac.write_timed(buf, 400 * len(buf), mode=DAC.CIRCULAR)
Especially for a pure analog designer like myself.