Louie NRT Story

[Firmware] M058 - GPIO Interrupt 본문

스마트홈

[Firmware] M058 - GPIO Interrupt

hyeok0724.kim@gmail.com 2019. 5. 3. 17:40
반응형

[설명]

- PIN 34에 스위치를 설치함
- 스위치가 눌리면 하던 동작과 관계없이 Interrupt 동작함

 

[동작화면]

 

[소스코드]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/***********************************
* @Auther: hyeok0724@naver.com
* @Create Date: 19.05.03
* @Latest Modify Date: 19.05.03
* @Brief: 
***********************************/
 
#include <stdio.h>
#include "M051Series.h"
 
#define PLL_CLOCK    50000000 //50GHz Clock
 
/**
 * @brief       Port0/Port1 IRQ
 * @param       None
 * @return      None
 * @details     The Port0/Port1 default IRQ, declared in startup_M051Series.s.
 */
void GPIOP0P1_IRQHandler(void)
{
    /* To check if P1.3 interrupt occurred */
    if(GPIO_GET_INT_FLAG(P1, BIT3))
    {
        GPIO_CLR_INT_FLAG(P1, BIT3);
        printf("P1.3 INT Occurred.\n");
    }
    else
    {
        /* Un-expected interrupt. Just clear all PORT0, PORT1 interrupts */
        P0->ISRC = P0->ISRC;
        P1->ISRC = P1->ISRC;
        printf("Un-expected interrupts.\n");
    }
}
 
/**
 * @brief       Port2/Port3/Port4 IRQ
 * @param       None
 * @return      None
 * @details     The Port2/Port3/Port4 default IRQ, declared in startup_M051Series.s.
 */
void GPIOP2P3P4_IRQHandler(void)
{
    /* To check if P4.5 interrupt occurred */
    if(GPIO_GET_INT_FLAG(P3, BIT4))
    {
        GPIO_CLR_INT_FLAG(P3, BIT4);
        printf("P3.4 INT occurred.\n");
    }
    else
    {
        /* Un-expected interrupt. Just clear all PORT2, PORT3 and PORT4 interrupts */
        P2->ISRC = P2->ISRC;
        P3->ISRC = P3->ISRC;
        P4->ISRC = P4->ISRC;
        printf("Un-expected interrupts.\n");
    }
}
 
void UART0_Init(void)
{
    /*-----------------------------*/
    /* Init UART                   */
    /*-----------------------------*/
    /* Reset UART0 */
    SYS_ResetModule(UART0_RST);
 
    /* Configure UART0 and set UART0 Baudrate */
    UART_Open(UART0, 115200);
}
 
void SYS_Init(void)
{
    /*------------------------------*/
    /* Init System Clock            */
    /*------------------------------*/
    
    /* Enable Internal RC 22.1184MHz clock */
    CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);
    
    /* Waiting for Internal RC clock ready */
    CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
 
    /* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
    CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));
    
    //Tip* This code is not used because setting for using external clock
    /* Enable external XTAL 12MHz clock */
    //CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);
 
    /* Waiting for external XTAL clock ready */
    //CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);
    
    /* Set core clock as PLL_CLOCK from PLL */
    CLK_SetCoreClock(PLL_CLOCK);
 
    /* Enable UART module clock */
    CLK_EnableModuleClock(UART0_MODULE);
 
    /* Select UART module clock source */
    CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_PLL, CLK_CLKDIV_UART(1));
    
    /*--------------------------------*/
    /* Init I/O Multi-function        */
    /*--------------------------------*/
    
    //Tip* Normally pins are setted for speical function. Have to set pin feather.
    
    /* Set P3 multi-function pins for UART0 RXD and TXD */
    SYS->P3_MFP &= ~(SYS_MFP_P30_Msk | SYS_MFP_P31_Msk);
    SYS->P3_MFP |= (SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0);
}
 
int main(void)
{    
    /* 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 @ %d Hz\n", SystemCoreClock);
    printf("+------------------------------------------------+\n");
    printf("|    GPIO P1.3 and P3.4 Interrupt Sample Code_v0.1    |\n");
    printf("+------------------------------------------------+\n\n");
    printf("P1.3 and P3.4 are used to test interrupt ......\n");
    
    GPIO_SetMode(P3, BIT4, GPIO_PMD_INPUT);
    GPIO_EnableInt(P3, 4, GPIO_INT_FALLING);
    NVIC_EnableIRQ(GPIO_P2P3P4_IRQn);
    
    while(1);
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none; color:white">cs
반응형

'스마트홈' 카테고리의 다른 글

[Nuvoton] M058 - UART Interrupt  (0) 2019.05.15
[Firmware] M058 - Timer Interrupt  (0) 2019.05.03
[Firmware] M058 - UART  (0) 2019.05.03
[Firmware] M058 - PWM  (0) 2019.05.03
[Firmware] M058 - gpio_timer_delay  (0) 2019.05.03
Comments