[DemoCode下载] 时钟输出方式

[复制链接]
1462|11
 楼主| xuanhuanzi 发表于 2018-9-29 18:57 | 显示全部楼层 |阅读模式
\Mini51DE_Series_BSP_CMSIS_v3.01.002\SampleCode\StdDriver\SYS
在寄存器里面有个clkout的功能。
    /* Set P3 multi-function pins for Clock Output */
    SYS->P3_MFP = SYS_MFP_P36_CKO;

  /* Switch HCLK clock source to Internal 22MHz */
    CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_IRC22M,0);

    /* Enable CKO and output frequency = system clock / 4 */
    CLK_EnableCKO(CLK_CLKSEL2_FRQDIV_HCLK,1,0);

 楼主| xuanhuanzi 发表于 2018-9-29 18:58 | 显示全部楼层
  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. * $Revision: 12 $
  5. * $Date: 15/01/16 1:45p $
  6. * @brief
  7. *           Change system clock to different PLL frequency and output system clock from CLKO pin.
  8. *
  9. * @note
  10. * Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved.
  11. *
  12. ******************************************************************************/
  13. #include <stdio.h>
  14. #include "NUC131.h"


  15. #define PLL_CLOCK   50000000

  16. #define SIGNATURE       0x125ab234
  17. #define FLAG_ADDR       0x20000FFC


  18. /*---------------------------------------------------------------------------------------------------------*/
  19. /* Define functions prototype                                                                              */
  20. /*---------------------------------------------------------------------------------------------------------*/
  21. int32_t pi(void);
  22. void Delay(uint32_t x);
  23. void SYS_PLL_Test(void);
  24. void SYS_Init(void);
  25. void UART0_Init(void);
  26. int32_t main(void);


  27. /*---------------------------------------------------------------------------------------------------------*/
  28. /*  Simple calculation test function                                                                       */
  29. /*---------------------------------------------------------------------------------------------------------*/
  30. #define PI_NUM  256
  31. int32_t f[PI_NUM + 1];
  32. uint32_t piTbl[19] =
  33. {
  34.     3141,
  35.     5926,
  36.     5358,
  37.     9793,
  38.     2384,
  39.     6264,
  40.     3383,
  41.     2795,
  42.     288,
  43.     4197,
  44.     1693,
  45.     9937,
  46.     5105,
  47.     8209,
  48.     7494,
  49.     4592,
  50.     3078,
  51.     1640,
  52.     6284
  53. };

  54. int32_t piResult[19];

  55. int32_t pi(void)
  56. {
  57.     int32_t i, i32Err;
  58.     int32_t a = 10000, b = 0, c = PI_NUM, d = 0, e = 0, g = 0;

  59.     for(; b - c;)
  60.         f[b++] = a / 5;

  61.     i = 0;
  62.     for(; d = 0, g = c * 2; c -= 14,/*printf("%.4d\n",e+d/a),*/ piResult[i++] = e + d / a, e = d % a)
  63.     {
  64.         if(i == 19)
  65.             break;

  66.         for(b = c; d += f[b] * a, f[b] = d % --g, d /= g--, --b; d *= b);
  67.     }
  68.     i32Err = 0;
  69.     for(i = 0; i < 19; i++)
  70.     {
  71.         if(piTbl[i] != piResult[i])
  72.             i32Err = -1;
  73.     }

  74.     return i32Err;
  75. }

  76. void Delay(uint32_t x)
  77. {
  78.     int32_t i;

  79.     for(i = 0; i < x; i++)
  80.     {
  81.         __NOP();
  82.         __NOP();
  83.     }
  84. }

  85. uint32_t g_au32PllSetting[] =
  86. {
  87.     CLK_PLLCON_PLL_SRC_HXT | CLK_PLLCON_NR(3) | CLK_PLLCON_NF(25) | CLK_PLLCON_NO_4,  /* PLL = 25MHz */
  88.     CLK_PLLCON_PLL_SRC_HXT | CLK_PLLCON_NR(3) | CLK_PLLCON_NF(29) | CLK_PLLCON_NO_4,  /* PLL = 29MHz */
  89.     CLK_PLLCON_PLL_SRC_HXT | CLK_PLLCON_NR(3) | CLK_PLLCON_NF(33) | CLK_PLLCON_NO_4,  /* PLL = 33MHz */
  90.     CLK_PLLCON_PLL_SRC_HXT | CLK_PLLCON_NR(3) | CLK_PLLCON_NF(37) | CLK_PLLCON_NO_4,  /* PLL = 37MHz */
  91.     CLK_PLLCON_PLL_SRC_HXT | CLK_PLLCON_NR(3) | CLK_PLLCON_NF(41) | CLK_PLLCON_NO_4,  /* PLL = 41MHz */
  92.     CLK_PLLCON_PLL_SRC_HXT | CLK_PLLCON_NR(3) | CLK_PLLCON_NF(45) | CLK_PLLCON_NO_4,  /* PLL = 45MHz */
  93.     CLK_PLLCON_PLL_SRC_HXT | CLK_PLLCON_NR(3) | CLK_PLLCON_NF(49) | CLK_PLLCON_NO_4   /* PLL = 49MHz */
  94. };

  95. void SYS_PLL_Test(void)
  96. {
  97.     int32_t  i;

  98.     /*---------------------------------------------------------------------------------------------------------*/
  99.     /* PLL clock configuration test                                                                             */
  100.     /*---------------------------------------------------------------------------------------------------------*/

  101.     printf("\n-------------------------[ Test PLL ]-----------------------------\n");

  102.     for(i = 0; i < sizeof(g_au32PllSetting) / sizeof(g_au32PllSetting[0]) ; i++)
  103.     {
  104.         /* Switch HCLK clock source to HXT and HCLK source divide 1 */
  105.         CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HXT, CLK_CLKDIV_HCLK(1));

  106.         /* Set PLL to power down mode and PLL_STB bit in CLKSTATUS register will be cleared by hardware. */
  107.         CLK_DisablePLL();

  108.         /* Set PLL frequency */
  109.         CLK->PLLCON = g_au32PllSetting[i];

  110.         /* Waiting for PLL clock ready */
  111.         CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk);

  112.         /* Switch HCLK clock source to PLL */
  113.         CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_PLL, CLK_CLKDIV_HCLK(1));

  114.         printf("  Change system clock to %d Hz ...................... ", SystemCoreClock);

  115.         /* Output selected clock to CKO, CKO Clock = HCLK / 2^(1 + 1) */
  116.         CLK_EnableCKO(CLK_CLKSEL2_FRQDIV_S_HCLK, 1, 0);

  117.         /* The delay loop is used to check if the CPU speed is increasing */
  118.         Delay(0x400000);

  119.         if(pi())
  120.         {
  121.             printf("[FAIL]\n");
  122.         }
  123.         else
  124.         {
  125.             printf("[OK]\n");
  126.         }

  127.         /* Disable CKO clock */
  128.         CLK_DisableCKO();
  129.     }
  130. }

  131. void SYS_Init(void)
  132. {
  133.     /*---------------------------------------------------------------------------------------------------------*/
  134.     /* Init System Clock                                                                                       */
  135.     /*---------------------------------------------------------------------------------------------------------*/

  136.     /* Enable Internal RC 22.1184MHz clock */
  137.     CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

  138.     /* Waiting for Internal RC clock ready */
  139.     CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

  140.     /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
  141.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  142.     /* Enable external XTAL 12MHz clock */
  143.     CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

  144.     /* Waiting for external XTAL clock ready */
  145.     CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

  146.     /* Set core clock as PLL_CLOCK from PLL */
  147.     CLK_SetCoreClock(PLL_CLOCK);

  148.     /* Enable UART module clock */
  149.     CLK_EnableModuleClock(UART0_MODULE);

  150.     /* Select UART module clock source */
  151.     CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));


  152.     /*---------------------------------------------------------------------------------------------------------*/
  153.     /* Init I/O Multi-function                                                                                 */
  154.     /*---------------------------------------------------------------------------------------------------------*/

  155.     /* Set GPB multi-function pins for UART0 RXD(PB.0), TXD(PB.1) and Clock Output(PB.8) */
  156.     SYS->GPB_MFP &= ~(SYS_GPB_MFP_PB0_Msk | SYS_GPB_MFP_PB1_Msk | SYS_GPB_MFP_PB8_Msk);
  157.     SYS->ALT_MFP &= ~SYS_ALT_MFP_PB8_Msk;
  158.     SYS->ALT_MFP3 &= ~SYS_ALT_MFP3_PB8_Msk;
  159.     SYS->GPB_MFP |= (SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD | SYS_GPB_MFP_PB8_CLKO);
  160.     SYS->ALT_MFP |=  SYS_ALT_MFP_PB8_CLKO;
  161.     SYS->ALT_MFP3 |= SYS_ALT_MFP3_PB8_CLKO;

  162. }

  163. void UART0_Init(void)
  164. {
  165.     /*---------------------------------------------------------------------------------------------------------*/
  166.     /* Init UART                                                                                               */
  167.     /*---------------------------------------------------------------------------------------------------------*/
  168.     /* Reset UART0 module */
  169.     SYS_ResetModule(UART0_RST);

  170.     /* Configure UART0 and set UART0 Baudrate */
  171.     UART_Open(UART0, 115200);
  172. }

  173. /*---------------------------------------------------------------------------------------------------------*/
  174. /* MAIN function                                                                                           */
  175. /*---------------------------------------------------------------------------------------------------------*/
  176. int32_t main(void)
  177. {
  178.     uint32_t u32data;

  179.     /* In end of main function, program issued CPU reset and write-protection will be disabled. */
  180.     if(SYS_IsRegLocked() == 0)
  181.         SYS_LockReg();

  182.     /* Unlock protected registers */
  183.     SYS_UnlockReg();

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

  186.     /* Lock protected registers */
  187.     SYS_LockReg();

  188.     /* Init UART0 for printf */
  189.     UART0_Init();

  190.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);

  191.     /*
  192.         This sample code will show some function about system manager controller and clock controller:
  193.         1. Read PDID
  194.         2. Get and clear reset source
  195.         3. Setting about BOD
  196.         4. Change system clock depended on different PLL settings
  197.         5. Output system clock from CKO pin, and the output frequency = system clock / 4
  198.     */

  199.     printf("+----------------------------------------+\n");
  200.     printf("|    NUC131 System Driver Sample Code    |\n");
  201.     printf("+----------------------------------------+\n");

  202.     if(M32(FLAG_ADDR) == SIGNATURE)
  203.     {
  204.         printf("  CPU Reset success!\n");
  205.         M32(FLAG_ADDR) = 0;
  206.         printf("  Press any key to continue ...\n");
  207.         getchar();
  208.     }

  209.     /*---------------------------------------------------------------------------------------------------------*/
  210.     /* Misc system function test                                                                               */
  211.     /*---------------------------------------------------------------------------------------------------------*/

  212.     /* Read Part Device ID */
  213.     printf("Product ID 0x%x\n", SYS_ReadPDID());

  214.     /* Get reset source from last operation */
  215.     u32data = SYS_GetResetSrc();
  216.     printf("Reset Source 0x%x\n", u32data);

  217.     /* Clear reset source */
  218.     SYS_ClearResetSrc(u32data);

  219.     /* Unlock protected registers for Brown-Out Detector settings */
  220.     SYS_UnlockReg();

  221.     /* Check if the write-protected registers are unlocked before BOD setting and CPU Reset */
  222.     if(SYS_IsRegLocked() == 0)
  223.     {
  224.         printf("Protected Address is Unlocked\n");
  225.     }

  226.     /* Enable Brown-Out Detector, and set Brown-Out Detector voltage 2.7V */
  227.     SYS_EnableBOD(SYS_BODCR_BOD_INTERRUPT_EN, SYS_BODCR_BOD_VL_2_7V);

  228.     /* Enable BOD IRQ */
  229.     NVIC_EnableIRQ(BOD_IRQn);

  230.     /* Enable Low Voltage Reset function */
  231.     SYS_ENABLE_LVR();

  232.     /* Run PLL Test */
  233.     SYS_PLL_Test();

  234.     /* Write a signature work to SRAM to check if it is reset by software */
  235.     M32(FLAG_ADDR) = SIGNATURE;
  236.     printf("\n\n  >>> Reset CPU <<<\n");

  237.     /* Waiting for message send out */
  238.     UART_WAIT_TX_EMPTY(UART0);

  239.     /* Switch HCLK clock source to Internal RC 22.1184MHz clock and HCLK source divide 1 */
  240.     CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

  241.     /* Set PLL to Power down mode and HW will also clear PLL_STB bit in CLKSTATUS register */
  242.     CLK_DisablePLL();

  243.     /* Reset CPU */
  244.     SYS_ResetCPU();

  245. }
 楼主| xuanhuanzi 发表于 2018-9-29 19:14 | 显示全部楼层
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technoledge Corp.
  8. //  Website: http://www.nuvoton.com
  9. //  E-Mail : MicroC-8bit@nuvoton.com
  10. //  Date   : Apr/21/2017
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E003 Clock Output demo code
  14. //***********************************************************************************************************

  15. #include "N76E003.h"
  16. #include "SFR_Macro.h"
  17. #include "Function_define.h"
  18. #include "Common.h"
  19. #include "Delay.h"

  20. //======================================================================================================

  21. void main(void)
  22. {
  23.        
  24.     /* Note
  25.        MCU power on system clock is HIRC (16.0000MHz), if need change Fsys source pleaes copy hange system closk source demo
  26.     */
  27.    
  28.                 Set_All_GPIO_Quasi_Mode;                    //in Function_define.h
  29.     set_CLOEN;                                  //System Clock Output Enable
  30. //     Please measure P1.1 (CLO) pin for system clock output, it should as system clock;

  31.     while(1)
  32.     {
  33.         clr_GPIO1;
  34.         Timer0_Delay1ms(500);
  35.         set_GPIO1;
  36.         Timer0_Delay1ms(500);
  37.     }
  38. /* =================== */
  39. }

 楼主| xuanhuanzi 发表于 2018-9-29 19:18 | 显示全部楼层
新唐的51内核和ARM内核单片机都具备输出时钟的功能,不仅可以给其他设备作为时钟使用,还可以作为一个测量接口。用于测量时钟的频率是否为自己所需的。
xixi2017 发表于 2018-9-29 19:26 | 显示全部楼层
输出功能很有用的。
磨砂 发表于 2018-9-30 09:48 | 显示全部楼层
非常感谢楼主分享
xinpian101 发表于 2018-10-9 11:53 | 显示全部楼层
丢丢手绢666 发表于 2018-10-9 14:53 来自手机 | 显示全部楼层
感谢,这个看起来很全面,输出功能十分有用的。
yiyigirl2014 发表于 2018-10-9 22:37 | 显示全部楼层
CLK_EnableCKO(CLK_CLKSEL2_FRQDIV_S_HCLK, 1, 0);
第三个参数什么意思啊
734774645 发表于 2018-10-12 19:46 | 显示全部楼层
搞不懂最后一个参数为何是0
dongnanxibei 发表于 2018-10-12 22:08 | 显示全部楼层
多谢,这给我不少启发。
稳稳の幸福 发表于 2018-10-16 18:28 | 显示全部楼层
这种操作用内部时钟比较好
您需要登录后才可以回帖 登录 | 注册

本版积分规则

183

主题

2331

帖子

3

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