当前位置:   article > 正文

OpenMV与MSP430f5529串口通信(基于库函数发送多个数据)_msp430 openmv

msp430 openmv

OpenMV发送数据

  1. # Untitled - By: Administrator FK - 周日 7月 23 2023
  2. import sensor, image, time
  3. from pyb import UART
  4. uart = UART(3,9600) #定义串口3变量
  5. uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
  6. data = bytearray([0xFD,30,10,0xFE]) # 0xFD帧头,0xFE帧尾,中间30和10换成需要的变量如色块面积和坐标信息
  7. while(True):
  8. uart.write(data)

MSP430f5529uart初始化

清除接收中断标志位和使能接收中断函数要放在使能串口的下面,不然初始化后不能开启接收中断。

  1. #include "driverlib.h"
  2. #include "stdio.h"
  3. #include <string.h>
  4. #include <stdarg.h>
  5. #include <msp430.h>
  6. void uartinit0()
  7. {
  8. GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3,GPIO_PIN4);
  9. GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P3, GPIO_PIN3);
  10. USCI_A_UART_initParam param0 = {0};
  11. param0.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK;
  12. param0.clockPrescalar = 6;
  13. param0.firstModReg = 13;
  14. param0.secondModReg = 0;
  15. param0.parity = USCI_A_UART_NO_PARITY;
  16. param0.msborLsbFirst = USCI_A_UART_LSB_FIRST;
  17. param0.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;
  18. param0.uartMode = USCI_A_UART_MODE;
  19. param0.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;
  20. if (STATUS_FAIL == USCI_A_UART_init(USCI_A0_BASE, &param0)){
  21. return;
  22. }
  23. //Enable UART module for operation
  24. USCI_A_UART_enable(USCI_A0_BASE);
  25. USCI_A_UART_clearInterrupt(USCI_A0_BASE,USCI_A_UART_RECEIVE_INTERRUPT);
  26. USCI_A_UART_enableInterrupt (USCI_A0_BASE, USCI_A_UART_RECEIVE_INTERRUPT);
  27. }
  28. void uartinit1()
  29. {
  30. GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P4,GPIO_PIN5);
  31. GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P4, GPIO_PIN4);
  32. USCI_A_UART_initParam param1 = {0};
  33. param1.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK;
  34. param1.clockPrescalar = 6;
  35. param1.firstModReg = 13;
  36. param1.secondModReg = 0;
  37. param1.parity = USCI_A_UART_NO_PARITY;
  38. param1.msborLsbFirst = USCI_A_UART_LSB_FIRST;
  39. param1.numberofStopBits = USCI_A_UART_ONE_STOP_BIT;
  40. param1.uartMode = USCI_A_UART_MODE;
  41. param1.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION;
  42. if (STATUS_FAIL == USCI_A_UART_init(USCI_A1_BASE, &param1)){
  43. return;
  44. }
  45. //Enable UART module for operation
  46. USCI_A_UART_enable(USCI_A1_BASE);
  47. USCI_A_UART_clearInterrupt(USCI_A1_BASE,USCI_A_UART_RECEIVE_INTERRUPT);
  48. USCI_A_UART_enableInterrupt (USCI_A1_BASE, USCI_A_UART_RECEIVE_INTERRUPT);
  49. }
  50. void UART_printf(uint16_t baseAddress, const char *format,...)
  51. {
  52. uint32_t length;
  53. va_list args;
  54. uint32_t i;
  55. char TxBuffer[128] = {0};
  56. va_start(args, format);
  57. length = vsnprintf((char*)TxBuffer, sizeof(TxBuffer), (char*)format, args);
  58. va_end(args);
  59. for(i = 0; i < length; i++)
  60. USCI_A_UART_transmitData(baseAddress, TxBuffer[i]);
  61. }

主函数与多个数据的接收

将每次接收到的第一个数据赋值给变量area,第二个数据赋值给变量coor。

  1. int x ;
  2. static int data[10];
  3. static int i=0;
  4. static int f=0;
  5. static int area=0;
  6. static int coor=0;
  7. void main (void)
  8. {
  9. WDT_A_hold(WDT_A_BASE);
  10. uartinit1();
  11. uartinit0();
  12. __bis_SR_register(GIE);
  13. GPIO_setAsOutputPin (GPIO_PORT_P1,GPIO_PIN0);
  14. GPIO_setAsOutputPin (GPIO_PORT_P4,GPIO_PIN7);
  15. while(1)
  16. {
  17. UART_printf(USCI_A1_BASE, "%d,%d\r\n", area,coor);
  18. __delay_cycles(100000);
  19. }
  20. }
  21. #pragma vector=USCI_A1_VECTOR
  22. __interrupt void USCI_A1_ISR()
  23. {
  24. GPIO_toggleOutputOnPin (GPIO_PORT_P1, GPIO_PIN0);
  25. }
  26. #pragma vector=USCI_A0_VECTOR
  27. __interrupt void USCI_A0_ISR()
  28. {
  29. GPIO_toggleOutputOnPin (GPIO_PORT_P4, GPIO_PIN7);
  30. x=USCI_A_UART_receiveData (USCI_A0_BASE);
  31. if(x==0xFD)
  32. {
  33. f=1;
  34. }
  35. if(x==0xFE)
  36. {
  37. f=0;
  38. }
  39. if(f==1)
  40. {
  41. data[i]=x;
  42. i++;
  43. }
  44. if(f==0)
  45. {
  46. area=data[1];
  47. coor=data[2];
  48. i=0;
  49. }
  50. }

效果

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/399519
推荐阅读
相关标签
  

闽ICP备14008679号