/* main.c */
#include "uart.h"
#include "clock.h"
#include "watchdog.h"
int Main(void)
{
char key = ' ';
clock_init(); //初始化时钟
uart_init(); //初始化串口
close_watchdog();
uart_send("uart communication success!\r\n");
while(1)
{
uart_send("If you want to quit ,please pess 'e'\r\n");
key = uart_get();
if (key == 'e')
{
uart_send ("you pressed 'e' and you'll quit!\r\n");
break;
}
else
{
uart_send("you pressed ");
uart_send(&key);
uart_send(",retry!\r\n");
}
}
uart_send("the program exited by user!\r\n");
return 0;
}
下面是串口相关部分源码:
void uart_init(void)
{
ULCON0 = 0x03; //8N1
UCON0 = 0x005; //中断或查询方式
UFCON0 = 0x00; //不使用FIFO
UMCON0 = 0x00; //不使用流控
UBRDIV0 = 27; //波特率为115200
GPHCON |= 0xa0; //GPH2,GPH3 set as TXD0,RXD0
GPHUP = 0x0c; //GPH2,GPH3内部上拉
}
void uart_send(char * c)
{
for (; *c != '\0'; c++)
{
while(!(UTRSTAT0 & TXD0READY)) ; //不断查询,直到可以发送数据
UTXH0 = *c ; //发送数据
}
}
unsigned char uart_get(void)
{
while(!(UTRSTAT0 & RXD0READY)) ; //不断查询,直到接收到了数据
return URXH0; //返回接收到的数据