1.前言
重新回来审视单片机的开发,发现C51居然也支持库函数的开发。库函数中支持printf的串口输出函数、memcpy类的内存操作函数、malloc/free类的内存申请与释放函数、sin类的数学计算函数。
库函数的使用不仅可以加快开发效率,还能提供友好的程序跨平台移植特性。
2.库函数
C51库函数的帮助文档可以在Keil4的安装目录下找到,相对地址为:Keil4\C51\hlp\c51.chm,文档内的Library Reference目录下有以类别划分的库函数(Routines By Category)、有头文件介绍(Include Files)、有单个函数的介绍(Reference)。其中划分的库函数类别主要有:
缓存操作类(Buffer Manipulation Routines)
字符类(Character Routines)
数据转换类(Data Conversion Routines)
数学类(Math Routines)
内存分配类(Memory Allocation Routines)
IO流类(Stream I/O Routines)
字符串类(String Routines)
可变长度参数(Variable Length Argument Routines)
本征库函数(Intrinsic Routines)
其他(Miscellaneous Routines)
3.详细分类
缓存操作类(Buffer Manipulation Routines)
Routine | Include | Description |
---|---|---|
memccpy | string.h | Copies data bytes from one buffer to another until a specified character or specified number of characters has been copied. |
memchr | string.h | Returns a pointer to the first occurrence of a specified character in a buffer. |
memcmp | string.h | Compares a given number of characters from two different buffers. |
memcpy | string.h | Copies a specified number of data bytes from one buffer to another. |
memmove | string.h | Copies a specified number of data bytes from one buffer to another. This routine is typically used when the two buffers may overlap. |
memset | string.h | Initializes a specified number of data bytes in a buffer to a specified character value. |
字符类(Character Routines)
Routine | Include | Description |
---|---|---|
isalnum | ctype.h | Tests for an alphanumeric character. |
isalpha | ctype.h | Tests for an alphabetic character. |
iscntrl | ctype.h | Tests for a Control character. |
isdigit | ctype.h | Tests for a decimal digit. |
isgraph | ctype.h | Tests for a printable character with the exception of space. |
islower | ctype.h | |
isprint | ctype.h | Tests for a printable character. |
ispunct | ctype.h | Tests for a punctuation character. |
isspace | ctype.h | Tests for a whitespace character. |
isupper | ctype.h | Tests for an uppercase alphabetic character. |
isxdigit | ctype.h | Tests for a hexadecimal digit. |
toascii | ctype.h | Converts a character to an ASCII code. |
toint | ctype.h | Converts a hexadecimal digit to a decimal value. |
tolower | ctype.h | Tests a character and converts it to lowercase if it is uppercase. |
_tolower | ctype.h | Unconditionally converts a character to lowercase. |
toupper | ctype.h | Tests a character and converts it to uppercase if it is lowercase. |
_toupper | ctype.h | Unconditionally converts a character to uppercase. |
数据转换类(Data Conversion Routines)
Routine | Include | Description |
---|---|---|
abs | math.h | Generates the absolute value of an integer type. |
atof | stdlib.h | Converts a string to a float. |
atoi | stdlib.h | Converts a string to an int. |
atol | stdlib.h | Converts a string to a long. |
cabs | math.h | Generates the absolute value of a character type. |
labs | math.h | Generates the absolute value of a long type. |
strtod | stdlib.h | Converts a string to a float. |
strtol | stdlib.h | Converts a string to a long. |
strtoul | stdlib.h | Converts a string to an unsigned long. |
数学类(Math Routines)
Routine | Include | Description |
---|---|---|
acos | math.h | Calculates the arc cosine of a specified number. |
asin | math.h | Calculates the arc sine of a specified number. |
atan | math.h | Calculates the arc tangent of a specified number. |
atan2 | math.h | Calculates the arc tangent of a fraction. |
ceil | math.h | Finds the integer ceiling of a specified number. |
cos | math.h | Calculates the cosine of a specified number. |
cosh | math.h | Calculates the hyperbolic cosine of a specified number. |
exp | math.h | Calculates the exponential function of a specified number. |
fabs | math.h | |
floor | math.h | Finds the largest integer less than or equal to a specified number. |
fmod | math.h | Calculates the floating-point remainder. |
log | math.h | Calculates the natural logarithm of a specified number. |
log10 | math.h | Calculates the common logarithm of a specified number. |
modf | math.h | Generates integer and fractional components of a specified number. |
pow | math.h | Calculates a value raised to a power. |
rand | stdlib.h | Generates a pseudo random number. |
sin | math.h | Calculates the sine of a specified number. |
sinh | math.h | Calculates the hyperbolic sine of a specified number. |
srand | stdlib.h | Initializes the pseudo random number generator. |
sqrt | math.h | Calculates the square root of a specified number. |
tan | math.h | Calculates the tangent of a specified number. |
tanh | math.h | Calculates the hyperbolic tangent of a specified number. |
_chkfloat_ | intrins.h | Checks the status of a floating-point number. |
_crol_ | intrins.h | Rotates an unsigned char left a specified number of bits. |
_cror_ | intrins.h | Rotates an unsigned char right a specified number of bits. |
_irol_ | intrins.h | Rotates an unsigned int left a specified number of bits. |
_iror_ | intrins.h | Rotates an unsigned int right a specified number of bits. |
_lrol_ | intrins.h | Rotates an unsigned long left a specified number of bits. |
_lror_ | intrins.h | Rotates an unsigned long right a specified number of bits. |
内存分配类(Memory Allocation Routines)
Routine | Include | Description |
---|---|---|
calloc | stdlib.h | Allocates storage for an array from the memory pool. |
free | stdlib.h | Frees a memory block that was allocated using calloc, malloc, or realloc. |
init_mempool | stdlib.h | Initializes the memory location and size of the memory pool. |
malloc | stdlib.h | Allocates a block from the memory pool. |
realloc | stdlib.h | Reallocates a block from the memory pool. |
IO流类(Stream I/O Routines)
Routine | Include | Description |
---|---|---|
getchar | stdio.h | Reads and echoes a character using the _getkey and putchar routines. |
_getkey | stdio.h | Reads a character using the 8051 serial interface. |
gets | stdio.h | Reads and echoes a character string using the getchar routine. |
printf | stdio.h | Writes formatted data using the putchar routine. |
putchar | stdio.h | Writes a character using the 8051 serial interface. |
puts | stdio.h | Writes a character string and newline ('\n') character using the putchar routine. |
scanf | stdio.h | Reads formatted data using the getchar routine. |
sprintf | stdio.h | Writes formatted data to a string. |
sscanf | stdio.h | Reads formatted data from a string. |
ungetchar | stdio.h | Places a character back into the getchar input buffer. |
vprintf | stdio.h | Writes formatted data using the putchar function. |
vsprintf | stdio.h | Writes formatted data to a string. |
字符串类(String Routines)
Routine | Include | Description |
---|---|---|
strcat | string.h | Concatenates two strings. |
strchr | string.h | Returns a pointer to the first occurrence of a specified character in a string. |
strcmp | string.h | Compares two strings. |
strcpy | string.h | Copies one string to another. |
strcspn | string.h | Returns the index of the first character in a string that matches any character in a second string. |
strlen | string.h | Returns the length of a string. |
strncat | string.h | Concatenates up to a specified number of characters from one string to another. |
strncmp | string.h | Compares two strings up to a specified number of characters. |
strncpy | string.h | Copies up to a specified number of characters from one string to another. |
strpbrk | string.h | Returns a pointer to the first character in a string that matches any character in a second string. |
strpos | string.h | Returns the index of the first occurrence of a specified character in a string. |
strrchr | string.h | Returns a pointer to the last occurrence of a specified character in a string. |
strrpbrk | string.h | Returns a pointer to the last character in a string that matches any character in a second string. |
strrpos | string.h | Returns the index of the last occurrence of a specified character in a string. |
strspn | string.h | Returns the index of the first character in a string that does not match any character in a second string. |
strstr | string.h | Returns a pointer in a string that is identical to a second sub-string. |
可变长度参数(Variable Length Argument Routines)
Routine | Include | Description |
---|---|---|
va_arg | stdarg.h | Retrieves an argument from an argument list. |
va_end | stdarg.h | Resets an argument pointer. |
va_start | stdarg.h | Sets a pointer to the beginning of an argument list. |
本征库函数(Intrinsic Routines)
Routine | Include | Description |
---|---|---|
_chkfloat_ | intrins.h | Checks the status of a floating-point number. |
_crol_ | intrins.h | Rotates an unsigned char left a specified number of bits. |
_cror_ | intrins.h | Rotates an unsigned char right a specified number of bits. |
_irol_ | intrins.h | Rotates an unsigned int left a specified number of bits. |
_iror_ | intrins.h | Rotates an unsigned int right a specified number of bits. |
_nop_ | intrins.h | Executes one NOP instruction. |
_pop_ | intrins.h | Pops an SFR from the hardware stack using the POP instruction. |
_push_ | intrins.h | Pushes an SFR onto the hardware stack using the PUSH instruction. |
_testbit_ | intrins.h | Tests the value of a bit and clears it to 0. |
其他(Miscellaneous Routines)
Routine | Include | Description |
---|---|---|
setjmp | setjump.h | Saves the current stack condition and program address. |
longjmp | setjump.h | Restores the stack condition and program address. |
_nop_ | intrins.h | Inserts an 8051 NOP instruction. |
_testbit_ | intrins.h | Tests the value of a bit and clears it to 0. |
4.例程
Keil4为C51提供了较为丰富的例程,例程在Keil的安装目录下,相对地址为:Keil4\C51\Examples。
参考资料:
附件: