This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Tool/software: Code Composer Studio
Dear team,
The data returned by the sensor is a group of 8-byte data, but I only got the last byte of data through interruption. How can I solve this situation?
The problem now is that when the serial port receives 8 bytes of data, it will be lost. Normally, the lengt value should be 8 every time, but it is not every time during the test, sometimes 4 times and sometimes 6 times, and it is always impossible to receive all stable data.
#include <msp430.h> #include "driverlib/MSP430F5xx_6xx/driverlib.h" #include <stdint.h> #include <stdbool.h> #include <string.h> #include <stdio.h> extern int TVOC[9]; int flag = 0; char fff[1]; int RxBuf[20]; static int lengt = 0; float TVOC0; char TVOC[9]; uint8_t TVOCSend[9]={0xFF,0x00,0x86,0x00,0x00,0x00,0x00,0x00,0x7A}; //用于查询空气质量传感器的命令 int TVOCtest[9]; //用于接收空气质量传递过来的数据 int main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT UART0_Init(); UART2_Init(); while (getTVOC() == false)//让空气质量传感器能够正常地采集数据 { mDelay(1000); } while(1) { getTVOC(); mDelay(1000); } } void UART0_Init() { //P3.4,5 = USCI_A2 TXD/RXD GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3,GPIO_PIN4 + GPIO_PIN5); //Baudrate = 9600, clock freq = 1.048MHz //UCBRx = 109, UCBRFx = 0, UCBRSx = 2, UCOS16 = 0 USCI_A_UART_initParam param = {0}; param.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK; param.clockPrescalar = 109; param.firstModReg = 0; param.secondModReg = 2; param.parity = USCI_A_UART_NO_PARITY; param.msborLsbFirst = USCI_A_UART_LSB_FIRST; param.numberofStopBits = USCI_A_UART_ONE_STOP_BIT; param.uartMode = USCI_A_UART_MODE; param.overSampling = USCI_A_UART_LOW_FREQUENCY_BAUDRATE_GENERATION; //param.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION; if (STATUS_FAIL == USCI_A_UART_init(USCI_A0_BASE, ¶m)) { return; } //Enable UART module for operation USCI_A_UART_enable(USCI_A0_BASE); //Enable Receive Interrupt USCI_A_UART_clearInterrupt(USCI_A0_BASE,USCI_A_UART_RECEIVE_INTERRUPT); USCI_A_UART_enableInterrupt(USCI_A0_BASE,USCI_A_UART_RECEIVE_INTERRUPT); __enable_interrupt(); } #pragma vector=USCI_A0_VECTOR __interrupt void USCI_A0_ISR(void) { switch (__even_in_range(UCA0IV,4)) { //Vector 2 - RXIFG case 2: //RxBuf[lengt++]=USCI_A_UART_receiveData(USCI_A0_BASE); RxBuf[lengt++]=UCA0RXBUF; break; default: break; } } int getTVOC() { uint8_t check=0; for(lengt=0;lengt<9;lengt++) { RxBuf[lengt] = 0x00; } lengt = 0; UARTSend0(TVOCSend,9); mDelay(100); lengt = 0; if(RxBuf[0] == 0xFF) { int i; for(i=0;i<9;i++) { TVOCtest[i] = RxBuf[i]; RxBuf[i] = 0x00; } if(CheckSum(TVOCtest) == true&&TVOCtest[1] == 0x86) { TVOC0=((TVOCtest[2]<<8)+TVOCtest[3]); sprintf(TVOC,"%.2f",TVOC0); return true; } else return false; } else return false; }
This code appears to wait for however long "mDelay(100)" takes, and considers the transaction (reception) done. Are you fairly certain the other side will send all the data in that time?
Maybe it would be more useful to watch "lengt" and when it reaches 8 then consider the transaction done. (You could have a timeout as well.)
Thanks for your reply.
In the current debugging process, the customer found that all data has been returned when sending the "UARTSend0 (TVOCSend, 9)" command. It is received through the serial port of MSP430, and the received data is confused and inconsistent with the actual data.
After each reception, he also checked the lengt. The lengt often does not match the number of bytes that should have been received, more or less. He suspected that it was a problem with the sensor, but when tested with other types of microcontrollers such as CC3200, the sensor was normal.
>
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3,GPIO_PIN4 + GPIO_PIN5);
As I read data sheet (SLAS590N) Table 6-48, P3.5 isn't UCA0TXD, rather P3.3 is. Since this would mean the sensor would never see anything from the host (MSP) it's not obvious how to tie this to your symptom, but maybe it sends things sporadically on its own?
Anyway, try:
>
GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P3,GPIO_PIN4 + GPIO_PIN3);
**Attention** This is a public forum