完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
大家好,最近在做一个项目关于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); } |
|
相关推荐
5个回答
|
|
谢谢分享学习一下
|
|
|
|
|
|
|
|
楼主有何高见没?
|
|
|
|
|
|
|
|
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
1187 浏览 0 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1122 浏览 2 评论
2223 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1311 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1735 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-27 05:34 , Processed in 0.617215 second(s), Total 79, Slave 63 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号