?? uart.c
字號:
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "R44b0.h"
#include "lib.h"
#include "uart.h"
static int UartNum=0;
//#define WrUTXH0(ch) (*(volatile unsigned char *)0x1d00020)=(unsigned char)(ch)
//#define WrUTXH1(ch) (*(volatile unsigned char *)0x1d04020)=(unsigned char)(ch)
//#define RdURXH01() (*(volatile unsigned char *)(0x1d00027))
//#define RdURXH1() (*(volatile unsigned char *)(0x1d04024))
void Uart_Init(int whichuart,int baud)
{
if(whichuart==0)
{
UartNum=0;
rUFCON0=0x0;//不使用FIFO
rUMCON0=0x0;
rULCON0=0x3;
rUCON0=0x245;
rUBRDIV0=((int)(MCLK/16./baud+0.5)-1);
}
else if(whichuart==1)
{
UartNum=0;
rUFCON1=0x0;//不使用FIFO
rUMCON1=0x0;
rULCON1=0x3;
rUCON1=0x245;
rUBRDIV1=((int)(MCLK/16./baud+0.5)-1);
}
}
void SendByte(char ch)
{
if(UartNum==0)
{
if(ch=='\n')
{
while(!(rUTRSTAT0 & 0x2));
Delay(10);
WrUTXH0('\r');
}
while(!(rUTRSTAT0 & 0x2));
Delay(10);
WrUTXH0(ch);
}
else
{
if(ch=='\n')
{
while(!(rUTRSTAT1 & 0x2));
Delay(10);
WrUTXH1('\r');
}
while(!(rUTRSTAT1 & 0x2));
Delay(10);
WrUTXH1(ch);
}
}
void Uart_SendString(char *pt)
{
while(*pt)
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);
}
char Uart_Getch(void)
{
if(UartNum==0)
{
while(!(rUTRSTAT0 & 0x1));
return RdURXH0();
}
else
{
while(!(rUTRSTAT1 & 0x1));
return RdURXH1();
}
}
char Uart_GetKey(void)
{
if(UartNum==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;
SendByte(c);
}
}
*string='\0';
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;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -