[技术问答] M261芯片,如何用定时器计数不同频率的外部脉冲?

[复制链接]
 楼主| nbiot 发表于 2020-2-22 11:40 | 显示全部楼层 |阅读模式
M261芯片,如何用定时器计数不同频率的外部脉冲?

外部脉冲的频率是变化的,从几HZ,~50KHz,随机变化,现在只要用定时器来对外部脉冲来计数,可以吗?
gejigeji521 发表于 2020-2-22 13:41 | 显示全部楼层
应该是可以的。
gejigeji521 发表于 2020-2-22 13:41 | 显示全部楼层
甚至最简单的方法是用查询法都可以。如果你的单片机只干一件事。。。
小灵通2018 发表于 2020-2-22 15:25 | 显示全部楼层
楼主到底研究出来ARM内核单片机怎么搞这个没
玛尼玛尼哄 发表于 2020-2-22 15:59 | 显示全部楼层
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V3.00
  4. * [url=home.php?mod=space&uid=247401]@brief[/url]    Demonstrates the timer event counter function.
  5. *
  6. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2019 Nuvoton Technology Corp. All rights reserved.
  7. ******************************************************************************/
  8. #include <stdio.h>
  9. #include "NuMicro.h"


  10. /**
  11. * @brief       Timer0 IRQ
  12. *
  13. * @param       None
  14. *
  15. * [url=home.php?mod=space&uid=266161]@return[/url]      None
  16. *
  17. * [url=home.php?mod=space&uid=1543424]@Details[/url]     The Timer0 default IRQ, declared in startup_M261.s.
  18. */
  19. void TMR0_IRQHandler(void)
  20. {
  21.     printf("Count 1000 falling events! Test complete.\n");
  22.     TIMER_ClearIntFlag(TIMER0);
  23. }

  24. void SYS_Init(void)
  25. {
  26.     /*---------------------------------------------------------------------------------------------------------*/
  27.     /* Init System Clock                                                                                       */
  28.     /*---------------------------------------------------------------------------------------------------------*/
  29.     /* Enable HIRC clock */
  30.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  31.     /* Waiting for HIRC clock ready */
  32.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

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

  35.     /* Enable PLL */
  36.     CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;

  37.     /* Waiting for PLL stable */
  38.     CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);

  39.     /* Select HCLK clock source as PLL and HCLK source divider as 2 */
  40.     CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));

  41.     /* Set SysTick source to HCLK/2 */
  42.     CLK_SetSysTickClockSrc(CLK_CLKSEL0_STCLKSEL_HCLK_DIV2);

  43.     /* Enable UART module clock */
  44.     CLK_EnableModuleClock(UART0_MODULE);
  45.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

  46.     /* Enable TIMER module clock */
  47.     CLK_EnableModuleClock(TMR0_MODULE);
  48.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK0, 0);

  49.     /*---------------------------------------------------------------------------------------------------------*/
  50.     /* Init I/O Multi-function                                                                                 */
  51.     /*---------------------------------------------------------------------------------------------------------*/
  52.     /* Set multi-function pins for UART0 RXD and TXD */
  53.     SYS->GPB_MFPH = (SYS->GPB_MFPH & (~(UART0_RXD_PB12_Msk | UART0_TXD_PB13_Msk))) | UART0_RXD_PB12 | UART0_TXD_PB13;

  54.     /* Set Timer0 event counter pin */
  55.     SYS->GPB_MFPL &= ~TM0_PB5_Msk;
  56.     SYS->GPB_MFPL |= TM0_PB5;
  57. }

  58. void UART_Init(void)
  59. {
  60.     /*---------------------------------------------------------------------------------------------------------*/
  61.     /* Init UART                                                                                               */
  62.     /*---------------------------------------------------------------------------------------------------------*/
  63.     /* Reset UART module */
  64.     SYS_ResetModule(UART0_RST);

  65.     /* Configure UART and set UART Baudrate */
  66.     UART_Open(DEBUG_PORT, 115200);
  67. }

  68. /*---------------------------------------------------------------------------------------------------------*/
  69. /*  MAIN function                                                                                          */
  70. /*---------------------------------------------------------------------------------------------------------*/
  71. int main(void)
  72. {
  73.     volatile uint32_t i;

  74.     /* Unlock protected registers */
  75.     SYS_UnlockReg();

  76.     /* Init System, peripheral clock and multi-function I/O */
  77.     SYS_Init();

  78.     /* Init UART for printf */
  79.     UART_Init();

  80.     /* Lock protected registers */
  81.     SYS_LockReg();

  82.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  83.     printf("+---------------------------------------------+\n");
  84.     printf("|    Timer Event Counter Input Sample Code    |\n");
  85.     printf("+---------------------------------------------+\n\n");

  86.     printf("# Timer0 Settings:\n");
  87.     printf("    - Clock source is HCLK      \n");
  88.     printf("    - Continuous counting mode  \n");
  89.     printf("    - Interrupt enable          \n");
  90.     printf("    - Event counter mode enable \n");
  91.     printf("# Connect PB.4 pin to event counter pin TM0(PB.5) and pull PB.4 Low to generate TM0 event input source.\n\n");
  92.     printf("Press any key to continue.\n\n");
  93.     getchar();

  94.     /* Configure PB.4 as GPIO output pin and pull initial pin status to High */
  95.     PB->MODE = (PB->MODE & ~GPIO_MODE_MODE4_Msk) | (GPIO_MODE_OUTPUT << GPIO_MODE_MODE4_Pos);
  96.     PB4 = 1;

  97.     /* Give a dummy target frequency here. Will over write prescale and compare value with macro. */
  98.     TIMER_Open(TIMER0, TIMER_ONESHOT_MODE, 100);
  99.     /* Update prescale and compare value to what we need in event counter mode. */
  100.     TIMER_SET_PRESCALE_VALUE(TIMER0, 0);
  101.     TIMER_SET_CMP_VALUE(TIMER0, 1000);

  102.     /* Counter increase on falling edge */
  103.     TIMER_EnableEventCounter(TIMER0, TIMER_COUNTER_EVENT_FALLING);

  104.     /* Enable timer interrupt */
  105.     TIMER_EnableInt(TIMER0);
  106.     NVIC_EnableIRQ(TMR0_IRQn);

  107.     /* Start Timer 0 */
  108.     TIMER_Start(TIMER0);

  109.     for(i = 0; i < 1000; i++)
  110.     {
  111.         PB4 = 0;    // low
  112.         CLK_SysTickDelay(1);
  113.         PB4 = 1;    // high
  114.         CLK_SysTickDelay(1);
  115.     }

  116.     while(1) {}
  117. }

  118. /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/
玛尼玛尼哄 发表于 2020-2-22 15:59 | 显示全部楼层
422765e50df74e3590.png
BSP里这个例子就是啊。
zhuotuzi 发表于 2020-2-22 16:08 | 显示全部楼层
定时器外部事件计数模式。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:QQ 2419286292

82

主题

181

帖子

3

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