【测评SGP40】+UART通信+at32f407开发板使用
[复制链接]
本帖最后由 damiaa 于 2021-2-17 21:43 编辑
【测评SGP40】+UART通信+at32f407开发板使用
1,准备at32f407开发板1块,连接它的串口2和传感器( P0.5 == 传感器PIN3 P0.6 == 传感器PIN4 ,传感器的VCC PIN1和地 PIN2 接到开发板的VCC和地)
at32f407 VCC to 传感器的VCC PIN1
at32f407 地 to 传感器的地 PIN2
USART2 Tx pin (PD.05) to 传感器PIN3
USART2 Rx pin (PD.06) to 传感器PIN4
2,准备程序:
- 定义了一个串口收发数据的结构体
- typedef struct
- {
- uint8_t TxBuffer[200];
- uint8_t RxBuffer[200];
- uint16_t NbrOfDataToTransfer;
- uint16_t NbrOfDataToRead;
- uint16_t TxCounter;
- uint16_t RxCounterIn;
- uint16_t RxCounterOut;
- }TXDEF;
- 主程序中定义TXDEF tx1,tx2,tx3;
初始化串口1,2,3未用到。
- int main(void)
- {
-
- RCC_Configuration();
-
- NVIC_Configuration();
-
- GPIO_Configuration();
-
- AT32_Board_Init();
- USART_StructInit(&USART_InitStructure);
- USART_InitStructure.USART_BaudRate = 115200;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
-
- USART_Init(USART1, &USART_InitStructure);
-
- USART_Init(USART2, &USART_InitStructure);
-
- USART_Init(USART3, &USART_InitStructure);
-
- USART_INTConfig(USART1, USART_INT_RDNE, ENABLE);
-
-
- USART_INTConfig(USART2, USART_INT_RDNE, ENABLE);
- USART_INTConfig(USART2, USART_INT_TDE, ENABLE);
-
- USART_INTConfig(USART3, USART_INT_RDNE, ENABLE);
- USART_INTConfig(USART3, USART_INT_TDE, ENABLE);
- USART_Cmd(USART1, ENABLE);
- USART_Cmd(USART2, ENABLE);
- USART_Cmd(USART3, ENABLE);
-
中断里发送和接收处理。
- void USART1_IRQHandler(void)
- {
- if(USART_GetITStatus(USART1, USART_INT_RDNE) != RESET)
- {
- /* Read one byte from the receive data register */
- tx1.RxBuffer[tx1.RxCounterIn++] = (USART_ReceiveData(USART1) & 0x7F);
- if(tx1.RxCounterIn == tx1.NbrOfDataToRead)
- {
- tx1.RxCounterIn =0;
- }
- }
- if(USART_GetITStatus(USART1, USART_INT_TDE) != RESET)
- {
- /* Write one byte to the transmit data register
- USART_SendData(USART1, tx1.TxBuffer[tx1.TxCounter++]);
- if(tx1.TxCounter == tx1.NbrOfDataToTransfer)
- {
- /* Disable the EVAL_COM1 Transmit interrupt */
- USART_INTConfig(USART1, USART_INT_TDE, DISABLE);
- }
- }
- }
- void USART2_IRQHandler(void)
- {
- if(USART_GetITStatus(USART2, USART_INT_RDNE) != RESET)
- {
- /* Read one byte from the receive data register */
- tx2.RxBuffer[tx2.RxCounterIn++] = USART_ReceiveData(USART2);
- if(tx2.RxCounterIn == tx2.NbrOfDataToRead)
- {
- tx2.RxCounterIn =0;
- }
- }
- if(USART_GetITStatus(USART2, USART_INT_TDE) != RESET)
- {
- /* Write one byte to the transmit data register */
- USART_SendData(USART2, tx2.TxBuffer[tx2.TxCounter++]);
- if(tx2.TxCounter == tx2.NbrOfDataToTransfer)
- {
- /* Disable the USART2 Transmit interrupt */
- USART_INTConfig(USART2, USART_INT_TDE, DISABLE);
- }
- }
- }
-
主程序里发送开启测量命令一次,
svm40_start_measurement 开启测量
每次读都发送 svm40_get_signals 命令
-
- svm40_start_measurement();
- delayms(500);
- static uint8_t cont=0;
- static uint16_t tempH,tempL,temp, humiH,humiL,humi,voc;
- uint8_t buf_T[]={"Temperature is :\n\r"};
- uint8_t buf_H[]={"Humidity is :\n\r"};
- uint8_t buf_V[]={"Noxious gas is :\n\r"};
- uint8_t buf_K[]={"\n\r"};
- uint8_t buf1[200];
- //uint8_t buf2[200];
- while(1)
- {
- delayms(1000);
- tx2.RxCounterIn = tx2.RxCounterOut=0;
- read_svm40(&voc,&humi,&temp);
- humiH = humi / 100;
- humiL = humi %100;
- tempH =temp / 100;
- tempL =temp % 100;
- tx_send(&tx1,buf_T,sizeof(buf_T));
- delayms(10);
- sprintf(buf1,"%2d.%2d\n\r",tempH,tempL);
- tx_send(&tx1,buf1,10);
- delayms(10);
- tx_send(&tx1,buf_H,sizeof(buf_H));
- delayms(10);
- sprintf(buf1,"%2d.%2d\n\r",humiH,humiL);
- tx_send(&tx1,buf1,10);
- delayms(10);
- tx_send(&tx1,buf_V,sizeof(buf_V));
- delayms(10);
- sprintf(buf1,"%4d \n\r",voc);
- tx_send(&tx1,buf1,10);
- delayms(10);
- tx_send(&tx1,buf_K,sizeof(buf_K));
- delayms(10);
- tx_send(&tx1,buf_K,sizeof(buf_K));
- delayms(10);
- tx_send(&tx1,buf_K,sizeof(buf_K));
- delayms(10);
- }
- void read_svm40(uint16_t *pvoc,uint16_t *phumi,uint16_t *ptemp)
- {
- uint8_t buf2[200];
- svm40_get_signals();
- delayms(200);
- uint8_t cont=0;
- //read SWVM40
- //0x7E 0x00 0x03 0x00 0x06 0x00 0x00 0x00 0x00 0x00 0x00 0xF6 0x7E
- while(tx2.RxCounterIn != tx2.RxCounterOut)
- {
- buf2[cont++] = tx2.RxBuffer[tx2.RxCounterOut++];
- }
- // buf2[5] buf2[6]
- //int16 provides the VOC Index (no unit) with a scaling factor of 10, e.g., an output of +250 corresponds to a VOC Index of +25.0.
- *pvoc = buf2[5];
- *pvoc <<=8;
- *pvoc += buf2[6];
- *pvoc =*pvoc / 10;
- //buf2[7] buf2[8] int16 provides the relative humidity (in % RH) compensated for the temperature offset with a scaling factor of 100, e.g., an output of +2500 corresponds to +25.00 % RH.
- *phumi = buf2[7];
- *phumi <<=8;
- *phumi += buf2[8];
- //buf2[9] buf2[10] int16 provides the temperature (in °C) with a scaling factor of 200, e.g., an output of +5’000 corresponds to +25.00 C
- *ptemp = buf2[9];
- *ptemp <<=8;
- *ptemp += buf2[10];
- *ptemp = *ptemp/2;
- }
- void tx_send(TXDEF *tx,uint8_t *ptxbuf,uint8_t num)
- {
- for(uint8_t i=0;i<num;i++)
- tx->TxBuffer =*(ptxbuf+i);
- tx->TxCounter =0;
- tx->NbrOfDataToTransfer =num;
- if(tx == &tx1)
- USART_INTConfig(USART1, USART_INT_TDE, ENABLE);
- if(tx == &tx2)
- USART_INTConfig(USART2, USART_INT_TDE, ENABLE);
- if(tx == &tx3)
- USART_INTConfig(USART3, USART_INT_TDE, ENABLE);
- }
-
- //Start Measurement svm40_start_measurement
- //This command triggers the operation mode of all sensors. It must be called once prior to the
- //svm40_get_signals or svm40_get_raw_signals commands, respectively. Per default the SVM40 starts in idle
- //mode after powering up.
- // 0x7E 0x00 0x00 0x01 0x00 0xFE 0x7E
- // 0x7E 0x00 0x00 0x00 0x00 0xFF 0x7E
- void svm40_start_measurement(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x00, 0x01, 0x00, 0xFE, 0x7E};
- tx_send(&tx2,buf2,sizeof(buf2));
- }
- //Get Signals
- //this command reads out VOC Index, relative humidity, and temperature. It returns 6 bytes.
- //0, 1 int16 provides the VOC Index (no unit) with a scaling factor of 10, e.g., an output of +250
- //corresponds to a VOC Index of +25.0.
- //2, 3 int16 provides the relative humidity (in % RH) compensated for the temperature offset with a
- //scaling factor of 100, e.g., an output of +2’500 corresponds to +25.00 % RH.
- //4, 5 int16 provides the temperature (in °C) with a scaling factor of 200, e.g., an output of +5’000
- //corresponds to +25.00 °C.
- //0x7E 0x00 0x03 0x01 0x0A 0xF1 0x7E
- //0x7E 0x00 0x03 0x00 0x06 0x00 0x00 0x00 0x00 0x00 0x00 0xF6 0x7E
- void svm40_get_signals(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x03, 0x01, 0x0A, 0xF1, 0x7E};
- tx_send(&tx2,buf2,sizeof(buf2));
- }
编译运行连接putty查看结果:
效果还行。
|