STM32F2定时器1通道1输入捕获

STM32F2定时器1通道1输入捕获

一、中文手册上的配置步骤

STM32F2定时器1通道1输入捕获_第1张图片1.1初始化通道对应的IO口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//选择通道对应的IO口
STM32F2定时器1通道1输入捕获_第2张图片
1.2选择输入捕获的通道
InitStructure.TIM_Channel = TIM_Channel_1;
1.3选择有效输入端
选择有效输入端:TIMx_CCR1必须连接到TI1输入,所以写入TIMx_CCR1寄存器中的
CC1S=01,只要CC1S不为’00’,通道被配置为输入
,并且TIMx_CCR1寄存器变为只读
注意 CC1S仅在通道关闭时(TIMx_CCER寄存器的CC1E=0)才是可写的,配置之前调用
void TIM_DeInit(TIM_TypeDef* TIMx)即可
STM32F2定时器1通道1输入捕获_第3张图片1.3配置滤波器为所需带宽–TIM_ICInitStructure.TIM_ICFilter = 0x00;
根据输入信号的特点,配置输入滤波器为所需的带宽(即输入为TIx时,输入滤波器控制位是
TIMx_CCMRx寄存器中的ICxF位)。假设输入信号在最多5个内部时钟周期的时间内抖动,
我们须配置滤波器的带宽长于5个时钟周期;因此我们可以(以f DTS 频率)连续采样8次,以确
认在TI1上一次真实的边沿变换,即在TIMx_CCMR1寄存器中写入IC1F=0011。
1.4选择有效转换边沿–TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
选择TI1通道的有效转换边沿,在TIMx_CCER寄存器中写入CC1P=0(上升沿)。
STM32F2定时器1通道1输入捕获_第4张图片1.5配置输入预分频器–TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
在本例中,我们希望捕获发生在每一个有效的电平转换时刻,因此预
分频器被禁止(写TIMx_CCMR1寄存器的IC1PS=00)。
1.6 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICSelection_DirectTI -->TI1,TI2,TI3,TI4 对应IC1,IC2,IC3,IC4;
TIM_ICSelection_IndirectTI–>TI1,TI2,TI3,TI4 对应IC2,IC1,IC4,IC3;
STM32F2定时器1通道1输入捕获_第5张图片2.1完整代码
`

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 
GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_10;//根据通道所对应的IO口修改
GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP ;
GPIO_Init(GPIOA, &GPIO_InitStructure);  
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_TIM1);

TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//TIM_Input_Capture_Polarity 
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//TIM_Input_Capture_Selection
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM1, &TIM_ICInitStructure);

NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

`
3.1输入捕获启动DMA传输配置

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 , ENABLE);
	DMA_DeInit(DMA2_Stream6);
	DMA_InitStructure.DMA_Channel = DMA_Channel_6;  
	DMA_InitStructure.DMA_PeripheralBaseAddr =(uint32_t) GPIOD->IDR;
	DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)DEF_Buffer;
	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
	DMA_InitStructure.DMA_BufferSize = 40000;
	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte;
	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
	DMA_InitStructure.DMA_Priority = DMA_Priority_High;
	DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
	DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
	DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
	DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
	DMA_Init(DMA2_Stream6, &DMA_InitStructure);

3.2启动传输

	TIM_DMAConfig(TIM1, TIM_DMABase_CCR1, TIM_DMABurstLength_1Transfer);
	TIM_DMACmd(TIM1, TIM_DMA_CC1, ENABLE);
	
	/* Enable the CC2 Interrupt Request */
	TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
	/* TIM enable counter */
	TIM_Cmd(TIM1, ENABLE);
	DMA_Cmd(DMA2_Stream6, ENABLE);

你可能感兴趣的:(STM32F2定时器1通道1输入捕获)