赞
踩
- # Untitled - By: Administrator FK - 周日 7月 23 2023
-
- import sensor, image, time
- from pyb import UART
-
- uart = UART(3,9600) #定义串口3变量
- uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
-
- data = bytearray([0xFD,30,10,0xFE]) # 0xFD帧头,0xFE帧尾,中间30和10换成需要的变量如色块面积和坐标信息
-
- while(True):
- uart.write(data)
清除接收中断标志位和使能接收中断函数要放在使能串口的下面,不然初始化后不能开启接收中断。
- #include "driverlib.h"
- #include "stdio.h"
- #include <string.h>
- #include <stdarg.h>
- #include <msp430.h>
-
- void uartinit0()
- {
- GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3,GPIO_PIN4);
- GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P3, GPIO_PIN3);
- USCI_A_UART_initParam param0 = {0};
- param0.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK;
- param0.clockPrescalar = 6;
- param0.firstModReg = 13;
- param0.secondModReg = 0;
- param0.parity = USCI_A_UART_NO_PARITY;
- param0.msborLsbFirst = USCI_A_UART_LSB_FIRST;
- param0.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;
- param0.uartMode = USCI_A_UART_MODE;
- param0.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;
- if (STATUS_FAIL == USCI_A_UART_init(USCI_A0_BASE, ¶m0)){
- return;
- }
- //Enable UART module for operation
- USCI_A_UART_enable(USCI_A0_BASE);
- USCI_A_UART_clearInterrupt(USCI_A0_BASE,USCI_A_UART_RECEIVE_INTERRUPT);
- USCI_A_UART_enableInterrupt (USCI_A0_BASE, USCI_A_UART_RECEIVE_INTERRUPT);
-
-
- }
- void uartinit1()
- {
-
- GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P4,GPIO_PIN5);
- GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P4, GPIO_PIN4);
- USCI_A_UART_initParam param1 = {0};
- param1.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK;
- param1.clockPrescalar = 6;
- param1.firstModReg = 13;
- param1.secondModReg = 0;
- param1.parity = USCI_A_UART_NO_PARITY;
- param1.msborLsbFirst = USCI_A_UART_LSB_FIRST;
- param1.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;
- param1.uartMode = USCI_A_UART_MODE;
- param1.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;
- if (STATUS_FAIL == USCI_A_UART_init(USCI_A1_BASE, ¶m1)){
- return;
- }
- //Enable UART module for operation
- USCI_A_UART_enable(USCI_A1_BASE);
-
- USCI_A_UART_clearInterrupt(USCI_A1_BASE,USCI_A_UART_RECEIVE_INTERRUPT);
- USCI_A_UART_enableInterrupt (USCI_A1_BASE, USCI_A_UART_RECEIVE_INTERRUPT);
-
-
-
- }
- void UART_printf(uint16_t baseAddress, const char *format,...)
- {
- uint32_t length;
- va_list args;
- uint32_t i;
- char TxBuffer[128] = {0};
-
- va_start(args, format);
- length = vsnprintf((char*)TxBuffer, sizeof(TxBuffer), (char*)format, args);
- va_end(args);
-
- for(i = 0; i < length; i++)
- USCI_A_UART_transmitData(baseAddress, TxBuffer[i]);
- }
将每次接收到的第一个数据赋值给变量area,第二个数据赋值给变量coor。
- int x ;
- static int data[10];
- static int i=0;
- static int f=0;
- static int area=0;
- static int coor=0;
-
- void main (void)
- {
- WDT_A_hold(WDT_A_BASE);
- uartinit1();
- uartinit0();
- __bis_SR_register(GIE);
- GPIO_setAsOutputPin (GPIO_PORT_P1,GPIO_PIN0);
- GPIO_setAsOutputPin (GPIO_PORT_P4,GPIO_PIN7);
- while(1)
- {
- UART_printf(USCI_A1_BASE, "%d,%d\r\n", area,coor);
- __delay_cycles(100000);
- }
- }
-
-
-
- #pragma vector=USCI_A1_VECTOR
- __interrupt void USCI_A1_ISR()
- {
- GPIO_toggleOutputOnPin (GPIO_PORT_P1, GPIO_PIN0);
- }
-
- #pragma vector=USCI_A0_VECTOR
- __interrupt void USCI_A0_ISR()
- {
- GPIO_toggleOutputOnPin (GPIO_PORT_P4, GPIO_PIN7);
- x=USCI_A_UART_receiveData (USCI_A0_BASE);
- if(x==0xFD)
- {
- f=1;
- }
- if(x==0xFE)
- {
- f=0;
- }
- if(f==1)
- {
- data[i]=x;
- i++;
- }
- if(f==0)
- {
- area=data[1];
- coor=data[2];
- i=0;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。