[DemoCode下载] 基于寄存器操作的IO输出

[复制链接]
737|10
 楼主| xixi2017 发表于 2019-9-26 23:53 | 显示全部楼层 |阅读模式
  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: 7 $
  5. * $Date: 15/07/13 1:27p $
  6. * [url=home.php?mod=space&uid=247401]@brief[/url]    Show how to set GPIO pin mode and use pin data input/output control.
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. ******************************************************************************/
  11. #include <stdio.h>
  12. #include "M051Series.h"


  13. #define PLLCON_SETTING      CLK_PLLCON_50MHz_HXT
  14. #define PLL_CLOCK           50000000


  15. void SYS_Init(void)
  16. {
  17.     /*---------------------------------------------------------------------------------------------------------*/
  18.     /* Init System Clock                                                                                       */
  19.     /*---------------------------------------------------------------------------------------------------------*/

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

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

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

  27.     /* Set PLL to Power down mode and HW will also clear PLL_STB bit in CLKSTATUS register */
  28.     CLK->PLLCON |= CLK_PLLCON_PD_Msk;   
  29.    
  30.     /* Enable external XTAL 12MHz clock */
  31.     CLK->PWRCON |= CLK_PWRCON_XTL12M_EN_Msk;

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

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

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

  44.     /* Enable UART module clock */
  45.     CLK->APBCLK |= CLK_APBCLK_UART0_EN_Msk;

  46.     /* Select UART module clock source */
  47.     CLK->CLKSEL1 = (CLK->CLKSEL1 & (~CLK_CLKSEL1_UART_S_Msk)) | CLK_CLKSEL1_UART_S_PLL;

  48.     /*---------------------------------------------------------------------------------------------------------*/
  49.     /* Init I/O Multi-function                                                                                 */
  50.     /*---------------------------------------------------------------------------------------------------------*/

  51.     /* Set P3 multi-function pins for UART0 RXD and TXD */
  52.     SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
  53.     SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);

  54. }

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

  63.     /* Configure UART0 and set UART0 Baudrate */
  64.     UART0->BAUD = UART_BAUD_MODE2 | UART_BAUD_MODE2_DIVIDER(PLL_CLOCK, 115200);
  65.     UART0->LCR = UART_WORD_LEN_8 | UART_PARITY_NONE | UART_STOP_BIT_1;
  66. }

  67. /*---------------------------------------------------------------------------------------------------------*/
  68. /* MAIN function                                                                                           */
  69. /*---------------------------------------------------------------------------------------------------------*/
  70. int main(void)
  71. {
  72.     int32_t i32Err;

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

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

  77.     /* Lock protected registers */
  78.     SYS_LockReg();

  79.     /* Init UART0 for printf */
  80.     UART0_Init();

  81.     printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %d Hz\n", SystemCoreClock);
  82.     printf("+-------------------------------------------------+\n");
  83.     printf("|    P1.2(Output) and P4.1(Input) Sample Code     |\n");
  84.     printf("+-------------------------------------------------+\n\n");

  85.     /* Configure P1.2 as Output mode and P4.1 as Input mode then close it */
  86.     P1->PMD = (P1->PMD & (~GPIO_PMD_PMD2_Msk)) | (GPIO_PMD_OUTPUT << GPIO_PMD_PMD2_Pos);
  87.     P4->PMD = (P4->PMD & (~GPIO_PMD_PMD1_Msk)) | (GPIO_PMD_INPUT << GPIO_PMD_PMD1_Pos);

  88.     i32Err = 0;
  89.     printf("GPIO P1.2(output mode) connect to P4.1(input mode) ......");

  90.     /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
  91.     P12 = 0;
  92.     if(P41 != 0)
  93.     {
  94.         i32Err = 1;
  95.     }

  96.     P12 = 1;
  97.     if(P41 != 1)
  98.     {
  99.         i32Err = 1;
  100.     }

  101.     if(i32Err)
  102.     {
  103.         printf("  [FAIL].\n");
  104.     }
  105.     else
  106.     {
  107.         printf("  [OK].\n");
  108.     }

  109.     /* Configure P1.2 and P4.1 to default Quasi-bidirectional mode */
  110.     P1->PMD = (P1->PMD & (~GPIO_PMD_PMD2_Msk)) | (GPIO_PMD_QUASI << GPIO_PMD_PMD2_Pos);
  111.     P4->PMD = (P4->PMD & (~GPIO_PMD_PMD1_Msk)) | (GPIO_PMD_QUASI << GPIO_PMD_PMD1_Pos);

  112.     while(1);
  113. }

  114. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/


 楼主| xixi2017 发表于 2019-9-26 23:53 | 显示全部楼层
ARM单片机的IO操作其实非常简单。
幸福小强 发表于 2019-9-27 21:42 | 显示全部楼层
很好懂的样子。新唐的库函数和头文件都做的很好。
598330983 发表于 2019-9-28 18:27 | 显示全部楼层
多功能选择配置。
小灵通2018 发表于 2019-9-28 21:03 | 显示全部楼层
好多的逻辑位操作。
eyu66 发表于 2019-9-29 08:56 | 显示全部楼层
我是寄存器名称都不用,直接粗爆的操作寄存器地址,是不是没救了
比如类似下面这一句:
CLK->CLKSEL0 = (CLK->CLKSEL0 & (~CLK_CLKSEL0_HCLK_S_Msk)) | CLK_CLKSEL0_HCLK_S_HIRC;

我的写法基本是:
unsigned int *pCLK_CLKSEL0 = (unsigned int *)(0x40000200 + 0x10);
*pCLK_CLKSEL0 &= ~0x00000100;   *pCLK_CLKSEL0 |= 0x00000100;
 楼主| xixi2017 发表于 2019-9-29 10:21 | 显示全部楼层
eyu66 发表于 2019-9-29 08:56
我是寄存器名称都不用,直接粗爆的操作寄存器地址,是不是没救了?
比如类似下面这一句:
CLK->CLKS ...

这种才是原生态,比较生猛。你太牛了。
john_lee 发表于 2019-9-29 13:29 | 显示全部楼层
我对位操作比较来气,就这样写了:
  1.   RCC.CR().HSION(true);                         // Set HSION bit
  2.   RCC.CFGR().SW(rcc::sw_t::HSI)                 // HSI Selected as System Clock source
  3.             .HPRE(rcc::hpre_t::DIV1)            // HCLK = SYSCLK
  4.             .PPRE1(rcc::ppre_t::DIV1)           // PCLK1 = HCLK
  5.             .PPRE2(rcc::ppre_t::DIV1)           // PCLK2 = HCLK
  6.             .ADCPRE(rcc::adcpre_t::DIV2)        // Reset ADCPRE bit
  7.             .MCO(rcc::mco_t::NONE);             // Reset MCO bit
  8.   RCC.CR().HSEON(false)                         // Reset HSEON bit
  9.           .HSEBYP(false)                        // Reset HSEBYP bit
  10.           .CSSON(false)                         // Reset CSSON bit
  11.           .PLLON(false);                        // Reset PLLON bit

评论

这是高手。  发表于 2019-9-29 18:28
 楼主| xixi2017 发表于 2019-9-29 18:28 | 显示全部楼层
john_lee 发表于 2019-9-29 13:29
我对位操作比较来气,就这样写了:

这种写法我还真不会,是定义的宏还是C语言本身的功能?
john_lee 发表于 2019-9-30 12:01 | 显示全部楼层
xixi2017 发表于 2019-9-29 18:28
这种写法我还真不会,是定义的宏还是C语言本身的功能?

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

本版积分规则

145

主题

2034

帖子

2

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