[技术问答] 新手求助M031关于定时器计数中断问题

[复制链接]
1688|11
 楼主| liuyuncs11 发表于 2020-5-8 15:36 | 显示全部楼层 |阅读模式
  1. #include <stdio.h>
  2. #include "NuMicro.h"


  3. void TMR0_IRQHandler(void)
  4. {
  5. //  printf("Count 1000 falling events! Test complete !\n");
  6.     TIMER_ClearIntFlag(TIMER0);
  7. }

  8. void SYS_Init(void)
  9. {
  10.     /* Unlock protected registers */
  11.     SYS_UnlockReg();

  12.     /* Enable HIRC */
  13.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  14.     /* Waiting for HIRC clock ready */
  15.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

  16.     /* Switch HCLK clock source to HIRC */
  17.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

  18.     /* Set both PCLK0 and PCLK1 as HCLK/2 */
  19.     CLK->PCLKDIV = (CLK_PCLKDIV_APB0DIV_DIV2 | CLK_PCLKDIV_APB1DIV_DIV2);

  20.     /* Switch UART0 clock source to HIRC */
  21.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  22.     /* Enable UART peripheral clock */
  23.     CLK_EnableModuleClock(UART0_MODULE);

  24.     /* Enable IP clock */
  25.     CLK_EnableModuleClock(TMR0_MODULE);

  26.     /* Select IP clock source */
  27.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_HIRC, 0);

  28.     /* Update System Core Clock */
  29.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  30.     SystemCoreClockUpdate();

  31.     /*----------------------------------------------------------------------*/
  32.     /* Init I/O Multi-function                                              */
  33.     /*----------------------------------------------------------------------*/
  34.     /* Set GPB multi-function pins for UART0 RXD and TXD */
  35.     SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk)) |
  36.                     (SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

  37.     /* Set timer event counting pin */
  38. //    SYS->GPB_MFPL = (SYS->GPB_MFPL & ~(SYS_GPB_MFPL_PB5MFP_Msk)) |
  39. //                    (SYS_GPB_MFPL_PB5MFP_TM0);

  40.     /* Lock protected registers */
  41.     SYS_LockReg();
  42. }

  43. /*----------------------------------------------------------------------*/
  44. /* Init UART0                                                           */
  45. /*----------------------------------------------------------------------*/
  46. void UART0_Init(void)
  47. {
  48.     /* Reset UART0 */
  49.     SYS_ResetModule(UART0_RST);

  50.     /* Configure UART0 and set UART0 baud rate */
  51.     UART_Open(UART0, 115200);
  52. }

  53. int main(void)
  54. {
  55.     int i;

  56.     /* Init System, IP clock and multi-function I/O. */
  57.     SYS_Init();

  58.     /* Init UART0 for printf */
  59.     UART0_Init();

  60.     printf("\nThis sample code use TM0 (PB.5) to count PB.4 input event\n");
  61.     printf("Please connect PB.5 to PB.4, press any key to continue\n");


  62. //    PB4 = 0;    /* Set init state to high */
  63. //    GPIO_SetMode(PB, BIT4, GPIO_MODE_OUTPUT);
  64.           GPIO_SetMode(PB, BIT14, GPIO_MODE_OUTPUT);

  65.     /* Give a dummy target frequency here. Will over write prescale and compare value with macro */
  66.     TIMER_Open(TIMER0, TIMER_ONESHOT_MODE, 1);

  67.     /* Update prescale and compare value to what we need in event counter mode. */
  68.     TIMER_SET_PRESCALE_VALUE(TIMER0, 0);
  69.     TIMER_SET_CMP_VALUE(TIMER0, 56789);
  70.     /* Counter increase on falling edge */
  71. //    TIMER_EnableEventCounter(TIMER0, TIMER_COUNTER_FALLING_EDGE);
  72.     /* Start Timer 0 */
  73.     TIMER_Start(TIMER0);
  74.     /* Enable timer interrupt */
  75.     TIMER_EnableInt(TIMER0);
  76.     NVIC_EnableIRQ(TMR0_IRQn);     
  77.                
  78.                 if(TIMER_GetCounter(TIMER0) == 56789)
  79.                 {
  80.                         for(i = 0; i < 10; i++)
  81.                         {
  82.                                 PB14 ^= 1;
  83.                         }
  84.                 }
  85.     while(1);
  86.                
  87. }
根据例程修改而来,if里面的TIMER_GetCounter(TIMER0) == 56789不对么?完全进不去if中的循环
微信截图_20200508153221.png
 楼主| liuyuncs11 发表于 2020-5-8 15:50 | 显示全部楼层
例程是外部IO口短接到PB5上触发的计数中断,应该不用外部触发就能实现计数中断吧,请大神们指教一下
wanduzi 发表于 2020-5-8 21:53 | 显示全部楼层
例子能跑的动吗
wanduzi 发表于 2020-5-8 21:55 | 显示全部楼层
原版的例子。
wanduzi 发表于 2020-5-8 22:24 | 显示全部楼层
    TIMER_Open(TIMER1, TIMER_CONTINUOUS_MODE, 1);
模式错误,应该连续计数模式。你搞的一次模式肯定不行。
wanduzi 发表于 2020-5-8 22:34 | 显示全部楼层
连续模式
 楼主| liuyuncs11 发表于 2020-5-9 08:47 | 显示全部楼层
wanduzi 发表于 2020-5-8 22:24
TIMER_Open(TIMER1, TIMER_CONTINUOUS_MODE, 1);
模式错误,应该连续计数模式。你搞的一次模式肯定不行 ...

例程是PB5与PB4短接,PB4输出一定次数的高低电平转换,然后PB5采集下降沿次数进中断,那个例程可以跑,但是我看手册,单周期模式写的好像应该也可以直接内部计数然后进中断,我再试试连续模式的吧
 楼主| liuyuncs11 发表于 2020-5-9 10:05 | 显示全部楼层

连续模式下也不计数进中断,我查看TIF (TIMERx_INTSTS[0])的状态,也没有计数达到设定值后置1
杨光光光, 发表于 2021-9-7 17:03 | 显示全部楼层
楼主解决了吗,我现在也是遇到这个问题
jasontu 发表于 2021-9-7 21:08 | 显示全部楼层
他的描述有写,timer cnt已经被清了,当然你一直读不到, 要用连续计数模式
杨光光光, 发表于 2021-9-8 08:48 | 显示全部楼层
昨天晚上,成功摸索完定时器中断。不容易
杨光光光, 发表于 2021-9-8 10:22 | 显示全部楼层
想问一下大佬,库函数对应的寄存器操作怎么移来移去的,看不明白。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

8

帖子

1

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