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

Software use case in micropython kernel development notebook: print based program debugging

編輯:Python

Jane Medium : This paper gives MicroPython Kernel Development Notes : Experimental tasks are embedded in the book Part of the fourth chapter of the software use case .

key word MicroPython,MM32F3277,print

The contents of the manuscript Objective record
Contents
Basic experiments Get user input The problem is solution control LED The lamp 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


One 、 Basic experiments

In embedded software development , download - test Pattern is a common software development method . First, the software is written from the simplest function , Then download the test , If the function is normal , Then continue to add software functions , The development of Zhidao software is completed . Use print The function outputs information at the key nodes of the software to judge whether the software is running normally , Common methods to check the running status of software . besides , You can also use other information output resources on the embedded development board , such as LED、LCD 、DAC Port, etc. to test whether the program runs normally .

stay MicroPython Software development , Use print, dir The command can help us view object properties and functions that can be used . such as :

from machine import Pin,UART
import machine
import time
print(Pin(1))
print(UART(1))
dir(machine)
dir(time)

Run the above code to get Pin(1),UART(1) Corresponding pin resource , get machine, time Functions that can be used in . The following is the output corresponding to the program operation .

Pin(PE3)
UART(1): baudrate=9600 on RX(PA3), TX(PA2)
['__name__', 'sleep', 'sleep_ms', 'sleep_us', 'ticks_add', 'ticks_cpu', 'ticks_diff', 'ticks_ms', 'ticks_us']
['__name__', 'ADC', 'DAC', 'I2C', 'PWM', 'Pin', 'SDCard', 'SPI', 'SoftI2C', 'SoftSPI', 'Timer', 'UART', 'freq', 'mem16', 'mem32', 'mem8', 'reset']

utilize print You can view the contents of variables and output calculation results . For example, the following applet can output a list a And print out Yang Hui's triangle array .

a = list(range(10))
print(a)
b = [1]
for i in range(10):
strout = ' '.join([str(s) for s in b])
print(' '*((32-len(strout))//2) + strout)
b = [1] + [b[n]+b[n+1] for n in range(len(b)-1)] + [1]

The results of the program are as follows :

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1

Two 、 Get user input

1、 The problem is

MicroPython utilize REPL Development mechanism , It also embodies the characteristics of this interactive development . Make good use of REPL Establish procedures and external exercises , It can speed up program development .

Program usage print Function can output information , But if you want users to pass REPL The input information requires another scheme .REPL Generally, the hardware of single chip microcomputer is used UART0, The most direct idea is through UART0 Read user through REPL Sent characters . But it's a pity. ,MicroPython Shielded UART0 Of read command .

For example, the following program demonstrates the passing UART0 The process of reading user input characters .

from machine import UART,Pin
import time
uart0 = UART(0,115200)
print(uart0)
dir(uart0)
while True:
if uart0.any() > 0:
inb = uart0.read(uart0.any())
print(inb)
time.sleep_ms(200)

The following is the output information during program execution . When the user REPL Type any character in the interactive window , Procedural judgment uart0 There are acceptable characters , Carry out the following read When the sentence is , You can see the emergence of OSError : UART(0) can’t read Error prompt for .

UART(0): baudrate=115200 on RX(PA10), TX(PA9)
['__next__', 'any', 'read', 'readinto', 'readline', 'write', 'PARITY_EVEN', 'PARITY_NONE', 'PARITY_ODD', 'init']
Traceback (most recent call last):
File "<stdin>", line 19, in <module>
OSError: UART(0) can't read
>>>

2、 solution

solve MicroPython The kernel prohibits reading UART0 The problem of , have access to MicroPython Memory direct access function in mem32,mem16,mem8. About MicroPython Direct memory access will be discussed in Chapter 17 later . The following is based on mem32 Read user REPL How to input information .

Used in the following program mem32 Direct access UART1_RDR, You can read the currently entered characters , And then use it init Function to re initialize the serial port . The main program calls procREPL function , Read user through REPL String sent by serial port , Know to get carriage return b‘\r’, Then use the received string function f(s) Finish processing .

from machine import UART,mem32
from micropython import const
APB2PERIPH_BASE = const(0x40010000)
UART1_BASE = const(APB2PERIPH_BASE + 0x3800)
UART1_RDR = const(UART1_BASE + 1*4)
UART1_CSR = const(UART1_BASE + 2*4)
uart0 = UART(0, 115200)
replbuf = bytes(0)
def procREPL(f):
global replbuf
if uart0.any() > 0:
replbuf += bytes([mem32[UART1_RDR]])
uart0.init(115200)
if replbuf[-1:] == b'\r':
f(replbuf[:-1])
replbuf = bytes(0)
def f(s):
print(s)
while True:
procREPL(f)

After program running , stay REPL Enter any string in the input window , Press the Enter key , The program will display the string just entered by the user .

b'Test REPL'
b'Hello world.'

Based on this program framework , Many interactive program applications can be developed .

3、 control LED The lamp

Modify the middle part of the above interactive code , Users can pass REPL Enter the command , The dynamic change PLUS-F3270 On the board Tricolor LED Color .

Code modifications include :

  • Added three LED Control port ;
  • In function f(s) in , Set three... According to the received string corresponding to three digits LED The state of ;
led1 = Pin('PA1', Pin.OUT_PUSHPULL, value=1)
led2 = Pin('PA2', Pin.OUT_PUSHPULL, value=1)
led3 = Pin('PA3', Pin.OUT_PUSHPULL, value =1)
def f(s):
sn = int(s)
if sn%10 == 1: led1(0)
else: led1(1)
sn //= 10
if sn%10 == 1: led2(0)
else: led2(1)
sn //= 10
if sn%10 == 1: led3(0)
else: led3(1)

After program running , The user inputs three numbers to control three LED On state . 111 Indicates that all lights up ;000 It means all are off ;001 Indicates that the first... Is lit LED, Other functions are similar .

 

※ total junction ※


This paper gives MicroPython Kernel Development Notes : Experimental tasks are embedded in the book Part of the fourth chapter of the software use case .

Here is the complete code of the last test program .

from machine import UART,mem32,Pin
from micropython import const
APB2PERIPH_BASE = const(0x40010000)
UART1_BASE = const(APB2PERIPH_BASE + 0x3800)
UART1_RDR = const(UART1_BASE + 1*4)
UART1_CSR = const(UART1_BASE + 2*4)
uart0 = UART(0, 115200)
replbuf = bytes(0)
def procREPL(f):
global replbuf
if uart0.any() > 0:
replbuf += bytes([mem32[UART1_RDR]])
uart0.init(115200)
if replbuf[-1:] == b'\r':
f(replbuf[:-1])
replbuf = bytes(0)
led1 = Pin('PA1', Pin.OUT_PUSHPULL, value=1)
led2 = Pin('PA2', Pin.OUT_PUSHPULL, value=1)
led3 = Pin('PA3', Pin.OUT_PUSHPULL, value =1)
def f(s):
sn = int(s)
if sn%10 == 1: led1(0)
else: led1(1)
sn //= 10
if sn%10 == 1: led2(0)
else: led2(1)
sn //= 10
if sn%10 == 1: led3(0)
else: led3(1)
while True:
procREPL(f)


■ Links to related literature :

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

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