/**************************************************************************//**
* @file main.c
* @version V3.00
* $Revision: 5 $
* $Date: 15/07/02 11:18a $
* @brief Demonstrate how to perform timer counting in periodic mode.
* @note
* Copyright (C) 2014~2015 Nuvoton Technology Corp. All rights reserved.
******************************************************************************/
#include <stdio.h>
#include "NUC123.h"
#define HCLK_CLOCK 72000000
unsigned int t_flag=0;
/**
* @brief Timer0 IRQ
*
* @param None
*
* @return None
*
* @Details The Timer0 default IRQ, declared in startup_NUC123.s.
*/
void TMR0_IRQHandler(void)
{
if(TIMER_GetIntFlag(TIMER0) == 1)
{
/* Clear Timer0 time-out interrupt flag */
TIMER_ClearIntFlag(TIMER0);
t_flag=1;
}
}
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable IRC22M clock */
CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);
/* Waiting for IRC22M clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);
/* Switch HCLK clock source to HIRC */
CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));
/* Enable XT1_OUT(PF.0) and XT1_IN(PF.1) */
SYS->GPF_MFP |= SYS_GPF_MFP_PF0_XT1_OUT | SYS_GPF_MFP_PF1_XT1_IN;
/* Enable external 12 MHz XTAL, IRC10K */
CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC10K_EN_Msk);
/* Waiting for clock ready */
while(!CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_OSC10K_STB_Msk));
/* Set core clock as HCLK_CLOCK */
CLK_SetCoreClock(HCLK_CLOCK);
/* Enable peripheral clock */
CLK_EnableModuleClock(UART0_MODULE);
CLK_EnableModuleClock(TMR0_MODULE);
/* Peripheral clock source */
CLK_SetModuleClock(TMR0_MODULE, CLK_CLKSEL1_TMR0_S_HXT, 0);
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set PB multi-function pins for UART0 RXD, TXD */
SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD;
}
void UART0_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset IP */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
}
unsigned int g_year=2015;
unsigned char g_month=2;
unsigned char g_day=28;
unsigned char g_day_old;
unsigned char g_weekly=2;
unsigned char hour=23;
unsigned char min=59;
unsigned char sec=30;
void SoftwareRTC(void)
{
if(t_flag==0)
return;
sec++;
if(sec==60)
{
sec=0;
min++;
}
if(min==60)
{
min=0;
hour++;
}
if(hour==24)
{
hour=0;
g_day++;
g_weekly++;
}
printf("hr.:%d :min: %d: sec: %d\n\r",hour,min,sec);
t_flag=0;
}
int isLeapYear(int year) {
return (year%400==0) || ((year%4==0) && (year%100!=0));
}
void SoftwareYMD(void)
{
int days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
days[1] += isLeapYear(g_year);
if(g_day==g_day_old)
return;
g_day_old=g_day;
if(g_day>days[g_month-1])
{
g_day=1;
g_month++;
}
if(g_weekly>7)//7 is sum day
g_weekly=1;
if(g_month>12)
{
g_month=1;
g_year++;
}
printf("Y:%d, M:%d, D:%d, Weekly:%d\n\r",g_year,g_month,g_day,g_weekly);
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
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);
/* Open Timer0 in periodic mode, enable interrupt and 1 interrupt tick per second */
TIMER_Open(TIMER0, TIMER_PERIODIC_MODE, 1);
TIMER_EnableInt(TIMER0);
/* Enable Timer0 ~ Timer3 NVIC */
NVIC_EnableIRQ(TMR0_IRQn);
/* Start Timer0 ~ Timer3 counting */
TIMER_Start(TIMER0);
//g_offset=g_weekly-getWeekday(g_year, g_month, g_day, 0);
while(1){
SoftwareRTC();
SoftwareYMD();
}
}
/*** (C) COPYRIGHT 2014~2015 Nuvoton Technology Corp. ***/
|