[STM8] TIM1使用普通重载定时器功能,程序卡死了

[复制链接]
3848|19
 楼主| wangyinhuai 发表于 2017-2-13 15:27 | 显示全部楼层 |阅读模式
如题,程序卡在TIM1_Cmd(ENABLE);这个函数里,我的函数如下,请大神看看哪里有问题,新人表示TIM1搞了好久,连中断函数都进不去,帮帮忙啊

这是主函数
  1. main()
  2. {
  3.         //u8 k,j;
  4.         CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
  5.         GPIO_Init(GPIOB,GPIO_PIN_5,GPIO_MODE_OUT_PP_HIGH_FAST);

  6.         TIM1_DeInit();//CLEAN
  7.         TIM1_TimeBaseInit(15,TIM1_COUNTERMODE_UP,1000,0);//INIT
  8.         TIM1_ARRPreloadConfig(ENABLE);//ENABLE AUTOMATIC RELOAD REGISTER
  9.         TIM1_ITConfig(TIM1_IT_UPDATE,ENABLE);//ENABLE UPDATE INTERRUPT
  10.         TIM1_Cmd(ENABLE);//START THE TIMER
  11.         //enableInterrupts();//ENABLE ALL INTERRUPT
  12.         while (1)
  13. {
  14. //        GPIO_WriteReverse(GPIOB,GPIO_PIN_5);
  15. //        for(k=1000;k>0;k--)
  16. //                for(j=1000;j>0;j--);
  17. }
  18. }

这是中断函数
  1. INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)
  2. {
  3.   /* In order to detect unexpected events during development,
  4.      it is recommended to set a breakpoint on the following instruction.
  5.   */
  6.         static u16 i=0;

  7.         if(i<1000)
  8. {
  9.         i++;
  10. }
  11. else
  12. {
  13.         i=0;
  14.         GPIO_WriteReverse(GPIOB,GPIO_PIN_5);
  15.         TIM1_ClearFlag(TIM1_IT_UPDATE);
  16. }
  17. }
 楼主| wangyinhuai 发表于 2017-2-13 15:28 | 显示全部楼层
功能就是实现一个led的一秒定时闪烁。
 楼主| wangyinhuai 发表于 2017-2-13 15:28 | 显示全部楼层
这是debug图片
无标题.png
 楼主| wangyinhuai 发表于 2017-2-13 15:30 | 显示全部楼层
用的是从st官网下的最新的库函数。
 楼主| wangyinhuai 发表于 2017-2-13 15:30 | 显示全部楼层
来个大神点拨一下
windows100 发表于 2017-2-13 16:02 | 显示全部楼层
  /* TIM1 counter enable */
           TIM1_Cmd(ENABLE);
  /* TIM1 Main Output Enable */
  TIM1_CtrlPWMOutputs(ENABLE);
使能中断要有,中断里if里要清标志
 楼主| wangyinhuai 发表于 2017-2-13 16:15 | 显示全部楼层
windows100 发表于 2017-2-13 16:02
/* TIM1 counter enable */
           TIM1_Cmd(ENABLE);
  /* TIM1 Main Output Enable */
  1.         TIM1_TimeBaseInit(15,TIM1_COUNTERMODE_UP,1000,0);//INIT
  2.         TIM1_ARRPreloadConfig(ENABLE);//ENABLE AUTOMATIC RELOAD REGISTER
  3.         TIM1_ITConfig(TIM1_IT_UPDATE,ENABLE);//ENABLE UPDATE INTERRUPT
  4.   TIM1_Cmd(ENABLE);//START THE TIMER
  5.         //enableInterrupts();//ENABLE ALL INTERRUPT
  6.          TIM1_CtrlPWMOutputs(ENABLE);
  1. TIM1_UPD_OVF_TRG_BRK_IRQHandler(void)
  2. {
  3. //                static u16 i=0;
  4. GPIO_WriteLow(GPIOB,GPIO_PIN_5);
  5. /*        if(i<1000)
  6. {
  7.         i++;
  8. }
  9. else
  10. {
  11.         i=0;
  12.         //GPIO_WriteReverse(GPIOB,GPIO_PIN_5);
  13.        
  14. }*/
  15. TIM1_ClearFlag(TIM1_IT_UPDATE);
  16. }

我做了修改。但是还是进不了中断
windows100 发表于 2017-2-13 16:21 | 显示全部楼层
使能中断要有,!!!!!!
enableInterrupts();//ENABLE ALL INTERRUPT 不要屏蔽,这一句要,放在调用函数后
 楼主| wangyinhuai 发表于 2017-2-13 16:34 | 显示全部楼层
windows100 发表于 2017-2-13 16:21
使能中断要有,!!!!!!
enableInterrupts();//ENABLE ALL INTERRUPT 不要屏蔽,这一句要,放在调用函数后 ...

好的!我好像也没有注册那个中断向量表,那个需要弄吗
  1. struct interrupt_vector const _vectab[] = {
  2.         {0x82, (interrupt_handler_t)_stext}, /* reset */
  3.         {0x82, NonHandledInterrupt}, /* trap  */
  4.         {0x82, NonHandledInterrupt}, /* irq0  */
  5.         {0x82, NonHandledInterrupt}, /* irq1  */
  6.         {0x82, NonHandledInterrupt}, /* irq2  */
  7.         {0x82, NonHandledInterrupt}, /* irq3  */
  8.         {0x82, NonHandledInterrupt}, /* irq4  */
  9.         {0x82, NonHandledInterrupt}, /* irq5  */
  10.         {0x82, NonHandledInterrupt}, /* irq6  */
  11.         {0x82, NonHandledInterrupt}, /* irq7  */
  12.         {0x82, NonHandledInterrupt}, /* irq8  */
  13.         {0x82, NonHandledInterrupt}, /* irq9  */
  14.         {0x82, NonHandledInterrupt}, /* irq10 */
  15.         {0x82, NonHandledInterrupt}, /* irq11 */
  16.         {0x82, NonHandledInterrupt}, /* irq12 */
  17.         {0x82, NonHandledInterrupt}, /* irq13 */
  18.         {0x82, NonHandledInterrupt}, /* irq14 */
  19.         {0x82, NonHandledInterrupt}, /* irq15 */
  20.         {0x82, NonHandledInterrupt}, /* irq16 */
  21.         {0x82, NonHandledInterrupt}, /* irq17 */
  22.         {0x82, NonHandledInterrupt}, /* irq18 */
  23.         {0x82, NonHandledInterrupt}, /* irq19 */
  24.         {0x82, NonHandledInterrupt}, /* irq20 */
  25.         {0x82, NonHandledInterrupt}, /* irq21 */
  26.         {0x82, NonHandledInterrupt}, /* irq22 */
  27.         {0x82, NonHandledInterrupt}, /* irq23 */
  28.         {0x82, NonHandledInterrupt}, /* irq24 */
  29.         {0x82, NonHandledInterrupt}, /* irq25 */
  30.         {0x82, NonHandledInterrupt}, /* irq26 */
  31.         {0x82, NonHandledInterrupt}, /* irq27 */
  32.         {0x82, NonHandledInterrupt}, /* irq28 */
  33.         {0x82, NonHandledInterrupt}, /* irq29 */
  34. };
windows100 发表于 2017-2-13 16:57 | 显示全部楼层
哥们,这个帮不到你了,我这是IAR的软件。我没弄过你这个。
我的中断服务函数是INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)
mmuuss586 发表于 2017-2-13 17:08 | 显示全部楼层
TIM1_ClearFlag(TIM1_IT_UPDATE);
这句放在进入中断程序里面,或退出地方;

另外中断初始化程序没看到啊
biechedan 发表于 2017-2-13 17:12 | 显示全部楼层
没有设置周期吗?
biechedan 发表于 2017-2-13 17:13 | 显示全部楼层
没有映射中断的时钟信号变量吧。
 楼主| wangyinhuai 发表于 2017-2-13 17:23 | 显示全部楼层
windows100 发表于 2017-2-13 16:57
哥们,这个帮不到你了,我这是IAR的软件。我没弄过你这个。
我的中断服务函数是INTERRUPT_HANDLER(TIM1_UPD ...

谢谢啦,我用的stvd。。
 楼主| wangyinhuai 发表于 2017-2-13 17:38 | 显示全部楼层
biechedan 发表于 2017-2-13 17:13
没有映射中断的时钟信号变量吧。
  1. INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)
  2. {
  3.   /* In order to detect unexpected events during development,
  4.      it is recommended to set a breakpoint on the following instruction.
  5.   */
  6.         //static u8 i=0;
  7.         //GPIO_WriteLow(GPIOB,GPIO_PIN_5);
  8.         static u8 i=0;
  9.         if(i<1000)
  10. {
  11.         i++;
  12. }
  13.         else
  14. {
  15.         i=0;
  16.         GPIO_WriteLow(GPIOB,GPIO_PIN_5);
  17. }
  18.         TIM1_ClearITPendingBit(TIM1_IT_UPDATE);
  19. }

刚刚注册了中断向量,填了向量表,刚刚测试过,现在可以进入中断函数了,但是,你看我在记满1000的地方,把led点亮,但是运行的时候并不亮。。是不是,标志位清除有问题?还是之前设置有问题
 楼主| wangyinhuai 发表于 2017-2-13 19:18 | 显示全部楼层
解决啦,static u8 i=0;这个变量只够计数到256,所以是行不通的,我改为int就没问题啦!多谢大家的指点
huangcunxiake 发表于 2017-2-13 19:21 | 显示全部楼层
wangyinhuai 发表于 2017-2-13 19:18
解决啦,static u8 i=0;这个变量只够计数到256,所以是行不通的,我改为int就没问题啦!多谢大家的指点{:lo ...

楼主厉害,这么 隐蔽的问题都查到了。
huangcunxiake 发表于 2017-2-13 19:22 | 显示全部楼层
static u8 i=0;
        if(i<1000)

话说,这里也太明显了,
不过还是为楼主点赞,知道这种错误会死机。。
huangcunxiake 发表于 2017-2-13 19:23 | 显示全部楼层
u8的数据只能在0到255范围波动,所以永远跳不出去。。。。确实有一套,
 楼主| wangyinhuai 发表于 2017-2-14 10:36 | 显示全部楼层
huangcunxiake 发表于 2017-2-13 19:23
u8的数据只能在0到255范围波动,所以永远跳不出去。。。。确实有一套,

哈哈哈
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

28

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部