本帖最后由 jonyzhu 于 2014-8-5 13:29 编辑
这篇帖子将介绍如何使用 STM32F303VCT6 的 ADC 功能,属于[
记录我的 STM32 示波器的研发经历]的副产品。STM32F3 系列的模拟电路部分功能比较强(相对于ST其它的MCU),其中STM32F303VCT6 最高主频 72MHz,256K Flash,48K RAM;最牛的,是它的72MHz的ADC时钟,以及6bit分辨率下8个时钟周期的采样+转换时间,这相当于9M/s的采样率了 。
屏幕快照 2014-08-05 上午10.30.19.png (55.07 KB, 下载次数: 0)
下载附件
2014-8-5 13:14 上传
双ADC的交替采样模式下,有几个参数是需要精心设计的:
- 采样周期。共分8挡,最短1.5个ADC时钟周期,最长601个时钟周期。采样周期加上前面讲的PLL时钟分频系数,可以搭配出多种不同的采样率,在捕获低频信号时会非常有用。
- 转换周期。这个参数和分辨率有关。采样周期+转换周期=总周期,采样频率/总周期=采样率。就这么简单。
- 通道间采样间隔。别忘了我们可是双ADC采样,ADC1和ADC2在对同一个通道进行交替采样,在ADC1完成采样(注意,是采样,不是转换)后,要等几个(最少1个)ADC时钟周期才让开始ADC2的采样。
仍然以6bit分辨率为例:采样周期=1.5,转换周期=6.5,总周期=1.5+6.5=8,通道间采样间隔=3 时,ADC1在 t0 时刻采样,然后ADC2就会在 t0+1.5+3=t0+4.5 的时刻采样,然后如此继续下去。
这样,相当于8个ADC时钟周期里面,采样了2次(虽然2次并不是严格对齐的),72MHz/(8/2)=72MHz/4 = 18M/s采样率。
我为什么老抓住6bit分辨率不放?因为我的显示屏只有320X240啊,垂直不能同时显示2个8bit分辨率的波形啊!!!而且我SPI的显示屏啊,刷新率是硬伤啊,6bit分辨率意味着我能少刷几个像素啊!!!哎。。。(我后来都想整单 {MOD}LCD了,那刷新率杠杠的,不过没关系,我还有STM32F429i Discovery,哈哈哈!)
Common 设置:
- cs.ADC_Mode = ADC_Mode_Interleave;
- cs.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
- cs.ADC_DMAMode = ADC_DMAMode_OneShot;
- cs.ADC_TwoSamplingDelay = 2; //2就是3个周期的通道间间隔
- ADC_CommonInit(ADC1, &cs); //双通道模式下,只需配置Master ADC
复制代码
每个 ADC 的设置:
- ADC_StructInit(&adcs);
- ADC_VoltageRegulatorCmd(ADC1, ENABLE);
- ADC_VoltageRegulatorCmd(ADC2, ENABLE);
- osDelay(1); //延迟 1ms,需要用你自己的延时函数替换。其实2us就足够了,CMSIS_RTOS 没有那么精细,1ms算了。
- ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC1);
- ADC_SelectCalibrationMode(ADC2, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC2);
- while(ADC_GetCalibrationStatus(ADC1) != RESET );
- while(ADC_GetCalibrationStatus(ADC2) != RESET );
- adcs.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
- adcs.ADC_Resolution = ADC_Resolution_6b;
- adcs.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_7;
- adcs.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
- adcs.ADC_DataAlign = ADC_DataAlign_Right;
- adcs.ADC_OverrunMode = ADC_OverrunMode_Enable;
- adcs.ADC_AutoInjMode = ADC_AutoInjec_Disable;
- adcs.ADC_NbrOfRegChannel = 1;
- ADC_Init(ADC1, &adcs);
- ADC_Init(ADC2, &adcs);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_1Cycles5);
- ADC_RegularChannelConfig(ADC2, ADC_Channel_4, 1, ADC_SampleTime_1Cycles5);
- ADC_Cmd(ADC1, ENABLE);
- ADC_Cmd(ADC2, ENABLE);
复制代码
DMA的设置
ADC转换那么快,用CPU等中断再往数组里面存那是不现实的哇!(我用注入交替触发模式试过,用中断存数组大约只有200K/s的速率)
ADC1、2做双交替采样的DMA设置,有2个选择:
- 只用1个DMA通道,一次在ADC2的EOC事件后一次读取2个采样结果到一个数组中
- 用2个DMA通道,分别读取2个ADC的采样结果到2个数组中
我选了第二个,原因么:
- 我不缺DMA
- 以后从双采样切换到独立采样(也就是双通道示波器变身四通道示波器)时,比较自然
DMA里面需要确定你的采样存储深度,让DMA在读取完这么多组数据后自动停止,发送给LCD去显示。当然,你也可以让DMA继续采样来提高捕获率或者实现余辉效果,不过那需要内存支持,算法就复杂了,我还没有搞到那么深。等以后用到那个地步了,可能我也不会选择 soc 的 ADC 了。
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1|RCC_AHBPeriph_DMA2, ENABLE);
- ds.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
- ds.DMA_MemoryBaseAddr = (uint32_t)&appADCValue1[0];
- ds.DMA_BufferSize = ADCDeepth;
- ds.DMA_DIR = DMA_DIR_PeripheralSRC;
- ds.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
- ds.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
- ds.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- ds.DMA_MemoryInc = DMA_MemoryInc_Enable;
- ds.DMA_Mode = DMA_Mode_Circular;
- ds.DMA_Priority = DMA_Priority_VeryHigh;
- ds.DMA_M2M = DMA_M2M_Disable;
- DMA_Init(DMA1_Channel1, &ds);
- ds.DMA_PeripheralBaseAddr = (uint32_t)&ADC2->DR;
- ds.DMA_MemoryBaseAddr = (uint32_t)&appADCValue2[0];
- DMA_Init(DMA2_Channel1, &ds);
- DMA_Cmd(DMA1_Channel1, ENABLE);
- DMA_Cmd(DMA2_Channel1, ENABLE);
- DMA_ITConfig(DMA2_Channel1, DMA_IT_TC, ENABLE);
- ns.NVIC_IRQChannel = DMA2_Channel1_IRQn;
- ns.NVIC_IRQChannelPreemptionPriority = 4;
- ns.NVIC_IRQChannelSubPriority = 8;
- ns.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&ns);
复制代码
好了,最后启动 Master ADC的转换就开始了 ADC_StartConversion(ADC1);
最后,把上面的代码整合一下,看个全貌(不是全部工程,但应该更容易应用到你自己到项目中去,因为少了很多乱七八糟的和我个人环境设置有关的东西):
- #define ADCDeepth 300
- uint8_t appADCValue1[ADCDeepth];
- uint8_t appADCValue2[ADCDeepth];
- void ADCInit(void){
- GPIO_InitTypeDef gs;
- ADC_CommonInitTypeDef cs;
- ADC_InitTypeDef adcs;
- DMA_InitTypeDef ds;
- NVIC_InitTypeDef ns;
- RCC_AHB1PeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- gs.GPIO_Mode = GPIO_Mode_AN;
- gs.GPIO_OType = GPIO_OType_PP;
- gs.GPIO_PuPd = GPIO_PuPd_NOPULL;
- gs.GPIO_Speed = GPIO_Speed_50MHz;
- gs.GPIO_Pin = GPIO_Pin_3;
- GPIO_Init(GPIOA, &gs);
- gs.GPIO_Pin = GPIO_Pin_7;
- GPIO_Init(GPIOA, &gs);
- RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div1);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_ADC12, ENABLE);
- cs.ADC_Clock = ADC_Clock_AsynClkMode;
- cs.ADC_Mode = ADC_Mode_Interleave;
- cs.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
- cs.ADC_DMAMode = ADC_DMAMode_OneShot;
- cs.ADC_TwoSamplingDelay = 2; //2就是3个周期的通道间间隔
- ADC_CommonInit(ADC1, &cs); //双通道模式下,只需配置Master ADC
- ADC_StructInit(&adcs);
- ADC_VoltageRegulatorCmd(ADC1, ENABLE);
- ADC_VoltageRegulatorCmd(ADC2, ENABLE);
- osDelay(1); //延迟 1ms,需要用你自己的延时函数替换。其实2us就足够了,CMSIS_RTOS 没有那么精细,1ms算了。
- ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC1);
- ADC_SelectCalibrationMode(ADC2, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC2);
- while(ADC_GetCalibrationStatus(ADC1) != RESET );
- while(ADC_GetCalibrationStatus(ADC2) != RESET );
- adcs.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable;
- adcs.ADC_Resolution = ADC_Resolution_6b;
- adcs.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_7;
- adcs.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None;
- adcs.ADC_DataAlign = ADC_DataAlign_Right;
- adcs.ADC_OverrunMode = ADC_OverrunMode_Enable;
- adcs.ADC_AutoInjMode = ADC_AutoInjec_Disable;
- adcs.ADC_NbrOfRegChannel = 1;
- ADC_Init(ADC1, &adcs);
- ADC_Init(ADC2, &adcs);
- ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_1Cycles5);
- ADC_RegularChannelConfig(ADC2, ADC_Channel_4, 1, ADC_SampleTime_1Cycles5);
- ADC_Cmd(ADC1, ENABLE);
- ADC_Cmd(ADC2, ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1|RCC_AHBPeriph_DMA2, ENABLE);
- ds.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
- ds.DMA_MemoryBaseAddr = (uint32_t)&appADCValue1[0];
- ds.DMA_BufferSize = ADCDeepth;
- ds.DMA_DIR = DMA_DIR_PeripheralSRC;
- ds.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
- ds.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
- ds.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
- ds.DMA_MemoryInc = DMA_MemoryInc_Enable;
- ds.DMA_Mode = DMA_Mode_Circular;
- ds.DMA_Priority = DMA_Priority_VeryHigh;
- ds.DMA_M2M = DMA_M2M_Disable;
- DMA_Init(DMA1_Channel1, &ds);
- ds.DMA_PeripheralBaseAddr = (uint32_t)&ADC2->DR;
- ds.DMA_MemoryBaseAddr = (uint32_t)&appADCValue2[0];
- DMA_Init(DMA2_Channel1, &ds);
- DMA_Cmd(DMA1_Channel1, ENABLE);
- DMA_Cmd(DMA2_Channel1, ENABLE);
- DMA_ITConfig(DMA2_Channel1, DMA_IT_TC, ENABLE);
- ns.NVIC_IRQChannel = DMA2_Channel1_IRQn;
- ns.NVIC_IRQChannelPreemptionPriority = 4;
- ns.NVIC_IRQChannelSubPriority = 8;
- ns.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&ns);
- }
- void DMA2_Channel1_IRQHandler(void){
- if(DMA_GetITStatus(DMA2_IT_TC1)!=RESET){
- DMA_ClearITPendingBit(DMA2_IT_TC1);
- //数据拿到了,接下来看你的了!
- }
- }
复制代码
比如ADC1通道 1和通道2采集电压,这个值就是当通道1转化完成后等待多少后启动通道2采集?是同一个ADC,这样做的好处是什么?能保证数据采集的稳定正确?谢谢楼主!
一周热门 更多>