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

Software use cases in micropython kernel development notebook: experimental use cases of utime

編輯:Python

Jane Medium : This paper gives MicroPython Kernel Development Notes : Experimental tasks are embedded in the book Medium utime Some contents of software use cases .

key word Software ,MicroPython,MM32F3277,utime,time

The contents of the manuscript Objective record
Contents
Basic experiments Accuracy of delay Measurement experiment total junction Suggestions for modification

 

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

Software use case :
This part of the manuscript includes :

  1. Basic experiments : Interactive view time The basic function of the module ; drive LED flashing ;
  2. utilize time Measure software execution time , To measure Time interval of port signal .
  • Position in manuscript :

 

§01 book Draft content


One 、 Basic experiments

1、 see time function

By the following command , You can see time Basic functions of .

import time
dir(time)
time.ticks_cpu()
time.ticks_ms()
time.ticks_us()

The output of the command is :

['__name__', 'sleep', 'sleep_ms', 'sleep_us', 'ticks_add', 'ticks_cpu', 'ticks_diff', 'ticks_ms', 'ticks_us']
158519
158520
158521000
>>>

You can see from above time The main functions in include :

  • Delay command : sleep, sleep_ms, sleep_us
  • Clock command :ticks_cpu, ticks_ms, ticks_us, tikcs_diff

ticks_cpu And ticks_ms In fact, the output values are the same , ticks_us Is in ticks_ms Just multiply by 1000, That is, this is actually a fake us Time delay , For some time delay measurements that require high accuracy, it will have adverse effects .

2、 flashing LED

utilize time The delay function of , The loop of the main program can be controlled more accurately . This is very important in some occasions where high timing accuracy is required , For example, use the main program PID control , It is required to strictly maintain the set duration of the control cycle . So precise latency is very medium for embedded software development .

The following program uses time Delay implementation external LED flashing .

from machine import Pin
import time
led = Pin("PC0", mode=Pin.OUT_PUSHPULL)
while True:
led(1)
time.sleep_ms(250)
led(0)
time.sleep_ms(250)

The image below shows LED Flashing condition .

▲ chart 1.1.1 time Delay generation LED flashing

Two 、 Accuracy of delay

Right up there LED In the scintillation experiment , Set different... By measuring time.sleep_ms The values correspond to LED Flicker rate , analysis time Delay time accuracy .

The following is true. time Time delay n The values are respectively from 1 To 25 change , Output LED Square wave frequency value .

▲ Corresponding to different time Time delay measurement LED Flicker rate

n Value Output frequency value (Hz)1200.0099952133.3399963100.0480.0566.669998657.150002750.0844.450001940.01036.3699991133.3300021230.771328.571426.671525.01623.5300011722.2199991821.0499991920.02019.0499992118.182217.3899992316.672416.02515.39

By using the function for the above numerical interval 1 / f = 2 a ⋅ ( n + b ) 1/f = 2a \cdot \left( {n + b} \right) 1/f=2a⋅(n+b) Fit , Can be found a,b about 1.25 and 1. therefore , This time can prove , Implemented in this version time Delay in progress , There are systematic errors . about time.sleep_ms(n) sentence , In fact, the delay time is 1.25(n+1) ms.

3、 ... and 、 Measurement experiment

application time Medium ticks_cpu You can get the time delay between two pieces of code in the program , This feature can Application in ultrasonic measurement 、 Capacitor charging and discharging time, etc , It can also be used to measure MicroPython The time consumed by program execution , This is of great significance in program optimization .

It is a pity that , The current version can only provide ms Level of time resolution . Therefore, if the measurement accuracy is improved , There is also a need to improve the internal implementation mechanism .

The following applies the single chip microcomputer pin, which can be configured into the input and output mode , And it has comparative characteristics for external input voltage . application RC Charge discharge characteristics can measure unknown capacitance . The measuring circuit is as follows , among R1 Is the known resistance , In the experiment 10kΩ. C1 Is the capacitance to be measured .

▲ chart 1.3.1 Experimental circuit for measuring capacitance

Before measurement , take PC1 Configure to output mode , take C1 The voltage on the is released to 0V about . And then PC1 Set to high impedance input state , 3.3V The power supply will pass through R1 Yes C1 Charge , When the input voltage exceeds the port threshold voltage U 1 U_1 U1​ after , PC1 Input logic from 0 become 1, utilize time Of ticks_cpu Function to get the charging time t 1 t_1 t1​ . according to RC The charging and discharging laws are known t 1 = R 1 C 1 ⋅ ln ⁡ ( 3.3 3.3 − U 1 ) t_1 = R_1 C_1 \cdot \ln \left( { { {3.3} \over {3.3 - U_1 }}} \right) t1​=R1​C1​⋅ln(3.3−U1​3.3​)

According to the known R1 The numerical , Then we can calculate to be tested C1 The capacity value of .

Here's the test program .

from machine import Pin
import time
pinc = Pin("PC1", Pin.OUT_OPENDRAIN)
pinc(0)
def measureC():
startc = time.ticks_cpu()
pinc = Pin("PC1", Pin.IN_FLOATING)
time.sleep_ms(1)
while True:
if pinc.value() != 0:
endc = time.ticks_cpu()
break
pinc = Pin("PC1", Pin.OUT_OPENDRAIN)
pinc(0)
return endc - startc
while True:
ret = measureC()
print(ret)
time.sleep_ms(1000)

The following is the voltage waveform of the capacitor to be measured . You can see that , The voltage is charged to 1.84V, Corresponding threshold voltage U 1 = 1.84 V U_1 = 1.84V U1​=1.84V .

▲ To be tested C1 Voltage waveform on

The value returned by the program running is 65. According to the previous delay accuracy measurement , Actual charging time t 1 = 65 × 1.25 = 81.25 m s t_1 = 65 \times 1.25 = 81.25\,ms t1​=65×1.25=81.25ms . According to what is known R 1 = 10 k Ω R_1 = 10k\Omega R1​=10kΩ , The capacitance to be measured can be obtained

C 1 = 81.25 R 1 ln ⁡ 3.3 3.3 − U 1 = 9.97 μ F C_1 = { {81.25} \over {R_1 \ln { {3.3} \over {3.3 - U_1 }}}} = 9.97\mu F C1​=R1​ln3.3−U1​3.3​81.25​=9.97μF

This value corresponds to the nominal value of the capacitance 10 Micro method Very close to .

 

※ total junction ※


This paper gives MicroPython Kernel Development Notes : Experimental tasks are embedded in the book Medium utime Some contents of software use cases .


One 、 Suggestions for modification

1、 Delay system error

Through the experiment, we can see , In the migration software version ,time There is a systematic error in the delay time , That is to say, there are some problems in porting software BUG, This is a common mistake that programmers make . According to the current version , time.sleep_ms(n) The actual delay is 1.25(n+1) ms. It is suggested that this part BUG Modify .

2、 To achieve true us Time delay

stay time.ticks_us(), ticks_cpu() It is better to realize the internal high-precision timer .


■ Links to related literature :

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

● Related chart Links :

  • chart 1.1.1 time Delay generation LED flashing
  • Corresponding to different time Time delay measurement LED Flicker rate
  • chart 1.3.1 Experimental circuit for measuring capacitance
  • To be tested C1 Voltage waveform on

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