[DemoCode下载] M058S单片机点灯

[复制链接]
 楼主| yiyigirl2014 发表于 2025-1-8 11:59 | 显示全部楼层 |阅读模式
原理图如下
17567677df77aeccb2.png
73836677df7c47cfed.png
由原理图可知LED在P3.6引脚上,低电平点亮LED。下面是BSP自带的例子
  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: 2 $
  5. * $Date: 15/02/13 3:18p $
  6. * @brief
  7. *           A sample code for LED blanking.
  8. *
  9. * @note
  10. * Copyright (C) 2015 Nuvoton Technology Corp. All rights reserved.
  11. *
  12. ******************************************************************************/
  13. #include <stdio.h>
  14. #include "M058S.h"

  15. #define PLLCON_SETTING  CLK_PLLCON_50MHz_HXT
  16. #define PLL_CLOCK       50000000

  17. void SYS_Init(void)
  18. {

  19.     /*---------------------------------------------------------------------------------------------------------*/
  20.     /* Init System Clock                                                                                       */
  21.     /*---------------------------------------------------------------------------------------------------------*/

  22.     /* Enable Internal RC 22.1184MHz clock */
  23.     CLK->PWRCON |= CLK_PWRCON_OSC22M_EN_Msk;

  24.     /* Waiting for Internal RC clock ready */
  25.     while(!(CLK->CLKSTATUS & CLK_CLKSTATUS_OSC22M_STB_Msk));

  26.     /* Switch HCLK clock source to Internal RC and and HCLK source divide 1 */
  27.     CLK->CLKSEL0 &= ~CLK_CLKSEL0_HCLK_S_Msk;
  28.     CLK->CLKSEL0 |= CLK_CLKSEL0_HCLK_S_HIRC;
  29.     CLK->CLKDIV &= ~CLK_CLKDIV_HCLK_N_Msk;
  30.     CLK->CLKDIV |= CLK_CLKDIV_HCLK(1);

  31.     /* Enable external XTAL 12MHz clock */
  32.     CLK->PWRCON |= CLK_PWRCON_XTL12M_EN_Msk;

  33.     /* Waiting for external XTAL clock ready */
  34.     while(!(CLK->CLKSTATUS & CLK_CLKSTATUS_XTL12M_STB_Msk));

  35.     /* Set core clock as PLL_CLOCK from PLL */
  36.     CLK->PLLCON = PLLCON_SETTING;
  37.     while(!(CLK->CLKSTATUS & CLK_CLKSTATUS_PLL_STB_Msk));
  38.     CLK->CLKSEL0 &= (~CLK_CLKSEL0_HCLK_S_Msk);
  39.     CLK->CLKSEL0 |= CLK_CLKSEL0_HCLK_S_PLL;

  40.     /* Update System Core Clock */
  41.     /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
  42.     //SystemCoreClockUpdate();
  43.     PllClock        = PLL_CLOCK;            // PLL
  44.     SystemCoreClock = PLL_CLOCK / 1;        // HCLK
  45.     CyclesPerUs     = PLL_CLOCK / 1000000;  // For SYS_SysTickDelay()

  46.     /* Enable UART module clock */
  47.     CLK->APBCLK |= CLK_APBCLK_UART0_EN_Msk;

  48.     /* Select UART module clock source */
  49.     CLK->CLKSEL1 &= ~CLK_CLKSEL1_UART_S_Msk;
  50.     CLK->CLKSEL1 |= CLK_CLKSEL1_UART_S_HXT;

  51.     /*---------------------------------------------------------------------------------------------------------*/
  52.     /* Init I/O Multi-function                                                                                 */
  53.     /*---------------------------------------------------------------------------------------------------------*/

  54.     /* Set P3 multi-function pins for UART0 RXD , TXD and CKO */
  55.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  56.     SYS->P3_MFP |= (SYS_MFP_P30_RXD | SYS_MFP_P31_TXD);

  57. }

  58. void UART0_Init()
  59. {
  60.     /*---------------------------------------------------------------------------------------------------------*/
  61.     /* Init UART                                                                                               */
  62.     /*---------------------------------------------------------------------------------------------------------*/
  63.     /* Reset UART0 */
  64.     SYS->IPRSTC2 |=  SYS_IPRSTC2_UART0_RST_Msk;
  65.     SYS->IPRSTC2 &= ~SYS_IPRSTC2_UART0_RST_Msk;

  66.     /* Configure UART0 and set UART0 Baudrate */
  67.     UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(__HXT, 115200);
  68.     UART0->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
  69. }

  70. /*---------------------------------------------------------------------------------------------------------*/
  71. /*  Main Function                                                                                          */
  72. /*---------------------------------------------------------------------------------------------------------*/
  73. int32_t main(void)
  74. {
  75.     int32_t i;
  76.    
  77.     /* Unlock protected registers */
  78.     SYS_UnlockReg();

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

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

  83.     /* Init UART0 for printf */
  84.     UART0_Init();

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

  86.     /*
  87.         This sample code will blinking LED on NuTiny EVB board of M058SSAN.
  88.         The I/O for LED is P2.3 or P3.6
  89.     */

  90.     printf("+---------------------------------------+\n");
  91.     printf("|    NuTiny EVB LED Sample Code         |\n");
  92.     printf("+---------------------------------------+\n");
  93.    
  94.     /* Init P2.3 and P3.6 to be output mode */
  95.     P2->PMD = (P2->PMD & ~(0x3 << 2*3)) | (1 << 2*3);
  96.     P3->PMD = (P3->PMD & ~(0x3 << 2*6)) | (1 << 2*6);
  97.    
  98.     while(1)
  99.     {
  100.         /* Toggle P2.3 */
  101.         P23 ^= 1;
  102.         
  103.         /* Toggle P3.6 */
  104.         P36 ^= 1;
  105.         
  106.         /* Delay 200ms */
  107.         for(i=0;i<2;i++)
  108.             CLK_SysTickDelay(100000);
  109.     }
  110.    
  111.    
  112. }


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

本版积分规则

229

主题

3675

帖子

10

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