1. 配置STM32CubeMX
2. 复制stm32xxxx_hal_msp.c文件
将 CubeMx 生成的代码 stm32f7xx_hal_msp.c 函数复制到 RT-Thread Studio 生成的工程中,并参与工程编译:
由于我们并没有使用 CubeMx 生成的工程,所以这里需要将 stm32f7xx_hal_msp.c 文件中 #include “main.h” 替换为 #include “board.h”。
3. 修改stm32f7xx_hal_conf.h文件
4. 使用外设
上述文件配置完成之后,ADC 外设就可以使用的,在 main.c 中添加外设的使用代码:
/*
* Copyright (c) 2006-2020, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-03-05 Mculover666 the first version
*/
#include <rtthread.h>
#include "board.h"
ADC_HandleTypeDef hadc1;
/* ADC1 init function */
void MX_ADC1_Init(void)
{
/* USER CODE BEGIN ADC1_Init 0 */
/* USER CODE END ADC1_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC1_Init 1 */
/* USER CODE END ADC1_Init 1 */
/** 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 = ADC_SCAN_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 for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
*/
sConfig.Channel = ADC_CHANNEL_3;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC1_Init 2 */
/* USER CODE END ADC1_Init 2 */
}
rt_uint32_t get_adc_value(void)
{
HAL_ADC_Start(&hadc1);
HAL_ADC_PollForConversion(&hadc1, 100);
return (rt_uint32_t)HAL_ADC_GetValue(&hadc1);
}
void mq2_collect_entry(void *parameter)
{
int count = 1;
rt_uint32_t read_value = 0;
MX_ADC1_Init();
while (count++)
{
read_value = get_adc_value();
rt_thread_mdelay(1000);
rt_kprintf("adc value = %d\r\n", read_value);
}
}
/* 创建线程 */
int mq2_collect(void)
{
rt_thread_t tid; //线程句柄
tid = rt_thread_create("mq2_collect",
mq2_collect_entry,
RT_NULL,
512,
9,
10);
if(tid != RT_NULL)
{
//线程创建成功,启动线程
rt_thread_startup(tid);
}
return 0;
}
/* 导出到 msh 命令列表中 */
MSH_CMD_EXPORT(mq2_collect, mq2 collect example);
接收更多精彩文章及资源推送,欢迎订阅我的微信公众号:『mculover666』。