from machine import Pin
import math
import utime
pines = [2, 3, 4, 5, 6, 7, 8, 9]
# Crear pines una sola vez
pines_gpio = [Pin(pin, Pin.OUT) for pin in pines]
def GPIO_SALP(valor):
"""
Escribe un valor de 0 a 255
en el DAC R-2R
"""
# Protección de rango
if valor > 255:
valor = 255
if valor < 0:
valor = 0
bit = 1
for i in range(8):
if valor & bit:
pines_gpio[i].value(1)
else:
pines_gpio[i].value(0)
bit = bit << 1
def serie_pulso_simetrico(nmax, xmin, xmax, delta):
T = 1.0
w0 = 2 * math.pi / T
a0 = 0.5
x = xmin
while x <= xmax:
suma = 0.0
# Sumatoria de Fourier
for k in range(nmax):
n = 2 * k + 1
ak = (
(2 / math.pi)
*
((-1) ** k)
/
n
)
suma += ak * math.cos(n * w0 * x)
fx = a0 + suma
valor_dac = int(fx * 255)
# Limitar rango
if valor_dac > 255:
valor_dac = 255
if valor_dac < 0:
valor_dac = 0
GPIO_SALP(valor_dac)
print(valor_dac)
# Incremento
x += delta
# Velocidad de actualización
utime.sleep_ms(10)
xmin = -0.5
xmax = 0.5
delta = 0.02
print("Serie de Fourier - Pulso Simétrico")
print("DAC R-2R + Plotter Thonny")
while True:
# Barrido de armónicos
for nmax in range(1, 31):
print("Armónicos:", nmax)
serie_pulso_simetrico(
nmax,
xmin,
xmax,
delta
)
utime.sleep_ms(500)