当前位置:网站首页>STM32 MCU ADC rule group multi-channel conversion DMA mode
STM32 MCU ADC rule group multi-channel conversion DMA mode
2022-04-23 04:00:00 【Chenxr32】
A / D converter (Analog To Digital Converter) abbreviation ADC( Or you could write it as A/D), It refers to a device that converts a continuously changing analog signal into a discrete digital signal .
Direct memory access technology (Direct Memory Access) abbreviation DMA.DMA Used to provide high-speed data transfer between peripheral and memory or between memory and memory . need not CPU intervention , Data can be obtained by DMA Moving fast , This saves money CPU Your resources can do other operations .DMA The essence of transmission is address to address operation , You can put DMA Understood as a CPU Of “ secretary ”.
ADC The reference voltage is ADC An important indicator of .STM32 The chip is just 100 And above pin models have external reference voltage pin VREF+ and VREF-;64 And less than 64 Foot model , The reference voltage pin is connected to by default inside the chip ADC The power supply pin VDDA and VSSA.
chart 1 ADC Power supply and reference power supply circuit |
because ADC Rule group multi-channel conversion , Only the data of the last channel can be read , therefore ADC Multi channel conversion for DMA Pattern , When each access conversion is completed , send out DMA request , adopt DMA Directly transfer to the set memory buffer , So it solved ADC The problem of multi-channel conversion data being covered , meanwhile CPU No need to read frequently ADC The data of , Greatly improve execution efficiency .
By looking up STM32F1 Reference manual for , We can know ADC1 Of DMA Transport channel mapped to DMA1 Of Channel1.ADC1 May by TIM3_TRGO Event triggered transformation . This passage TIM3 The update event of triggers ADC Rule group 4 Channel conversion . In actual projects , We use this 4 road ADC Read the output voltage of the four Hall angle sensors and then calculate the angle of the manipulator , Use external trigger ADC Rule group conversion can avoid ADC Waste of resources caused by circular conversion mode . Next on the code .
void Timer_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period=1000-1;
TIM_TimeBaseStructure.TIM_Prescaler=72-1;
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
TIM_Cmd(TIM3, ENABLE);
TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_Update);// Set update event as trigger output
}
void ADC_DMA_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);//72M/6=12M
//init GPIO
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_Init(GPIOB,&GPIO_InitStructure);
//init DMA
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr=(unsigned int)&(ADC1->DR);
DMA_InitStructure.DMA_MemoryBaseAddr=(unsigned int)&ADCBuffer[0];
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize=4;
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority=DMA_Priority_High;
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;
DMA_Init(DMA1_Channel1,&DMA_InitStructure);
DMA_Cmd(DMA1_Channel1,ENABLE);
//init ADC1
ADC_DeInit(ADC1);
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode=ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode=DISABLE;
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_T3_TRGO;// Set the external signal TIM3_TRGO set out
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel=4;
ADC_Init(ADC1,&ADC_InitStructure);
ADC_RegularChannelConfig(ADC1,ADC_Channel_6,1,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_7,2,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_8,3,ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_9,4,ADC_SampleTime_239Cycles5);
ADC_DMACmd(ADC1,ENABLE);
ADC_Cmd(ADC1,ENABLE);
//calibrate ADC1
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
//enable ADC1 external trigger
ADC_ExternalTrigConvCmd(ADC1,ENABLE);
}
stay main Middle note initialization function in function ,TIM3、ADC、DMA After configuration ,TIM3 Will each 1ms Trigger once ADC Rule group channel conversion , The result of the conversion will be DMA Transfer to a predefined buffer ADCBuffer, Just read it in time ADCBuffer The data in can be obtained ADC Value of channel conversion . If the data is not read in time , The read data is easily overwritten , To this end, we can ADCBuffer And DMA Channel cache (DMA_BufferSize) Set as the of the measured data 2 Multiple size , When the first rule group 4 After the channels are converted in turn ,DMA Transfer the data to the first half of the buffer in turn , Simultaneous setting DMA Transmit half flag bit , You can now read the current 4 Data channel conversion . At the same time, the rule group continues to convert , When the second time 4 When the channels are converted in turn , The conversion data is stored in the second half of the buffer , Simultaneous setting DMA Transmission completion flag bit , At this time, the data of the second conversion can be read .
Code download link :STM32F1 Series single chip microcomputer multichannel ADC-DMA Mode configuration method _ Single chip microcomputer dma-C Document resources -CSDN download
版权声明
本文为[Chenxr32]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230356325461.html
边栏推荐
- Express中间件①(中间件的使用)
- Basic usage of Google colab (I)
- 【李宏毅2022 机器学习春】hw6_GAN(不懂..)
- RuntimeError: output with shape [4, 1, 512, 512] doesn‘t match the broadcast shape[4, 4, 512, 512]
- 列表、元组、字典和集合的区别
- 標識符、關鍵字、數據類型
- php导出Excel表格
- [AI vision · quick review of NLP natural language processing papers today, issue 30] Thu, 14 APR 2022
- 硬核拆芯片
- Thought of reducing Governance -- detailed summary of binary search
猜你喜欢
阿里云IoT流转到postgresql数据库方案
STM32单片机ADC规则组多通道转换-DMA模式
Openvino only supports Intel CPUs of generation 6 and above
秒杀所有区间相关问题
Retrieval question answering system baseline
STM32 advanced timer com event
Network principle | connection management mechanism in TCP / IP important protocol and core mechanism
Xiaomi, which has set the highest sales record of domestic mobile phones in overseas markets, paid renewed attention to the domestic market
ROS series (III): introduction to ROS architecture
Alibaba cloud IOT transfer to PostgreSQL database scheme
随机推荐
[BIM introduction practice] wall hierarchy and FAQ in Revit
Openvino only supports Intel CPUs of generation 6 and above
Summary of knowledge map (3)
matlab讀取多張fig圖然後合並為一張圖(子圖的形式)
VSCode配置之Matlab极简配置
Definition, understanding and calculation of significant figures in numerical analysis
Numpy's broadcasting mechanism (with examples)
Picture synthesis video
matlab读取多张fig图然后合并为一张图(子图的形式)
The super large image labels in remote sensing data set are cut into specified sizes and saved into coco data set - target detection
Cause analysis of incorrect time of AI traffic statistics of Dahua Equipment Development Bank
QtSpim手册-中文翻译
[latex] formula group
Alibaba cloud IOT transfer to PostgreSQL database scheme
【BIM入门实战】Revit建筑墙体:构造、包络、叠层图文详解
Cmake qmake simple knowledge
伦敦银最新价格走势图与买卖点
单极性非归零NRZ码、双极性非归零NRZ码、2ASK、2FSK、2PSK、2DPSK及MATLAB仿真
vscode删除卸载残余
Nel ASA:挪威Herøya设施正式启用