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
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_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交流信號