- /**************************************************************************//**
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=895143]@version[/url] V3.00
- * [url=home.php?mod=space&uid=247401]@brief[/url] Show how to set GPIO pin mode and use pin data input/output control.
- *
- * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2019 Nuvoton Technology Corp. All rights reserved.
- *
- ******************************************************************************/
- #include <stdio.h>
- #include "NuMicro.h"
- void SYS_Init(void)
- {
- /* Set PF multi-function pins for XT1_OUT(PF.2) and XT1_IN(PF.3) */
- SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF2MFP_Msk)) | SYS_GPF_MFPL_PF2MFP_XT1_OUT;
- SYS->GPF_MFPL = (SYS->GPF_MFPL & (~SYS_GPF_MFPL_PF3MFP_Msk)) | SYS_GPF_MFPL_PF3MFP_XT1_IN;
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init System Clock */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Enable HIRC clock */
- CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
- /* Wait for HIRC clock ready */
- CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
- /* Select HCLK clock source as HIRC and HCLK clock divider as 1 */
- CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
- /* Enable HXT clock */
- CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
- /* Wait for HXT clock ready */
- CLK_WaitClockReady(CLK_STATUS_HXTSTB_Msk);
- /* Enable PLL */
- CLK->PLLCTL = CLK_PLLCTL_128MHz_HIRC;
- /* Waiting for PLL stable */
- CLK_WaitClockReady(CLK_STATUS_PLLSTB_Msk);
- /* Select HCLK clock source as PLL and HCLK source divider as 2 */
- CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_PLL, CLK_CLKDIV0_HCLK(2));
- /* Enable UART module clock */
- CLK_EnableModuleClock(UART0_MODULE);
- /* Select UART module clock source as HIRC and UART module clock divider as 1 */
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init I/O Multi-function */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Set multi-function pins for UART0 RXD and TXD */
- SYS->GPB_MFPH = (SYS->GPB_MFPH & (~(UART0_RXD_PB12_Msk | UART0_TXD_PB13_Msk))) | UART0_RXD_PB12 | UART0_TXD_PB13;
- }
- void UART0_Init(void)
- {
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init UART */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Reset UART0 */
- SYS_ResetModule(UART0_RST);
- /* Configure UART0 and set UART0 baud rate */
- UART_Open(UART0, 115200);
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* Main Function */
- /*---------------------------------------------------------------------------------------------------------*/
- int32_t main(void)
- {
- int32_t i32Err, i32TimeOutCnt;
- /* Unlock protected registers */
- SYS_UnlockReg();
- /* Init System, peripheral clock and multi-function I/O */
- SYS_Init();
- /* Lock protected registers */
- SYS_LockReg();
- /* Init UART0 for printf */
- UART0_Init();
- printf("\n\nCPU [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);
- printf("+-------------------------------------------------+\n");
- printf("| PB.3(Output) and PD.7(Input) Sample Code |\n");
- printf("+-------------------------------------------------+\n\n");
- /*-----------------------------------------------------------------------------------------------------*/
- /* GPIO Basic Mode Test --- Use Pin Data Input/Output to control GPIO pin */
- /*-----------------------------------------------------------------------------------------------------*/
- printf(" >> Please connect PB.3 and PD.7 first << \n");
- printf(" Press any key to start test by using [Pin Data Input/Output Control] \n\n");
- getchar();
- /* Configure PB.3 as Output mode and PD.7 as Input mode then close it */
- GPIO_SetMode(PB, BIT3, GPIO_MODE_OUTPUT);
- GPIO_SetMode(PD, BIT7, GPIO_MODE_INPUT);
- i32Err = 0;
- printf("GPIO PB.3(output mode) connect to PD.7(input mode) ......");
- /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
- /* Set PB.3 output pin value is low */
- PB3 = 0;
- /* Set time out counter */
- i32TimeOutCnt = 100;
- /* Wait for PD.7 input pin status is low for a while */
- while(PD7 != 0)
- {
- if(i32TimeOutCnt > 0)
- {
- i32TimeOutCnt--;
- }
- else
- {
- i32Err = 1;
- break;
- }
- }
- /* Set PB.3 output pin value is high */
- PB3 = 1;
- /* Set time out counter */
- i32TimeOutCnt = 100;
- /* Wait for PD.7 input pin status is high for a while */
- while(PD7 != 1)
- {
- if(i32TimeOutCnt > 0)
- {
- i32TimeOutCnt--;
- }
- else
- {
- i32Err = 1;
- break;
- }
- }
- /* Print test result */
- if(i32Err)
- {
- printf(" [FAIL].\n");
- }
- else
- {
- printf(" [OK].\n");
- }
- /* Configure PB.3 and PD.7 to default Quasi-bidirectional mode */
- GPIO_SetMode(PB, BIT3, GPIO_MODE_QUASI);
- GPIO_SetMode(PD, BIT7, GPIO_MODE_QUASI);
- while(1);
- }
- /*** (C) COPYRIGHT 2019 Nuvoton Technology Corp. ***/
|