[Evaluation of SGP40] + STM32CUBE + STM32G4 + UART communication test sensor
[Copy link]
This post was last edited by damiaa on 2021-3-27 16:26
[Evaluation of SGP40] + STM32CUBE + STM32G4 + UART communication test sensor
1. Open the STM32CUBE environment.
2. Configure and set serial ports 1 and 3 to enable, receive interrupt configuration, and the baud rate is 115200.
3. Generate MDK project.
4. Add code
Because startup_stm32g491xx.s was not added when the project was generated, add it manually
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
I2C_HandleTypeDef hi2c1;
UART_HandleTypeDef hlpuart1;
UART_HandleTypeDef huart4;
UART_HandleTypeDef huart3;
TIM_HandleTypeDef htim3;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
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);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/***************增加第一部分****************/
uint8_t uart3sv[4],uart4sv[4];
uint8_t uart3buf[100],uart4buf[100];
static uint8_t p_uart3=0,p_uart4=0;
static uint16_t uart3_sent_tim=0,uart4_sent_tim=0;
void delayms(uint32_t x)
{
for(uint32_t i=0;i<x;i++)
{
for(uint32_t j=0;j<50000;j++){};
}
}
//svm40控制命令
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);
}
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);
}
//串口回调函数处理
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(huart->Instance == USART3)
{
if(p_uart3 >=100) p_uart3=0;
uart3buf[p_uart3]=uart3sv[0];
p_uart3++;
HAL_UART_Receive_IT(&huart3,&uart3sv[0],1);
}
}
//读svm40
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
if(p_uart3 >=13)
{
for(uint8_t i=0;i<13;i++)
buf2=uart3buf;
}
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;
}
/***************增加第一部分结束****************/
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
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(30);
HAL_UART_Transmit(&hlpuart1,buf_H,sizeof(buf_H),0xffff);
delayms(30);
sprintf(buf1,"%2d.%2d\n\r",humiH,humiL);
HAL_UART_Transmit(&hlpuart1,buf1,7,0xffff);
delayms(30);
HAL_UART_Transmit(&hlpuart1,buf_V,sizeof(buf_V),0xffff);
delayms(30);
sprintf(buf1,"%d \n\r",voc);
HAL_UART_Transmit(&hlpuart1,buf1,6,0xffff);
delayms(30);
/***************增加第三部分结束****************/
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4;
RCC_OscInitStruct.PLL.PLLN = 85;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
/** Initializes the peripherals clocks
*/
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_UART4
|RCC_PERIPHCLK_LPUART1|RCC_PERIPHCLK_I2C1;
PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
PeriphClkInit.Uart4ClockSelection = RCC_UART4CLKSOURCE_PCLK1;
PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
PeriphClkInit.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief I2C1 Initialization Function
* @param None
* @retval None
*/
static void MX_I2C1_Init(void)
{
/* USER CODE BEGIN I2C1_Init 0 */
/* USER CODE END I2C1_Init 0 */
/* USER CODE BEGIN I2C1_Init 1 */
/* USER CODE END I2C1_Init 1 */
hi2c1.Instance = I2C1;
hi2c1.Init.Timing = 0x30A0A7FB;
hi2c1.Init.OwnAddress1 = 0;
hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
hi2c1.Init.OwnAddress2 = 0;
hi2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK;
hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
if (HAL_I2C_Init(&hi2c1) != HAL_OK)
{
Error_Handler();
}
/** Configure Analogue filter
*/
if (HAL_I2CEx_ConfigAnalogFilter(&hi2c1, I2C_ANALOGFILTER_ENABLE) != HAL_OK)
{
Error_Handler();
}
/** Configure Digital filter
*/
if (HAL_I2CEx_ConfigDigitalFilter(&hi2c1, 0) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN I2C1_Init 2 */
/* USER CODE END I2C1_Init 2 */
}
/**
* @brief LPUART1 Initialization Function
* @param None
* @retval None
*/
static void MX_LPUART1_UART_Init(void)
{
/* USER CODE BEGIN LPUART1_Init 0 */
/* USER CODE END LPUART1_Init 0 */
/* USER CODE BEGIN LPUART1_Init 1 */
/* USER CODE END LPUART1_Init 1 */
hlpuart1.Instance = LPUART1;
hlpuart1.Init.BaudRate = 115200;
hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
hlpuart1.Init.StopBits = UART_STOPBITS_1;
hlpuart1.Init.Parity = UART_PARITY_NONE;
hlpuart1.Init.Mode = UART_MODE_TX_RX;
hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
hlpuart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&hlpuart1) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&hlpuart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&hlpuart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&hlpuart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN LPUART1_Init 2 */
/* USER CODE END LPUART1_Init 2 */
}
/**
* @brief UART4 Initialization Function
* @param None
* @retval None
*/
static void MX_UART4_Init(void)
{
/* USER CODE BEGIN UART4_Init 0 */
/* USER CODE END UART4_Init 0 */
/* USER CODE BEGIN UART4_Init 1 */
/* USER CODE END UART4_Init 1 */
huart4.Instance = UART4;
huart4.Init.BaudRate = 115200;
huart4.Init.WordLength = UART_WORDLENGTH_8B;
huart4.Init.StopBits = UART_STOPBITS_1;
huart4.Init.Parity = UART_PARITY_NONE;
huart4.Init.Mode = UART_MODE_TX_RX;
huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart4.Init.OverSampling = UART_OVERSAMPLING_16;
huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart4) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart4, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart4, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart4) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN UART4_Init 2 */
/* USER CODE END UART4_Init 2 */
}
/**
* @brief USART3 Initialization Function
* @param None
* @retval None
*/
static void MX_USART3_UART_Init(void)
{
/* USER CODE BEGIN USART3_Init 0 */
/* USER CODE END USART3_Init 0 */
/* USER CODE BEGIN USART3_Init 1 */
/* USER CODE END USART3_Init 1 */
huart3.Instance = USART3;
huart3.Init.BaudRate = 115200;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.Mode = UART_MODE_TX_RX;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart3.Init.OverSampling = UART_OVERSAMPLING_16;
huart3.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart3.Init.ClockPrescaler = UART_PRESCALER_DIV1;
huart3.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetTxFifoThreshold(&huart3, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_SetRxFifoThreshold(&huart3, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
{
Error_Handler();
}
if (HAL_UARTEx_DisableFifoMode(&huart3) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART3_Init 2 */
/* USER CODE END USART3_Init 2 */
}
/**
* @brief TIM3 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM3_Init(void)
{
/* USER CODE BEGIN TIM3_Init 0 */
/* USER CODE END TIM3_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM3_Init 1 */
/* USER CODE END TIM3_Init 1 */
htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 10000;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM3_Init 2 */
/* USER CODE END TIM3_Init 2 */
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : B1_Pin */
GPIO_InitStruct.Pin = B1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : LD2_Pin */
GPIO_InitStruct.Pin = LD2_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
6. Connect the board according to the cube picture and board data. VCC GND RX TX Serial port 3 is PB10 (TX) PB11 (RX)
6. Compile and debug.
7. Observation. Comparison
Open putty and set the serial port. Connect. Observe the data.
Compared with official tool data:
The data is correct.
--------------------------over--------------------------------------
|