基于STM32HAL库数字引脚直接读取出的温度通过广和通通讯模组L610运用串口打印AT指令上传腾讯云的代码
时间: 2023-06-18 22:03:14 浏览: 173
以下是基于 STM32HAL 库数字引脚直接读取出的温度通过广和通通讯模组 L610 运用串口打印 AT 指令上传腾讯云的代码示例:
```c
#include "stm32f4xx_hal.h"
#include <string.h>
#include <stdio.h>
/* UART handler declaration */
UART_HandleTypeDef huart2;
/* GPIO Pin declaration */
#define TEMP_GPIO_PORT GPIOA
#define TEMP_GPIO_PIN GPIO_PIN_0
/* Variables declaration */
uint16_t adc_value;
float temperature;
/* Function declaration */
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_ADC1_Init(void);
/* Main function */
int main(void)
{
/* MCU Configuration----------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_ADC1_Init();
/* Infinite loop */
while (1)
{
/* Start ADC conversion */
HAL_ADC_Start(&hadc1);
/* Wait for the conversion to complete */
if (HAL_ADC_PollForConversion(&hadc1, 100) == HAL_OK)
{
/* Read the ADC converted value */
adc_value = HAL_ADC_GetValue(&hadc1);
/* Calculate temperature */
temperature = (float)(adc_value * 3.3 / 4095.0 - 0.76) / 0.0025 + 25.0;
/* Print temperature to UART */
char temp_str[30];
sprintf(temp_str, "Temperature: %.2f °C\n", temperature);
HAL_UART_Transmit(&huart2, (uint8_t *)temp_str, strlen(temp_str), HAL_MAX_DELAY);
/* Upload temperature to Tencent Cloud */
char at_cmd[50];
sprintf(at_cmd, "AT+TCIOTTXDATA=\"Temperature\",\"%.2f\"\r\n", temperature);
HAL_UART_Transmit(&huart2, (uint8_t *)at_cmd, strlen(at_cmd), HAL_MAX_DELAY);
HAL_Delay(1000);
}
/* Stop ADC conversion */
HAL_ADC_Stop(&hadc1);
}
}
/* ADC1 Initialization Function */
static void MX_ADC1_Init(void)
{
ADC_ChannelConfTypeDef sConfig = {0};
/* Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion) */
hadc1.Instance = ADC1;
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc1.Init.Resolution = ADC_RESOLUTION_12B;
hadc1.Init.ScanConvMode = DISABLE;
hadc1.Init.ContinuousConvMode = DISABLE;
hadc1.Init.DiscontinuousConvMode = DISABLE;
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc1.Init.NbrOfConversion = 1;
hadc1.Init.DMAContinuousRequests = DISABLE;
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
if (HAL_ADC_Init(&hadc1) != HAL_OK)
{
Error_Handler();
}
/* Configure the selected ADC channel */
sConfig.Channel = ADC_CHANNEL_0;
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
sConfig.SamplingTime = ADC_SAMPLETIME_480CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
}
/* USART2 Initialization Function */
static void MX_USART2_UART_Init(void)
{
/* USART2 parameter configuration*/
huart2.Instance = USART2;
huart2.Init.BaudRate = 115200;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
}
/* GPIO Initialization Function */
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin : TEMP_GPIO_PIN */
GPIO_InitStruct.Pin = TEMP_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(TEMP_GPIO_PORT, &GPIO_InitStruct);
}
/* System Clock Configuration */
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** 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 = 16;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 7;
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_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
Error_Handler();
}
}
/* Error Handler */
void Error_Handler(void)
{
/* User can add his own implementation to report the HAL error return state */
while (1)
{
}
}
#ifdef USE_FULL_ASSERT
/* Assert function */
void assert_failed(uint8_t *file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number */
while (1)
{
}
}
#endif /* USE_FULL_ASSERT */
```
在上述代码中,我们使用了 STM32F4 的 ADC 模块读取了一个温度传感器的模拟电压值,并通过一定的计算得到了温度值。然后我们使用了串口2将温度值打印出来,并通过广和通 L610 模组的 AT 指令将温度值上传到了腾讯云平台。需要注意的是,在使用 L610 模组时需要先进行初始化,并设置好串口的波特率等参数。
希望这段示例代码能够对你有所帮助。
阅读全文