PYB操作 UART操作

2021-10-01 14:43:51 字數 2304 閱讀 1487

uart是universal asynchronous receiver/transmitter(通用非同步收發傳輸器)的簡稱,在微控制器和嵌入式系統中,串列埠(uart)一直都是非常重要的外設。雖然串列埠的速度並不快,但是因為它使用簡單(串列埠可能是兩個晶元之間傳輸資料最簡單的方式),占用軟體和硬體資源少,所以在通訊、控制、資料傳輸、**除錯等許多方面有非常廣泛應用。很多裝置或者模組甚至都會提供專用的串列埠介面用於通訊和控制,如gprs模組、藍芽/wifi透傳模組等。uart使用乙個gpio做傳送,乙個gpio做接收,沒有單獨的時鐘訊號。收發雙方需要先約定好相同的

波特率、資料位、校驗位、停止位等引數才能正常通訊,所以也叫做異

步序列匯流排。

在micropython中,操作串列埠和使用gpio一樣簡單。

putty、pyb nano

串列埠的基本使用方式是,首先定義使用的串列埠,再設定串列埠引數(最主要就是設定波特率),然後通過 write()函式傳送緩衝區或者字串。或者用 any()函式判斷是否接收到資料,再用read()函式讀取資料。

uart的函式如下:

(1)class pyb.uart(bus,…)

bus:1-6,或者『xa』,『xb』,『ya』,『yb』。

the physical pins of the uart busses are:

uart(4) is on xa: (tx, rx) = (x1, x2) = (pa0, pa1)

uart(1) is on xb: (tx, rx) = (x9, x10) = (pb6, pb7)

uart(6) is on ya: (tx, rx) = (y1, y2) = (pc6, pc7)

uart(3) is on yb: (tx, rx) = (y9, y10) = (pb10, pb11)

uart(2) is on: (tx, rx) = (x3, x4) = (pa2, pa3)

class pyb.uart(bus, …)

bus: 1-6,或者『xa』,『xb』,『ya』,『yb』.

uart.init(baudrate, bits=8, parity=none, stop=1, *,timeout=1000,flow=none, timeout_char=0,read_buf_len=64),初始化

baudrate:波特率

bits:資料位,7/8/9

parity:校驗,none, 0 (even) or 1 (odd)

stop:停止位,1/2

flow:流控,可以是 none, uart.rts, uart.cts or uart.rts | uart.cts

timeout:讀取乙個位元組超時時間(ms)

timeout_char:兩個位元組之間超時時間

read_buf_len:讀快取長度

uart.deinit():關閉串列埠

uart.any():返回緩衝區資料個數,大於0代表有資料

uart.writechar(char):寫入乙個位元組

uart.read([nbytes]):讀取最多nbytes個位元組。如果資料位是9bit,那麼乙個資料占用兩個位元組,並且nbytes必須是偶數

uart.readall():讀取所有資料

uart.readchar():讀取乙個位元組

uart.readinto(buf[, nbytes])

buf:資料緩衝區

nbytes:最大讀取數量

uart.readline():讀取一行

uart.write(buf):寫入緩衝區。在9bits模式下,兩個位元組算乙個資料

uart.sendbreak():往匯流排上傳送停止狀態,拉低匯流排13bit時間

from pyb import uart

u1 = uart(2,

9600

)#設定波特率和串口號

u1.writechar(97)

#傳送單個字元a

u1.write(

'123'

)#傳送字串

from pyb import uart

u1 = uart(2,

9600

)#設定波特率和串口號

u1.writechar(97)

#傳送單個字元a

u1.write(

'123'

)#傳送字串

while(1

):buf=u1.readchar(

)#從緩衝區讀取乙個字元

if(buf==97)

:print

("pass"

)

PYB操作 ADC操作

1.配置pyb內部adc 2.配置pyb內部dac 這裡由於nano沒有dac這裡就不貼出 操作了 1.putty 2.pyb nano adc 模擬訊號轉換為數字量 是嵌入式中最常用的功能之一,在micropython 同樣也提供了相應的函式,可以直接讀取 adc 轉換的結果。在pyboard中,...

只用 操作實現 , , 操作

減法 a b a b 乘法 a b相當於將a累加abs b 次 除法 相當於將abs a 減去 abs b 的次數 求lhs的反面值rhs,即求rhs使得lhs rhs 0 int negate int lhs return ret int abs int lhs 返回i的絕對值 lhs rhs l...

Habse API操作(刪除操作)

根據rowkey刪除整行的所有列族 所有行 所有版本 public static void deleterow string tablename,string rowkey throws ioexception 刪除某個row的指定列 public static void deletecol str...