本帖最后由 tomps 于 2024-2-26 09:09 编辑
//TIM1
int main(void)
{
/*
单脉冲模式
PA10 TIM1_CH1 单脉冲输出通道
PA8 TIM1_CH2 输入捕获,用来捕获上升沿
*/
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRInitStruct;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
//PA10 TIM1_CH1 单脉冲输出通道
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PA8 TIM1_CH2 输入捕获,用来捕获上升沿
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_6);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 9999; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock / 1000000 - 1); //设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_RepetitionCounter = 1 - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//输出比较结构体
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //配置为PWM模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //CCER: CCxE 输出使能
TIM_OCInitStructure.TIM_Pulse = 0; //设置跳变时间
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //CCxP 输出通道电平极性配置
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
//
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
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 = 0;
TIM_ICInit(TIM1,&TIM_ICInitStructure);
TIM_ARRPreloadConfig(TIM1, ENABLE); //CR1:ARPE = 1,使能TIMx在ARR上的预装载寄存器
TIM_SelectOnePulseMode(TIM1, TIM_OPMode_Single);
TIM_SelectInputTrigger(TIM1, TIM_TS_TI2FP2);
TIM_SelectSlaveMode(TIM1,TIM_SlaveMode_Trigger);
TIM_SetCounter(TIM1,0);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_Cmd(TIM1, ENABLE);
while(1){
}
}
//TIM2
int main(void)
{
/*
单脉冲模式
PB3 TIM2_CH1 单脉冲输出通道
PB4 TIM2_CH2 输入捕获,用来捕获上升沿
*/
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
//PB3 TIM2_CH1 单脉冲输出通道
GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_7);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//PB4 TIM2_CH2 输入捕获,用来捕获上升沿
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_7);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 9999; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock / 1000000 - 1); //设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
//输出比较结构体
TIM_OCStructInit(&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //配置为PWM模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //CCER: CCxE 输出使能
TIM_OCInitStructure.TIM_Pulse = 5000; //设置跳变时间
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //CCxP 输出通道电平极性配置
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM2,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
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 = 0;
TIM_ICInit(TIM2,&TIM_ICInitStructure);
TIM_ARRPreloadConfig(TIM2, ENABLE); //CR1:ARPE = 1,使能TIMx在ARR上的预装载寄存器
TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);
TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
TIM_SelectSlaveMode(TIM2,TIM_SlaveMode_Trigger);
TIM_SetCounter(TIM2,0);
TIM_CtrlPWMOutputs(TIM2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
while(1){
}
}
|