📄 uart.c
字号:
UARTTXH1 = ch;
}
else {
WaitXmitter(UARTSTAT0);
UARTTXH0 = ch;
}
}
char get_char(uint32 channel)
{
char ch;
if(channel) {
WaitRcver(UARTSTAT1);
ch = UARTRXB1;
}
else { WaitRcver(UARTSTAT0);
ch = UARTRXB0;
}
return ch;
}
void put_string(char *ptr )
{
while(*ptr )
{
put_byte(*ptr++ );
}
}
void dbg_out(char *fmt, ...)
{
va_list argptr;
char temp_buf[256];
va_start(argptr, fmt);
vsprintf(temp_buf, fmt, argptr);
put_string(temp_buf);
va_end(argptr);
}
/************************************************************************/
/* */
/* FUNCTION */
/* */
/* gethex2dec get hex-string from hexdecimal digit */
/* */
/* DESCRIPTION */
/* */
/* s = gethex2dec(3fff); */
/* return value s = "3fff" */
/* */
/* AUTHOR */
/* */
/* */
/* CALLED FROM */
/* */
/* Application routines */
/* */
/* ROUTINES CALLED */
/* */
/* */
/* INPUTS */
/* */
/* */
/* OUTPUTS */
/* */
/* return(c) return character */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* in4maker 10-16-1998 Created initial version 1.0 */
/************************************************************************/
uint32 gethex2dec(uint32 digitcnt)
{
uint32 decimal = 0;
uint8 hex;
uint32 i;
put_string("0x");
hex = get_byte();
for(i=0;(i<digitcnt)&&(!is_control(hex));i++) {
hex = to_upper(hex);
if(is_xdigit(hex)) {
if(is_digit(hex)) /* '0' - '9' */
decimal = decimal * 16 + hex - '0';
else /* 'A' - 'F' */
decimal = decimal * 16 + 10 + hex - 'A';
}
else {
Print("\r[ERROR] Re-enter %d hexdigit\r",digitcnt);
i = 0; // clear character counter
decimal = 0;
put_string("0x");
}
if(i!=digitcnt-1)
hex = get_byte(); //get next character
}
put_byte('\r');
return(decimal);
}
/************************************************************************/
/* */
/* FUNCTION */
/* */
/* get_num get decimal number from hexa digit */
/* */
/* DESCRIPTION */
/* */
/* */
/* AUTHOR */
/* */
/* */
/* CALLED FROM */
/* */
/* Application routines */
/* */
/* ROUTINES CALLED */
/* */
/* */
/* INPUTS */
/* */
/* */
/* OUTPUTS */
/* */
/* return(c) return character */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* hbahn 10-16-1998 Created initial version 1.0 */
/************************************************************************/
uint32 get_num(void)
{
char RcvData[10] ;
unsigned RcvNum=0 ;
int i = 0 ;
while ( (RcvData[i] = get_byte() ) != CR )
{
if ( (RcvData[i] >= '0') && (RcvData[i] <= '9') )
RcvNum=(RcvNum<<4) + (RcvData[i]-'0') ;
else if ( (RcvData[i] >= 'A') && (RcvData[i] <= 'F') )
RcvNum=(RcvNum<<4) + (RcvData[i]-'A'+ 10) ;
else if ( (RcvData[i] >= 'a') && (RcvData[i] <= 'f') )
RcvNum=(RcvNum<<4) + (RcvData[i]-'a'+ 10) ;
i++ ;
}
return RcvNum ;
}
/************************************************************************/
/* */
/* FUNCTION */
/* */
/* get_digit get decimal number from ASCII data. */
/* */
/* DESCRIPTION */
/* */
/* ex) If input character is "123",get_digit()'s return value */
/* will be 123. */
/* */
/* AUTHOR */
/* */
/* */
/* CALLED FROM */
/* */
/* Application routines */
/* */
/* ROUTINES CALLED */
/* */
/* */
/* INPUTS */
/* */
/* */
/* OUTPUTS */
/* */
/* return(c) return character */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* in4maker 10-16-1998 Created initial version 1.0 */
/************************************************************************/
uint32 get_digit(void)
{
uint8 RcvData[10] ;
int RcvNum=0 ;
int RcvDataSize=0 ;
int i ;
while ((RcvData[RcvDataSize] = get_byte()) != CR) RcvDataSize++ ;
for (i=0;i < RcvDataSize ; i++)
RcvNum = RcvNum*10 + (RcvData[i]-'0') ;
return RcvNum ;
}
/************************************************************************/
/* */
/* FUNCTION */
/* */
/* kbd_hit */
/* */
/* DESCRIPTION */
/* */
/* */
/* AUTHOR */
/* */
/* */
/* CALLED FROM */
/* */
/* Application routines */
/* */
/* ROUTINES CALLED */
/* */
/* */
/* INPUTS */
/* */
/* */
/* OUTPUTS */
/* */
/* return(c) return character */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* hbahn 10-16-1998 Created initial version 1.0 */
/************************************************************************/
uint32 kbd_hit(void) //不阻塞
{
uint32 KbdHit ;
int8 ch ;
if (UARTSTAT0 & USTAT_RCV_READY)
{
KbdHit = 1 ;
ch = UARTRXB0;
}
else
KbdHit = 0 ;
return KbdHit ;
}
/************************************************************************/
/* */
/* FUNCTION "BaudRateVal" */
/* */
/* */
/* DESCRIPTION */
/* */
/* This function return the value of UART baudrate divisior, */
/* UBRDIVn according to the baudrate. */
/* */
/* AUTHOR */
/* */
/* */
/* CALLED FROM */
/* */
/* UART_Initialize */
/* */
/* ROUTINES CALLED */
/* */
/* */
/* INPUTS */
/* */
/* baud UART baud rate(ex. 38400,57600,..) */
/* */
/* OUTPUTS */
/* */
/* baudrate divisor value */
/* */
/* HISTORY */
/* */
/* NAME DATE REMARKS */
/* */
/* in4maker 10-16-1998 Created initial version 1.0 */
/************************************************************************/
uint32 BaudRateVal(uint32 baud)
{
uint32 index;
for(index = 0; index < BAUD_TABLE; index++)
{
if(U_BaudRate[index].baud == baud) return(index);
}
return(0); /* baudrate data doesn't in table */
}
uint32 BaudRate(uint32 div)
{
uint32 index;
for(index = 0; index < BAUD_TABLE; index++)
{
if(U_BaudRate[index].div == div) return(index);
}
return(0); /* baudrate data doesn't in table */
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -