1. 程式人生 > >STM32F103 定時器+ADC+DMA中斷+雙快取+實現資料採集

STM32F103 定時器+ADC+DMA中斷+雙快取+實現資料採集

功能

TIM1_CC1觸發ADC1進行取樣,取樣使用DMA進行資料傳輸,DMA啟用半滿和全滿中斷,在中斷中進行資料儲存到其他Buff,方便進行處理。

環境

硬體平臺:STM32F103ZE
開發環境:MDK 4.70
作業系統:Windows 10

程式碼

ADCongif.h

#ifndef __ADCONFIG_H_
#define __ADCONFIG_H_

void ADC_Configuration(void);

void ADC1_Init(void);
void ADC1_DMA1_Init(void);
void ADC1_ExternalTrig_T1_CC1_Init(void
); void ADC1_GPIO_Config(void); #endif

ADConfig.c

/************************************************** 
 *  檔名: ADConfig.c file
 *  描述:  ADC配置檔案
 *------------------------------------------------
 * 作者: xbotao
 * 建立時間:2016-03-02
 * 最後修改:2016-03-02
 * 
 * Copyright (c) 2016 Xbotao
 * 
 *************************************************/

#include <stdio.h>
#include "string.h"
#include "stm32f10x.h" #include "ADConfig.h" #define ADC_BUFF_LEN 200 #define ADC1_DR_Address ((uint32_t)0x4001244C) uint16_t ADC_ConvertedValue[ADC_BUFF_LEN]; uint16_t WriteBuff[ADC_BUFF_LEN]; void ADC_Configuration(void) { ADC1_GPIO_Config(); ADC1_ExternalTrig_T1_CC1_Init(); ADC1_DMA1_Init();
ADC1_Init(); } void ADC1_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE); /* Configure TIM1_CH1 (PA8) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure PC.01 and PC.04 (ADC Channel11 and Channel14) as analog input */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOC, &GPIO_InitStructure); } void ADC1_Init(void) { ADC_InitTypeDef ADC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); ADC_TempSensorVrefintCmd(ENABLE); ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_13Cycles5); ADC_Cmd(ADC1, ENABLE); ADC_ExternalTrigConvCmd(ADC1, ENABLE); ADC_DMACmd(ADC1, ENABLE); ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1)); } void ADC1_DMA1_Init(void) { DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* DMA1 Channel1 Configuration ----------------------------------------------*/ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_ConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = ADC_BUFF_LEN; 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 | DMA_IT_HT, ENABLE); /* Enable DMA1 channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); } void ADC1_ExternalTrig_T1_CC1_Init(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); /* Time Base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 1000-1; TIM_TimeBaseStructure.TIM_Prescaler = 72-1; TIM_TimeBaseStructure.TIM_ClockDivision = 0x00; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /* TIM1 channel1 configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 500; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC1Init(TIM1, &TIM_OCInitStructure); TIM_CtrlPWMOutputs(TIM1, ENABLE); TIM_Cmd(TIM1, DISABLE); } void ADC1_DMA1_IT_Hander(void) { if(DMA_GetFlagStatus(DMA1_FLAG_HT1)) { DMA_ClearITPendingBit(DMA_IT_HT); memcpy(WriteBuff, ADC_ConvertedValue, ADC_BUFF_LEN*sizeof(uint16_t)/2); } if(DMA_GetFlagStatus(DMA1_FLAG_TC1)) { DMA_ClearITPendingBit(DMA1_FLAG_TC1); memcpy(WriteBuff+ADC_BUFF_LEN/2, ADC_ConvertedValue+ADC_BUFF_LEN/2, ADC_BUFF_LEN*sizeof(uint16_t)/2); //此處可以增加標誌位 } }

main.c

#include "stm32f10x.h"
#include "ADConfig.h"

int main(void)
{
    ADC_Configuration();
    TIM_Cmd(TIM1, ENABLE);

    while (1)
    {
    }
}