//============================================
//函數(shù)名稱:ADC1_Mode_Config(void)
//功能描述:配置ADC1的工作模式為MDA模式
//輸入:無
//輸出:無
//============================================
void ADC1_Mode_Config(void)
{
? ?? ???DMA_InitTypeDef DMA_InitStructure;
? ?? ???ADC_InitTypeDef ADC_InitStructure;
? ?? ???
? ?? ???/* DMA channel1 configuration */
? ?? ???DMA_DeInit(DMA1_Channel1);
? ?? ???
? ?? ???DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;? ?? ?? ?? ?? ?? ???//ADC地址
? ?? ???DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;? ?? ???//內(nèi)存地址??ADC_ConvertedValue[15]一個數(shù)組
? ?? ???DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
? ?? ???DMA_InitStructure.DMA_BufferSize = 15;
? ?? ???DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;? ?? ?? ?? ?? ?? ? //外設(shè)地址固定
? ?? ???DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ? //內(nèi)存地址自加
? ?? ???DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;? ?? ???//半字
? ?? ???DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
? ?? ???DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?? ?//循環(huán)傳輸
? ?? ???DMA_InitStructure.DMA_Priority = DMA_Priority_High;
? ?? ???DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
? ?? ???DMA_Init(DMA1_Channel1, &DMA_InitStructure);
? ?? ???
? ?? ???/* Enable DMA channel1 */
? ?? ???DMA_Cmd(DMA1_Channel1, ENABLE);
? ?? ???DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE); //傳輸結(jié)束中斷
? ?? ???
? ?? ???/* ADC1 configuration */? ?? ???
? ?? ???ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;? ?? ?? ?? ?? ?? ?? ?? ?//獨立ADC模式
? ?? ???ADC_InitStructure.ADC_ScanConvMode = DISABLE ;? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???//禁止掃描模式,掃描模式用于多通道采集
? ?? ???ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;? ?? ?? ?? ?? ?? ?? ?? ?//開啟連續(xù)轉(zhuǎn)換模式,即不停地進行ADC轉(zhuǎn)換
? ?? ???ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;? ?? ???//不使用外部觸發(fā)轉(zhuǎn)換
? ?? ???ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;? ?? ?? ?//采集數(shù)據(jù)右對齊
? ?? ???ADC_InitStructure.ADC_NbrOfChannel = 1;? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???//要轉(zhuǎn)換的通道數(shù)目1
? ?? ???ADC_Init(ADC1, &ADC_InitStructure);
? ?? ???
? ?? ???/*配置ADC時鐘,為PCLK2的8分頻,即9MHz*/
? ?? ???RCC_ADCCLKConfig(RCC_PCLK2_Div8);
? ?? ???/*配置ADC1的通道11為55.? ?? ???5個采樣周期,序列為1 */
? ?? ???ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5);
? ?? ???
? ?? ???/* Enable ADC1 DMA */
? ?? ???ADC_DMACmd(ADC1, ENABLE);
? ?? ???
? ?? ???/* Enable ADC1 */
? ?? ???ADC_Cmd(ADC1, ENABLE);
? ?? ???
? ?? ???/*復(fù)位校準寄存器 */? ?
? ?? ???ADC_ResetCalibration(ADC1);
? ?? ???/*等待校準寄存器復(fù)位完成 */
? ?? ???while(ADC_GetResetCalibrationStatus(ADC1));
? ?? ???
? ?? ???/* ADC校準 */
? ?? ???ADC_StartCalibration(ADC1);
? ?? ???/* 等待校準完成*/
? ?? ???while(ADC_GetCalibrationStatus(ADC1));
? ?? ???
? ?? ???/* 由于沒有采用外部觸發(fā),所以使用軟件觸發(fā)ADC轉(zhuǎn)換 */
? ?? ???ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
//============================================
//函數(shù)名稱:DMA1_Channel1_IRQHandler
//功能描述:DMA中斷 對AD采集值平均濾波
//輸入:無
//輸出:無
//============================================
void DMA1_Channel1_IRQHandler(void)
{? ?? ???
? ?? ???BYTE i;
? ?? ???WORD ADC_ConvertedValue_TEMP=0;
? ?? ???
? ?? ???for(i=0;i<15;i++)
? ?? ???{
? ?? ?? ?? ?? ? ADC_ConvertedValue_TEMP += ADC_ConvertedValue[i];
? ?? ???}
? ?? ???ADC_ConvertedValue_average = ADC_ConvertedValue_TEMP/15;? ???//全局變量ADC_ConvertedValue_average
? ?? ???
? ?? ???DMA_ClearITPendingBit(DMA1_IT_TC1);
? ?? ?? ? DMA_ClearFlag(DMA1_FLAG_TC1);
}
//============================================
//函數(shù)名稱:void GPIO_Configuration(void)
//功能描述:ADC引腳配置
//輸入:無
//輸出:無
//============================================
void GPIO_Configuration(void)
{
? ?? ???GPIO_InitTypeDef GPIO_InitStructure;? ?? ?? ?? ?? ?? ?? ?? ???
? ?? ???//=======================ADC通道配置========================
? ?? ???GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
? ?? ???GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
? ?? ???GPIO_Init(GPIOB, &GPIO_InitStructure);? ?? ?? ?? ?? ?? ?? ?? ?? ?? ???// PB0,輸入時不用設(shè)置速率
}
//============================================
//函數(shù)名稱:void NVIC_Configuration(void)
//功能描述:中斷配置
//輸入:無
//輸出:無
//============================================
void NVIC_Configuration(void)
{
? ?? ? NVIC_InitTypeDef NVIC_InitStructure;
? ?
? ?? ???/* Configure the NVIC Preemption Priority Bits */??
? ?? ? NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
? ?? ???
? ?? ???NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
? ?? ???NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
? ?? ???NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
? ?? ???NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
? ?? ???NVIC_Init(&NVIC_InitStructure);? ?? ???
}文章來源:http://www.zghlxwxcb.cn/news/detail-632061.html
參考:STM32 ADC采集,DMA傳輸 DMA中斷處理 - STM32/STM8單片機論壇 - ST MCU意法半導(dǎo)體官方技術(shù)支持論壇 - 21ic電子技術(shù)開發(fā)論壇文章來源地址http://www.zghlxwxcb.cn/news/detail-632061.html
到了這里,關(guān)于STM32 ADC采集 DMA中斷處理的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!