LuckfoxTECH / luckfox-pico

luckfox-pico sdk
283 stars 105 forks source link

How to set the frequency of SARADC port? #94

Closed XiudingCai closed 5 months ago

XiudingCai commented 5 months ago

Hello,

I am currently working on a project where I need to set the frequency of the SARADC (Successive Approximation Register Analog-to-Digital Converter) port. I have written some test code to read the voltage values from the ADC, but I am unsure how to properly set the frequency.

Here is the test code I am using:

` import time

ADC_DIR = "/sys/bus/iio/devices/iio:device0"

def read_value(file_path): with open(file_path, "r") as file: return file.read().strip()

def main(): print("Press Ctrl+C to quit") start_time = time.time() voltage_change = [] while time.time() - start_time < 1: scale_value = float(read_value(f"{ADC_DIR}/in_voltage_scale")) IN0_raw_value = float(read_value(f"{ADC_DIR}/in_voltage0_raw")) IN0_voltage = f"{IN0_raw_value * scale_value / 1000:.2f}" voltage_change.append(IN0_voltage) print(f"{len(voltage_change)}Hz")

if name == "main": try: main() except KeyboardInterrupt: pass `

When I run this code, I get an output of approximately 538Hz. However, when I add a sleep time of time.sleep(1/16000) inside the while loop, the output frequency decreases to around 491Hz.

I would appreciate any guidance on how to properly set the frequency of the SARADC port. Thank you for your help.

XiudingCai commented 5 months ago

refer to https://forums.luckfox.com/viewtopic.php?t=612, thank you!