【micropython】ESP32/ESP8266串口引脚重定向

当我们使用ESP32板件的时候,可能会使用多个串口,在micropython中,UART0用于REPL通讯,我们可以使用UART1和UART2。
在PINMAP中,UART1是引脚GPIO9和GPIO10,UART2是PIN16和PIN17
img

当我们直接使用ESP32的UART1时,会出现错误:
image-20210328204900378

短接16/17后,运行会出现错误:

image-20210328205154172

>>> uart1.write('a')
Guru Meditation Error: Core  0 panic'ed (IllegalInstruction). Exception was unhandled.
Memory dump at 0x401576fc: 0008e0d7 ffaf0000 ffffffff
Core  0 register dump:
PC      : 0x40157703  PS      : 0x00060031  A0      : 0x00000001  A1      : 0x3ffbe410  
A2      : 0x3ffbd6ac  A3      : 0x3ffbf774  A4      : 0x3ffbf770  A5      : 0x00000001  
A6      : 0x0000007f  A7      : 0x00000000  A8      : 0x80157700  A9      : 0x3ffbe3f0  
A10     : 0x3ffba780  A11     : 0x3ffba88c  A12     : 0x3ffbe424  A13     : 0x3ffba864  
A14     : 0x00000000  A15     : 0x3ffe5605  SAR     : 0x0000001f  EXCCAUSE: 0x00000000  
EXCVADDR: 0x00000000  LBEG    : 0x4000c349  LEND    : 0x4000c36b  LCOUNT  : 0xffffffff  

Backtrace:0x40157700:0x3ffbe410 0xfffffffe:0x3ffbe470 |<-CORRUPTED


ELF file SHA256: 56a3348b1921e984

Rebooting...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:4
load:0x3fff0034,len:5636
load:0x40078000,len:12696
load:0x40080400,len:4292
entry 0x400806b0
MicroPython v1.14 on 2021-03-28; ESP32 module with ESP32
Type "help()" for more information.

此时我们需要将UART1进行重定向

重定向

在ESP32中,可以将引脚进行重定向

UART.init(baudrate=9600, bits=8, parity=None, stop=1, *, …)

初始化函数具备两个参数

  • tx specifies the TX pin to use.
  • rx specifies the RX pin to use.

在初始化串口时,可以重新定向串口引脚位置,例如将UART1引脚重新定向至PIN32,PIN33

pin33:TX

pin32:RX

使用跳线短接PIN33,PIN32

初始化函数:

from machine import UART
uart1=UART(1)
uart1.init(115200,tx=32,rx=33)
uart1.write('abcde')
uart1.read()

运行结果:

>>> from machine import UART
>>> uart1=UART(1)
>>> uart1.init(115200,tx=32,rx=33)
>>> uart1.write('abcde')
5
>>> uart1.read()
b'abcde'
>>> 
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值