最重要的下载程序的option bety 设置;
函数1:
void Init_Timer1(void) { GPIO_Init(GPIOC, GPIO_PIN_6, GPIO_MODE_IN_PU_NO_IT); //输入浮动,没有外部中断 TIM1_DeInit(); //tim1 定时器寄存器全部复位 TIM1_TimeBaseInit(31, TIM1_COUNTERMODE_UP, 15625-1, 0);// 500ms 最小捕获5hz 设置时间分频 向上计数 捕捉5Hz及 以上的周期和占空比 TIM1_PWMIConfig(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV1, 0x00); //选择TI1输入上升沿触发 选择TI2输入下降沿触发 输入捕捉预定标器 无滤波 TIM1_SelectInputTrigger(TIM1_TS_TI1FP1); //滤波后输入TI1 TIM1_SelectSlaveMode(TIM1_SLAVEMODE_RESET); //复位输入 TIM1_ClearFlag(TIM1_FLAG_CC1); //指定要清除的标志 TIM1_ClearFlag(TIM1_FLAG_CC2); //指定要清除的标志 TIM1_ITConfig( TIM1_IT_CC1, ENABLE); //去掉是查询方式,否则是中断方式 TIM1_ITConfig( TIM1_IT_CC2, ENABLE); //去掉是查询方式,否则是中断方式 TIM1_CCxCmd(TIM1_CHANNEL_1, ENABLE); //使能输入捕获通道1 TIM1_CCxCmd(TIM1_CHANNEL_2, ENABLE); //使能输入捕获通道2 TIM1_Cmd(ENABLE); //使能定时器 }TIME1_INIT
函数2:
extern __IO uint32_t LSIClockFreq; extern __IO uint32_t LSIDuty; INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12) { /* In order to detect unexpected events during development, it is recommended to set a breakpoint on the following instruction. */ uint16_t num = 0; static uint16_t num1; static uint32_t num2; if(TIM1_GetITStatus(TIM1_IT_CC1) != RESET) //如果CH1边沿触 { num1 = TIM1_GetCapture1(); TIM1_ClearITPendingBit(TIM1_IT_CC1); //清除标志位 } else if(TIM1_GetITStatus(TIM1_IT_CC2) != RESET) //如果CH2边沿触发 { num2 = TIM1_GetCapture2(); // TIM1_ClearITPendingBit(TIM1_IT_CC2); //清除标志位 if(flag_send==0) { LSIDuty = ((num2 * 100) / num1); //计算占空比 SIClockFreq = 62500/( num1); //计算频率 if(LSIClockFreq>=0xff) { LSIClockFreq=0xff; } } } }中断12
标签:STM8,ENABLE,num1,--,CC1,CC2,TIM1,PWM,输入 From: https://www.cnblogs.com/zhouyuqing1024/p/18219641