本文来自http://blog.youkuaiyun.com/hellogv/
#include"SPCE061a.h"
unsigned int tmp_data;
void UART_Init()
{
asm("INT OFF");//禁止IRQ和FIQ中断
*P_IOB_Dir=0x0400;
*P_IOB_Attrib=0x0480;
*P_IOB_Data=0x0000;
*P_UART_Command1=C_UART_Reset;//UART内部复位
*P_UART_BaudScalarHigh=0x05; //波特率设置9600
*P_UART_BaudScalarLow=0x00;
*P_UART_Command1=0x80;//1000 0000 允许Rx中断
*P_UART_Command2=0xc0; //1100 0000 控制字
asm("INT IRQ");
tmp_data=*P_UART_Data; //清接收缓冲区(很必要)
}
char GetData()
{
tmp_data=*P_UART_Command2;
tmp_data=tmp_data&0x0080;
while(tmp_data==0)
{
tmp_data=*P_UART_Command2;
tmp_data=tmp_data&0x0080;
*P_Watchdog_Clear=C_WDTCLR;
}
return *P_UART_Data;
}
void SendData(char Data)
{
*P_Watchdog_Clear=0x0001;
tmp_data=*P_UART_Command2;
tmp_data=tmp_data&0x0040;//判断数据发送是否已经准备好
while(tmp_data==0) //等待发送完毕
{
tmp_data=*P_UART_Command2;
tmp_data=tmp_data&0x0040;
*P_Watchdog_Clear=0x0001;
}
*P_UART_Data=Data;
*P_Watchdog_Clear=0x0001;
}
int main()
{
UART_Init();
while(1)
{
*P_Watchdog_Clear=C_WDTCLR;
}
return 0;
}
void IRQ7(void) __attribute__ ((ISR));
void IRQ7(void)
{
__asm("IRQ OFF");
SendData(GetData());//收到什么,就发什么回去
__asm("IRQ ON");
}