Louie NRT Story

[Firmware] M058 - Timer Interrupt 본문

스마트홈

[Firmware] M058 - Timer Interrupt

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

[설명]

- PIN 30, 31 통하여 UART 통신함

- Timer Interrupt를 통해 Count 값을 올림

- 1초마다 1배, 2배, 4배, 8배 증가함

 

[동작화면]

[소스코드]

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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/***********************************
* @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
 
/*---------------------------------------------------------------------------------------------------------*/
/* Global Interface Variables Declarations                                                                 */
/*---------------------------------------------------------------------------------------------------------*/
volatile uint32_t g_au32TMRINTCount[4= {0};
 
/**
 * @brief       Timer0 IRQ
 * @param       None
 * @return      None
 * @details     The Timer0 default IRQ, declared in startup_M051Series.s.
 */
void TMR0_IRQHandler(void)
{
    if(TIMER_GetIntFlag(TIMER0) == 1)
    {
        /* Clear Timer0 time-out interrupt flag */
        TIMER_ClearIntFlag(TIMER0);
        g_au32TMRINTCount[0]++;
    }
}
 
/**
 * @brief       Timer1 IRQ
 * @param       None
 * @return      None
 * @details     The Timer1 default IRQ, declared in startup_M051Series.s.
 */
void TMR1_IRQHandler(void)
{
    if(TIMER_GetIntFlag(TIMER1) == 1)
    {
        /* Clear Timer1 time-out interrupt flag */
        TIMER_ClearIntFlag(TIMER1);
        g_au32TMRINTCount[1]++;
    }
}
 
/**
 * @brief       Timer2 IRQ
 * @param       None
 * @return      None
 * @details     The Timer2 default IRQ, declared in startup_M051Series.s.
 */
void TMR2_IRQHandler(void)
{
    if(TIMER_GetIntFlag(TIMER2) == 1)
    {
        /* Clear Timer2 time-out interrupt flag */
        TIMER_ClearIntFlag(TIMER2);
        g_au32TMRINTCount[2]++;
    }
}
 
/**
 * @brief       Timer3 IRQ
 * @param       None
 * @return      None
 * @details     The Timer3 default IRQ, declared in startup_M051Series.s.
 */
void TMR3_IRQHandler(void)
{
    if(TIMER_GetIntFlag(TIMER3) == 1)
    {
        /* Clear Timer3 time-out interrupt flag */
        TIMER_ClearIntFlag(TIMER3);
        g_au32TMRINTCount[3]++;
    }
}
 
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->APBCLK |= CLK_APBCLK_UART0_EN_Msk | CLK_APBCLK_TMR0_EN_Msk | CLK_APBCLK_TMR1_EN_Msk | CLK_APBCLK_TMR2_EN_Msk | CLK_APBCLK_TMR3_EN_Msk;
    CLK->CLKSEL1 |= CLK_CLKSEL1_UART_S_PLL | CLK_CLKSEL1_TMR0_S_HXT | CLK_CLKSEL1_TMR1_S_HCLK | CLK_CLKSEL1_TMR2_S_HIRC | CLK_CLKSEL1_TMR3_S_HXT;    
    
    /* Update System Core Clock */
    /* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
    SystemCoreClockUpdate();
    
    /*--------------------------------*/
    /* 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)
{    
    volatile uint32_t u32InitCount;
 
    /* 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("|    Timer Periodic and Interrupt Sample Code    |\n");
    printf("+------------------------------------------------+\n\n");
 
    printf("# Timer Settings:\n");
    printf("  Timer0: Clock source 12 MHz;       Periodic mode; Enable interrupt; 1 interrupt tick/sec.\n");
    printf("  Timer1: Clock source HCLK(50 MHz); Periodic mode; Enable interrupt; 2 interrupt ticks/sec.\n");
    printf("  Timer2: Clock source HIRC(22 MHz); Periodic mode; Enable interrupt; 4 interrupt ticks/sec.\n");
    printf("  Timer3: Clock source 12 MHz;       Periodic mode; Enable interrupt; 8 interrupt ticks/sec.\n");
    printf("# Check Timer0 ~ Timer3 interrupt counts are reasonable or not.\n\n");
    
    /* Open Timer0 frequency to 0.5 Hz in periodic mode, and enable interrupt */
    TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1);
    TIMER_EnableInt(TIMER0);
 
    /* Open Timer1 frequency to 1 Hz in periodic mode, and enable interrupt */
    TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, 2);
    TIMER_EnableInt(TIMER1);
 
    /* Open Timer2 frequency to 2 Hz in periodic mode, and enable interrupt */
    TIMER_Open(TIMER2, TIMER_PERIODIC_MODE, 4);
    TIMER_EnableInt(TIMER2);
 
    /* Open Timer3 frequency to 4 Hz in periodic mode, and enable interrupt */
    TIMER_Open(TIMER3, TIMER_PERIODIC_MODE, 8);
    TIMER_EnableInt(TIMER3);
 
    /* Enable Timer0 ~ Timer3 NVIC */
    NVIC_EnableIRQ(TMR0_IRQn);
    NVIC_EnableIRQ(TMR1_IRQn);
    NVIC_EnableIRQ(TMR2_IRQn);
    NVIC_EnableIRQ(TMR3_IRQn);
 
    /* Clear Timer0 ~ Timer3 interrupt counts to 0 */
    g_au32TMRINTCount[0= g_au32TMRINTCount[1= g_au32TMRINTCount[2= g_au32TMRINTCount[3= 0;
    u32InitCount = g_au32TMRINTCount[0];
 
    /* Start Timer0 ~ Timer3 counting */
    TIMER_Start(TIMER0);
    TIMER_Start(TIMER1);
    TIMER_Start(TIMER2);
    TIMER_Start(TIMER3);
 
    /* Check Timer0 ~ Timer3 interrupt counts */
    printf("# Timer interrupt counts :\n");
    while(u32InitCount < 20)
    {
        if(g_au32TMRINTCount[0!= u32InitCount)
        {
            printf("TMR0:%3d    TMR1:%3d    TMR2:%3d    TMR3:%3d\n",
                   g_au32TMRINTCount[0], g_au32TMRINTCount[1], g_au32TMRINTCount[2], g_au32TMRINTCount[3]);
            u32InitCount = g_au32TMRINTCount[0];
        }
    }
    printf("*** PASS ***\n");
    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
반응형

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

[제품분해] 탄소 발열매트  (0) 2019.12.27
[Nuvoton] M058 - UART Interrupt  (0) 2019.05.15
[Firmware] M058 - GPIO Interrupt  (0) 2019.05.03
[Firmware] M058 - UART  (0) 2019.05.03
[Firmware] M058 - PWM  (0) 2019.05.03
Comments