|
下載 (3).png (89.45 KB, 下載次數(shù): 33)
下載附件
2022-12-2 19:49 上傳
簡(jiǎn)介:配合Python上位機(jī)獨(dú)立控制51單片機(jī)的4個(gè)GPIO端口的輸入輸出,在9600波特率下通過(guò)上位機(jī)控制MCU翻轉(zhuǎn)IO速率達(dá)到218Hz,可適用于低速情況下的自動(dòng)化測(cè)試場(chǎng)景。本項(xiàng)目?jī)H供學(xué)習(xí)娛樂(lè),可能存在未知問(wèn)題和疏漏,不建議用于真實(shí)場(chǎng)景,對(duì)此不負(fù)任何責(zé)任。
關(guān)于通信協(xié)議:
默認(rèn)波特率為9600,使用偶校驗(yàn)
數(shù)據(jù)位 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | 說(shuō)明 | P3端口
read = 0
write = 1 | P2端口
read = 0
write = 1 | P1端口
read = 0
write = 1 | P0端口
read = 0
write = 1 | 使能P3端口 | 使能P2端口 | 使能P1端口 | 使能P0端口 |
示例:
Snipaste_2022-12-02_19-47-34.png (6.05 KB, 下載次數(shù): 37)
下載附件
2022-12-2 19:47 上傳
Snipaste_2022-12-02_19-46-43.png (23.51 KB, 下載次數(shù): 39)
下載附件
2022-12-2 19:46 上傳
MCU代碼:
下載.png (94.06 KB, 下載次數(shù): 32)
下載附件
2022-12-2 19:48 上傳
Python上位機(jī)代碼:
下載 (2).png (91.04 KB, 下載次數(shù): 36)
下載附件
2022-12-2 19:48 上傳
下載 (1).png (7.88 KB, 下載次數(shù): 39)
下載附件
2022-12-2 19:48 上傳
- # TEST = 0x00
- # ERASE_EXTERNAL_FLASH_SECTOR = 0x06
- # WRITE_EXTERNAL_FLASH_SECTOR = 0x07
- # READ_SECTOR = 0x08
- # RESET = 0x04
- import serial
- PORT = 0b1111
- PORT_0 = 0b0001
- PORT_1 = 0b0010
- PORT_2 = 0b0100
- PORT_3 = 0b1000
- PORT_READ = 0b00000000
- PORT_WRITE = 0b11110000
- PORT_0_WRITE = 0b00010000
- PORT_1_WRITE = 0b00100000
- PORT_2_WRITE = 0b01000000
- PORT_3_WRITE = 0b10000000
- def GPIO_CTL(serial_f,PORT, OP, data = []):
- readSize = 0
- readSize += (PORT & PORT_0 > 0)
- readSize += (PORT & PORT_1 > 0)
- readSize += (PORT & PORT_2 > 0)
- readSize += (PORT & PORT_3 > 0)
- CMD = bytes([OP | PORT])
- if (OP & PORT_0_WRITE):
- CMD += data[0].to_bytes(1,byteorder="big",signed=False)
- readSize -=1
- if (OP & PORT_1_WRITE):
- CMD += data[1].to_bytes(1,byteorder="big",signed=False)
- readSize -=1
- if (OP & PORT_2_WRITE):
- CMD += data[2].to_bytes(1,byteorder="big",signed=False)
- readSize -=1
- if (OP & PORT_3_WRITE):
- CMD += data[3].to_bytes(1,byteorder="big",signed=False)
- readSize -=1
- serial_f.write(CMD)
- readDatas = serial_f.read(readSize)
- return list(readDatas)
- if __name__ == "__main__":
- #打開(kāi)串口
- try:
- myserial = serial.Serial()
- myserial.port = "COM34"
- myserial.baudrate = 9600
- myserial.timeout = 5
- myserial.parity = serial.PARITY_EVEN
- myserial.open()
- # time.sleep(1)
- except Exception as e:
- print("ERROR: ", e)
- exit()
- for i in range(256):
- write = [i, 255-i, 0, 0]
- GPIO_CTL(myserial, PORT_0 | PORT_1, PORT_0_WRITE | PORT_1_WRITE, write)
- read = GPIO_CTL(myserial, PORT_1 , PORT_READ)
- print('write',write,'read',read)
復(fù)制代碼
單片機(jī)源程序如下:
- #include "REG52.h"
- #include <stdio.h>
- #include "SysTimer.h"
- #include "serial.h"
- #include "binary.h"
- #include "typedef.h"
- #define GPIO_P0 P0
- #define GPIO_P1 P1
- #define GPIO_P2 P2
- #define GPIO_P3 P3
- void main(void)
- {
- uint8_t UART_data, cmd, rw, GPIO_Port,i;
- //初始化滴答定時(shí)器
- Timer0_Init();
- //初始化串口
- UART_Init();
- // SCON = 0xD0; //1101,0000 9 位可變波特率,偶校驗(yàn)位, 串口1模式3
- // printf("READY\n");
- while (1) {
- if (UART_stcRingBuf.u16UsedSize) {
- RingBufRead(&UART_stcRingBuf, &cmd);
- // bit 0: Port 0
- // bit 1: Port 1
- // bit 2: Port 2
- // bit 3: Port 3
- // bit 4: Port r/w
- // bit 5: Port r/w
- // bit 6: Port r/w
- // bit 7: Port r/w
- // rw = UART_data & B100;
- // GPIO_Port = UART_data & B11;
- //檢查協(xié)議頭
- // printf("raw:%d\trw:%d\tPort:%d\n",(int)UART_data,(int)rw,(int)GPIO_Port);
- for (i=B0001;i<=B1000;i<<=1) {
- //掃描端口
- GPIO_Port = cmd & i;
- if (!GPIO_Port) continue;
- rw = (cmd & (i<<4));
- // printf("raw:%X\trw:%d\tPort:%d\n",(int)cmd,(int)rw,(int)GPIO_Port);
- if (rw>0) {
- //寫(xiě)
- while(RingBufRead(&UART_stcRingBuf, &UART_data));
- // printf("write:%X\n", (int)UART_data);
- switch(GPIO_Port) {
- case B0001: GPIO_P0 = UART_data; break;
- case B0010: GPIO_P1 = UART_data; break;
- case B0100: GPIO_P2 = UART_data; break;
- case B1000: GPIO_P3 = (UART_data & B11111100) | B11; break;
- }
- }else{
- //讀
- // printf("read\n");
- switch(GPIO_Port) {
- case B0001: putchar(GPIO_P0); break;
- case B0010: putchar(GPIO_P1); break;
- case B0100: putchar(GPIO_P2); break;
- case B1000: putchar(GPIO_P3); break;
- }
- }
- }
- }
- }
- }
復(fù)制代碼
51hei.png (8 KB, 下載次數(shù): 41)
下載附件
2022-12-2 22:14 上傳
上圖文件下載:
C51_UART_EIO.zip
(24.26 KB, 下載次數(shù): 19)
2022-12-2 19:43 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|