📄 44blib.c
字号:
/************************************************
* NAME : 44BLIB.C *
* File Descriptions : C Library functions *
************************************************/
#include "..\inc\44b.h"
#include "..\inc\44blib.h"
#include "..\inc\option.h"
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
/****************************************************************************
【功能说明】IO端口功能、方向设定
****************************************************************************/
void Port_Init(void)
{
// PORT A GROUP
/* BIT 9 8 7 6 5 4 3 2 1 0 */
/* A24 A23 A22 A21 A20 A19 A18 A17 A16 A0 */
rPCONA = 0x3ff;
// PORT B GROUP
/* BIT 10 9 8 7 6 5 4 3 2 1 0 */
/* CS5 CS4 CS3 CS2 CS1 nWBE3 nWBE2 SRAS SCAS SCLK SCKE */
rPCONB = 0x7ff;
//PORT C GROUP
/* BIT15 14 13 12 11 10 9 8 */
/* nCTS0 nRTS0 RXD1 TXD1 nCTS1 nRTS1 nEL_ON nDISP_ON */
/* BIT7 6 5 4 3 2 1 0 */
/* VD4 VD5 VD6 VD7 LED2 LED1 LED0 D12SUSPD*/
rPDATC = 0x0000; //All IO is low
rPCONC = 0xffffff55;
rPUPC = 0x0000; //disable all pull-up
//PORT D GROUP
/* PORT D GROUP(I/O OR LCD) */
/* BIT7 6 5 4 3 2 1 0 */
/* VFRAME VM VLINE VCLK VD3 VD2 VD1 VD0 */
rPCOND= 0xaaaa;
rPDATD= 0x55;
rPUPD = 0x00; //disable all pull-up
//PORT E GROUP
/* Bit 8 7 6 5 4 3 2 1 0 */
/* ENDLAN Output Output Output Output Output RXD0 TXD0 Output */
rPCONE = 0x25569;
rPUPE = 0x000; //disable all pull-up
//PORT F GROUP
/* Bit8 7 6 5 4 3 2 1 0 */
/* IISCLK Output IISDO IISLRCK nXDREQ0 nXDACK0 nWAIT IICSDA IICSCL */
rPCONF = 0x2193ec;
rPDATF = 0x3;
rPUPF = 0x000; //disable all pull-up
//PORT G GROUP
/* BIT7 6 5 4 3 2 1 0 */
/* KEYIN3 KEYIN2 KEYIN1 KEYIN0 INT3 INT2 ETH_INT D12_INT */
rPCONG = 0xffff; //
rPUPG = 0x00; //disable all pull-up
rSPUCR=0x7; //D15-D0 pull-up disable
/*所有的外部硬件中断为低电平触发*/
rEXTINT=0x0;
}
static int whichUart = 0;
/****************************************************************************
【功能说明】选择串口(0或1)
****************************************************************************/
void Uart_Select(int ch)
{
whichUart=ch;
}
/****************************************************************************
【功能说明】串口初始化
****************************************************************************/
void Uart_Init(int baud)
{
rUFCON0=0x0; //FIFO disable
rUFCON1=0x0;
rUMCON0=0x0;
rUMCON1=0x0;
//UART0
rULCON0=0x3; //Normal,No parity,1 stop,8 bit
rUCON0=0x245;
rUBRDIV0=((int)(60000000/16./baud + 0.5) -1 );
//UART1
rULCON1=0x3;
rUCON1=0x245;
rUBRDIV1=((int)(60000000/16./baud + 0.5) -1 );
}
/****************************************************************************
【功能说明】等待直到串口发送缓冲区内部的数据发送完毕
****************************************************************************/
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();
}
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));
WrUTXH0('\r');
}
while(!(rUTRSTAT0 & 0x2)); //Wait until THR is empty.
WrUTXH0(data);
}
else
{
if(data=='\n')
{
while(!(rUTRSTAT1 & 0x2));
rUTXH1='\r';
}
while(!(rUTRSTAT1 & 0x2)); //Wait until THR is empty.
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.
#define MAX_TBUF 10000
static char tbuf[MAX_TBUF]= {0, };
void Uart_Printf(char *fmt,...)
{
va_list v_list;
char *ptr;
int i= 0;
va_start(v_list, fmt); // Initialize variable arguments.
vsprintf(tbuf, fmt, v_list );
va_end(v_list);
ptr= tbuf;
while( (*ptr) && i<MAX_TBUF) {
Uart_SendByte(*ptr);
ptr++; i++;
}//while
}
/****************************************************************************
【功能说明】
****************************************************************************/
void MemCfgInit(void)
{
rNCACHBE0 = ((unsigned int)(Non_Cache_End>>12)<<16)|(Non_Cache_Start>>12);
}
/****************************************************************************
【功能说明】
****************************************************************************/
void CacheDisable(void)
{
rSYSCFG = SYSCFG_0KB;
}
/****************************************************************************
【功能说明】
****************************************************************************/
void CacheEnable(void)
{
rSYSCFG = SYSCFG_8KB;
}
/****************************************************************************
【功能说明】
****************************************************************************/
void Cache_Flush(void)
{
int i,saveSyscfg;
saveSyscfg=rSYSCFG;
rSYSCFG=SYSCFG_0KB;
for(i=0x10004000;i<0x10004800;i+=16)
*((int *)i)=0x0;
rSYSCFG=saveSyscfg;
}
/****************************************************************************
【功能说明】CRC16校验
****************************************************************************/
unsigned short CRC16(char *s, int len)
{
unsigned short acc=0;
int i;
while(len--)
{
acc = acc^(*s++<<8);
for(i=0; i<8; i++)
{
if(acc&0x8000)
acc = (acc<<1)^0x1021;
else
acc = acc<<1;
}
}
return acc;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -