大家好,最近在做一个项目关于PWM输入捕捉的,使用的单片机为STM8S105,我在用STM8的输入捕捉功能 利用库函数tiME1 CH1 捕捉PE0的频率 很准,但是我在此程序基础上将PE0口的频率 改为由 TIME2 产生,同样使用TIME1 CH1 捕捉,捕捉到的频率不对是怎么回事? TIME2 产生的PWM 用示波器测试很准的 望高手指点,谢谢!!!!!!
库函数捕捉程序如下:
void main(void)
{
TIM1_Channel = TIM1_CHANNEL_1
TIM1_ICPolarity = TIM1_ICPOLARITY_FALLING
TIM1_ICSelection = TIM1_ICSELECTION_DIRECTTI
TIM1_ICPrescaler = TIM1_ICPSC_DIV8
TIM1_ICFilter = 0x0
TIM1_ICInit( TIM1_CHANNEL_1, TIM1_ICPOLARITY_FALLING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV8, 0x0);
TIM1_ITConfig(TIM1_IT_CC1, ENABLE);
/* Enable TIM1 */
TIM1_Cmd(ENABLE);
/* Clear CC1 Flag*/
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* Connect LSI to COO pin*/
GPIO_Init(GPIOE, GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_FAST);
CLK_CCOConfig(CLK_OUTPUT_LSI);
CLK_CCOCmd(ENABLE);
/* wait a capture on CC1 */
while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
/* Get CCR1 value*/
ICValue1 = TIM1_GetCapture1();
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* wait a capture on cc1 */
while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
/* Get CCR1 value*/
ICValue2 = TIM1_GetCapture1();
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* Compute LSI clock frequency */
LSIClockFreq = (8 * TIM1ClockFreq) / (ICValue2 - ICValue1);
/* Insert a break point here */
while (1);
}
在此库函数基础上,改为TIME2产生PWM程序如下,
void init_hsiclk(void)
{
CLK_DeInit(); //寄存器复位
CLK_HSICmd(ENABLE); //内部高速时钟使能
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); //内部分频 2M
}
void TIM2_init(void)
{
TIM2_TimeBaseInit(TIM2_PRESCALER_16, 6665);
TIM2_OC3Init(TIM2_OCMODE_PWM1, TIM2_OUTPUTSTATE_ENABLE,CCR3_Val, TIM2_OCPOLARITY_HIGH);
TIM2_OC3PreloadConfig(ENABLE);
TIM2_ARRPreloadConfig(ENABLE);
TIM2_Cmd(ENABLE);
}
void main(void)
{
//CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);//主时钟16/16=1MHZ 4分频
init_hsiclk();
GPIO_Init(TIME2PWMOUT_PORT, TIME2PWMOUT_PIN , GPIO_MODE_OUT_PP_LOW_FAST); //设置PA3端口为推挽输出模式
GPIO_Init(TIME1PWMIN_PORT , TIME1PWMIN_PIN, GPIO_MODE_IN_FL_NO_IT);
TIM2_init();
TIM1_ICInit( TIM1_CHANNEL_1, TIM1_ICPOLARITY_FALLING, TIM1_ICSELECTION_DIRECTTI, TIM1_ICPSC_DIV8, 0x0);
/* Enable CC1 interrupt */
TIM1_ITConfig(TIM1_IT_CC1, ENABLE);
/* Enable TIM1 */
TIM1_Cmd(ENABLE);
/* Clear CC1 Flag*/
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* Connect LSI to COO pin*/
//GPIO_Init(GPIOE, GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_FAST); //不使用
//CLK_CCOConfig(CLK_OUTPUT_LSI);
//CLK_CCOCmd(ENABLE);
/* wait a capture on CC1 */
while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
/* Get CCR1 value*/
ICValue1 = TIM1_GetCapture1();
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* wait a capture on cc1 */
while((TIM1->SR1 & TIM1_FLAG_CC1) != TIM1_FLAG_CC1);
/* Get CCR1 value*/
ICValue2 = TIM1_GetCapture1();
TIM1_ClearFlag(TIM1_FLAG_CC1);
/* Compute LSI clock frequency */
LSIClockFreq = (8 * TIM1ClockFreq) / (ICValue2 - ICValue1);
/* Insert a break point here */
while (1);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
回一个看看,哈哈哈哈哈
一周热门 更多>