#define rGPBCON (*(volatile unsigned *)0x56000010) //Port B control
#define rGPBDAT (*(volatile unsigned *)0x56000014) //Port B data
#define rGPBUP (*(volatile unsigned *)0x56000018) //Pull-up control B
#define rGPHCON (*(volatile unsigned *)0x56000070) //Port H control
#define rGPHUP (*(volatile unsigned *)0x56000078) //Pull-up control H
#define rULCON0 (*(volatile unsigned *)0x50000000) //UART 0 Line control
#define rUCON0 (*(volatile unsigned *)0x50000004) //UART 0 Control
#define rUFCON0 (*(volatile unsigned *)0x50000008) //UART 0 FIFO control
#define rUMCON0 (*(volatile unsigned *)0x5000000c) //UART 0 Modem control
#define rUTRSTAT0 (*(volatile unsigned *)0x50000010) //UART 0 Tx/Rx status
#define rUERSTAT0 (*(volatile unsigned *)0x50000014) //UART 0 Rx error status
#define rUFSTAT0 (*(volatile unsigned *)0x50000018) //UART 0 FIFO status
#define rUMSTAT0 (*(volatile unsigned *)0x5000001c) //UART 0 Modem status
#define rUBRDIV0 (*(volatile unsigned *)0x50000028) //UART 0 Baud rate divisor
//little endian
#define rUTXH0 (*(volatile unsigned char *)0x50000020) //UART 0 Transmission Hold
#define rURXH0 (*(volatile unsigned char *)0x50000024) //UART 0 Receive buffer
void Main(void)
{
char ch;
rGPBCON = 0x015551;
rGPBUP = 0x7ff;
rGPBDAT = 0x1e0;
rGPHCON = 0x00faaa; //使用UART0功能
rGPHUP = 0x7ff;
rULCON0 = 0x3; //设置UART0无奇偶校验,一位停止位,8位数据
rUCON0 = 0x245; //PCLK为时钟源,接收和发送数据为查询或中断方式
//10 0100 0101
rUFCON0 = 0; //
rUMCON0 = 0; //
rUBRDIV0 = 26; //设置波特率,PCLK为50MHz,波特率为115.2kHz
while(!(rUTRSTAT0 & 0x2)); //等待并判断发送缓存是否为空
rUTXH0 = 0xaa; //是空,则发送0xAA字节
while(1)
{
while(!(rUTRSTAT0 & 0x1)); //等待并判断接收缓存是否准备好
ch = rURXH0; //接收一个字节数据
while(!(rUTRSTAT0 & 0x2)); //等待并判断发送缓存是否为空
rUTXH0 = ch; //发送一个字节数据
switch(ch) //根据所接收数据的不同,执行不同的程序
{
case 0x11: //灭LED
rGPBDAT |= 0x1e0;
break;
case 0x22: //亮LED
rGPBDAT &= 0x1f;
break;
case 0x33: //蜂鸣器不响
rGPBDAT &= 0x1e0;
break;
case 0x44: //蜂鸣器响
rGPBDAT |= 0x1;
break;
default: //LED灭,蜂鸣器不响
rGPBDAT = 0x1e0;
break;
}
}
}
下面是UART中断程序,它要比查询复杂一些,因为涉及到了中断处理,并且UART发送数据和接收数据是一个中断源。主程序循环体内不执行任何程序,都在UART中断程序内执行。当接收到0x55字节数据时,亮两个LED,当接收到其他数据时,发送该字节,并在发送部分执行亮4个LED程序。 |