- #include "stm32g4xx_hal.h"
- #include "stm32g4xx_hal_uart.h"
- extern UART_HandleTypeDef huart3;
- void svm40_start_measurement(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x00, 0x01, 0x00, 0xFE, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_get_signals(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x03, 0x01, 0x0A, 0xF1, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_get_raw_signals(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x03, 0x01, 0x0B, 0xF0, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_stop_measurement(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x01, 0x00, 0xFE, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_get_temperature_offset(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x60, 0x01, 0x01, 0x9D, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_set_temperature_offset(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x60, 0x05, 0x81, 0x00, 0x00, 0x00, 0x00, 0x19, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- //MOSI 0x7E 0x00 0x60 0x01 0x08 0x96 0x7E
- //MISO 0x7E 0x00 0x60 0x00 0x08 0x00 0x64 0x00 0x0C 0x00 0xB4 0x00 0x32 0x41 0x7E
- void svm40_get_voc_parameters(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x60, 0x01, 0x08, 0x96, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_set_voc_parameters(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x60, 0x09, 0x88, 0x00, 0x64, 0x00, 0x0C, 0x00, 0xB4, 0x00, 0x32, 0xB8, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_store_input_parameters(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x60, 0x01, 0x80, 0x1E, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_get_voc_states(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x61, 0x01, 0x08, 0x95, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_set_voc_states(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x61, 0x09, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0xDB, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_get_version(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0xD1, 0x00, 0x2E, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_device_reset(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0xD3, 0x00, 0x2C, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
9,main.c修改- #include "main.h"
- #include "SVM40.h"
- I2C_HandleTypeDef hi2c1;
- UART_HandleTypeDef hlpuart1;
- UART_HandleTypeDef huart4;
- UART_HandleTypeDef huart3;
- TIM_HandleTypeDef htim3;
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_I2C1_Init(void);
- static void MX_LPUART1_UART_Init(void);
- static void MX_UART4_Init(void);
- static void MX_USART3_UART_Init(void);
- static void MX_TIM3_Init(void);
- uint8_t uart3sv[4],uart4sv[4];
- uint8_t uart3buf[100],uart4buf[100];
- static uint8_t p_uart3=0;//,p_uart4=0;
- uint32_t volatile soft_tim1=0,soft_tim2=0;
- void delayms(uint32_t x)
- {
- for(uint32_t i=0;i<x;i++)
- {
- for(uint32_t j=0;j<50000;j++){};
- }
- }
- void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
- {
- if(htim == &htim3)
- {
- if((soft_tim1)>1000000) soft_tim1 =0;
- else soft_tim1++;
- if((soft_tim2)>1000000) soft_tim2 =0;
- else soft_tim2++;
- }
- }
- void read_svm40(uint16_t *pvoc,uint16_t *phumi,uint16_t *ptemp)
- {
- uint8_t buf2[200];
- svm40_get_signals();
- delayms(200);
- if(p_uart3 >=13)
- {
- for(uint8_t i=0;i<13;i++)
- buf2[i]=uart3buf[i];
- }
- p_uart3=0;
- *pvoc = buf2[5];
- *pvoc <<=8;
- *pvoc += buf2[6];
- *pvoc =*pvoc / 10;
- *phumi = buf2[7];
- *phumi <<=8;
- *phumi += buf2[8];
- *ptemp = buf2[9];
- *ptemp <<=8;
- *ptemp += buf2[10];
- *ptemp = *ptemp/2;
- }
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_I2C1_Init();
- MX_LPUART1_UART_Init();
- MX_UART4_Init();
- MX_USART3_UART_Init();
- MX_TIM3_Init();
- /* USER CODE BEGIN 2 */
- HAL_TIM_Base_Start_IT(&htim3);
- HAL_UART_Receive_IT(&huart3,&uart3sv[0],1);
- HAL_UART_Receive_IT(&huart4,&uart4sv[0],1);
- delayms(500);
- 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];
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
- delayms(500);
- HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_SET);
- delayms(500);
- p_uart3=0;//clear uart3 receive buf;
- read_svm40(&voc,&humi,&temp);
- humiH = humi / 100;
- humiL = humi %100;
- tempH =temp / 100;
- tempL =temp % 100;
- HAL_UART_Transmit(&hlpuart1,buf_T,sizeof(buf_T),0xffff);
- delayms(30);
- sprintf(buf1,"%2d.%2d\n\r",tempH,tempL);
- HAL_UART_Transmit(&hlpuart1,buf1,10,0xffff);
- delayms(10);
- HAL_UART_Transmit(&hlpuart1,buf_H,sizeof(buf_H),0xffff);
- delayms(10);
- sprintf(buf1,"%2d.%2d\n\r",humiH,humiL);
- HAL_UART_Transmit(&hlpuart1,buf1,7,0xffff);
- delayms(10);
- HAL_UART_Transmit(&hlpuart1,buf_V,sizeof(buf_V),0xffff);
- delayms(10);
- sprintf(buf1,"%d \n\r",voc);
- HAL_UART_Transmit(&hlpuart1,buf1,6,0xffff);
- delayms(10);
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- //后面是系统自动生成,省略。
- #include "stm32g4xx_hal.h"
- #include "stm32g4xx_hal_uart.h"
- extern UART_HandleTypeDef huart3;
- void svm40_start_measurement(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x00, 0x01, 0x00, 0xFE, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_get_signals(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x03, 0x01, 0x0A, 0xF1, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_get_raw_signals(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x03, 0x01, 0x0B, 0xF0, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_stop_measurement(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x01, 0x00, 0xFE, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_get_temperature_offset(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x60, 0x01, 0x01, 0x9D, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_set_temperature_offset(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x60, 0x05, 0x81, 0x00, 0x00, 0x00, 0x00, 0x19, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- //MOSI 0x7E 0x00 0x60 0x01 0x08 0x96 0x7E
- //MISO 0x7E 0x00 0x60 0x00 0x08 0x00 0x64 0x00 0x0C 0x00 0xB4 0x00 0x32 0x41 0x7E
- void svm40_get_voc_parameters(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x60, 0x01, 0x08, 0x96, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_set_voc_parameters(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x60, 0x09, 0x88, 0x00, 0x64, 0x00, 0x0C, 0x00, 0xB4, 0x00, 0x32, 0xB8, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_store_input_parameters(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x60, 0x01, 0x80, 0x1E, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_get_voc_states(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x61, 0x01, 0x08, 0x95, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_set_voc_states(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0x61, 0x09, 0x88, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32, 0x00, 0x00, 0xDB, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_get_version(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0xD1, 0x00, 0x2E, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
- void svm40_device_reset(void)
- {
- uint8_t buf2[]={0x7E, 0x00, 0xD3, 0x00, 0x2C, 0x7E};
- HAL_UART_Transmit(&huart3,buf2,sizeof(buf2),0xffff);
- }
10,编译,调试。连接传感器和板子。运行。观察串口1数据。
----------------------------------完------------------------------------