Jane Medium : This paper gives MicroPython Kernel Development Notes : Experimental tasks are embedded in the book Medium Timer Some contents of software use cases .
key word
:Timer,MM32F3277,MicroPython
In embedded application development, we need to consider the time accuracy of software execution , For example, perform certain operations according to precise cycles , Or finish a task at a certain time . Although it can be helped by time The delay function of completes the corresponding work , But the software is in a sleep state during the delay , Unable to perform other tasks , Therefore, the efficiency of software operation is greatly reduced . Use Timer Timer Time interrupt function , It can ensure the accuracy of time , At the same time, it will not reduce the execution efficiency of the software .
stay MM32F3277 Of MicroPython There are two Timer Can be used . The following code shows Timer Basic application methods of .
The following code gives Timer The initialization process . A function is defined in the software to_callback(self) , It is Timer Timed interrupt service function , The interrupt service function function of the sample program is relatively simple , Change LED Status of pins .
initialization Timer You need four parameters :
from machine import Pin,Timer
import time
led = Pin('PA1', Pin.OUT_PUSHPULL)
def t0_callback(self):
led(1-led())
t0 = Timer(1, mode=Timer.PERIODIC, callback=t0_callback, period=200)
print(t0)
dir(t0)
while True:
pass
After the execution of the above procedures , stay NANO_F3270 On the core board LED Period based 200 ms Change its own state .
▲ chart 1.1.1 The core board of the experiment is red LED Change your state
adopt print, dir The two instructions show the timer properties and the functions that can be used . The following is the information output by the program :
Timer(channel=1, mode=PERIODIC, period=200ms)
['__del__', 'ONE_SHOT', 'PERIODIC', 'deinit', 'init']
When Timer The mode of is set to ONE_SHOT after , Timer Only one timed interrupt is completed , Then stop running . If you restart the timer , have access to init Function to complete .
The following code is based on the previous experimental code , take Timer The working mode of is changed to ONE_SHOT. In the main loop , Set up LED Status is on , Then start the timer . The timer is in 100 ms Then enter the interrupt program , take LED close . Therefore, the effect of program execution is LED With 500ms Blink for the cycle , The lighting time is 100ms.
from machine import Pin,Timer
import time
led = Pin('PA1', Pin.OUT_PUSHPULL)
def t0_callback(self):
led(1-led())
t0 = Timer(1, mode=Timer.ONE_SHOT, callback=t0_callback, period=100)
while True:
time.sleep_ms(500)
led.low()
t0.init(mode=Timer.ONE_SHOT, callback=t0_callback, period=100)
pass
If you need to complete an operation periodically , Compared with using time Software delay , Using a timer can ensure that the cycle is more accurate . Next, use timer interrupt to complete the acquisition and output of analog signals . Use ADC 0 passageway ( Corresponding PA0) Acquisition signal , Use DAC 0 passageway ( Corresponding PA4) Output the collected analog signal . The collection cycle is 1ms .
from machine import Pin,Timer,DAC,ADC
import time
led = Pin('PB2', Pin.OUT_PUSHPULL)
adc = ADC(0, init=True)
dac = DAC(0)
dac.write_u16(0x8000)
def t0_callback(self):
dac.write_u16(adc.read_u16())
led(1-led())
t0 = Timer(1, mode=Timer.PERIODIC, callback=t0_callback, period=1)
while True:
pass
stay PA0 The input amplitude is 1V, Zero is 1.5V, 100Hz The sine wave of . After the program is executed , stay PA4(DAC0) The pin can measure DAC The output signal of . comparison Chapter ten adopt The effect of software delaying signal acquisition , Using the timer to interrupt the acquisition of signals can be accurately achieved 1 ms Conduct signal acquisition once .
▲ chart 1.2.1 Timer Complete the signal acquisition and output
This paper gives MicroPython Kernel Development Notes : Experimental tasks are embedded in the book Medium Timer Some contents of software use cases .
■ Links to related literature :
● Related chart Links :