正文程序及解释:
#include "stm32f10x.h"
#include
#include "ADC.h"
#define ADC_COUNT 320 //每通道采 320次 次数可修改
//0-15通道单次转换(0-4095) 定义范围(-32768~+32768)有符号16位
volatile int16_t ADC_Value[ADC_COUNT][16]; //用来存放 ADC 转换结果,也是DMA的目标地址
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure; //定义结构体
GPIO_InitTypeDef GPIO_InitStructure; //定义结构体
ADC_TIM4Configuration();//启动定时器
ADC_DMAConfiguration(); //启动DMA
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //使能ADC1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能GPIOA时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能GPIOB时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //使能GPIOC时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|
GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
// 各端口对应pin口芯片已经确定 //总共会用到16路,故有16个端口,
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚
GPIO_Init(GPIOA,&GPIO_InitStructure); //对GPIOA端口的引脚按照上述参数值进行初始化操作
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC,&GPIO_InitStructure);
// INITIAL ADC1
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
//独立模式 有多种模式可选择,此时就只有ADC1工作,故只能选独立模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //ADC多通道扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //是否启用连续转换模式,就是这里设置为NO
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T4_CC4;
//选择定时器4的捕获比较4作为转换外部触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 16; //ADC规则转换通道数量
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig( ADC1, ADC_Channel_0, 1, ADC_SampleTime_41Cycles5);
//选择ADC1的通道0,是第一个通道,周期是41.5
ADC_RegularChannelConfig( ADC1, ADC_Channel_1, 2, ADC_SampleTime_41Cycles5);
//应为41.5+12.5
ADC_RegularChannelConfig( ADC1, ADC_Channel_2, 3, ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig( ADC1, ADC_Channel_3, 4, ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig( ADC1, ADC_Channel_4, 5, ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig( ADC1, ADC_Channel_5, 6, ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig( ADC1, ADC_Channel_6, 7, ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig( ADC1, ADC_Channel_7, 8, ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig( ADC1, ADC_Channel_8, 9, ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig( ADC1, ADC_Channel_9, 10, ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig( ADC1, ADC_Channel_10, 11, ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig( ADC1, ADC_Channel_11, 12, ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig( ADC1, ADC_Channel_12, 13, ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig( ADC1, ADC_Channel_13, 14, ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig( ADC1, ADC_Channel_14, 15, ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig( ADC1, ADC_Channel_15, 16, ADC_SampleTime_41Cycles5);
ADC_ExternalTrigConvCmd(ADC1, ENABLE); //使能外部触发
ADC_TempSensorVrefintCmd(DISABLE); //关闭ADC内置温度
ADC_DMACmd(ADC1, ENABLE); //使能指定的ADC的DMA请求
ADC_Cmd(ADC1, ENABLE); // Enable ADC1 ADC-Power==ON
Delay(100); //delay 另外设置 自主选择
ADC_ResetCalibration(ADC1); //重置指定的ADC的校准寄存器
while(ADC_GetResetCalibrationStatus(ADC1)); //
ADC_StartCalibration(ADC1); //开始指定ADC的校准状态
while(ADC_GetCalibrationStatus(ADC1)); //waiting for set ok
DMA_Cmd(DMA1_Channel1, ENABLE); //使能DMA 通道1
TIM_Cmd(TIM4, ENABLE);
}
void ADC_TIM4Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitTypeDef;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM_TimeBaseInitTypeDef.TIM_Period = 2500; //每多久触发一次中断 系统时钟为72MHz 72M/320/2500=90
TIM_TimeBaseInitTypeDef.TIM_Prescaler = 90-1; //预分频,此值+1为分频的除数
TIM_TimeBaseInitTypeDef.TIM_ClockDivision = 0x0; //设置时钟分割
TIM_TimeBaseInitTypeDef.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitTypeDef);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//TIM输出比较触发模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1250;//CCR4_Val
TIM_OCInitStructure.TIM_OCPolarity= TIM_OCPolarity_Low;
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_ClearITPendingBit(TIM4,TIM_IT_CC4|TIM_IT_Update);
TIM_ITConfig(TIM4,TIM_IT_CC4, ENABLE);
}
void ADC_DMAConfiguration(void)
{ DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr =(u32)&(ADC1->DR);//ADC1数据寄存器
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_Value; //存入内存中去
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //片内外设作源头
DMA_InitStructure.DMA_BufferSize = 5120; //每次DMA16个数据 320*16
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_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); //DMA通道1传输完成中断
DMA_Cmd(DMA1_Channel1, ENABLE);
}
void ADC_DMAReConfig(void)
{ DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
过程问题:
1. 采样时最基本也是最重要的就是配置问题,ADC的配置,Timer的配置及DMA
的配置,根据所需采取相应的配置,很重要。
2. 由于配置一直存在这样那样的不正确,导致后期的采样值和计算后的实际值的问题,包括读取不到采样值valueA(DMA的配置不正确),以及后期计算的实际值无法显示等
遗留问题:
曾经出现过程序运行较慢的情况,但改变了Tim4中的period和Prescaler后,程序速度明显加快,但后来有人问起这个问题,同样的修改,却得不到想要的效果。
总结:
此次进行的ADC采样用时很长,但实际在原有程序的基础上的改变不大。最需要注意的是前期的配置问题,在针对相应问题处理时的思路必须明晰,而不是盲目的进行程序的修改。另外以后还有问题,再行补充。