首页 > 代码库 > stm32之TIM+ADC+DMA采集50HZ交流信号
stm32之TIM+ADC+DMA采集50HZ交流信号
http://cache.baiducontent.com/c?m=9d78d513d98207f04fece47f0d01d7174a02d1743ca6c76409c3e03984145b563710f4bb56644b5bc7823c390ef50f1aa8e737012a1e65f2dedf883d80f9c57478de6323706bd71c4dce5ff58b11769737902cefaa18ecb9e732e5adc5d3a95744ca245f3cdfae&p=8b2a975fca9d5bff57ee947a5b47cc&newp=9133c64ad49614f543bd9b7d0d12cc231610db2151d2d401298ffe0cc4241a1a1a3aecbf21231a01d0ce786c0aaa4e57e1f03772350034f1f689df08d2ecce7e7add7d2f&user=baidu&fm=sc&query=stm32+adc+%CD%AC%B2%BD%A1%A1tim&qid=809b98c2000086f6&p1=4
void ADC_DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
DMA_DeInit(DMA1_Channel1); //DMA复位
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; //外设的地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; //存储区的起始地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //DMA传输方向到内部存储区
DMA_InitStructure.DMA_BufferSize = ADC_NUM; //ADC转换的次数
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //ADC数据长度为16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //存储区数据长度为16位
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA工作方式为循环工作
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA 优先级高
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //存储器对存储器关闭
DMA_Init(DMA1_Channel1, &DMA_InitStructure); //DMA通道初始化
DMA_Cmd(DMA1_Channel1,ENABLE); //DMA使能
}
void ADC_START(void)
{
ADC1, ENABLE); //ADC的DMA功能开启
ADC1); //ADC_GetResetCalibrationStatus(ADC等待完成
ADC1); //开始校准
while(ADC1)); //ADC_SoftwareStartConvCmd(ADC的软件转换启动功能
}void ADC1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); ////PA4567时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); ////PC45时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); ////PB01时钟使能
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; ////初始化PA4567为模拟输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //管脚设为模拟输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5; ////初始化PC45为模拟输入
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; ////初始化PB0 1为模拟输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
ADC设置
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC时钟
ADC_Mode = ADC1和ADC2工作在独立模式
ADC_ScanConvMode = ENABLE; //多通道
///ADC_ContinuousConvMode = ENABLE; //连续转换
ADC_ContinuousConvMode = DISABLE; //连续转换
ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;
///ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //软件启动转换
ADC_DataAlign = ADC_DataAlign_Right; //转换结果右对齐
ADC_NbrOfChannel = ADC_NUM; //通道数目
ADC1, &ADC_InitStructure);
/// ADC_TempSensorVrefintCmd(ENABLE); //使能片内温度传感器
/// RCC_ADCCLKConfig(RCC_PCLK2_Div6); //PCLK 6分频
RCC_ADCCLKConfig(RCC_PCLK2_Div8); //ADC_RegularChannelConfig(ADC_Channel_4, 1, ADC_SampleTime_239Cycles5); //通道4,转换次序,转换时间
ADC1, ADC_SampleTime_239Cycles5); //通道5,转换次序,转换时间
ADC1, ADC_SampleTime_239Cycles5); //通道6,转换次序,转换时间
ADC1, ADC_SampleTime_239Cycles5); //通道7,转换次序,转换时间
ADC1, ADC_SampleTime_239Cycles5); //通道8,转换次序,转换时间
ADC1, ADC_SampleTime_239Cycles5); //通道9,转换次序,转换时间
ADC1, ADC_SampleTime_239Cycles5); //通道14,转换次序,转换时间
ADC1, ADC_SampleTime_239Cycles5); //通道15,转换次序,转换时间
// ADC1, ADC_SampleTime_239Cycles5);
ADC_START();
}
/*
系统时钟为72MHZ,FPCLK2=72 000 000HZ
对50HZ交流信号进行采集,一个周期为20MS, 采集8路信号,每个信号采用64次,
20*1000us/64=312us,即每隔312us要启动一次采集信号(需要采集8路信号),采集64次后正好是一个周期;
采集一次8路的时间:(239.5+12.5)*8/9M =224us
*/
void TIM2_Configuration(void)
{
TIM_TimeBaseStructure;
TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_Period = 312; //设置一个周期0.312ms
TIM_Prescaler = 71; //系统主频72M,这里分频72,相当于1000K的定时器2时钟
TIM_ClockDivision = 0x0;
TIM_CounterMode = TIM_CounterMode_Up; /////
TIM2, & TIM_TimeBaseStructure);
TIM_OCMode = TIM_OCMode_PWM1; //下面详细说明
TIM_OutputState = TIM_OutputState_Disable;
TIM_Pulse = 20; /////
TIM_OCPolarity = TIM_OCPolarity_Low; //如果是PWM1要为Low,PWM2则为High
TIM2, & TIM_OCInitStructure);
TIM2, ENABLE);
TIM2);
TIM2, TIM_OCPreload_Enable);
TIM2, DISABLE);
}
stm32之TIM+ADC+DMA采集50HZ交流信号