/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2010 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include "Common.h"
#define RXBUFSIZE 64
/* Global variables */
volatile uint8_t comRbuf[RXBUFSIZE];
volatile uint16_t comRbytes = 0; /* Available receiving bytes */
volatile uint16_t comRhead = 0;
volatile uint16_t comRtail = 0;
volatile int32_t g_bWait = 1;
/*en:************************************************************************************
Description: UART0 ISR routine
****************************************************************************************/
/*中:************************************************************************************
描述 : UART0中断代码
****************************************************************************************/
void UART0_IRQHandler(void)
{
uint8_t bInChar[1]={0xFF};
if(UA0_ISR & RDA_INT) //中:检查是否接收中断//en:Check if receive interrupt
{
printf("\nInput:");
while(UA0_ISR & RDA_IF) //中:检查接收到的数据是否有效//en:Check if received data avaliable
{
while (UA0_FSR & RX_EMPTY); //中:等字符//en:Wait until an avaliable char present in RX FIFO
bInChar[0] = UA0_RBR; //中:读取字符//en:Read the char
printf("%c ", bInChar[0]);
if(bInChar[0] == '0')
{
g_bWait = 0;
}
if(comRbytes < RXBUFSIZE) //中:测缓冲区满否?//en:Check if buffer full
{
comRbuf[comRtail] = bInChar[0]; //中:字符队列//en:Enqueue the character
comRtail = (comRtail == (RXBUFSIZE-1)) ? 0 : (comRtail+1);
comRbytes++;
}
}
printf("\nTransmission Test:");
}
else if(UA0_ISR & THRE_INT) //中:检查是否发送中断//en:Check if transmit interrupt
{
uint16_t tmp;
tmp = comRtail;
if(comRhead != tmp)
{
bInChar[0] = comRbuf[comRhead];
while((UA0_FSR&TX_FULL) != 0); //中:发送FIFO满时等待//en:Wait until UART transmit FIFO is not full
UA0_THR = bInChar[0]; //中:发送一个字符//en:Transmit a char via UART
comRhead = (comRhead == (RXBUFSIZE-1)) ? 0 : (comRhead+1);
comRbytes--;
}
}
}
int32_t main()
{
Un_Lock_Reg(); //中:解锁被保护的寄存器位,以便用户访问//en:Unlock protected register bits, so that user can access them
PWRCON |= XTL12M_EN; //中:使能外部12MHz晶振//en:Enable external 12MHz crystal
while((CLKSTATUS & XTL12M_STB) == 0); //中:等12M晶振时钟稳定//en:Wait until external 12M crystal stable
CLKSEL0 = (CLKSEL0 & (~HCLK)) | HCLK_12M; //中:选外部12MHz晶振为系统时钟//en:Select 12M as system clock
Lock_Reg(); //中:重新锁被保护的寄存器位//en:Re-lock protected register bits
UART_Init();
printf("\nUART Sample Demo. (Press '0' to exit)\n");
UA0_IER |= (RDA_IEN | THRE_IEN | RLS_IEN); //中:使能中断//en:Enable UART0 interrupt
NVIC_ISER = UART0_INT; //中:使能NVIC UART0中断//en:Enable NVIC UART0 interrupt
while(g_bWait);
UA0_IER &= ~(RDA_IEN | THRE_IEN | RLS_IEN); //中:禁中断//en:Disable UART0 interrupt
NVIC_ICER = UART0_INT; //中:禁止NVIC UART0中断//en:Disable NVIC UART0 interrupt
printf("\nUART Sample Demo End.\n");
return 0;
}
|