使用LPC2124的UART0发送数据
时间:2009-04-10 来源:creatory
500)this.width=500;" border=0>
使用LPC2124的UART0发送数据
根据Datasheet可以了解到要使用UART0功能,需要知道以下寄存器的配置:
UART0引脚说明:
RxD0:输入,串行数据输入
TxD0:输出,串行数据输出
1.UART0 Line Control Register(U0CLR:0xe000c00c)
该寄存器决定了发送/接收数据字符的格式
1:0 字长度选择,00:5位,01:6位,10:7位,11:8位
2 停止位选择,0:1个停止位,1:2个停止位(如果U0CLR[1:0]=00则为1.5个停止位)
3 奇偶使能,0:禁止奇偶校验,1:开启奇偶校验
5:4 奇偶选择,00:奇校验,01:偶校验,10:Forced'1' stick parity,11:Forced'0' stick parity
6 中断控制,0:禁止中断传输,1:允许中断传输,
7 除数锁存控制位,0:禁止访问除数锁存,1:允许除数锁存 2.UART0 Divisor Latch (M)LSB Register(U0DLL(M):0xe000c000(0xe000c004) when DLAB=1)
该寄存器是UART0波特率发生器的一部分,保存有用于除VPB时钟的数值用于产生波特率时钟.
U0DLL包含低8位,U0DLM包含高8位
U0DLL[7:0]:除数锁存低位寄存器,低8位
U0DLM[7:0]:除数锁存高位寄存器,高8位,决定了UART0的波特率 3.UART0 Transmitter Holding Register(U0THR:0xe000c000 when DLAB=0,write only)
串口0发送保存寄存器
U0THR[7:0] 写该寄存器写数据会使数据存入串口0的先入先出队列,当它到达先入先出队列的底部并 且发送器有效时字节将会被送出 4.UART0 Line Status Register (U0LSR:0xe000c014,read only)
串口状态寄存器
0 接收数据就绪,0:U0RBR为空,1:U0RBR包含有效数据
...
6 发送器空,0:U0THR/U0TSR包含有效数据,1:U0THR和U0TSR为空 /*Universal Asynchronous Reciver Transmitter 0(UART0)*/
/*通用异步串行口0,8位*/
#define U0RBR (*(volatile unsigned char*)0xe000c000)
#define U0THR (*(volatile unsigned char*)0xe000c000)
#define U0LCR (*(volatile unsigned char*)0xe000c00c)
#define U0DLL (*(volatile unsigned char *)0xe000c000)
#define U0DLM (*(volatile unsigned char *)0xe000c004)
#define U0LSR (*(volatile unsigned char *)0xe000c014) //UART0初始化:8位数据位,1位停止位,禁止奇偶校验,波特率为9600bps
void uart0Initilization(void){
U0LCR=0x83;
U0DLL=0x12;
U0DLM=0x00;
U0CLR=0x03;
}
//UART0发送一字节数据并等待发送完毕
void uart0SendByte(unsigned char data){
U0THR=data;
while((U0LSR & 0x40)==0){ //U0LSR中的第6位为0则说明还未发送完毕,延时一段时 间再查询直到发送完毕
delay(100);
}
}
//UART0发送一字符串
void uart0SendString(unsigned char *ptr){
while(1){
//发送回车换车
if(*ptr=='\0'){
uart0SendByte('\r');
uart0SendByte('\n');
break;
}
uart0SendByte(*ptr++);
}
} //UART0接收一字符
unsigned char uart0RecvByte(void){
while((U0LSR & 0x01)==0); //等待接收器有效
return U0RBR; //返回数据
} 别忘了让I/0连接UART0
由Datasheet可以看出PINSEL0的[1:0]位控制TxD(UART0)当为01时启用此功能,[3:2]位控制RxD, 也为01时启用,所以控制字写为...0101 ==>> 0x00000005(hex)即PINSEL0=0x00000005; //主程序main不断地由UART0送出[email protected]
int main(void){
unsigned char msg0[]="[email protected]";
unsigned char msg1[]="Work Hard to Learn ARM";
PINSEL0=0x00000005;
uart0Initilization();
while(1){
uart0SendString(msg0);
delay(100);
uart0SendString(msg1);
delay(100);
}
return 0;
} 500)this.width=500;" border=0>
根据Datasheet可以了解到要使用UART0功能,需要知道以下寄存器的配置:
UART0引脚说明:
RxD0:输入,串行数据输入
TxD0:输出,串行数据输出
1.UART0 Line Control Register(U0CLR:0xe000c00c)
该寄存器决定了发送/接收数据字符的格式
1:0 字长度选择,00:5位,01:6位,10:7位,11:8位
2 停止位选择,0:1个停止位,1:2个停止位(如果U0CLR[1:0]=00则为1.5个停止位)
3 奇偶使能,0:禁止奇偶校验,1:开启奇偶校验
5:4 奇偶选择,00:奇校验,01:偶校验,10:Forced'1' stick parity,11:Forced'0' stick parity
6 中断控制,0:禁止中断传输,1:允许中断传输,
7 除数锁存控制位,0:禁止访问除数锁存,1:允许除数锁存 2.UART0 Divisor Latch (M)LSB Register(U0DLL(M):0xe000c000(0xe000c004) when DLAB=1)
该寄存器是UART0波特率发生器的一部分,保存有用于除VPB时钟的数值用于产生波特率时钟.
U0DLL包含低8位,U0DLM包含高8位
U0DLL[7:0]:除数锁存低位寄存器,低8位
U0DLM[7:0]:除数锁存高位寄存器,高8位,决定了UART0的波特率 3.UART0 Transmitter Holding Register(U0THR:0xe000c000 when DLAB=0,write only)
串口0发送保存寄存器
U0THR[7:0] 写该寄存器写数据会使数据存入串口0的先入先出队列,当它到达先入先出队列的底部并 且发送器有效时字节将会被送出 4.UART0 Line Status Register (U0LSR:0xe000c014,read only)
串口状态寄存器
0 接收数据就绪,0:U0RBR为空,1:U0RBR包含有效数据
...
6 发送器空,0:U0THR/U0TSR包含有效数据,1:U0THR和U0TSR为空 /*Universal Asynchronous Reciver Transmitter 0(UART0)*/
/*通用异步串行口0,8位*/
#define U0RBR (*(volatile unsigned char*)0xe000c000)
#define U0THR (*(volatile unsigned char*)0xe000c000)
#define U0LCR (*(volatile unsigned char*)0xe000c00c)
#define U0DLL (*(volatile unsigned char *)0xe000c000)
#define U0DLM (*(volatile unsigned char *)0xe000c004)
#define U0LSR (*(volatile unsigned char *)0xe000c014) //UART0初始化:8位数据位,1位停止位,禁止奇偶校验,波特率为9600bps
void uart0Initilization(void){
U0LCR=0x83;
U0DLL=0x12;
U0DLM=0x00;
U0CLR=0x03;
}
//UART0发送一字节数据并等待发送完毕
void uart0SendByte(unsigned char data){
U0THR=data;
while((U0LSR & 0x40)==0){ //U0LSR中的第6位为0则说明还未发送完毕,延时一段时 间再查询直到发送完毕
delay(100);
}
}
//UART0发送一字符串
void uart0SendString(unsigned char *ptr){
while(1){
//发送回车换车
if(*ptr=='\0'){
uart0SendByte('\r');
uart0SendByte('\n');
break;
}
uart0SendByte(*ptr++);
}
} //UART0接收一字符
unsigned char uart0RecvByte(void){
while((U0LSR & 0x01)==0); //等待接收器有效
return U0RBR; //返回数据
} 别忘了让I/0连接UART0
由Datasheet可以看出PINSEL0的[1:0]位控制TxD(UART0)当为01时启用此功能,[3:2]位控制RxD, 也为01时启用,所以控制字写为...0101 ==>> 0x00000005(hex)即PINSEL0=0x00000005; //主程序main不断地由UART0送出[email protected]
int main(void){
unsigned char msg0[]="[email protected]";
unsigned char msg1[]="Work Hard to Learn ARM";
PINSEL0=0x00000005;
uart0Initilization();
while(1){
uart0SendString(msg0);
delay(100);
uart0SendString(msg1);
delay(100);
}
return 0;
} 500)this.width=500;" border=0>
相关阅读 更多 +