Code: Alles auswählen
import machine
from machine import Pin, SPI, ADC, Timer
import math
import time
spi_sck = Pin(2) # SCK pin at GP2
spi_tx = Pin(3) # TX pin at GP3
spi_rx = Pin(0) # RX pin at GP4 (not used)
spi = SPI(0, baudrate=100000, sck=spi_sck, mosi=spi_tx, miso=spi_rx)
CS = Pin(16, Pin.OUT) # CS
R = 2 * math.pi / 50
T = 100
Conv = 4095.0 / 3.3
PeaktoPeak = 1.4 * Conv # 1.4V
ReqDCoffset = 1.0 * Conv # 1.6V
k = 0
potentiometer = ADC(26) # Potentiometer connected to ADC pin 26
def DAC(timer):
global k, CS, sins
buff = bytearray(2)
k = k + 1
if k == 50:
k = 0
data = int(sins[k])
buff[0] = (data >> 8) & 0xFF # HIGH Byte
buff[1] = data & 0xFF # LOW Byte
CS.value(0) # Enable MCP4921
spi.write(buff) # Send to SPI bus
CS.value(1) # Disable MCP4921
# Main program
sins = [0] * 101
def update_frequency(pot):
global sins
frequency = map_range(pot.read_u16(), 0, 65535, 20, 20000) # Map ADC value to desired frequency range
print("Frequency:", frequency) # Debugging output
for i in range(101):
sins[i] = ReqDCoffset + PeaktoPeak/2 + PeaktoPeak/2 * math.sin(R*i * frequency)
def map_range(value, in_min, in_max, out_min, out_max):
return int((value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min)
tim = machine.Timer()
start_frequency = 20 # Start frequency (20 Hz)
end_frequency = 20000 # End frequency (20 kHz)
wobble_duration = 10 # Duration of the wobble in seconds
step_count = 100
step_delay = wobble_duration / step_count
current_frequency = start_frequency
frequency_step = (end_frequency - start_frequency) / step_count
for _ in range(step_count):
update_frequency(potentiometer) # Update frequency based on potentiometer
tim.init(freq=current_frequency, mode=machine.Timer.PERIODIC, callback=DAC)
time.sleep(step_delay)
current_frequency += frequency_step
tim.deinit() # Stop the timer
Hat jemand eine Idee was ich falsch mache?