本文主要是介绍STM8S003F3P6定时器输入捕获和输出比较,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
输入捕获定时器初始化
#include "tim1.h"
#include "common.h"
#include "stm8s.h"
/*** @brief Configure TIM1 16mhz* @param None* @retval None*/
void STM8_TIM1_Init(void) /* 定时器1初始化 ,配置为输入捕获 测频率和脉宽 */
{// 16分频 16M/(0x0f)=1M CNTR计数一次为 1/1M=1usTIM1_TimeBaseInit( 0x0f, TIM1_COUNTERMODE_UP, 0xffff, 0 ); TIM1_ICInit( TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV1, 0x0a );TIM1_ICInit( TIM1_CHANNEL_2, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV1, 0x0a );TIM1_ICInit( TIM1_CHANNEL_3, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV1, 0x0a );TIM1_ITConfig( TIM1_IT_CC1, ENABLE );TIM1_ITConfig( TIM1_IT_CC2, ENABLE );TIM1_ITConfig( TIM1_IT_CC3, ENABLE );// TIM1_ITConfig( TIM1_IT_UPDATE, ENABLE );TIM1_Cmd(DISABLE );TIM1_ClearITPendingBit( TIM1_IT_CC1 );TIM1_ClearITPendingBit( TIM1_IT_CC2 );TIM1_ClearITPendingBit( TIM1_IT_CC3 );
}
输入捕获定时器中断函数
捕获2个周期以获得频率、周期、占空比
//TIM1_CH1-CH3通道捕获
void TIM1_ISR_Capture( void )
{static uint16_t overload=0; //定时器溢出计数/* 输入捕获 */if (TIM1_GetITStatus(TIM1_IT_CC1) != RESET){/* CH1 */static uint8_t ch1_cc_bz = 0;uint16_t ch1_ICValue = 0;uint16_t ch1_ICValue1 = 0;static uint16_t ch1_num1;static uint16_t ch1_num2;static uint16_t ch1_num3;static uint16_t ch1_num4;switch(ch1_cc_bz){case 0:// TIM1_SetCompare1( 0 );ch1_num1 = TIM1_GetCapture1();TIM1->CCER1 &= (uint8_t)(~TIM1_CCER1_CC1P);//设为上升沿捕获ch1_cc_bz = 1;break;case 1:ch1_num2 = TIM1_GetCapture1(
这篇关于STM8S003F3P6定时器输入捕获和输出比较的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!