#define GPACON (*(volatile unsigned *)(0x7f008000))
#define GPAPUD (*(volatile unsigned *)(0x7f008008))
#define ULCON0 (*(volatile unsigned *)(0x7f005000))
#define UCON0 (*(volatile unsigned *)(0x7f005004))
#define UFCON0 (*(volatile unsigned *)(0x7f005008))
#define UMCON0 (*(volatile unsigned *)(0x7f00500c))
#define UBRDIV0 (*(volatile unsigned *)(0x7f005028))
#define rUTRSTAT0 (*(volatile unsigned *)(0x7f005010))
#define rUTXH0 (*(volatile unsigned *)(0x7f005020))
#define rURXH0 (*(volatile unsigned *)(0x7f005024))
#define PCLK 66000000
void uart0_init(void)
{
//配置GPA口为串口作用
GPACON&=~(0xff);
GPACON|=0x2|(0x2<<4);
GPAPUD&=~0xf;
//8个数据位,无校验位,1个停止位
ULCON0=0x03;
//查询方式,UART时钟源为PCLK
UCON0=0x805;
//不是使用FIFO
UFCON0=0x00;
//不使用流控
UMCON0=0x00;
//波特率为115200
UBRDIV0=((int)(PCLK/16/115200)-1);
}
void uart_send_byte(unsigned char byte)
{
//当UTRSTAT0发送缓冲为空
while(!(rUTRSTAT0&0x2));
//将byte写入UTXH0
rUTXH0=byte;
}
unsigned char uart_recv_byte(void)
{
unsigned char ch;
//当URTSTAT0接收缓冲满则从UTXH0接收一个字节
while(!(rUTRSTAT0&0x1));
//从函数返回该字节
ch=rURXH0;
return ch;
}
void uart_sendstring(char *pt)
{
while(*pt)
{
uart_send_byte(*pt);
pt++;
}
}
void main(void)
{
unsigned char ch='b';
char string[80]={"please input information\n"};
char st[50]="\n please input a charactor\n";
uart0_init();
uart_sendstring(string);
while(1)
{
uart_sendstring(st);
ch=uart_recv_byte();
if(ch==0x0a)
{
uart_send_byte(0x0d);
}
else
uart_send_byte(ch);
}
}