📄 2410lib.c
字号:
*********************************************************
*/
char Uart_GetKey(void)
{
if(whichUart==0)
{
if(rUTRSTAT0 & 0x1) //Receive data ready
return RdURXH0();
else
return 0;
}
else if(whichUart==1)
{
if(rUTRSTAT1 & 0x1) //Receive data ready
return RdURXH1();
else
return 0;
}
else if(whichUart==2)
{
if(rUTRSTAT2 & 0x1) //Receive data ready
return RdURXH2();
else
return 0;
}
}
/*
*********************************************************
* 函数介绍:本函数读取字符串数据。
* 输入参数:string---存储字符串的BUFF的首地址。
* 输出参数:无
* 返回值 :无
*********************************************************
*/
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 result = 0;
int lastIndex;
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(lastIndex<0)
return -1;
if(string[lastIndex]=='h' || string[lastIndex]=='H' )//十六进制
{
base = 16;
string[lastIndex] = 0;
lastIndex--;
}
if(base==10)//十进制数值
{
result = atoi_new(string);//将字符串转成数值
result = minus ? (-1*result):result;
}
else
{
for(i=0;i<=lastIndex;i++)//十六进制
{
//if(isalpha(string[i]))//在ADS1.2下编译时使用下面函数实现,因为库函数isalpha()不支持
if(isalpha_new(string[i]))//判断是否为a-z 或 A-z的字母
{
// if(isupper(string[i]))
if(isupper_new(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;
}
/*
*********************************************************
* 函数介绍:本函数发送串口数据。
* 输入参数:data---待发送的数据
* 输出参数:无
* 返回值 :无。
*********************************************************
*/
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(10);
WrUTXH0(data);
}
else if(whichUart==1)
{
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(10);
rUTXH1 = data;
}
else if(whichUart==2)
{
if(data=='\n')
{
while(!(rUTRSTAT2 & 0x2));
Delay(10); //because the slow response of hyper_terminal
rUTXH2 = '\r';
}
while(!(rUTRSTAT2 & 0x2)); //Wait until THR is empty.
Delay(10);
rUTXH2 = data;
}
}
/*
*********************************************************
* 函数介绍:本函数发送字符串。
* 输入参数:pt---待发送的数据指针
* 输出参数:无
* 返回值 :无。
*********************************************************
*/
void Uart_SendString(char *pt)
{
while(*pt)
Uart_SendByte(*pt++);
}
/*
*********************************************************
* 函数介绍:本函数以printf格式发送数据。
* 输入参数:fmt---待发送的数据指针
* 输出参数:无
* 返回值 :无。
*********************************************************
*/
//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);
}
/*
*********************************************************
* 函数介绍:本函数改变PLL倍频。
* 输入参数:
* 输出参数:无
* 返回值 :无。
*********************************************************
*/
void ChangeMPllValue(int mdiv,int pdiv,int sdiv)
{
rMPLLCON = (mdiv<<12) | (pdiv<<4) | sdiv;
}
/*
*********************************************************
* 函数介绍:本函数改变时钟(HCLK, PCLK)分屏。
* 输入参数:
* 输出参数:无
* 返回值 :无。
*********************************************************
*/
void ChangeClockDivider(int hdivn,int pdivn)
{
// hdivn,pdivn FCLK:HCLK:PCLK
// 0,0 1:1:1
// 0,1 1:1:2
// 1,0 1:2:2
// 1,1 1:2:4
rCLKDIVN = (hdivn<<1) | pdivn;
/* if(hdivn)
MMU_SetAsyncBusMode();
else
MMU_SetFastBusMode();
*/
}
/*
*********************************************************
* 函数介绍:本函数改变UPLL倍频。
* 输入参数:
* 输出参数:无
* 返回值 :无。
*********************************************************
*/
void ChangeUPllValue(int mdiv,int pdiv,int sdiv)
{
rUPLLCON = (mdiv<<12) | (pdiv<<4) | sdiv;
}
/*
*********************************************************
* 函数介绍:本函数分配一个内存区域。
* 输入参数:nbyte---字节数
* 输出参数:无
* 返回值 :返回内存区域首地址。
*********************************************************
*/
void * malloc(unsigned nbyte)
{
void *returnPt = mallocPt;
mallocPt = (int *)mallocPt+nbyte/4+((nbyte%4)>0); //To align 4byte
if( (int)mallocPt > HEAPEND )
{
mallocPt = returnPt;
return NULL;
}
return returnPt;
}
/*
*********************************************************
* 函数介绍:本函数释放一个由malloc分配的内存区域。
* 输入参数:pt---内存区域的首地址
* 输出参数:无
* 返回值 :无。
*********************************************************
*/
void free(void *pt)
{
mallocPt = pt;
}
/*
*********************************************************
* 函数介绍:本函数释中断初始化程序。
* 输入参数:无
* 输出参数:无
* 返回值 :无。
*********************************************************
*/
void Isr_Init(void)
{
pISR_UNDEF = (unsigned)HaltUndef;
pISR_SWI = (unsigned)HaltSwi;
pISR_PABORT = (unsigned)HaltPabort;
pISR_DABORT = (unsigned)HaltDabort;
rINTMOD = 0x0; //All=IRQ mode
// rINTCON=0x5; //Non-vectored,IRQ enable,FIQ disable
rINTMSK = BIT_ALLMSK; //All interrupt is masked.
rINTSUBMSK = BIT_SUB_ALLMSK; //All sub-interrupt is masked. <- April 01, 2002 SOP
}
/*
*********************************************************
* 函数介绍:本函数描述出现Undefined中断时调用的程序。
* 输入参数:无
* 输出参数:无
* 返回值 :无。
*********************************************************
*/
void HaltUndef(void)
{
Uart_Printf("Undefined instruction exception.\n");
while(1);
}
/*
*********************************************************
* 函数介绍:本函数描述出现SWI中断时调用的程序。
* 输入参数:无
* 输出参数:无
* 返回值 :无。
*********************************************************
*/
void HaltSwi(void)
{
Uart_Printf("SWI exception.\n");
while(1);
}
/*
*********************************************************
* 函数介绍:本函数描述出现Pabort中断时调用的程序。
* 输入参数:无
* 输出参数:无
* 返回值 :无。
*********************************************************
*/
void HaltPabort(void)
{
Uart_Printf("Pabort exception.\n");
while(1);
}
/*
*********************************************************
* 函数介绍:本函数描述出现Dabort中断时调用的程序。
* 输入参数:无
* 输出参数:无
* 返回值 :无。
*********************************************************
*/
void HaltDabort(void)
{
Uart_Printf("Dabort exception.\n");
while(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -