1.看原理图
找到管脚 GPA1_0:RXD GPA1_1:TXD
2.看数据手册
GPA1CON: 0x11400020 -> 3:0 -> 0x2 7:4->0x2
uart设置:
代码如下:
#define GPA1CON (*(volatile unsigned int *)0x11400020)
#define ULCON2 (*(volatile unsigned int *)0x13820000)
#define UBRDIV2 (*(volatile unsigned int *)0x13820028)
#define UFRACVAL2 (*(volatile unsigned int *)0x1382002C)
#define UCON2 (*(volatile unsigned int *)0x13820004)
#define UTXH2 (*(volatile unsigned int *)0x13820020)
#define UTRSTAT2 (*(volatile unsigned int *)0x13820010)
//延时
void mydelay(int x)
{
int i;
while(x--)
{
for(i=1000;i>0;i--);
}
}
//uart初始化
void UART_Init()
{
//1.对外设置
GPA1CON &= ~0xff;
GPA1CON |= 0x22;
//2.对内设置
ULCON2 = 0x3;//数据位:8 停止位:1 奇偶校验位:无 模式:Normal
//波特率: 115200
UBRDIV2 = 53;//波特率的分频值
UFRACVAL2 = 4;
UCON2 = 0x5;//轮询模式发送和接收
}
//发送字符
void uart_send_byte(char ch)
{
while((UTRSTAT2 & 0x2) == 0);//等发送buffer空
UTXH2 = ch;//发送ch
}
int main(int argc, char *argv[])
{
UART_Init();
while(1)
{
uart_send_byte('a');
mydelay(500);
}
return 0;
}