📄 uart_lib.c
字号:
#include "stdio.h"
#include <string.h>
#include "stdlib.h"
#include "stdarg.h"
#include <ctype.h>
#include "44b.h"
#include "44blib.h"
#include "def.h"
#include "option.h"
#include "uart_lib.h"
int screenX,screenY;
// 12345678901234567890123456789012345678901234567890123456789012345678901234567890
char Space_Str[80]=" ";
static int whichUart=0;
/****************************************************************************
【功能说明】选择串口通道为COM0或者COM1
****************************************************************************/
void Uart_Select(int ch)
{
whichUart=ch;
}
/****************************************************************************
【功能说明】异步串行口初始化
****************************************************************************/
void Uart_Init(int mclk,int baud)
{
int i;
if(mclk==0)
mclk=MCLK;
rUFCON0=0x0; //FIFO disable
rUFCON1=0x0;
rUMCON0=0x0;
rUMCON1=0x0;
//UART0
rULCON0=0x3; //Normal,No parity,1 stop,8 bit
// rULCON0=0x7; //Normal,No parity,2 stop,8 bit
rUCON0=0x245; //rx=edge,tx=level,disable timeout int.,enable rx error int.,normal,interrupt or polling
rUBRDIV0=( (int)(mclk/16./baud + 0.5) -1 );
//UART1
// rULCON1=0x7; //Normal,No parity,2 stop,8 bit
rULCON1=0x3;
rUCON1=0x245;
rUBRDIV1=( (int)(mclk/16./baud + 0.5) -1 );
for(i=0;i<100;i++);
Uart_Select(0);
}
//***************************************************************************
/****************************************************************************
【功能说明】等待直到串口发送缓冲区内部的数据发送完毕
****************************************************************************/
void Uart_TxEmpty(int ch)
{
if(ch==0)
while(!(rUTRSTAT0 & 0x4)); //wait until tx shifter is empty.
else
while(!(rUTRSTAT1 & 0x4)); //wait until tx shifter is empty.
}
//***************************************************************************
/****************************************************************************
【功能说明】从串口接收一个字符
****************************************************************************/
char Uart_Getch(void)
{
if(whichUart==0)
{
while(!(rUTRSTAT0 & 0x1)); //Receive data read
return RdURXH0();
//return rURXH0;
}
else
{
while(!(rUTRSTAT1 & 0x1)); //Receive data ready
return rURXH1;
}
}
//***************************************************************************
/****************************************************************************
【功能说明】从串口接收一个字符,如果没有收到数据返回0
****************************************************************************/
char Uart_GetKey(void)
{
if(whichUart==0)
{
if(rUTRSTAT0 & 0x1) //Receive data ready
return RdURXH0();
else
return 0;
}
else
{
if(rUTRSTAT1 & 0x1) //Receive data ready
return rURXH1;
else
return 0;
}
}
//***************************************************************************
/****************************************************************************
【功能说明】从串口获取一个字符串
****************************************************************************/
void Uart_GetString(char *string)
{
char *string2=string;
char c;
while((c=Uart_Getch())!='\r') //输入字符不等于回车
{
if(c=='\b') //输入字符等于退格
{
if( (int)string2 < (int)string )
{
Uart_Printf("\b \b");
string--;
}
}
else
{
*string++=c;
Uart_SendByte(c);
}
}
*string='\0'; //内容为空
Uart_SendByte('\n'); //换行
}
//***************************************************************************
/****************************************************************************
【功能说明】从串口获取一个整型数
****************************************************************************/
int Uart_GetIntNum(void)
{
char str[30];
char *string=str;
int base=10;
int minus=0;
int lastIndex;
int result=0;
int i;
Uart_GetString(string);
if(string[0]=='-')
{
minus=1;
string++;
}
if(string[0]=='0' && (string[1]=='x' || string[1]=='X'))
{
base=16;
string+=2;
}
lastIndex=strlen(string)-1;
if( string[lastIndex]=='h' || string[lastIndex]=='H' )
{
base=16;
string[lastIndex]=0;
lastIndex--;
}
if(base==10)
{
result=atoi(string);
result=minus ? (-1*result):result;
}
else
{
for(i=0;i<=lastIndex;i++)
{
if(isalpha(string[i]))
{
if(isupper(string[i]))
result=(result<<4)+string[i]-'A'+10;
else
result=(result<<4)+string[i]-'a'+10;
}
else
{
result=(result<<4)+string[i]-'0';
}
}
result=minus ? (-1*result):result;
}
return result;
}
//***************************************************************************
/****************************************************************************
【功能说明】向串口发送一个字节的整型数
****************************************************************************/
void Uart_SendByte(int data)
{
if(whichUart==0)
{
if(data=='\n')
{
while(!(rUTRSTAT0 & 0x2));
//Delay(10); //because the slow response of hyper_terminal
WrUTXH0('\r');
}
while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty.
//Delay(5);
WrUTXH0(data);
}
else
{
if(data=='\n')
{
while(!(rUTRSTAT1 & 0x2));
//Delay(10); //because the slow response of hyper_terminal
rUTXH1='\r';
}
while(!(rUTRSTAT1 & 0x2)); //Wait until THR is empty.
//Delay(5);
rUTXH1=data;
}
}
//***************************************************************************
/****************************************************************************
【功能说明】向串口送出一串字符
****************************************************************************/
void Uart_SendString(char *pt)
{
while(*pt)
Uart_SendByte(*pt++);
}
//***************************************************************************
/****************************************************************************
【功能说明】以标准输出格式向串口输出各种信息
****************************************************************************/
//if you don't use vsprintf(), the code size is reduced very much.
void Uart_Printf(char *fmt,...)
{
va_list ap;
char string[256];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
Uart_SendString(string);
va_end(ap);
}
//***************************************************************************
void SerialInit(int port,int mclk,int baud)
{
if(mclk <= 0)
mclk = MCLK;
if(port == 0)
{
rUFCON0 = 0x0;
//rUFCON0 = 0x1;
rUMCON0 = 0x0;
rULCON0 = 0x3;
rUCON0 = 0x245;
rUBRDIV0 = ( (int)(mclk/(16*baud) + 0.5) -1 );
}
else if(port == 1)
{
rUFCON1 = 0x0;
rUMCON1 = 0x0;
rULCON1 = 0x3;
rUCON1 = 0x245;
rUBRDIV1 = ( (int)(mclk/(16*baud) + 0.5) -1 );
}
Uart_Select(port);
return;
}
char SerialGetChar(int port)
{
if(port == 0)
{
while(!(rUTRSTAT0 & 0x1)); //Receive data read
return rURXH0;
}
else
{
while(!(rUTRSTAT1 & 0x1)); //Receive data ready
return rURXH1;
}
}
char SerialGetKey(int port)
{
if(port==0)
{
if(rUTRSTAT0 & 0x1) //Receive data ready
return rURXH0;
else
return 0;
}
else
{
if(rUTRSTAT1 & 0x1) //Receive data ready
return rURXH1;
else
return 0;
}
}
void SerialSendChar(int port,char ch)
{
if(port == 0)
{
if(ch == '\n')
{ screenY++;
if(screenY>24) screenY=24;
screenX = 1;
while(!(rUTRSTAT0 & 0x2));
rUTXH0 = '\r';
}
else
screenX++;
while(!(rUTRSTAT0 & 0x2));
rUTXH0 = ch;
}
else if(port == 1)
{
if(ch == '\n')
{
while(!(rUTRSTAT1 & 0x2));
rUTXH1 = '\r';
}
while(!(rUTRSTAT1 & 0x2));
rUTXH1 = ch;
}
}
void SerialSendString(int port,char *str)
{
while(*str != '\0')
{
SerialSendChar(port,*str);
str++;
}
}
void SerialPrintf(int port, char *fmt,...)
{
va_list ap;
char string[512];
va_start(ap,fmt);
vsprintf(string,fmt,ap);
SerialSendString(port,(char *)string);
va_end(ap);
}
void SerialGetString(int port,char *string)
{
char *string2=string;
char c;
while((c=SerialGetChar(port))!='\r')
{
if(c=='\b')
{
if( (int)string2 < (int)string )
{
SerialPrintf(port,"\b \b");
string--;
}
}
else
{
*string++=c;
SerialSendChar(port,c);
}
}
*string='\0';
SerialSendChar(port,'\n');
}
//*****************************************************************************
int SerialGetIntNum_GJ(int port)
{
char str[8] ;
char *string=str;
int i = 0 ;
int data = 0 ;
SerialGetString(port,string);
while( str[i] != '\0' )
{
data = data * 10 ;
if( str[i]<'0'||str[i]>'9' )
return -1 ;
data = data + ( str[i]-'0' ) ;
i++ ;
}
return data ;
}
//*****************************************************************************
int SerialGetIntNum(int port)
{
char str[30];
char *string=str;
int base=10;
int minus=0;
int lastIndex;
int result=0;
int i;
SerialGetString(port,string);
if(string[0]=='-')
{
minus=1;
string++;
}
if(string[0]=='0' && (string[1]=='x' || string[1]=='X'))
{
base=16;
string+=2;
}
lastIndex=strlen(string)-1;
if( string[lastIndex]=='h' || string[lastIndex]=='H' )
{
base=16;
string[lastIndex]=0;
lastIndex--;
}
if(base==10)
{
result=atoi(string);
result=minus ? (-1*result):result;
}
else
{
for(i=0;i<=lastIndex;i++)
{
if(isalpha(string[i]))
{
if(isupper(string[i]))
result=(result<<4)+string[i]-'A'+10;
else
result=(result<<4)+string[i]-'a'+10;
}
else
{
result=(result<<4)+string[i]-'0';
}
}
result=minus ? (-1*result):result;
}
return result;
}
char Wait_Key_press(void)
{ char c;
c = '\0';
while ( c != '\r' )
{ c = SerialGetKey(0);
}
return(c);
}
void SendCharFast(char ch)
{
while(!(rUTRSTAT0 & 0x2));
rUTXH0 = ch;
}
void TestSerialPort(int port)
{
char ch;
SerialInit(port,MCLK,115200);
SerialPrintf(port,"This is serial port %d output\n",port);
while(ch!='q'&& ch!='Q')
{
SerialPrintf(port,"\nPlease enter a key\n");
ch = SerialGetChar(port);
SerialPrintf(port,"\nInput by port %d is %c\n",port,ch);
}
Uart_Select(0);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -