我的芯片是STM32F103ZET6 打算使用TIM4进行PWM信号的捕获,从手册得知是绑定Channel1和Channel2 Channel3和Channel4 CC1和CC3捕获高电平 CC2和CC4捕获低电平 但是在实际编码中,在线调试发现始终是CC1/2在工作,CC3/4处于冻结状态,这样就无法采集Channel2所输入的信号。尝试过修改TIM的捕获设置的每个选项,发现没有效果,不知道问题所在。我的代码如下:
#include "stm32f10x.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_tim.h"
TIM_OCInitTypeDef TIM_OCInitStructure;
void Rcc_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Config(void);
void TIM_Configuration(void);
void Delay(uint32_t nCounts);
int IC2Value = 0;
int main ()
{
Rcc_Configuration();
NVIC_Configuration();
GPIO_Config();
TIM_Configuration();
while(1)
{}
}
void Rcc_Configuration(void)
{
SystemInit();
//启动GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE,ENABLE);
//启动AFIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
//启动TIM4
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
}
void NVIC_Configuration()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //NVIC配置
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Delay(uint32_t nCounts)
{
u32 uCounts = nCounts;
for(;nCounts > 0;nCounts--)
for(;uCounts > 0;uCounts--);
}
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//TIM4 PB7 CH2 PB9 CH4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_8|GPIO_Pin_7|GPIO_Pin_9; //GPIO配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//LED
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
//TIM4设置
//时基设置
TIM_BaseInitStructure.TIM_Prescaler = 36-1;
TIM_BaseInitStructure.TIM_ClockDivision = 0;
TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM4,&TIM_BaseInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2|TIM_Channel_4; //通道选择
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿触发
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI ; //管脚与寄存器对应关系
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //输入预分频。
TIM_ICInitStructure.TIM_ICFilter = 0x0; //滤波设置,经历几个周期跳变认定波形稳定0x0~0xF
TIM_PWMIConfig(TIM4, &TIM_ICInitStructure); //根据参数配置TIM外设信息
TIM_SelectInputTrigger(TIM4, TIM_TS_TI1FP1|TIM_TS_TI2FP2); //选择IC1,2为触发源 这里不大懂触发源是怎么分配到捕获单元的
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);//TIM从模式:触发信号的上升沿重新初始化计数器和触发寄存器的更新事件
TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable); //启动定时器的被动触发
TIM_Cmd(TIM4, ENABLE); //启动TIM4
TIM_ITConfig(TIM4, TIM_IT_CC2|TIM_IT_CC4, ENABLE); //打开中断
}
中断处理函数:
void TIM4_IRQHandler(void)
{
extern u16 IC2Value;
if(TIM_GetITStatus(TIM4,TIM_IT_CC1) == SET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC1); //清楚TIM的中断待处理位
IC2Value = TIM_GetCapture1(TIM4);
}else if(TIM_GetITStatus(TIM4,TIM_IT_CC4) == SET)
{
TIM_ClearITPendingBit(TIM4, TIM_IT_CC4); //清楚TIM的中断待处理位
IC2Value = TIM_GetCapture4(TIM4);
}
} |