
PIC单片机的C语言应用下的函数库.docx
39页PICC 库函数然后按照以下几本章将详细列出PICC编译器的库函数每个函数均从函数名开始,个标题给出详细解释提要:函数的C语言定义以及定义函数的头文件描述:对函数及其目的进行叙述性描述例程:给出一个能说明该函数的应用例子数据类型:列出函数中使用的一些特殊的数据类型(如结构体等)的C语言定义这些数据类型的定义包含在提要标题下列出的头文件中参阅: 给出相关联的函数返回值: 如 果函数有返回值,则在本标题下将给出返回值的类型和性质,同时还包括错误返回的信息1 ABS 函数1. 提 要#include <>int abs (int j)2. 描 述abs( ) 函数返回变量j 的绝对值3. 例 程#include <>#include <>voidmain (void){int a = -5;printf("The absolute value of %d is %d\n", a, abs(a)) ;}4. 返回值j 的绝对值2 ACOS函数1. 提 要#include <>double acos (double f)2. 描 述acos( ) 函数是 cos( ) 的反函数函数参数在[-1 , 1] 区间内,返回值是一个用弧度表示的角度,而且该返回值的余弦值等于函数参数。
3. 例 程#include <>#include <>/* 以度为单位,打印 [-1 , 1] 区间内的反余弦值 */ void main (void) { float i , a; for(i =, i <; i += {a = acos(i)* ; printf("acos(%f) = %f degrees\n" , i , a) ; } } 4 .参 阅sin( ) , cos( ) , tan( ) , asin( ) , atan( ) , atan2( ) 5 .返回值返回值是一个用弧度表示的角度,区间是 [0,兀]如果函数参数超出区间[-1 , 1], 则返回值将为 03 ASCTIMES 数1. 提 要#include <>char * asctime (struct tm * t)2. 描 述 asctime( ) 函数通过指针t 从上 struct tm 结构体中获得时间,返回描述当前日期和时间的26个字符串,其格式如下:Sun Sep 16 01:03:52 1973\n\0值得注意的是, 在字符串的末尾有换行符 字符串中的每个字长是固定的 以下例程得到当前时间,通过localtime()函数将其转换成一个struct tm 指针,最后转换成 ASCII码并打印出来。
其中, time( ) 函数需要用户提供(详情请参阅 time( ) 函数) 3. 例 程#include <>#include <> void main (void) { time_t clock ; struct tm * tp ; time(&clock) ; tp = localtime(&clock) ; printf("%s" , asctime(tp)) ; } 4. 参 阅ctime( ) , gmtime( ) , localtime( ), time( )5. 返回值指向字符串的指针注意:由于编译器不提供 time( ) 例行程序,故在本例程中它需要由用户提供详情请参照 time( )函数6. 数据类型struct tm {int tm_sec ;int tm_min ;int tm_hour ;int tm_mday ;int tm_mon ;int tm_year ;int tm_wday ;int tm_yday ;int tm_isdst ;};4 ASIN 函数1. 提 要#include <>double asin (double f)2. 描 述asin( ) 函数是 sin( ) 的反函数。
它的函数参数在[-1 , 1] 区间内,返回一个用弧度表示的角度值,而且这个返回值的正弦等于函数参数3. 例 程#include <>#include <>voidmain (void){float i , a;for(i =; i <; i += {a = asin(i)* ;printf("asin(%f) = %f degrees\n", i , a) ;}}4. 参 阅sin( ) , cos( ) , tan( ) , acos( ) , atan( ) , atan2( )5. 返回值本函数返回一个用弧度表示的角度值,其区间为卜71/2,兀/2]如果函数参数的值超出区间 [-1 , 1] ,则函数返回值将为 0 5 ATAN函数1. 提 要#include <>double atan (double x)2. 描 述函数返回参数的反正切值也就是说,本函数将返回一个在区间[-兀/2,兀/2]的角度e,而且有tan(e)=x (x为函数参数)3. 例 程#include <>#include <>voidmain (void){printf("%f\n" , atan) ;}4. 参 阅sin( ) , cos( ) , tan( ) , asin( ) , acos( ) , atan2( )5. 返回值返回函数参数的反正切值。
6 ATAN2函数1. 提 要#include <>double atan2 (double y , double x)2. 描 述本函数返回 y/x 的反正切值,并由两个函数参数的符号来决定返回值的象限3. 例 程#include <>#include <>voidmain (void){printf("%f\n" , atan2 , 1)) ;}4. 参 阅sin( ) , cos( ) , tan( ) , asin( ) , acos( ) , atan( )5. 返回值返回y/x的反正切值(用弧度表示),区间为[-兀,兀]如果y和x均为0,将出现定 义域错误,并返回 0 7 ATOF 函数1. 提 要#include <>double atof (const char * s)2. 描 述atof( ) 函数将扫描由函数参数传递过来的字符串,并跳过字符串开头的空格然后将一个数的 ASCII 表达式转换成双精度数 这个数可以用十进制数、 浮点数或者科学记数 法表示3. 例 程#include <>#include <>voidmain (void){char buf[80];double i ;gets(buf) ;i = atof(buf) ;printf("Read %s: converted to %f\n", buf , i) ;}4. 参 阅atoi( ), atol( )5. 返回值本函数返回一个双精度浮点数。
如果字符串中没有发现任何数字,则返回8 ATOI 函数1. 提 要#include <>int atoi (const char * s)2. 描 述atoi( ) 函数扫描传递过来的字符串,跳过开头的空格并读取其符号 ; 然后将一个十 进制数的 ASCII 表达式转换成整数 3. 例 程#include <>#include <> voidmain (void){char buf[80];int i ;gets(buf) ;i = atoi(buf) ;printf("Read %s: converted to %d\n", buf , i) ;}4. 参 阅xtoi( ), atof( ), atol( )5. 返回值返回一个有符号的整数如果在字符串中没有发现任何数字,则返回09 ATOL函数1 .提 要#include <>long atol (const char * s)2 .描 述atol( ) 函数扫描传递过来的字符串,并跳过字符串开头的空格;然后将十进制数的ASCII表达式转换成长整型3 .例 程#include <>#include <>voidmain (void){char buf[80];long i ;gets(buf) ;i = atol(buf) ;printf("Read %s: converted to %ld\n", buf , i) ;}4 .参 阅atoi( ), atof( )5 .返回值返回一个长整型数。
如果字符串中没有发现任何数字,返回值为 010 CEIL 函数1. 提 要#include <>double ceil (double f)2. 描 述本函数对函数参数 f 取整,取整后的返回值为大于或等于f 的最小整数3. 例程#include <>#include <>voidmain (void){double j ;scanf("%lf" , &j) ;printf("The ceiling of %lf is %lf\n", j , ceil(j)) ;}11 COS函数1. 提 要#include <>double cos (double f)2. 描 述本函数将计算函数参数的余弦值其中,函数参数用弧度表示余弦值通过多项式级数近似值展开式算得3. 例 程#include <>#include <>#define Cvoidmain (void){double i ;for(i = 0; i <= ; i += 10)printf("sin(% = %f , cos = %f\n" , i , sin(i*C) , cos(i*C)) ; }4. 参 阅sin( ) , tan( ) , asin( ) , acos( ) , atan( ) , atan2( )5. 返回值返回一个双精度数,区间为 [-1 , 1] 。
12 COSH SINH TAN函数1. 提 要#include <>double cosh (double f)double sinh (double f)double tanh (double f)2. 描 述这些函数都是cos( ) , sin()和tan()的双曲函数3. 例 程#include <>#include <>voidmain (void){printf("%f\n",cosh);printf("%f\n",sinh);printf("%f\n",tanh);}4. 返回值cosh( )函数返回双曲余弦值, sinh( ) 函数返回双曲正弦值, tanh( ) 函数返回双曲 正切值13 CTIME 函数1. 提 要#include <>char * ctime (time_t * t)2. 描 述ctime( ) 函数将函数参数所指的时间转换成字符串,其结构与asctim。
