Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use micropython

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces "how to use micropython" in detail, the content is detailed, the steps are clear, and the details are handled properly. I hope this "how to use micropython" article can help you solve your doubts.

Micro Python is a concise and fast implementation of the Python 3 programming language, designed to run optimally on a microcontroller. It is based on ANSIC, has the same syntax as Pyton 3, and has its own parser, compiler, virtual machine, class library, and so on. Here are some common development boards supported by MicroPython:

1 、 Pyboard

2 、 Pymagic

3 、 TPYBoardV102

TPYBoard single-chip microcomputer development board, the existing TPYBoardV101, v102 two versions, of which v102 is the upgraded version of v101, adding swd interface. Using python development language, provides 30 GPIO, easy to use python to develop Internet of things products. Support Python3.0 and above version of direct operation, support gravity accelerometer, support hundreds of peripheral accessories.

0x03 uses TPYBoardV102 as an example to explain how to use micropython.

Let's take the TPYBoardV102 development board as an example to explain in detail how to use micropython. For example, if you want to run the first script now, you only need to do a few steps on TPYBoardV102:

Step 1: plug the TPYBoardV102 into the USB cable to connect to the computer, and then a USB disk named TPFLASH will pop up on the computer, as shown below:

The space is a little small, only 95KB, you read it right, not 95MB, only 95KB. This is because the flash space built into the STM32 chip is now used, so it is only this large, but it is enough to write N python script files so small. Moreover, I will tell you that after inserting the TF card, it can be expanded to 4 gigabytes.

Step 2: open the flash drive and open it with the following four files:

Main.py is the boot auto-running file, in which you can put your own code; boot.py is the boot file, which loads the virtual serial port driver file under main.py;pybcdc.inf for windows; README.txt contains the necessary basic information to open the development board.

Step 3: edit mian.py:

Now we can start writing our own python programs. Open the main.py file with a text editor. Notepad or other editors can be used in the Windows environment. Just use your favorite text editor under Mac and linux. When you open the file, you will see the following line:

The line starts with the # character, which means it's just a comment. Such a command line will not be executed, only providing information for the code to be used in the main.py plus two more lines, as shown below:

The first line indicates the use of the pyb module, which contains all the functions and classes that control the development board.

The second line opens the blue LED: first you use the LED class in the pyb module, create an instance of LED 4, and then light it up

Step 4: reset the development board

To run this little script, we need to save and close the main.py file, and then exit (or uninstall) from the USB device, just like exiting the removable disk.

When the device securely exits or unmounts, it comes to the place where the function is realized: pressing the reset key on the board will reset the development board and run the written program. The black reset button is located on the right, under the USB interface. When the reset button is pressed, the green LED flashes quickly, and then the blue LED stays bright.

A practical case of 0x04 TPYBoard single Chip Microcomputer Development Board

Example 1: light up LEDS

The easiest thing to do on the pyboard board is to light the small lights attached to the board. Connect the development board, follow the login method mentioned in the previous tutorial, and start lighting the LED light in the interpreter, the code is as follows:

Myled = pyb.LED (1) myled.on () myled.off ()

These commands will control whether the LED is on or off.

This is a good approach, but we will try to make it smarter. Open the MAIN.PY file in pyboard in your good text editor and write or paste the following code. If you are a novice to python, then I hope you can have a correct impression of python from now on.

Led = pyb.LED (2) while True: led.toggle () pyb.delay (1000)

When you have saved the file, press the RST key and the green LED on the pyboard will continue to flash.

Example 2: TPYboard controls LCD5110 to display 6x8 characters

Let's take a look at the meaning of LCD5110 stitches (note: LCD5110 stitches are a little different)

The pins of TPYBoard correspond to the pins of 5110 as follows:

TPYBoard LCD5110 memo

-

# any Pin = > RST Reset pin (0=reset, 1=normal)

# any Pin = > CE Chip Enable (0=listen for input, 1=ignore input)

# any Pin = > DC Data/Command (0=commands, 1=data)

# MOSI = > DIN data flow (Master out, Slave in)

# SCK = > CLK SPI clock

# 3V3 or any Pin = > VCC 3.3V logic voltage (0=off, 1=on)

# any Pin = > LIGHT Light (0=on, 1=off)

# GND = > GND

If you still don't understand, just put the pin number on it.

TPYBoard LCD5110 memo

-

Y10 = > RST Reset pin (0=reset, 1=normal)

Y11 = > CE Chip Enable (0=listen for input,1=ignore input)

Y9 = > DC Data/Command (0=commands, 1=data)

X8 = > DIN data flow (Master out, Slave in)

X6 = > CLK SPI clock

VCC

Y12 = > LIGHT Light (0=on, 1=off)

GND

After connecting the ok, you can run main.py.

Main.py source code:

# main.pyimport pybimport upcd8544from machine import SPI,Pin def main (): SPI = pyb.SPI (1) # DIN= > X8 Mosi data flow CLK = > X6-SCK # DIN= > SPI (1). MOSI 'X8' data flow (Master out) Slave in) # CLK = > SPI (1). SCK 'X6' SPI clock RST = pyb.Pin (' Y10') CE = pyb.Pin ('Y11') DC = pyb.Pin (' Y9') LIGHT = pyb.Pin ('Y12') lcd_5110 = upcd8544.PCD8544 (SPI, RST, CE, DC, LIGHT) lcd_5110.lcd_write_string 6 TPYBoard',12,2 1) lcd_5110.lcd_write_string ('TPYBoard',12,2) lcd_5110.lcd_write_string (' v102 This is a test of LCD5110',0,4) if _ _ name__ = ='_ _ main__': main () read here This article "how to use micropython" has been introduced, and if you want to master the knowledge points of this article, you still need to practice and use it. If you want to know more about the article, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report