📄 putchar.c
字号:
/***********************************************************************/
/* This file is part of the C51 Compiler package */
/* Copyright KEIL ELEKTRONIK GmbH 1990 - 2002 */
/***********************************************************************/
/* */
/* PUTCHAR.C: This routine is the general character output of C51. */
/* You may add this file to a uVision2 project. */
/* */
/* To translate this file use C51 with the following invocation: */
/* C51 PUTCHAR.C <memory model> */
/* */
/* To link the modified PUTCHAR.OBJ file to your application use the */
/* following Lx51 invocation: */
/* Lx51 <your object file list>, PUTCHAR.OBJ <controls> */
/* */
/***********************************************************************/
#include <intrins.h>
#include <Mreg51.h>
#include "board.h"
#include "sysinfo.h"
#define __putcharUART0( c ) \
do { \
if ( ENABLE_UART0_INTERRUPT ) \
UD = _testbit_( ES0 ); \
TI0 = 0; \
S0BUF = c; \
while ( !TI0 ) ; \
TI0 = 0; \
if ( ENABLE_UART0_INTERRUPT && UD ) \
ES0 = 1; \
} while ( 0 )
#define __putcharUART1( c ) \
do { \
if ( ENABLE_UART1_INTERRUPT ) \
{ \
UD = (IEN2 & BIT0); \
IEN2 &= ~BIT0; \
} \
S1CON &= ~BIT1; \
S1BUF = c; \
while ( !(S1CON & BIT1) ) ; \
S1CON &= ~BIT1; \
if ( ENABLE_UART1_INTERRUPT && UD ) \
IEN2 |= BIT0; \
} while ( 0 )
char putchar( char c )
{
if ( ENABLE_UART0 && STDOUT_DEVICE == IO_DEV_UART0 )
{
__putcharUART0( c );
}
else if ( ENABLE_UART1 && STDOUT_DEVICE == IO_DEV_UART1 )
{
__putcharUART1( c );
}
return c;
}
#if ( ENABLE_UART0 && STDOUT_DEVICE != IO_DEV_UART0 )
char putcharUART0( char c )
{
__putcharUART0( c );
return c;
}
#endif
#if ( ENABLE_UART1 && STDOUT_DEVICE != IO_DEV_UART1 )
char putcharUART1( char c )
{
__putcharUART1( c );
return c;
}
#endif
#if 0 // removed later if not necessary
#if ( ENABLE_UART1 ) // Test Code, Remove later
void putcharUART1 (char c)
{
if ( ENABLE_UART1_INTERRUPT )
{
UD = (IEN2 & BIT0);
IEN2 &= ~BIT0; // disable uart interrupt
}
S1CON &= ~BIT1; //TI_1 = 0;
if( c == '\r\n' )
{
SBUF1 = '\r';
while( !(S1CON & BIT1) ); // !TI_1 );
S1CON &= ~BIT1; //TI_1 = 0;
}
S1BUF = c; // transfer to uart
// wait transfer completing
while( !(S1CON & BIT1));//TI_1 );
S1CON &= ~BIT1; //TI_1 = 0; // clear flag
if ( ENABLE_UART1_INTERRUPT )
{
if (UD)
IEN2 |= BIT0; // restore uart interrupt
}
}
void puthexUART1 (U8 x)
{
code char table[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char ch;
if ( ENABLE_UART1_INTERRUPT )
{
UD = (IEN2 & BIT0);
IEN2 &= ~BIT0; // disable uart interrupt
}
S1CON &= ~BIT1;
ch=(x>>4) & 0x0f;
S1BUF = table[ch]; // transfer to uart
while( !(S1CON & BIT1));
S1CON &= ~BIT1;
ch=x & 0x0f;
S1BUF = table[ch]; // transfer to uart
while( !(S1CON & BIT1));
S1CON &= ~BIT1;
S1BUF =' '; // transfer to uart
while( !(S1CON & BIT1));
S1CON &= ~BIT1;
if ( ENABLE_UART1_INTERRUPT )
{
if (UD)
IEN2 |= BIT0; // restore uart interrupt
}
}
void printfUART1(U8 code *pFmt, U16 wVal)
{
U8 ucBff, ucDisp;
BOOLEAN bNotZero=FALSE, bHex=FALSE;
U16 wDivider=10000;
// if(g_bUartToolSelect == UART_TOOL_TVLINK) return;
// CLEAR_WATCHDOG;
// msAPI_Timer_ResetWDT();
//Add newline char
putcharUART1('\r');
putcharUART1('\r\n');
while( ucBff = *(pFmt++) )
{
if(ucBff == '%') // check special case
{
switch( *(pFmt++) ) // check next character
{
case 'x': // hexadecimal number
case 'X':
wDivider = 0x1000;
bHex = TRUE;
case 'd': // decimal number
case 'i':
if(wVal)
{
while(wDivider)
{
ucDisp = wVal/wDivider;
wVal = wVal%wDivider;
if(ucDisp)
bNotZero=TRUE;
if(bNotZero)
{
if (ucDisp>9)
putcharUART1(ucDisp-0x0a+'A');
else
putcharUART1(ucDisp+'0');
}
if(bHex)
wDivider /= 0x10;
else
wDivider /= 10;
}
}
else
putcharUART1('0');
break;
} // switch
}
else // general
putcharUART1(ucBff); // put a character
} // while
}
#endif
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -