📄 rprint.c
字号:
/********************************************************************
* 版本说明:1.0 *
* 作者: andylee *
* 日期: 2006年10月 *
* 平台: mega16 16M *
* 说明: 通用的显示驱动功能函数库 *
********************************************************************/
#include "rprint.h"
#define RPRINTF_FLOAT
//-----------------------------------------------------------------------------
#ifdef RPRINTF_COMPLEX
static unsigned char buf[128];
#endif
//-----------------------------------------------------------------------------
#define pgm_read_byte(char_ptr) *(const char *) char_ptr
#define READMEMBYTE(a,char_ptr) ((a)?(pgm_read_byte(char_ptr)):(*char_ptr))
//-----------------------------------------------------------------------------
static char HexChars[] = "0123456789ABCDEF";
//const static char HexChars[]="123456789ABCDEF";
// function pointer to single character output routine
static void (*rputchar)(unsigned char c);
//-----------------------------------------------------------------------------
//******************************************************************************
// *** rprintf initialization ***
//*****************************************************************************
//=============================================================================
void rprintfInit(void (*putchar_func)(unsigned char c))
//=============================================================================
{
rputchar = putchar_func;
}
//==============================================================================
void rprintfChar(unsigned char c)
//==============================================================================
{
// send character
rputchar(c);
}
//=============================================================================
void rprintfStr(char str[])
//=============================================================================
{
// send a string stored in RAM
// check to make sure we have a good pointer
if (!str) return;
// print the string until a null-terminator
while (*str)
rprintfChar(*str++);
}
//=============================================================================
void rprintfStrLen(char str[], unsigned int start, unsigned int len)
//=============================================================================
{
register int i=0;
// check to make sure we have a good pointer
if (!str) return;
// spin through characters up to requested start
// keep going as long as there's no null
while((i++<start) && (*str++));
// then print exactly len characters
for(i=0; i<len; i++)
{
// print data out of the string as long as we haven't reached a null yet
// at the null, start printing spaces
if(*str)
rprintfChar(*str++);
else
rprintfChar(' ');
}
}
//*****************************************************************************
// *** rprintfProgStr ***
// prints a null-terminated string stored in program ROM
//*****************************************************************************
//=============================================================================
void rprintfProgStr(const char *str)
//=============================================================================
{
// print a string stored in program memory
register char c;
// check to make sure we have a good pointer
if (!str) return;
// print the string until the null-terminator
while((c =*str++))
rprintfChar(c);
}
//******************************************************************************
// *** rprintfCRLF ***
// prints carriage return and line feed
//******************************************************************************
//==============================================================================
void rprintfCRLF(void)
//==============================================================================
{
// print CR/LF
rprintfChar('\r');
rprintfChar('\n');
}
//******************************************************************************
// *** rprintfu04 ***
// prints an unsigned 4-bit number in hex (1 digit)
//******************************************************************************
//==============================================================================
void rprintfu04(unsigned char data)
//==============================================================================
{
rprintfChar( HexChars[data&0x0f] );
}
//=============================================================================
void rprintfu08(unsigned char data)
//=============================================================================
{
// print 8-bit hex value
rprintfu04(data>>4);
rprintfu04(data);
}
//=============================================================================
void rprintfu16(unsigned short data)
//=============================================================================
{
// print 16-bit hex value
rprintfu08(data>>8);
rprintfu08(data);
}
//=============================================================================
void rprintfu32(unsigned long data)
//=============================================================================
{
// print 32-bit hex value
rprintfu16(data>>16);
rprintfu16(data);
}
#ifdef RPRINTF_FLOAT
//******************************************************************************
// *** rprintfFloat ***
// floating-point print
//******************************************************************************
//=============================================================================
void rprintfFloat(char numDigits, double x)
//=============================================================================
{
unsigned char firstplace = FALSE;
unsigned char negative;
unsigned char i, digit;
double place = 1.0;
// save sign
negative = (x<0);
// convert to absolute value
x = (x>0)?(x):(-x);
// find starting digit place
for(i=0; i<15; i++)
{
if((x/place) < 10.0)
break;
else
place *= 10.0;
}
// print polarity character
if(negative)
rprintfChar('-');
else
rprintfChar('+');
// print digits
for(i=0; i<numDigits; i++)
{
digit = (x/place);
if(digit | firstplace | (place == 1.0))
{
firstplace = TRUE;
rprintfChar(digit+0x30);
}
else
rprintfChar(' ');
if(place == 1.0)
{
rprintfChar('.');
}
x -= (digit*place);
place /= 10.0;
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -