2 MicroPython 介绍(二)

MicroPython 介绍(二)

   MicroPythonPython为基础,可以运行在嵌入式环境。提供交互式的prompt (REPL),我们可以使用REPL做一些Python编程操作,REPL支持历史命令,自动缩进,黏贴模式等,极大提高用户体验。

   MicroPython也致力于能够和正常的PythonCPython)兼容,这样就可以做到,学会 MicroPythonCPython其中任何一个,另一个也就会用了。

   MicroPython包含了CPython的一些核心包,例如os等,还有一些自己独有的包,例如用于驱动硬件的machine等。

例子(摘自官网)

点亮LED
import pyb

# turn on an LED
pyb.LED(1).on()

# print some text to the serial console
print('Hello MicroPython!')
引脚电平切换
from machine import Pin

# create an I/O pin in output mode
p = Pin('X1', Pin.OUT)

# toggle the pin
p.high()
p.low()
IIC
from machine import Pin, I2C

# creat an I2C bus
i2c = I2C(scl=Pin('X1'), sda=Pin('X2'))

# scan for list of attached devices
dev_list = i2c.scan()

# write to and read from a device
i2c.writeto(0x42, b'4')
data = i2c.readfrom(0x42, 4)

# memory transactions
i2c.writeto_mem(0x42, 0x12, b'')
data = i2c.readfrom_mem(0x42, 0x12, 2)
数值类型
# full range of numeric types

# small integer (fits in a machine word)
>>> 123
123

# big integer
>>> 1 << 160
1461501637330902918203684832716283019655932542976

# floating point
>>> 1.23e6
1230000.0

# complex numbers
>>> (1 + 2j) * 4j
(-8+4j)
内嵌汇编
# MicroPython has an inline assembler

import micropython

# define a Thumb-code inline-assembler function
@micropython.asm_thumb
def asm_add(r0, r1):
    add(r0, r0, r1)

# use it as a normal Python function
total = asm_add(1, 2)
OS操作
import os

# list root directory
print(os.listdir('/'))

# print current directory
print(os.getcwd())

# open and read a file from the SD card
with open('/sd/readme.txt') as f:
    print(f.read())
主板配置(频率)
import machine

# set CPU frequency to 84MHz
machine.freq(84000000)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cuntou0906

玛莎拉蒂是我的目标!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值