📄 uart.c
字号:
/**********************************************************************
UART.c file
LPC213X的串口函数
本程序只供学习使用,不得用于其它任何用途,否则后果自负。
ARM_OS_main.c file
LPC213x上的操作系统——ARM_OS
作者:heciang
联系电话:13811129591
QQ:176780142
Email:heciang@126.com
建立日期:2006-5-1
修改日期:2006-5-15
最后修改时间:2006-08-25
版本:V1.0
Copyright(C) Computer-lov 2006-2016
All rights reserved
**********************************************************************************************/
**********************************************************************/
#include "CPU.H"
#include "My_type.h"
#include "UART.H"
//////////////////////////////////////////////////////////////////////
void UART_init(void)
{
PINSEL0 = 0x00000005; //设置串口0的相应引脚
U0IER=0; //禁止串口0所有中断
U0LCR = 0x83; //8个数据位,无奇偶校验,1个停止位,DLAB=1
#define Fpclk (5*11059200)/*Pclk为5*11059200Hz*/
#define BitRate 57600 /*波特率设置为57600bps*/
U0DLM=(Fpclk/16/BitRate)/256;
U0DLL=(Fpclk/16/BitRate)%256;
U0LCR = 0x03; // DLAB = 0
}
//////////////////////////////////////////////////////////////////////
//////////////////////////////往串口0发送一字节数据//////////////////////////////
void send_a_byte(uint8 a_byte)
{
while (!(U0LSR & 0x20)); //等待发送完毕
U0THR=a_byte;
}
//////////////////////////////////////////////////////////////////////
/////////////////////////从串口0读取一字节数据//////////////////////////////////
/*int get_a_byte(void) //
{
while (!(U0LSR & 0x01));
return (U0RBR);
}*/
//////////////////////////////////////////////////////////////////////
////////////////////////////// 发送一个字符串 ///////////////////////////////////////////
//////////////////入口参数: s-要发送的字符串的指针(字符串长度不得大于255)/////////////
///////////////////////////////////// newline-是否需要换行 0-不换行 非0-换行////////
void prints(uint8 * s,uint32 newline)
{
unsigned char i;
i=0;
while(s[i]!=0) //判断字符串是否结束
{
while (!(U0LSR & 0x20)); //等待发送完毕
U0THR=s[i];
i++; //移到下字节
}
if(newline) //如果需要换行
{
send_a_byte(13);
send_a_byte(10); //发送回车换行
}
}
////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////
void cls(void)
{
send_a_byte(0x0C); //超级终端清屏指令
send_a_byte(0x0C);
}
//////////////////////////////////////////////////////////////////////////////////
//转换成十六进制时用的表
const uint8 HEX_TABLE[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
//////////////////////////将一个16位的整数按十六进制发到串口/////////////////////////////////
void print_uint16(uint16 number)
{
send_a_byte('0'); //发送0x
send_a_byte('x');
send_a_byte(HEX_TABLE[(number>>12) & 0x0F]); //依次发送各部分
send_a_byte(HEX_TABLE[(number>>8) & 0x0F]);
send_a_byte(HEX_TABLE[(number>>4) & 0x0F]);
send_a_byte(HEX_TABLE[(number) & 0x0F]);
send_a_byte(' ');
}
//////////////////////////////////////////////////////////////////////////////////
/////////////////////////将一个32位的整数按十进制发送到串口/////////////////////////////
void print_uint32(uint32 number)
{
uint8 buf[12];
uint32 i;
for(i=0;i<10;i++) //转换成字符串
{
buf[9-i]=(number%10)+'0';
number/=10;
}
for(i=0;i<9;i++) //将前面的0转换成空格
{
if(buf[i]=='0')
{
buf[i]=' ';
}
else
{
break;
}
}
buf[10]=' ';
buf[11]=0; //字符串结束符
prints(buf,0);
}
//////////////////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -