一、串口编写总框图
二、分步介绍
#define GPHCON (*(volatile unsigned long *)0x56000070)
#define ULCON0 (*(volatile unsigned long *)0x50000000)
#define UCON0 (*(volatile unsigned long *)0x50000004)
#define UBRDIV0 (*(volatile unsigned long *)0x50000028)
#define UTRSTAT0 (*(volatile unsigned long *)0x50000010)
#define UTXH0 (*(volatile unsigned long *)0x50000020)
#define URXH0 (*(volatile unsigned long *)0x50000024)
#define PCLK 50000000
#define BAUDRATE 115200
1、初始化
①、配置引脚功能
GPHCON &= (~(0b11<<4))|(~(0b11<<6));//端口清零
GPHCON |= (0b10<<4)|(0b10<<6);//设置功能
②、设置数据格式
ULCON0 = (0b11<<0) | (0b0<<2) | (0b000<<3);
设置为 No parity;One stop bit per frame;8-bits
③、设置工作模式
UCON0 = (0b01<<0) | (0b01<<2);
发送和接受都设置为Interrupt request or polling mode
④、设置波特率
UBRDIV0 = (int)(PCLK/(BAUDRATE*16)) - 1;
本实验中UART0使用的是PCLK
2、发送数据
void putc(unsigned char ch)
{
while( !(UTRSTAT0 & (1<<2)));
UTXH0 = ch;
}
只使用低八位
3、接收数据
unsigned char getc()
{
while( !(UTRSTAT0 & (1<<0)));
return URXH0;
}
只使用低八位