[STM32F411] ADC.read resolution problem
Posted: Thu Apr 01, 2021 7:53 pm
Good day!
I am trying to read an analog signal. I do it in two ways and they give different results.
Using the ADC.read () function, I get the result with a resolution of 12 bits without any problems.
But if I use the ADC.read_timed (buf.tim) function, then the result is saved to a buffer with a resolution of 8 bits.
I can't understand why this is happening?
I am trying to read an analog signal. I do it in two ways and they give different results.
Using the ADC.read () function, I get the result with a resolution of 12 bits without any problems.
Code: Select all
pin = pyb.Pin(pyb.Pin.board.PA4, pyb.Pin.ANALOG)
adc = pyb.ADC(pin) # пин датчик ванны
ls = []
def printer(x):
val = adc.read()
RealVolt = (val * 3.3) / 4096.0
if len(ls)<20:
ls.append(RealVolt)
else:
ls.pop(0)
ls.append(RealVolt)
print(val, RealVolt, min(ls),max(ls), "delta=",max(ls)-min(ls))
machine.Timer(mode=machine.Timer.PERIODIC,period = 100, callback = printer)
Code: Select all
pin = pyb.Pin(pyb.Pin.board.PA4, pyb.Pin.ANALOG)
adc = pyb.ADC(pin) # пин датчик ванны
ls = []
def myMultiplyFunction ():
#""" расчет значения напряжения датчика .
#Делитель напряжения с входным напряжением 26В и выходным напряжения до 3.33 В """
R1 = 68000.0
R2 = 10000.0
tim = pyb.Timer(4, freq=10) # create a timer running at 10Hz
buf = bytearray(10) # creat a buffer to store the samples
adc.read_timed(buf, tim) # sample 100 values, taking 10s
print("buf ", list(buf))
test_average = sum(list(buf)) / len(list(buf))
print("test_average ",test_average)
arduino_IN = (test_average * 3.3) / 256.0
voltage = arduino_IN / (R2/(R1+R2))
print("voltage",voltage)
return voltage
machine.Timer(mode=machine.Timer.PERIODIC,period = 100, callback = myMultiplyFunction() )