《RT-Thread设备驱动开发指南》之ADC
[复制链接]
本文基于STM32F103以及RT-Thread Studio来大概表描述一下RT-Thread的ADC。
一、配置ADC:
/*-------------------------- ADC CONFIG BEGIN --------------------------*/
/** if you want to use adc you can use the following instructions.
*
* STEP 1, open adc driver framework support in the RT-Thread Settings file
*
* STEP 2, define macro related to the adc
* such as #define BSP_USING_ADC1
*
* STEP 3, copy your adc init function from stm32xxxx_hal_msp.c generated by stm32cubemx to the end of board.c file
* such as void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
*
* STEP 4, modify your stm32xxxx_hal_config.h file to support adc peripherals. define macro related to the peripherals
* such as #define HAL_ADC_MODULE_ENABLED
*
*/
/*#define BSP_USING_ADC1*/
/*#define BSP_USING_ADC2*/
/*#define BSP_USING_ADC3*/
/*-------------------------- ADC CONFIG END --------------------------*/
1、打开ADC设备驱动框架的支持
2、定义ADC相关的宏,使用ADC1
#define BSP_USING_ADC1
3、拷贝ADC初始化函数到board.c结尾,初始化ADC1-5
void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hadc->Instance==ADC1)
{
/* USER CODE BEGIN ADC1_MspInit 0 */
/* USER CODE END ADC1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_ADC1_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**ADC1 GPIO Configuration
PA5 ------> ADC1_IN5
*/
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN ADC1_MspInit 1 */
/* USER CODE END ADC1_MspInit 1 */
}
}
4、开启ADC使能
#define HAL_ADC_MODULE_ENABLED
二、使用ADC
1、查找ADC设备
adc_dev=(rt_adc_device_t)rt_device_find("ad1");
2、使能ADC设备
ret=rt_adc_enable(adc_dev, 5);
3、读采样值并打印
value=rt_adc_read(adc_dev, 5);
rt_kprintf("the value is:d% \n",value);
4、关闭ADC
ret=rt_adc_disable(adc_dev, 5);
三、终端打印值4095,即3.3V
the value is:4095
|