/**************************************************************************//**
* [url=home.php?mod=space&uid=288409]@file[/url] main.c
* [url=home.php?mod=space&uid=895143]@version[/url] V3.00
* $Revision: 4 $
* $Date: 14/01/28 11:45a $
* [url=home.php?mod=space&uid=247401]@brief[/url] M051 Series Timer Driver Sample Code
*
* @note
* Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
******************************************************************************/
#include <stdio.h>
#include "M051Series.h"
#define PLLCON_SETTING CLK_PLLCON_50MHz_HXT
#define PLL_CLOCK 50000000
unsigned int data;
unsigned int count;
unsigned int p_flag;
/**
* @brief Timer0 IRQ
*
* @param None
*
* [url=home.php?mod=space&uid=266161]@return[/url] None
*
* [url=home.php?mod=space&uid=1543424]@Details[/url] The Timer0 default IRQ, declared in startup_M051Series.s.
*/
void TMR0_IRQHandler(void)
{
/* Clear Timer0 time-out interrupt flag */
TIMER_ClearIntFlag(TIMER0);
if(count!=0)
{
if(data&0x01==0x01)
P27=1;
else
P27=0;
data=data>>1;
count--;
}
}
void UART_GPIO(unsigned char uart_data)
{
data=0;
data=(uart_data<<1)|(1<<9);
count=10;
}
void SYS_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init System Clock */
/*---------------------------------------------------------------------------------------------------------*/
/* Enable IRC22M clock */
CLK->PWRCON |= CLK_PWRCON_IRC22M_EN_Msk;
/* Waiting for IRC22M clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_IRC22M_STB_Msk);
/* Switch HCLK clock source to HIRC */
CLK->CLKSEL0 = CLK_CLKSEL0_HCLK_S_HIRC;
/* Set PLL to Power-down mode and PLL_STB bit in CLKSTATUS register will be cleared by hardware.*/
CLK->PLLCON |= CLK_PLLCON_PD_Msk;
/* Enable external 12 MHz XTAL, IRC10K */
CLK->PWRCON |= CLK_PWRCON_XTL12M_EN_Msk | CLK_PWRCON_OSC10K_EN_Msk;
/* Enable PLL and Set PLL frequency */
CLK->PLLCON = PLLCON_SETTING;
/* Waiting for clock ready */
CLK_WaitClockReady(CLK_CLKSTATUS_PLL_STB_Msk | CLK_CLKSTATUS_XTL12M_STB_Msk | CLK_CLKSTATUS_IRC10K_STB_Msk);
/* Switch HCLK clock source to PLL, STCLK to HCLK/2 */
CLK->CLKSEL0 = CLK_CLKSEL0_STCLK_S_HCLK_DIV2 | CLK_CLKSEL0_HCLK_S_PLL;
/* Enable peripheral clock */
CLK->APBCLK = CLK_APBCLK_UART0_EN_Msk |
CLK_APBCLK_TMR0_EN_Msk ;
/* Peripheral clock source */
CLK->CLKSEL1 = CLK_CLKSEL1_UART_S_PLL |CLK_CLKSEL1_TMR0_S_HIRC;
/* Update System Core Clock */
/* User can use SystemCoreClockUpdate() to calculate PllClock, SystemCoreClock and CycylesPerUs automatically. */
SystemCoreClockUpdate();
/*---------------------------------------------------------------------------------------------------------*/
/* Init I/O Multi-function */
/*---------------------------------------------------------------------------------------------------------*/
/* Set P3 multi-function pins for UART0 RXD, TXD */
SYS->P3_MFP = SYS_MFP_P30_RXD0 | SYS_MFP_P31_TXD0;
}
void UART0_Init(void)
{
/*---------------------------------------------------------------------------------------------------------*/
/* Init UART */
/*---------------------------------------------------------------------------------------------------------*/
/* Reset IP */
SYS_ResetModule(UART0_RST);
/* Configure UART0 and set UART0 Baudrate */
UART_Open(UART0, 115200);
}
/*---------------------------------------------------------------------------------------------------------*/
/* MAIN function */
/*---------------------------------------------------------------------------------------------------------*/
int main(void)
{
int i;
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();
/* Configure P1.5 as Output mode*/
GPIO_SetMode(P2, BIT7, GPIO_PMD_OUTPUT);
/* Open Timer0 frequency 38400 in periodic mode, and enable interrupt */
TIMER0->TCMPR = (22118400/38400);
TIMER0->TCSR = TIMER_TCSR_IE_Msk | TIMER_PERIODIC_MODE;
TIMER_SET_PRESCALE_VALUE(TIMER0, 0);
/* Enable Timer0 NVIC */
NVIC_EnableIRQ(TMR0_IRQn);
/* Start Timer0 counting */
TIMER_Start(TIMER0);
while(1){
if(count==0)
{
CLK_SysTickDelay(1000);
UART_GPIO(i);
i++;
}
}
}
/*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/
|