- /**************************************************************************//**
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=895143]@version[/url] V3.00
- * $Revision: 3 $
- * $Date: 15/09/02 10:03a $
- * [url=home.php?mod=space&uid=247401]@brief[/url] Demonstrate how to set GPIO pin mode and use pin data control RGB LED.
- * @note
- * Copyright (C) 2019 Nuvoton Technology Corp. All rights reserved.
- *
- ******************************************************************************/
- #include "stdio.h"
- #include "M451Series.h"
- #include "NuEdu-Basic01.h"
- #define PLL_CLOCK 72000000
- /*---------------------------------------------------------------------------------------------------------*/
- /* Global variables */
- /*---------------------------------------------------------------------------------------------------------*/
- uint32_t LED1_R, LED1_G, LED1_B, Blink,brea=0,LED_cnt=0,brea_cnt=0;
- void SYS_Init(void)
- {
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init System Clock */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Enable HIRC clock (Internal RC 22.1184MHz) */
- CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);
- /* Wait for HIRC clock ready */
- CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);
- /* Select HCLK clock source as HIRC and and HCLK clock divider as 1 */
- CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));
- /* Enable HXT clock (external XTAL 12MHz) */
- CLK_EnableXtalRC(CLK_PWRCTL_HXTEN_Msk);
- /* Wait for HXT clock ready */
- CLK_WaitClockReady(CLK_STATUS_HXTSTB_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 as HXT and UART module clock divider as 1 */
- CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UARTSEL_HXT, CLK_CLKDIV0_UART(1));
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init I/O Multi-function */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Set PD multi-function pins for UART0 RXD(PD.6) and TXD(PD.1) */
- SYS->GPD_MFPL &= ~(SYS_GPD_MFPL_PD6MFP_Msk | SYS_GPD_MFPL_PD1MFP_Msk);
- SYS->GPD_MFPL |= (SYS_GPD_MFPL_PD6MFP_UART0_RXD | SYS_GPD_MFPL_PD1MFP_UART0_TXD);
- }
- void UART0_Init()
- {
- /*---------------------------------------------------------------------------------------------------------*/
- /* Init UART */
- /*---------------------------------------------------------------------------------------------------------*/
- /* Reset UART module */
- SYS_ResetModule(UART0_RST);
- /* Configure UART0 and set UART0 baud rate */
- UART_Open(UART0, 115200);
- }
- void TMR1_IRQHandler(void)
- {
- uint32_t LED_duty,RLED,BLED,GLED,LED_brea;
- if(TIMER_GetIntFlag(TIMER1) == 1) {
- /* Clear Timer1 time-out interrupt flag */
- TIMER_ClearIntFlag(TIMER1);
- LED_cnt++;
- LED_duty=LED_cnt%100;
- if((brea==1)&&((brea_cnt%2)==0))
- LED_brea=100-(LED_cnt/100);
- else if(brea==1)
- LED_brea=LED_cnt/100;
- else
- LED_brea=0;
- RLED=((int32_t)(LED1_R-LED_duty-LED_brea)>0)?1:0;
- BLED=((int32_t)(LED1_B-LED_duty-LED_brea)>0)?1:0;
- GLED=((int32_t)(LED1_G-LED_duty-LED_brea)>0)?1:0;
- if(LED_cnt>=(Blink*1000)) {
- RLED=0;
- BLED=0;
- GLED=0;
- }
- //PC->DOUT = (PC->DOUT&(~(BIT9|BIT10|BIT11)))|(RLED<<9)|(GLED<<10)|(BLED<<11);
- PC->DOUT = (PC->DOUT|BIT9|BIT10|BIT11)&(~((RLED<<9)|(GLED<<10)|(BLED<<11)));
- if(LED_cnt==10000) {
- LED_cnt=0;
- brea_cnt++;
- }
- }
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* Main Function */
- /*---------------------------------------------------------------------------------------------------------*/
- int32_t 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 [url=home.php?mod=space&uid=72445]@[/url] %dHz\n", SystemCoreClock);
- printf("LED test\n\r");
- GPIO_SetMode(PC, BIT11, GPIO_MODE_OUTPUT); //BLED
- GPIO_SetMode(PC, BIT10, GPIO_MODE_OUTPUT); //GLED
- GPIO_SetMode(PC, BIT9, GPIO_MODE_OUTPUT); //RLED
- /* Enable peripheral clock */
- CLK_EnableModuleClock(TMR1_MODULE);
- CLK_SetModuleClock(TMR1_MODULE, CLK_CLKSEL1_TMR1SEL_HIRC, 0);
- TIMER_Open(TIMER1, TIMER_PERIODIC_MODE, 10000);
- TIMER_EnableInt(TIMER1);
- /* Enable Timer1 NVIC */
- NVIC_EnableIRQ(TMR1_IRQn);
- /*setting RGB LED*/
- LED1_R=100;
- LED1_G=100;
- LED1_B=100;
- Blink=10;
- brea=1;
- TIMER_Start(TIMER1);
- while(1);
- }
|