C51库函数.docx
- 文档编号:26478340
- 上传时间:2023-06-19
- 格式:DOCX
- 页数:33
- 大小:19.03KB
C51库函数.docx
《C51库函数.docx》由会员分享,可在线阅读,更多相关《C51库函数.docx(33页珍藏版)》请在冰豆网上搜索。
C51库函数
C51库函数积累:
(1)_chkfloat_:
函数定义:
unsignedchar_chkfloat_(floatval);/*numbertocheck*/
函数功能:
_chkfloat_函数检查浮点数val的类型。
返回值:
_chkfloat_函数返回浮点数val的类型。
返回值
意义
0
标准浮点数
1
浮点0
2
+INF正溢出
3
-INF负溢出
4
NaN非数
/*
本实验测试本征库中的_chkfloat_函数:
函数定义:
unsignedchar_chkfloat_(floatval);
返回值:
ReturnValue Meaning
0 Standardfloating-pointnumber.
1 Floating-pointvalue0.
2 +INF(positiveoverflow).
3 -INF(negativeoverflow).
4 NaN(NotaNumber)errorstatus.
用P1指示返回值
*/
//函数测试:
#include
#include
unsignedcharf[4]={0xff,0xff,0xff,0xff};
voiddelay(unsignedinttime)
{
while(time--);
}
voidmain()
{
P2=~_chkfloat_(1.2455); //Standardfloating-pointnumber.
delay(50000);
P2=~_chkfloat_(0.00); //Floating-pointvalue0.
delay(50000);
P2=~_chkfloat_(0000000000.0000000000); //+INF(positiveoverflow).
delay(50000);
P2=~_chkfloat_(-0000000000.0000000000); //-INF(negativeoverflow).
delay(50000);
P2=~_chkfloat_(*((float*)f)); //NaN(NotaNumber)errorstatus.
while
(1);
}
(2)_crol_:
函数定义:
unsignedchar_crol_(unsignedcharc, unsignedcharb); /*charactertorotateleft*/
/*bitpositionstorotate*/
函数功能:
_crol_函数将一个字节c循环左移b位。
返回值:
_crol函数返回将c循环左移b位后的值。
/*
本实验测试本征库中的_crol_函数
函数定义:
unsignedchar_crol_(unsignedcharc,unsignedcharb);
返回值:
_crol_函数返回将c循环左移b位后的值。
用P1指示返回值
*/
//函数测试:
#include
#include
voiddelay(unsignedinttime)
{
while(time--);
}
voidmain()
{
unsignedchari;
i=0;
P2=~0x01;
for(i=0;i<8;i++)
{
P2=_crol_(P2,1);
delay(50000);
}
while
(1);
}
(3)_cror_:
函数定义:
unsignedchar_cror_(unsignedcharc, /*charactertorotateright*/
unsignedcharb);/*bitpositionstorotate*/
函数功能:
_cror_函数将一个字节c循环右移b位。
返回值:
_cror_函数返回将c循环右移b位后的值。
/*
本实验测试本征库中的_cror_函数
函数定义:
unsignedchar_cror_(unsignedcharc,unsignedcharb);
返回值:
_cror_函数返回将c循环右移b位后的值。
用P1指示返回值
*/
//函数测试:
#include
#include
voiddelay(unsignedinttime)
{
while(time--);
}
voidmain()
{
unsignedchari;
i=0;
P2=~0x80;
for(i=0;i<8;i++)
{
P2=_cror_(P2,1);
delay(50000);
}
while
(1);
}
(4)_getkey:
函数定义:
char_getkey(void);
函数功能:
等待接收串口的一个字节的数据。
返回值:
从串口接收到的字节。
/*
本实验测试标准输入输出库中的_getkey函数
函数定义:
char_getkey(void);
返回值:
从串口接收到的字节。
用P1指示返回值
*/
//函数测试:
#include
#include
voiddelay(unsignedinttime)
{
while(time--);
}
voidUART_Init() /*通讯有关参数初始化*/
{
PCON&=0x7f;
TH1=0xfd; /*T1usesio*/
TL1=0xfd;/*选择通讯速率:
0=1200,1=2400,2=4800,3=9600,4=19.2k*/
/*T1usesio*/
TMOD=0x21; /*T1=MODE2,sio;T0=MODE1,16bit,usetime*/
PS=1; /*SIOinthigh 优先级*/
EA=1;
ET1=0;
SM0=0;
SM1=1; /*SM0=0SM1=1,mode1,10bit */
SM2=0; /*dataint,无校验(TB8=bit_duble偶)*/
TR1=1;
REN=1;
RI=0;
TI=0;
ES=1;
}
voidmain()
{
delay(50000);
UART_Init();
P2=0xff;
while
(1)
{
P2=_getkey();
}
}
(5)_irol_:
函数定义:
unsignedint_irol_(unsignedinti,/*integertorotateleft*/
unsignedcharb);/*bitpositionstorotate*/
函数功能:
对整型数i循环左移b位。
返回值:
i循环左移b位后的值。
/*
本实验测试本征库中的_irol_函数
函数定义:
unsignedint_irol_(unsignedinti,unsignedcharb);
返回值:
i循环左移b位后的值。
*/
//函数测试:
#include
#include
unsignedinttest;
voidmain()
{
test=0xa5a5;
test=_irol_(test,3); //test=0x2d2d
while
(1);
}
(6)_iror_:
函数定义:
unsignedint_iror_(unsignedinti,/*integertorotateright*/
unsignedcharb);/*bitpositionstorotate*/
函数功能:
对整型数i循环右移b位。
返回值:
i循环右移b位后的值。
/*
本实验测试本征库中的_iror_函数
函数定义:
unsignedint_iror_(unsignedinti,unsignedcharb);
返回值:
i循环右移b位后的值。
*/
//函数测试:
#include
#include
unsignedinttest;
voidmain()
{
test=0xa5a5;
test=_iror_(test,3); //test=0xb4b4
while
(1);
}
(7)_lrol_:
函数定义:
unsignedlong_lrol_(unsignedlongi,/*32-bitintegertorotateleft*/
unsignedcharb);/*bitpositionstorotate*/
函数功能:
对长整型数i循环左移b位。
返回值:
i循环左移b位后的值。
/*
本实验测试本征库中的_lrol_函数
函数定义:
unsignedlong_lrol_(unsignedlongi,unsignedcharb);
返回值:
i循环左移b位后的值。
*/
//函数测试:
#include
#include
unsignedlongtest;
voidmain()
{
test=0xa5a5a5a5;
test=_lrol_(test,3); //test=0x2d2d2d2d
while
(1);
}
(8)_lror_:
函数定义:
unsignedlong_lror_(unsignedlongi,/*32-bitintegertorotateright*/
unsignedcharb);/*bitpositionstorotate*/
函数功能:
对长整型数i循环右移b位。
返回值:
i循环右移b位后的值。
/*
本实验测试本征库中的_lror_函数
函数定义:
unsignedlong_lror_(unsignedlongi,unsignedcharb);
返回值:
i循环左移b位后的值。
*/
u函数测试:
#include
#include
nsignedlongtest;
voidmain()
{
test=0xa5a5a5a5;
test=_lror_(test,3); //test=0xb4b4b4b4
while
(1);
}
(9)_nop_:
函数定义:
void_nop_(void);
函数功能:
等同于汇编里的nop指令,用来实现一个机器周期的延时。
返回值:
无
/*
本实验测试本征库中的_nop_函数
函数定义:
void_nop_(void);
返回值:
无
*/
函数测试:
#include
#include
voidmain()
{
P1=0x00;
_nop_(); //如果是22.1184M的晶振,12分频后为1.8432M,机器周期为543ns
_nop_();
_nop_();
P1=0xff;
while
(1);
}
(10)_pop_:
函数定义:
void_pop_(unsignedcharsfr);/*SFRtoPOPfromthestack*/
函数功能:
在代码中插入一个POP指令,将一个SFR推出
返回值:
无
函数测试:
#include
#include
sfrXXX=0xFF;
voidmain(void)
{
_push_(XXX); //main函数就是一个栈,参数就是压栈进入,_push_用来将一个SFR压入此栈
XXX=1; //对压入的SFR进行赋值
XXX=2;
XXX=3;
_pop_(XXX); //将SFR推出此栈,其值被释放
while
(1);
}
(11)_push_:
函数定义:
void_push_(unsignedcharsfr);/*SFRtoPUSHontothestack*/
函数功能:
在代码中插入一个PUSH指令,将一个SFR压入
返回值:
无
函数测试:
参照_pop_的测试程序。
(12)_testbit_:
函数定义:
bit_testbit_(bitb);/*bittotestandclear*/
函数功能:
测试位b是1还是0
返回值:
同b
/*
本实验测试本征库中的_testbit_函数
函数定义:
bit_testbit_(bitb);
返回值:
同b
*/
函数测试:
#include
#include
voidmain()
{
bittest_flag;
test_flag=1;
if(_testbit_(test_flag))
P1=0xff;
else
P1=0x00;
while
(1);
}
(13)_tolower:
函数定义:
int_tolower(intc);/*charactertoconvert*/
函数功能:
将一个字符转换为相应的小写字符。
返回值:
转换后的字符。
/*
本实验测试ctype库中的_tolower函数
函数定义:
int_tolower(intc);
返回值:
转换后的字符。
*/
函数测试:
#include
#include
voidmain()
{
charc;
c=_tolower('A');
while
(1);
}
(14)_toupper:
函数定义:
int_toupper(intc);/*charactertoconvert*/
函数功能:
将一个字符转换为相应的大写字符。
返回值:
转换后的字符。
/*
本实验测试ctype库中的_toupper函数
函数定义:
int_toupper(intc);
返回值:
转换后的字符。
*/
函数测试:
#include
#include
voidmain()
{
charc;
c=_toupper('a');
while
(1);
}
(15)abs:
函数定义:
intabs(intval);/*numbertotakeabsolutevalueof*/
函数功能:
取一个整型数的绝对值。
返回值:
取绝对值后的值。
/*
本实验测试数学库中的abs函数
函数定义:
intabs(intval);
返回值:
取绝对值后的值。
*/
函数测试:
#include
#include
voidmain()
{
intx,y;
x=-123;
y=abs(x); //y=123;
while
(1);
}
(16)acos:
函数定义:
floatacos(floatx);/*numbertocalculatearccosineof*/
函数功能:
取一个值反余弦值。
返回值:
取反余弦后的值。
/*
本实验测试数学库中的acos函数
函数定义:
floatacos(floatx);
返回值:
取反余弦后的值。
*/
函数测试:
#include
#include
voidmain()
{
floatx,y;
x=0.500;
y=acos(x); //60degree1.0472rad
while
(1);
}
(17)asin:
函数定义:
floatasin(floatx);/*numbertocalculatearcsineof*/
函数功能:
取一个值反正弦值。
返回值:
取反正弦后的值。
/*
本实验测试数学库中的asin函数
函数定义:
floatasin(floatx);
返回值:
取反正弦后的值。
*/
函数测试:
#include
#include
voidmain()
{
floatx,y;
x=0.500;
y=asin(x); //30degree 0.523rad
while
(1);
}
(18)atan:
函数定义:
floatatan(floatx);/*numbertocalculatearctangentof*/
函数功能:
取一个值反正切值。
返回值:
取反正切后的值。
/*
本实验测试数学库中的atan函数
函数定义:
floatatan(floatx);
返回值:
取反正切后的值。
*/
函数测试:
#include
#include
voidmain()
{
floatx,y;
x=1.000;
y=atan(x); //45degree 0.7854rad
while
(1);
}
(19)atan2:
函数定义:
floatatan2(floaty,/*denominatorforarctangent*/
floatx);/*numeratorforarctangent*/
函数功能:
取(x,y)确定的弧度的反正切值。
返回值:
取(x,y)确定的弧度的反正切后的值。
/*
本实验测试数学库中的atan2函数
函数定义:
floatatan2(floaty,floatx);
返回值:
取(x,y)确定的弧度的反正切后的值。
*/
函数测试:
#include
#include
voidmain()
{
floatx,y;
x=1.000;
y=1.000;
y=atan2(y,x); //45degree 0.7854rad
while
(1);
}
(20)atof:
函数定义:
floatatof(char*string);/*stringtoconvert*/
函数功能:
把一个字符串转为浮点数。
返回值:
转换后的浮点数。
/*
本实验测试标准库中的atof函数
函数定义:
floatatof(char*string);
返回值:
转换后的浮点数。
*/
函数测试:
#include
#include
voidmain()
{
floatf;
chars[]="12.8746";
f=atof(s); //f=12.8746
while
(1);
}
(21)atoi:
函数定义:
intatoi(char*string);/*stringtoconvert*/
函数功能:
把一个字符串转为整型数。
返回值:
转换后的整型数。
/*
本实验测试标准库中的atoi函数
函数定义:
intatoi(char*string);
返回值:
转换后的整型数。
*/
函数测试:
#include
#include
voidmain()
{
inti;
chars[]="1225";
i=atoi(s); //i=1225
while
(1);
}
(22)atol:
函数定义:
longatol(char*string);/*stringtoconvert*/
函数功能:
把一个字符串转为长整型数。
返回值:
转换后的长整型数。
/*
本实验测试标准库中的atol函数
函数定义:
intatol(char*string);
返回值:
转换后的长整型数。
*/
函数测试:
#include
#include
voidmain()
{
longl;
chars[]="1225578";
l=atol(s); //l=1225578
while
(1);
}
(23)cabs:
函数定义:
charcabs(charval);/*numbertotakeabsolutevalueof*/
函数功能:
取一个char型值的绝对值。
返回值:
取绝对值后的值。
/*
本实验测试标准库中的cabs函数
函数定义:
charcabs(charval);
返回值:
取绝对值后的值。
。
*/
函数测试:
#include
#include
voidmain()
{
charx;
chary;
x=-41;
y=cabs(x); //y=41
while
(1);
}
(25)calloc:
函数定义:
voidxdata*calloc(unsignedintnum,/*numberofitems*/
unsignedintlen);/*lengthofeachitem*/
函数功能:
建立一个有num个元素的数组,每个元素有len个字节,并被初始化为0。
数组长度为len*num个字节。
返回值:
函数如果调用成功,返回一个指向数组首地址的指针,否则返回一个空指针。
函数测试:
程序需要测试。
ceil:
函数定义:
floatceil(floatval);/*numbertocalculateceilingfor*/
函数功能:
取比val大的最小的整数。
返回值:
比val大的最小的整数。
/*
本实验测试标准库中的ceil函数
函数定义:
floatceil(floatval);
返回值:
比val大的最小的整数。
*/
函数测试:
#include
#include
voidmain()
{
floatx,y;
x=34.234;
y=ceil(x); //y=35
while
(1);
}
(26)cos:
函数定义:
floatcos(floatx);/*numbertoca
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C51 库函数