Jane Medium : This paper gives MicroPython Kernel Development Notes : Experimental tasks are embedded in the book Medium ADC Some contents of software use cases .
key word
:MicroPython,MM32F3277
Software use case :
This part of the manuscript includes :
1.
2.
- Position in manuscript :
stay MM32F3277 in ADC in total Yes 16 Channels , The corresponding external ports are :
CH0~7
:PA0 ~ PA7 CH8,CH9
: PB0, PB1 CH10~CH13
:PC0 ~ PC3 CH14,CH15
:NULL The following code reads ADC0 passageway , Corresponding PA0 Voltage on the pin . Please note that , first ADC When initializing , Need to put init=True Parameters are substituted into .
Read ADC The conversion value is through read_u16() function , It returns 0 ~ 0xffff Value between , Corresponding ADC Input voltage from 0V To Reference voltage (3.3V) Value between .
from machine import Pin,ADC,DAC
import utime
adc0 = ADC(0, init=True)
while True:
print(adc0.read_u16())
utime.sleep_ms(200)
Through potentiometer , Manual adjustment PA0 Voltage of pin , Observe the value output by the program , It can be seen that as the input voltage increases , ADC Convert values from 0 To 0xffff change .
In the application ,ADC Conversion rate is a key parameter . The following code reads 1024 individual ADC Conversion value , Measure the total time required , Can be determined ADC Maximum conversion rate .
from machine import ADC
import time
adc0 = ADC(0, init=True)
BUF_LENGTH = 1024
buf = [0] * BUF_LENGTH
startc = time.ticks_cpu()
for i in range(BUF_LENGTH):
buf[i] = adc0.read_u16()
endc = time.ticks_cpu()
print(endc - startc)
while True:
pass
The program runs , The output is 25. This shows that through MicroPython collection 1024 individual ADC The number About the cost 25ms. therefore , The highest sampling frequency can reach 40kHz about .
The following code demonstrates how to start from ADC After reading the data , And then through DAC The channel outputs the signal .
from machine import DAC,ADC
import time
adc = ADC(0, init=True)
dac = DAC(0)
print("ADC to DAC .")
nowtime = time.ticks_ms()
while True:
dac.write_u16(adc.read_u16())
while True:
if time.ticks_ms() != nowtime:
nowtime = time.ticks_ms()
break
In the code time Read system time , The control sampling frequency is 1kHz.
Use a signal source , stay PA0 Pin input The frequency is 100Hz, The peak to peak is 1V, The mean for 1.5V The sine wave signal of . The input signals are shown below ADC Signals and outputs DAC The signal .
▲ chart 1.2.1 Input ADC Acquisition signal and output DAC The signal
Through the above waveform , You can see in the sine wave A cycle (10ms) Completed in 8 Time AD,DA transformation , This experimental result also verifies , At present MicroPython The kernel time There is a systematic error in the delay . The actual delay time needs to be multiplied by 1.25 times .
Ben This paper gives MicroPython Kernel Development Notes : Experimental tasks are embedded in the book Medium ADC Some contents of software use cases .
In the second experiment , Because of the need to use time Read system time , Will find , Now in this version time There is a systematic error in the reading time . The difference between the actual time 1.25 times , It is estimated that it is still in the kernel implementation , about CPU The internal reference frequency constant is not set correctly .
■ Links to related literature :
● Related chart Links :