21ic问答首页 - 国民技术 N32G455 TIM1 输入捕获 误触发
国民技术 N32G455 TIM1 输入捕获 误触发 赏3000家园币
yanhh12024-12-10
大家好:我使用国民技术的N32G455这款单片机的TM1 //TIM_CH_4做输入捕获,频率17~32K。设置是下降沿触发,但有时候上升沿也会触发,这是什么原因呢?
void pwm_int (void)
{
GPIO_InitStructure.Pin = GPIO_PIN_11;//TIM1_CH4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_9;//TIM1_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
NVIC_InitType NVIC_InitStructure;
ADTIMClockFrequency = (GET_RCC_Clocks_Pclk2Freq(TIM1));
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);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#if 1
TimerPeriod = (ADTIMClockFrequency / 33000) - 1;
/* Compute CCDAT1 value to generate a duty cycle at 50% for channel 1 */
Channel1Pulse = (uint16_t)(((uint32_t)5 * (TimerPeriod - 1)) / 10);
TIM_TimeBaseStructure1.Prescaler = 0;
TIM_TimeBaseStructure1.CntMode = TIM_CNT_MODE_UP;
TIM_TimeBaseStructure1.Period = TimerPeriod;
TIM_TimeBaseStructure1.ClkDiv = 0;
TIM_TimeBaseStructure1.RepetCnt = 0;
TIM_InitTimeBase(TIM1, &TIM_TimeBaseStructure1);
#endif
TIM_ICInitStructure.Channel = TIM_CH_2;
TIM_ICInitStructure.IcPolarity = TIM_IC_POLARITY_RISING;
TIM_ICInitStructure.IcSelection = TIM_IC_SELECTION_DIRECTTI;
TIM_ICInitStructure.IcPrescaler = TIM_IC_PSC_DIV1;
TIM_ICInitStructure.IcFilter = 0x0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
#if 1
/* TIM1 Input Capture Configuration */
TIM_ICInitStructure.Channel = TIM_CH_4;
TIM_ICInitStructure.IcPolarity = TIM_IC_POLARITY_RISING;
TIM_ICInitStructure.IcSelection = TIM_IC_SELECTION_DIRECTTI;
TIM_ICInitStructure.IcPrescaler = TIM_IC_PSC_DIV1;
TIM_ICInitStructure.IcFilter = 0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
#endif
TIM_Output_Channel_Struct_Initialize(&TIM_OCInitStructure);
/* Channel 1, 2 and 3 Configuration in PWM mode */
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;//PWM模式1
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;//输出使能
TIM_OCInitStructure.OutputNState = TIM_OUTPUT_NSTATE_ENABLE;//互补输出使能
TIM_OCInitStructure.Pulse = Channel1Pulse;//占空比
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_HIGH;//输出有效电平为高电平
TIM_OCInitStructure.OcNPolarity = TIM_OCN_POLARITY_HIGH;//互补输出有效电平为高电平
TIM_OCInitStructure.OcIdleState = TIM_OC_IDLE_STATE_RESET;//输出空闲时为高电平
TIM_OCInitStructure.OcNIdleState = TIM_OCN_IDLE_STATE_RESET;//互补输出空闲时为低电平
TIM_InitOc1(TIM1, &TIM_OCInitStructure);
//#endif
TIM_Output_Channel_Struct_Initialize(&TIM_OCInitStructure);
/* Channel 1, 2 and 3 Configuration in PWM mode */
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;//PWM模式1
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;//输出使能
TIM_OCInitStructure.OutputNState = TIM_OUTPUT_NSTATE_DISABLE;//互补输出使能
TIM_OCInitStructure.Pulse = Channel1Pulse;//占空比
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_LOW;//输出有效电平为高电平
TIM_OCInitStructure.OcNPolarity = TIM_OCN_POLARITY_LOW;//互补输出有效电平为高电平
TIM_OCInitStructure.OcIdleState = TIM_OC_IDLE_STATE_RESET;//输出空闲时为高电平
TIM_OCInitStructure.OcNIdleState = TIM_OCN_IDLE_STATE_RESET;//互补输出空闲时为低电平
TIM_InitOc3(TIM1, &TIM_OCInitStructure);
#if 1
m_delay_ms(2);
Half_all_** = GPIO_ReadInputDataBit(GPIOB,GPIO_PIN_7);
TIM_BDTRInitStructure.OssrState = TIM_OSSR_STATE_ENABLE; //运行模式下“关闭模式”=1
TIM_BDTRInitStructure.OssiState = TIM_OSSI_STATE_ENABLE;//空闲模式下“关闭模式”=1
TIM_BDTRInitStructure.LockLevel = TIM_LOCK_LEVEL_1;//锁定级别
TIM_BDTRInitStructure.DeadTime = Dead_Time;
TIM_BDTRInitStructure.Break = TIM_BREAK_IN_DISABLE;//开启输出功能
TIM_BDTRInitStructure.BreakPolarity = TIM_BREAK_POLARITY_HIGH;
TIM_BDTRInitStructure.AutomaticOutput = TIM_AUTO_OUTPUT_DISABLE;//开启自动输出
TIM_BDTRInitStructure.IomBreakEn = true;
TIM_ConfigBkdt(TIM1, &TIM_BDTRInitStructure);
#endif
TIM_ConfigInt(TIM1, TIM_INT_UPDATE|TIM_INT_CC4, ENABLE);
Fer_test_PWM = 32000;
TIM_Enable(TIM1,DISABLE);
}
void TIM1_CC_IRQHandler(void)
{
if (TIM_GetIntStatus(TIM1, TIM_INT_CC4) == SET)
{
TIM_ClrIntPendingBit(TIM1, TIM_INT_CC4);
TIM1_CH4_count = TIM_GetCap4(TIM1);
}
}
void pwm_int (void)
{
GPIO_InitStructure.Pin = GPIO_PIN_11;//TIM1_CH4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_9;//TIM1_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure);
NVIC_InitType NVIC_InitStructure;
ADTIMClockFrequency = (GET_RCC_Clocks_Pclk2Freq(TIM1));
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);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
#if 1
TimerPeriod = (ADTIMClockFrequency / 33000) - 1;
/* Compute CCDAT1 value to generate a duty cycle at 50% for channel 1 */
Channel1Pulse = (uint16_t)(((uint32_t)5 * (TimerPeriod - 1)) / 10);
TIM_TimeBaseStructure1.Prescaler = 0;
TIM_TimeBaseStructure1.CntMode = TIM_CNT_MODE_UP;
TIM_TimeBaseStructure1.Period = TimerPeriod;
TIM_TimeBaseStructure1.ClkDiv = 0;
TIM_TimeBaseStructure1.RepetCnt = 0;
TIM_InitTimeBase(TIM1, &TIM_TimeBaseStructure1);
#endif
TIM_ICInitStructure.Channel = TIM_CH_2;
TIM_ICInitStructure.IcPolarity = TIM_IC_POLARITY_RISING;
TIM_ICInitStructure.IcSelection = TIM_IC_SELECTION_DIRECTTI;
TIM_ICInitStructure.IcPrescaler = TIM_IC_PSC_DIV1;
TIM_ICInitStructure.IcFilter = 0x0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
#if 1
/* TIM1 Input Capture Configuration */
TIM_ICInitStructure.Channel = TIM_CH_4;
TIM_ICInitStructure.IcPolarity = TIM_IC_POLARITY_RISING;
TIM_ICInitStructure.IcSelection = TIM_IC_SELECTION_DIRECTTI;
TIM_ICInitStructure.IcPrescaler = TIM_IC_PSC_DIV1;
TIM_ICInitStructure.IcFilter = 0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
#endif
TIM_Output_Channel_Struct_Initialize(&TIM_OCInitStructure);
/* Channel 1, 2 and 3 Configuration in PWM mode */
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;//PWM模式1
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;//输出使能
TIM_OCInitStructure.OutputNState = TIM_OUTPUT_NSTATE_ENABLE;//互补输出使能
TIM_OCInitStructure.Pulse = Channel1Pulse;//占空比
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_HIGH;//输出有效电平为高电平
TIM_OCInitStructure.OcNPolarity = TIM_OCN_POLARITY_HIGH;//互补输出有效电平为高电平
TIM_OCInitStructure.OcIdleState = TIM_OC_IDLE_STATE_RESET;//输出空闲时为高电平
TIM_OCInitStructure.OcNIdleState = TIM_OCN_IDLE_STATE_RESET;//互补输出空闲时为低电平
TIM_InitOc1(TIM1, &TIM_OCInitStructure);
//#endif
TIM_Output_Channel_Struct_Initialize(&TIM_OCInitStructure);
/* Channel 1, 2 and 3 Configuration in PWM mode */
TIM_OCInitStructure.OcMode = TIM_OCMODE_PWM1;//PWM模式1
TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;//输出使能
TIM_OCInitStructure.OutputNState = TIM_OUTPUT_NSTATE_DISABLE;//互补输出使能
TIM_OCInitStructure.Pulse = Channel1Pulse;//占空比
TIM_OCInitStructure.OcPolarity = TIM_OC_POLARITY_LOW;//输出有效电平为高电平
TIM_OCInitStructure.OcNPolarity = TIM_OCN_POLARITY_LOW;//互补输出有效电平为高电平
TIM_OCInitStructure.OcIdleState = TIM_OC_IDLE_STATE_RESET;//输出空闲时为高电平
TIM_OCInitStructure.OcNIdleState = TIM_OCN_IDLE_STATE_RESET;//互补输出空闲时为低电平
TIM_InitOc3(TIM1, &TIM_OCInitStructure);
#if 1
m_delay_ms(2);
Half_all_** = GPIO_ReadInputDataBit(GPIOB,GPIO_PIN_7);
TIM_BDTRInitStructure.OssrState = TIM_OSSR_STATE_ENABLE; //运行模式下“关闭模式”=1
TIM_BDTRInitStructure.OssiState = TIM_OSSI_STATE_ENABLE;//空闲模式下“关闭模式”=1
TIM_BDTRInitStructure.LockLevel = TIM_LOCK_LEVEL_1;//锁定级别
TIM_BDTRInitStructure.DeadTime = Dead_Time;
TIM_BDTRInitStructure.Break = TIM_BREAK_IN_DISABLE;//开启输出功能
TIM_BDTRInitStructure.BreakPolarity = TIM_BREAK_POLARITY_HIGH;
TIM_BDTRInitStructure.AutomaticOutput = TIM_AUTO_OUTPUT_DISABLE;//开启自动输出
TIM_BDTRInitStructure.IomBreakEn = true;
TIM_ConfigBkdt(TIM1, &TIM_BDTRInitStructure);
#endif
TIM_ConfigInt(TIM1, TIM_INT_UPDATE|TIM_INT_CC4, ENABLE);
Fer_test_PWM = 32000;
TIM_Enable(TIM1,DISABLE);
}
void TIM1_CC_IRQHandler(void)
{
if (TIM_GetIntStatus(TIM1, TIM_INT_CC4) == SET)
{
TIM_ClrIntPendingBit(TIM1, TIM_INT_CC4);
TIM1_CH4_count = TIM_GetCap4(TIM1);
}
}
您需要登录后才可以回复 登录 | 注册