打印
[DemoCode下载]

Nano100B的GPIO测试

[复制链接]
1176|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
zhuomuniao110|  楼主 | 2017-5-4 17:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
GPIO, AN, TE, rc
/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url]     main.c
* [url=home.php?mod=space&uid=895143]@version[/url]  V2.10
* $Date: 15/07/20 9:38a $
* [url=home.php?mod=space&uid=247401]@brief[/url]    Use GPIO driver to control the GPIO pin direction and the high/low state,
*           and show how to use GPIO interrupts.
*
* @note
* Copyright (C) 2012 Nuvoton Technology Corp. All rights reserved.
*
******************************************************************************/
#include <stdio.h>
#include "nano100series.h"

/**
* @brief       PortA/PortB/PortC IRQ
*
* @param       None
*
* [url=home.php?mod=space&uid=266161]@return[/url]      None
*
* [url=home.php?mod=space&uid=1543424]@Details[/url]     The PortA/PortB/PortC default IRQ, declared in startup_nano100series.s.
*/
void GPABC_IRQHandler(void)
{
    /* To check if PB.5 interrupt occurred */
    if (PB->ISRC & BIT5) {
        PB->ISRC = BIT5;
        PD0 = PD0 ^ 1;
        printf("PB.5 INT occurred. \n");

    } else {
        /* Un-expected interrupt. Just clear all PORTA, PORTB, PORTC interrupts */
        PA->ISRC = PA->ISRC;
        PB->ISRC = PB->ISRC;
        PC->ISRC = PC->ISRC;
        printf("Un-expected interrupts. \n");
    }
}


/**
* @brief       PortD/PortE/PortF IRQ
*
* @param       None
*
* @return      None
*
* @details     The PortD/PortE/PortF default IRQ, declared in startup_nano100series.s.
*/
void GPDEF_IRQHandler(void)
{
    /* To check if PE.2 interrupt occurred */
    if (PE->ISRC & BIT2) {
        PE->ISRC = BIT2;
        PD0 = PD0 ^ 1;
        printf("PE.2 INT occurred. \n");
    } else {
        /* Un-expected interrupt. Just clear all PORTD, PORTE and PORTF interrupts */
        PD->ISRC = PD->ISRC;
        PE->ISRC = PE->ISRC;
        PF->ISRC = PF->ISRC;
        printf("Un-expected interrupts. \n");
    }
}


/**
* @brief       External INT0 IRQ
*
* @param       None
*
* @return      None
*
* @details     The External INT0(PA.2) default IRQ, declared in startup_nano100series.s.
*/
void EINT0_IRQHandler(void)
{
    /* For PB.14, clear the INT flag */
    PB->ISRC = BIT14;
    PD0 = PD0 ^ 1;
    printf("PB.14 EINT0 occurred. \n");
}


/**
* @brief       External INT1 IRQ
*
* @param       None
*
* @return      None
*
* @details     The External INT1(PF.2) default IRQ, declared in startup_Nano100series.s.
*/
void EINT1_IRQHandler(void)
{
    /* For PB.15, clear the INT flag */
    PB->ISRC = BIT15;
    PD0 = PD0 ^ 1;
    printf("PB.15 EINT1 occurred. \n");
}

void SYS_Init(void)
{
    /* Unlock protected registers */
    SYS_UnlockReg();

    /* Enable external 12MHz HXT */
    CLK_EnableXtalRC(CLK_PWRCTL_HXT_EN_Msk);
    CLK_EnablePLL(CLK_PLLCTL_PLL_SRC_HXT, 96000000);
    /* Waiting for clock ready */
    CLK_WaitClockReady(CLK_CLKSTATUS_HXT_STB_Msk | CLK_CLKSTATUS_PLL_STB_Msk);

    CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_PLL, CLK_HCLK_CLK_DIVIDER(3));

    /* Select IP clock source */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_UART_CLK_DIVIDER(1));
    /* Enable IP clock */
    CLK_EnableModuleClock(UART0_MODULE);

    /*---------------------------------------------------------------------------------------------------------*/
    /* Init I/O Multi-function                                                                                 */
    /*---------------------------------------------------------------------------------------------------------*/
    /* Set PA multi-function pins for UART0 RXD and TXD */
//    SYS->PA_H_MFP &= ~( SYS_PA_H_MFP_PA15_MFP_Msk | SYS_PA_H_MFP_PA14_MFP_Msk);
//    SYS->PA_H_MFP |= (SYS_PA_H_MFP_PA15_MFP_UART0_TX|SYS_PA_H_MFP_PA14_MFP_UART0_RX);       
    SYS->PB_L_MFP &= ~( SYS_PB_L_MFP_PB0_MFP_Msk | SYS_PB_L_MFP_PB1_MFP_Msk);
    SYS->PB_L_MFP |= (SYS_PB_L_MFP_PB1_MFP_UART0_TX|SYS_PB_L_MFP_PB0_MFP_UART0_RX);       

    /* Lock protected registers */
    SYS_LockReg();
}

void UART0_Init(void)
{
    /* Reset IP */
    SYS_ResetModule(UART0_RST);
    UART0->BAUD = 0x67;              /* Baud Rate:115200  OSC:12MHz */
    UART0->TLCTL = 0x03;             /* Character len is 8 bits */       
}

/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function                                                                                           */
/*---------------------------------------------------------------------------------------------------------*/
int main (void)
{
    int32_t i32Err;

    /* Init System, IP clock and multi-function I/O */
    SYS_Init(); //In the end of SYS_Init() will issue SYS_LockReg() to lock protected register. If user want to write protected register, please issue SYS_UnlockReg() to unlock protected register.

    /* Init UART for printf */
    UART0_Init();

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

    printf("+--------------------------------------+ \n");
    printf("|    NANO100 GPIO Driver Sample Code   | \n");
    printf("+--------------------------------------+ \n");

    /*-----------------------------------------------------------------------------------------------------*/
    /* GPIO Basic Mode Test --- Use Pin Data Input/Output to control GPIO pin                              */
    /*-----------------------------------------------------------------------------------------------------*/
    printf("  >> Please connect PA.0 and PA.1 first << \n");
    printf("     Press any key to start test by using [Pin Data Input/Output Control] \n\n");
    getchar();

    /* Configure PA.0 as Output mode and PA.1 as Input mode then close it */
    GPIO_SetMode(PA, BIT0, GPIO_PMD_OUTPUT);
    GPIO_SetMode(PA, BIT1, GPIO_PMD_INPUT);

    i32Err = 0;
    printf("  GPIO Output/Input test ...... \n");

    /* Use Pin Data Input/Output Control to pull specified I/O or get I/O pin status */
    PA0 = 0;
    CLK_SysTickDelay(10);   /* wait for IO stable */
    if (PA1 != 0) {
        i32Err = 1;
    }

    PA0 = 1;
    CLK_SysTickDelay(10);   /* wait for IO stable */
    if (PA1 != 1) {
        i32Err = 1;
    }

    if ( i32Err ) {
        printf("  [FAIL] --- Please make sure PA.0 and PA.1 are connected. \n");
    } else {
        printf("  [OK] \n");
    }

    /* Configure PA.0 and PA.1 to default Input mode */
    GPIO_SetMode(PA, BIT0, GPIO_PMD_INPUT);
    GPIO_SetMode(PA, BIT1, GPIO_PMD_INPUT);


    /*-----------------------------------------------------------------------------------------------------*/
    /* GPIO Interrupt Function Test                                                                        */
    /*-----------------------------------------------------------------------------------------------------*/
    printf("\n  GPB5, GPE2, GPB14(EINT0) and GPB15(EINT1) are used to test interrupt\n  and control LED (GPA7)\n");   

    /*Configure PA7 for LED control */
    GPIO_SetMode(PA, BIT7, GPIO_PMD_OUTPUT);

    /* Configure PB5 as Input mode and enable interrupt by rising edge trigger */
    GPIO_SetMode(PB, BIT5, GPIO_PMD_INPUT);
    GPIO_ENABLE_PULL_UP(PB, BIT5);
    GPIO_EnableInt(PB, 5, GPIO_INT_RISING);

    NVIC_EnableIRQ(GPABC_IRQn);

    /*  Configure PE2 as Input mode pull-up and enable interrupt by low level trigger */
    GPIO_SetMode(PE, BIT2, GPIO_PMD_INPUT);
    GPIO_ENABLE_PULL_UP(PE, BIT2);
    GPIO_EnableInt(PE, 2, GPIO_INT_LOW);

    NVIC_EnableIRQ(GPDEF_IRQn);

    /* Configure PB14 as EINT0 pin and enable interrupt by falling edge trigger */
    SYS->PB_H_MFP = (SYS->PB_H_MFP & (~0x07000000)) | 0x01000000;
    GPIO_SetMode(PB, BIT14, GPIO_PMD_INPUT);
    GPIO_EnableEINT0(PB, 14, GPIO_INT_FALLING);
    NVIC_EnableIRQ(EINT0_IRQn);

    /* Configure PB15 as EINT1 pin and enable interrupt by rising and falling edge trigger */
    SYS->PB_H_MFP = (SYS->PB_H_MFP & (~0x70000000)) | 0x10000000;
    GPIO_SetMode(PB, BIT15, GPIO_PMD_INPUT);
    GPIO_EnableEINT1(PB, 15, GPIO_INT_BOTH_EDGE);
    NVIC_EnableIRQ(EINT1_IRQn);

    /* Enable interrupt de-bounce function and select de-bounce sampling cycle time */
    GPIO_SET_DEBOUNCE_TIME(GPIO_DBCLKSRC_HCLK, GPIO_DBCLKSEL_1);
    GPIO_ENABLE_DEBOUNCE(PB, BIT5);
    GPIO_ENABLE_DEBOUNCE(PE, BIT2);
    GPIO_ENABLE_DEBOUNCE(PB, BIT14);
    GPIO_ENABLE_DEBOUNCE(PB, BIT15);

    /* Waiting for interrupts */
    while (1);

}



沙发
zhuomuniao110|  楼主 | 2017-5-4 17:30 | 只看该作者
我们注意到虽然是个简单的程序,配置上就很麻烦,但是也就是配置,配置完成后用起来超级简单好用了。

使用特权

评论回复
板凳
zhuomuniao110|  楼主 | 2017-5-4 17:31 | 只看该作者
根据中断的处理函数名称也可以清楚的看出来ABC三个端口是共用了一个中断入口的。进来后判断。

使用特权

评论回复
地板
zhuomuniao110|  楼主 | 2017-5-4 17:31 | 只看该作者
其实这种多端口共用一个中断入口的函数处理,用switch case比较好,特别非常多的情况下。少的话,那就是if else

使用特权

评论回复
5
wahahaheihei| | 2017-5-4 21:29 | 只看该作者
两种不同的外部中断。高级任务采用纯粹的中断。

使用特权

评论回复
6
zhuomuniao110|  楼主 | 2017-5-5 11:27 | 只看该作者
/* Un-expected interrupt. Just clear all PORTA, PORTB, PORTC interrupts */
        PA->ISRC = PA->ISRC;
        PB->ISRC = PB->ISRC;
        PC->ISRC = PC->ISRC;
        printf("Un-expected interrupts. \n");
根据这个可以看出来,清理中断标志的方式就是写对应的寄存器为1,就清零,这个地方不是写0

使用特权

评论回复
7
yiyigirl2014| | 2017-5-6 22:59 | 只看该作者
不同型号某些寄存器清零方式是不同的,有的可以写0就行了,有的是写1完成清零。

使用特权

评论回复
8
wahahaheihei| | 2017-5-7 16:25 | 只看该作者
官方应该这些用串口1打印比较好,这样还可以提供个串口1使用的例程。

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

188

主题

3247

帖子

10

粉丝