程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Micropython kernel development notebook software use case: timer related experiments

編輯:Python

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

The contents of the manuscript Objective record
Contents
Basic experiments initialization Timer ONE_SHOT function Periodic signal acquisition total junction

 

  • The contents of this manuscript belong to MicroPython Kernel Development Notes : Experimental tasks are embedded in the book The content in .

 

§01 book Draft content


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 .

One 、 Basic experiments

1、 initialization Timer

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 :

  • Timer passageway : You can go to 0,1;
  • Working mode :PERIODIC perhaps ONE_SHOT;
  • Interrupt the program : Predefined interrupt program ;
  • Timing period : Timer cycle length , Company ms;
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']

2、ONE_SHOT function

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

Two 、 Periodic signal acquisition

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

 

※ total junction ※


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 :

  • MicroPython Kernel Development Notes : Experimental tasks are embedded in the book

● Related chart Links :

  • chart 1.1.1 The core board of the experiment is red LED Change your state
  • chart 1.2.1 Timer Complete the signal acquisition and output

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved