[DemoCode下载] 时钟由IO输出的方法,实现定时器触发IO翻转

[复制链接]
1252|1
 楼主| 小灵通2018 发表于 2024-5-19 19:37 | 显示全部楼层 |阅读模式
  1. /**************************************************************************//**
  2. * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
  3. * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
  4. * [url=home.php?mod=space&uid=247401]@brief[/url]    Demonstrate the timer 0 toggle out function on TM0 pin.
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2020 Nuvoton Technology Corp. All rights reserved.
  8. ****************************************************************************/
  9. #include <stdio.h>
  10. #include "NuMicro.h"


  11. void SYS_Init(void)
  12. {
  13.     /* Unlock protected registers */
  14.     SYS_UnlockReg();

  15.     /* Enable HIRC */
  16.     CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

  17.     /* Waiting for HIRC clock ready */
  18.     CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

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

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

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

  25.     /* Enable UART peripheral clock */
  26.     CLK_EnableModuleClock(UART0_MODULE);

  27.     /* Enable IP clock */
  28.     CLK_EnableModuleClock(TMR0_MODULE);

  29.     /* Select IP clock source */
  30.     CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0SEL_PCLK0, 0);

  31.     /* Update System Core Clock */
  32.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  33.     SystemCoreClockUpdate();

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

  40.     /* Set timer toggle out pin */
  41.     SYS->GPB_MFPL = (SYS->GPB_MFPL & ~(SYS_GPB_MFPL_PB5MFP_Msk)) |
  42.                     (SYS_GPB_MFPL_PB5MFP_TM0);

  43.     /* Lock protected registers */
  44.     SYS_LockReg();
  45. }

  46. /*----------------------------------------------------------------------*/
  47. /* Init UART0                                                           */
  48. /*----------------------------------------------------------------------*/
  49. void UART0_Init(void)
  50. {
  51.     /* Reset UART0 */
  52.     SYS_ResetModule(UART0_RST);

  53.     /* Configure UART0 and set UART0 baud rate */
  54.     UART_Open(UART0, 115200);
  55. }

  56. int main(void)
  57. {
  58.     /* Init System, IP clock and multi-function I/O. */
  59.     SYS_Init();

  60.     /* Init UART0 for printf */
  61.     UART0_Init();

  62.     printf("\nThis sample code use timer 0 to generate 500Hz toggle output to PB.5\n");

  63.     /* To generate 500HZ toggle output, timer frequency must set to 1000Hz.
  64.        Because toggle output state change on every timer timeout event */
  65.     TIMER_Open(TIMER0, TIMER_TOGGLE_MODE, 1000);
  66.     TIMER_SELECT_TOUT_PIN(TIMER0, TIMER_TOUT_PIN_FROM_TMX);
  67.     TIMER_Start(TIMER0);

  68.     while(1);
  69. }


 楼主| 小灵通2018 发表于 2024-5-19 19:38 | 显示全部楼层
这个方法有个好处,就是配置好,不需要CPU干扰,就会自动实现时钟信号输出,可以用于外部需要时钟信号的情况。用于给外设提供时钟信号。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

158

主题

1732

帖子

4

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