[QuecPython] Hardware TIM timer of EC800M IoT development board with precise delay

[QuecPython] Hardware TIM timer of EC800M IoT development board with precise delay


First of all, this timer is at the bottom level of the hardware and has the highest priority. If called, it will cause threads such as GNSS to be occupied, making it impossible to obtain data.

Import library

from machine import Timer

Timer initialization

timer.start(period, mode, callback)

This method is used to start the timer.

Parameter Description:

period - interrupt period, int type, unit millisecond, greater than or equal to 1.

mode - running mode, int type, described as follows:
ONE_SHOT - one-shot mode, the timer only executes once
PERIODIC - periodic mode, Loop execution

callback - timer execution function, function type, prototype is callback(arg), arg is not actually used, and can be passed in None directly. After the callback function is triggered, the stop interface must be called to stop the timer, otherwise the callback will not be executed the next time the start interface is called.

Return value description:

The integer value 0 is returned if the startup is successful, and the integer value -1 is returned if it fails.

delay function

TIM0=Timer(Timer.Timer0)
TIM0_Flag=0
def Delay_ms(ms):
    global TIM0_Flag
    TIM0_Flag=0
    TIM0.start(period=ms, mode=Timer.ONE_SHOT, callback=TIM0_Callback)
    while TIM0_Flag==0:
        if TIM0_Flag==1:
            break
        pass
    TIM0_Flag=0

Configure for one-time operation mode and wait for interrupt generation

Scheduled interrupt callback

def TIM0_Callback(arg):
    global TIM0_Flag
    TIM0_Flag=1
    TIM0.stop()

When an interrupt occurs, the timer is turned off.

transfer

Delay_ms(50)

Function packaging

# -*- coding: utf-8 -*-
from misc import PWM_V2
from machine import Pin
import usr.Delay as Delay

C0 = 1635159
Db0 = 1732391
D0 = 1835404
Eb0 = 1944543
E0 = 2060172
F0 = 2182676
Gb0 = 2312465
G0 = 2449971
Ab0 = 2595654
A0 = 2750000
Bb0 = 2913523
B0 = 3086770

C1 = 3270319
Db1 = 3464782
D1 = 3670809
Eb1 = 3889087
E1 = 4120344
F1 = 4365352
Gb1 = 4624930
G1 = 4899942
Ab1 = 5191308
A1 = 5500000
Bb1 = 5827047
B1 = 6173541

C2 = 6540639
Db2 = 6929565
D2 = 7341619
Eb2 = 7778174
E2 = 8240688
F2 = 8730705
Gb2 = 9249860
G2 = 9799885
Ab2 = 10382617
A2 = 11000000
Bb2 = 11654094
B2 = 12347082

C3 = 13081278
Db3 = 13859131
D3 = 14683238
Eb3 = 15556349
E3 = 16481377
F3 = 17461411
Gb3 = 18499721
G3 = 19599771
Ab3 = 20765234
A3 = 22000000
Bb3 = 23308188
B3 = 24694165

C4 = 26162556
Db4 = 27718263
D4 = 29366476
Eb4 = 31112698
E4 = 32962755
F4 = 34922823
Gb4 = 36999442
G4 = 39199543
Ab4 = 41530469
A4 = 44000000
Bb4 = 46616376
B4 = 49388330

C5 = 52325113
Db5 = 55436526
D5 = 58732953
Eb5 = 62225396
E5 = 65925511
F5 = 69845646
Gb5 = 73998884
G5 = 78399087
Ab5 = 83060939
A5 = 88000000
Bb5 = 93232752
B5 = 98776660

C6 = 104650226
Db6 = 110873052
D6 = 117465907
Eb6 = 124450793
E6 = 131851022
F6 = 139691292
Gb6 = 147997769
G6 = 156798174
Ab6 = 166121879
A6 = 176000000
Bb6 = 186465504
B6 = 197553320

C7 = 209300452
Db7 = 221746104
D7 = 234931814
Eb7 = 248901586
E7 = 263702045
F7 = 279382585
Gb7 = 295995538
G7 = 313596348
Ab7 = 332243758
A7 = 352000000
Bb7 = 372931009
B7 = 395106641

C8 = 418600904
Db8 = 443492209
D8 = 469863628
Eb8 = 497803173
E8 = 527404091
F8 = 558765170
Gb8 = 591991076
G8 = 627192697
Ab8 = 664487516
A8 = 704000000
Bb8 = 745862018
B8 = 790213282

C9 = 837201808
Db9 = 886984419
D9 = 939727257
Eb9 = 995606347
E9 = 1054808182
F9 = 1117530340
Gb9 = 1183982152
G9 = 1254385395
Ab9 = 1328975032
A9 = 1408000000
Bb9 = 1491724036
B9 = 1580426564


def Pitch_Play(pwm,pitch,s):
    pitch=pitch/100000
    pwm.open(pitch, 50)
    Delay.Delay_ms(int(s*950))
    pwm.close()
    Delay.Delay_ms(int(s*50))

def Pitch_Play_HB_Start(GPIO,V2_PWM):
    Pin(GPIO, Pin.OUT,Pin.PULL_PD,0)
    pwm=PWM_V2(V2_PWM, G5/100000, 50)
    Pitch_Play(pwm,G5,0.25)
    Pitch_Play(pwm,G5,0.25)
    Pitch_Play(pwm,A5,0.5)
    Pitch_Play(pwm,G5,0.5)
    Pitch_Play(pwm,C6,0.5)
    Pitch_Play(pwm,B5,0.5)

def Pitch_Play_HB_End(GPIO,V2_PWM):
    Pin(GPIO, Pin.OUT,Pin.PULL_PD,0)
    pwm=PWM_V2(V2_PWM, G5/100000, 50)
    Pitch_Play(pwm,F5,0.25)
    Pitch_Play(pwm,F5,0.25)
    Pitch_Play(pwm,E6,0.5)
    Pitch_Play(pwm,C6,0.5)
    Pitch_Play(pwm,D6,0.5)
    Pitch_Play(pwm,C6,0.5)

Appendix: List assignment types and py packaging

list assignment

BUG recurrence

I wrote a small program when I had nothing to do. The code is as follows:

# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 19:47:01 2021

@author: 16016
"""

a_list = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
#print(len(a_list))
#b_list = ['','','','','','','','','','','','','','','','']
c_list = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
#for i in range(16):
if len(a_list):
    for j in range(16):
        a_list[j]=str(a_list[j])+'_'+str(j)
        print("序号:",j)
        print('a_list:\n',a_list)
        
        
        c_list[j]=a_list
        print('c_list[0]:\n',c_list[0])
        print('\n')
#        b_list[j]=a_list[7],a_list[8]
#        print(b_list[j])
        # 写入到Excel:
#print(c_list,'\n')    

I made a 16-time for loop in the program and added "_" and the loop serial number after each value of list a.
For example, the xth time of looping is Add _x to the , '1_1', '2_2', '3_3', '4_4', '5_5', '6_6', '7_7', '8_8', '9_9', '10_10', '11_11', '12_12', ' 13_13', '14_14', '15_15'] This is also correct

At the same time, I write the value of list a into the empty list c during each cycle. For example, the xth cycle is to write the changed value of list a into the xth position of list c
The value of c[0] after the 0th loop should be ['0_0', '1', '2', '3', '4', '5', '6', '7' , '8', '9', '10', '11', '12', '13', '14', '15'] This is also correct
But in the 1st After this cycle, the value of c[0] has been changing into the value of c[x]
, which is equivalent to changing c_list[0] into c_list[1]...and so on. The value of the resulting list c is exactly the same for every item
I don’t understand what’s going on
My c[0] is only at the 0th time It is assigned a value during the loop but its value changes later

As shown in the figure:
Insert image description here
The first time a bug occurred. After the assignment, the value of c[0] was changed every time it was looped. It took a long time and it didn’t work out
No matter whether it is added with the append function or defined with a two-dimensional array or a third empty array is added for transition, it cannot be solved

Code improvements

Later, under the guidance of my Huake classmates, I suddenly thought that the assignment can be assigned to an address. The value in the address keeps changing, causing the assignment to keep changing. So I used the loop within loop deep copy in the second picture to implement it.

code show as below:

# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 19:47:01 2021

@author: 16016
"""

a_list = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
#print(len(a_list))
#b_list = ['','','','','','','','','','','','','','','','']
c_list = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
#for i in range(16):
if len(a_list):
    for j in range(16):
        a_list[j]=str(a_list[j])+'_'+str(j)
        print("序号:",j)
        print('a_list:\n',a_list)
        
        
        for i in range(16):
            c_list[j].append(a_list[i])
        print('c_list[0]:\n',c_list[0])
        print('\n')
#        b_list[j]=a_list[7],a_list[8]
#        print(b_list[j])
        # 写入到Excel:
print(c_list,'\n')    

solved the problem

Insert image description here

optimization

The third time I asked the teacher to use the copy function to assign a true value.

code show as below:

# -*- coding: utf-8 -*-
"""
Created on Fri Nov 19 19:47:01 2021

@author: 16016
"""

a_list = ['0','1','2','3','4','5','6','7','8','9','10','11','12','13','14','15']
#print(len(a_list))
#b_list = ['','','','','','','','','','','','','','','','']
c_list = [[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
#for i in range(16):
if len(a_list):
    for j in range(16):
        a_list[j]=str(a_list[j])+'_'+str(j)
        print("序号:",j)
        print('a_list:\n',a_list)
        
        
        c_list[j]=a_list.copy()
        print('c_list[0]:\n',c_list[0])
        print('\n')
#        b_list[j]=a_list[7],a_list[8]
#        print(b_list[j])
        # 写入到Excel:
#print(c_list,'\n')    

The problem can also be solved
Insert image description here
Finally, the problem is concluded that the pointer is to blame!

a_list points to an address, not a value. a_list[i] points to a single value. The copy() function also copies a value, not an address.

If this is written in C language, it will be more intuitive. No wonder C language is the basis. If you don’t learn C in optical Python, you will not be able to solve such a problem.

C language yyds What kind of rubbish retarded language is Python?

Summarize

Since Python cannot define a value as a pointer or an independent value, it can only be transferred using a list
As long as the assignment points to a list as a whole, it is a pointer memory address. There is only one solution, which is to deeply copy and assign each value (extract the elements in the sublist and reconnect them in sequence) or use the copy function to assign values ​​individually

Test as shown:
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Part of the code:

# -*- coding: utf-8 -*-
"""
Created on Sat Nov 20 16:45:48 2021

@author: 16016
"""

def text1():
    A=[1,2,3]
    B=[[],[],[]]
    for i in range(len(A)):
        A[i]=A[i]+i
        B[i]=A
        print(B)

def text2():
    A=[1,2,3]
    B=[[],[],[]]
    
    A[0]=A[0]+0
    B[0]=A
    print(B)
    A[1]=A[1]+1
    B[1]=A
    print(B)
    A[2]=A[2]+2
    B[2]=A
    print(B)
    
if __name__ == '__main__':
    text1()
    print('\n')
    text2()

py package

Pyinstaller packages exe (including packaging resource files, never error version)

Dependent packages and their corresponding version numbers

PyQt5 5.10.1
PyQt5-Qt5 5.15.2
PyQt5-sip 12.9.0

pyinstaller 4.5.1
pyinstaller-hooks-contrib 2021.3

Pyinstaller -F setup.py package exe

Pyinstaller -F -w setup.py packaging without console

Pyinstaller -F -i xx.ico setup.py packages the specified exe icon package

Package exe parameter description:

-F: Only a single exe format file is generated after packaging;

-D: Default option, creates a directory containing exe files and a large number of dependent files;

-c: Default option, use the console (a black box similar to cmd);

-w: Do not use the console;

-p: Add a search path to find the corresponding library;

-i: Change the icon of the generated program.

If you want to package resource files
you need to convert the path in the code
Also note that if you want to package resource files py The path in the program needs to be changed from ./xxx/yy to xxx/yy and the path conversion is performed
But if the resource file is not packaged, it is best to use the path as ./xxx/yy and not perform the path conversion. Path conversion

def get_resource_path(relative_path):
    if hasattr(sys, '_MEIPASS'):
        return os.path.join(sys._MEIPASS, relative_path)
    return os.path.join(os.path.abspath("."), relative_path)

Then add the directory in the datas part of the spec file
For example:

a = Analysis(['cxk.py'],
             pathex=['D:\\Python Test\\cxk'],
             binaries=[],
             datas=[('root','root')],
             hiddenimports=[],
             hookspath=[],
             hooksconfig={
    
    },
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)

Then directly Pyinstaller -F setup.spec

If the packaged file is too large, just change the excludes in the spec file and include the libraries you don’t need (but have already been installed in the environment).

These unnecessary libraries are output in the shell during the last compilation
For example:
Insert image description here

Insert image description here
Then use pyinstaller --clean -F XX.spec

Guess you like

Origin blog.csdn.net/weixin_53403301/article/details/134397208