stm32 ADC DMA rt-thread studio
1.Rt-Thrad Studio
2.CubeMX Settings
- System Core >> RCC >> HSE >> Crystal(外部晶振)
- System Core >> SYS >> Debug(选择调试接口)
- Connectivity >> USART3 >> Asynchronous(用于finsh命令行)
- Analog >> ADC1 >>
Parameter Settings
- Analog >> ADC1 >> DMA Settings
- Clock Configuration(配置时钟) HCLK >> 168 >> Enter
- GENERATE COED (default seting)
Code
- applications/main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #include <rtthread.h>
#define DBG_TAG "main" #define DBG_LVL DBG_LOG #include <rtdbg.h>
extern void usr_board_init(void); extern uint16_t read_adc_value(uint8_t channel);
int main(void) { int count = 1;
usr_board_init();
while (count++) { LOG_D("Hello RT-Thread!"); rt_kprintf("PA3_adc = %d ", read_adc_value(0)); rt_kprintf("PA4_adc = %d ", read_adc_value(1)); rt_kprintf("PA5_adc = %d ", read_adc_value(2)); rt_kprintf("PA6_adc = %d ", read_adc_value(3)); rt_kprintf("\n"); rt_thread_mdelay(1000); }
return RT_EOK; }
|
- cubemx/Src/main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#define ADC_CHANNEL_SUM 4 #define ADC_BUFF_LOOP 30 uint16_t adc_dma_value[ADC_CHANNEL_SUM * ADC_BUFF_LOOP];
void usr_board_init(void) { MX_DMA_Init(); MX_ADC1_Init(); HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&adc_dma_value, ADC_CHANNEL_SUM * ADC_BUFF_LOOP); }
uint16_t read_adc_value(uint8_t channel) { if (channel < ADC_CHANNEL_SUM) { return adc_dma_value[channel]; } return 0; }
static void MX_DMA_Init(void) {
__HAL_RCC_DMA2_CLK_ENABLE();
}
|
3.Build && Test
PA3_adc