#include "includes.h"
TIM_ICInitTypeDef TIM5_ICInitStructure;
//定时器5通道1输入捕获配置
//arr:自动重装值(TIM2,TIM5是32位的!!)
//psc:时钟预分频数
void TIM5_CH1_Cap_DMA_Init(u32 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE); //TIM5时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //使能PORTA时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //使能PORTD时钟
NVIC_InitStructure.NVIC_IRQChannel=DMA1_Stream2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All; //GPIOD
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
//GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //下拉
GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化PD
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //GPIOA0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //下拉
GPIO_Init(GPIOA,&GPIO_InitStructure); //初始化PA0
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_TIM5); //PA0复用位定时器5
TIM_TimeBaseStructure.TIM_Period = arr; //自动重装载值
TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseStructure);//初始化TIM5
//初始化TIM2输入捕获参数
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 选择输入端 IC1映射到TI1上
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM5_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
//我将一个普通的引脚按一定频率进行电平翻转 此引脚连接到了 TIM5的ch1(PA0上)
//将下面这段代码添加上 在定时器5的中断里 判断进入捕获的频率 的确与我产生的脉冲频率一致
//也就是说 定时器捕获以及引脚都是正常的
/*TIM_ITConfig(TIM5,TIM_IT_CC1|TIM_IT_Update,ENABLE);//允许更新中断 ,允许CC1IE捕获中断
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
*/
//Enable TIM5 counter
TIM_Cmd( TIM5, ENABLE );
DMA_DeInit(DMA1_Stream2);
while (DMA_GetCmdStatus(DMA1_Stream2) != DISABLE){}
//DMA_StructInit( &DMA_InitStructure);
//DMA1 Stream2 channel6 configuration *************************************
DMA_InitStructure.DMA_Channel = DMA_Channel_6;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &GPIOD->IDR;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCConvertedValue0;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 8192*4-2;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc =DMA_MemoryInc_Enable ;//
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//DMA_Mode_Normal;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;//DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_DoubleBufferModeConfig(DMA1_Stream2,(uint32_t)ADCConvertedValue1,DMA_Memory_0);
DMA_DoubleBufferModeCmd(DMA1_Stream2,ENABLE);
DMA_Init(DMA1_Stream2,&DMA_InitStructure);
// DMA_Cmd(DMA1_Stream2, DISABLE); //关闭DMA传输
while (DMA_GetCmdStatus(DMA1_Stream2) != DISABLE); //确保DMA可以被设置
//DMA_SetCurrDataCounter(DMA1_Stream2,8192*4-2); //数据传输量
DMA_ITConfig(DMA1_Stream2,DMA_IT_TC,ENABLE); //传输完成中断
DMA_ClearFlag(DMA1_Stream2,DMA_IT_TCIF2);//
TIM_DMACmd(TIM5,TIM_DMA_CC1,ENABLE );
DMA_Cmd(DMA1_Stream2, ENABLE);
}
//DMA
void DMA1_Stream2_IRQHandler(void)
{
if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1))
{
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
else
{
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
if(DMA_GetFlagStatus(DMA1_Stream2,DMA_IT_TCIF2)==SET)
{
DMA_ClearFlag(DMA1_Stream2,DMA_IT_TCIF2);//
//GPIO_ResetBits(GPIOA,GPIO_Pin_1);
//DMA_ITConfig(DMA1_Stream2,DMA_IT_TC,DISABLE);//
DMA_free_buf_OK = 1;
if(transt_count++ >65530)
{
transt_count = 1;
}
}
} |