單片機學習庫函數(shù)詳解_第1頁
單片機學習庫函數(shù)詳解_第2頁
單片機學習庫函數(shù)詳解_第3頁
單片機學習庫函數(shù)詳解_第4頁
單片機學習庫函數(shù)詳解_第5頁
已閱讀5頁,還剩36頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

PICCPICC編譯器的庫函數(shù)。每個函數(shù)均從函數(shù)名開始,然后按照以下PICCPICC編譯器的庫函數(shù)。每個函數(shù)均從函數(shù)名開始,然后按照以下例程:給出一個能說明該函數(shù)的應用例參閱:給出相關聯(lián)的函數(shù)返回值:如果函數(shù)有返回值,則在本標題下將給出返回值的類型和性質,同時還包括 ABS#includeintabs(int例#include<stdio.h>#include<stdlib.h>main{inta=-printf("Theabsolutevalueof%dis}返回j ACOS#includedoubleacos(double3.1#includemain{floatfor(i=-1.0,i<1.0;i+={a=acos(i)*180.0/3.141592;}}參sin(),cos(),tan(),asin(),atan(),atan2(返回返回值將為0。 ASCTIME#includechar*asctime(structtm*asctime()t從上structtm結構體中獲得時間,返回描述當前日期和時SunSep1601:03:52當前時間,通過localtime()函數(shù)將其轉換成一個structtm指針,最后轉換成ASCII碼并打印出來。其中,time()函數(shù)需要用戶提供(詳情請參閱time()函數(shù)。3.#include<stdio.h>#include<time.h>main{time_tclock;structtm*tp;tp=2}參ctime(),gmtime(),localtime(),time(返回注意:由于編譯器不提供time(例行程序,故在本例程中它需要由用戶提供。詳情請參照time(數(shù)據(jù)類structtminttm_sec;inttm_min;inttm_hour;inttm_mon;inttm_year;inttm_wday;inttm_yday;inttm_isdst; ASIN#includedoubleasin(doubleasin(函數(shù)是sin的反函數(shù)。它的函數(shù)參數(shù)在[-1,1]區(qū)間內(nèi),返回一個用弧度表示3.#include<math.h>#include<stdio.h>main{floatfor(i=-1.0;i<1.0;i+={a=asin(i)*180.0/3.141592;}}3參sin(),cos(),tan()參sin(),cos(),tan(),acos(),atan(),atan2(返回 ATAN#includedoubleatan(double例#include<stdio.h>#include<math.h>main{}參sin(),cos(),tan(),asin(),acos(),atan2(返回 ATAN2#includedoubleatan2(doubley,double例#include<stdio.h>#include<math.h>main{}參4sin(),cos(),tan(),sin(),cos(),tan(),asin(),acos(),atan(5.回 ATOF#includedoubleatof(constchar*()例#include<stdlib.h>#include<stdio.h>main{doublei;i=printf("Read%s:convertedto}參atoi(),atol(返回 ATOI#includeintatoi(constchar*atoi(函數(shù)掃描傳遞過來的字符串,跳過開頭的空格并讀取其符號;然后將一個十進3.#include<stdio.h>5main{intimain{inti;i=printf("Read%s:convertedto}參xtoi(),atof(),atol(返回 ATOL#includelongatol(constchar*atol(函數(shù)掃描傳遞過來的字符串,并跳過字符串開頭的空格;然后將十進制數(shù)的例#include<stdio.h>main{longi;i=printf("Read%s:convertedto}參atoi(),atof(返回返回一個長整型數(shù)。如果字符串中沒有發(fā)現(xiàn)任何數(shù)字,返回值為0CEIL#include6doubleceil(doublef取整,取整后的返回值為大于或等于例程#include<stdio.h>#includedoubleceil(doublef取整,取整后的返回值為大于或等于例程#include<stdio.h>#include<math.h>main{doublej;scanf("%lf",printf("Theceilingof%lfis} COS#includedoublecos(double例#include<math.h>#include<stdio.h>main{doublefor(i=0;i<=180.0;i+=printf("sin(%3.0f)=%f,cos=}參sin(),tan(),asin(),acos(),atan(),atan2(返回COSH、SINH、TANH#includedoublecosh(double7doublesinh(doubledoubletanh(doubledoublesinh(doubledoubletanh(double例#include<stdio.h>#include<math.h>main{}返回cosh()函數(shù)返回雙曲余弦值,sinh()函數(shù)返回雙曲正弦值,tanh()函數(shù)返回雙曲正CTIME#includechar*ctime(time_t*))例#include<stdio.h>#include<time.h>main{time_tclock;}參gmtime(),localtime(),asctime(),time(返回注意:由于編譯器不會提供time(程序,故它需要由用戶給定。詳情請參閱time(數(shù)據(jù)類8typedeflong DI、EItypedeflong DI、EIvoidei(void)void的連續(xù)字期間出現(xiàn)中斷,則函數(shù)getticks()例longcount;voidinterrupt{}long{di();ei();returnval;}DIV#includediv_tdiv(intnumer,int)例#include<stdio.h>main{9div_tx=div_tx=printf("quotient=%d,remainder=}返回數(shù)據(jù)類typedef{int} EEPROM_READ、EEPROM_WRITE#includeunsignedchareeprom_read(unsignedcharvoideeprom_write(unsignedcharaddr,unsignedcharEEPROM3.main{unsignedchardata;address=0x10;data=}注意:如果調(diào)用eeprom_write()函數(shù)后需即刻調(diào)用eeprom_read()函數(shù),則必須查詢EEPROM以確保寫入完畢。全局中斷使能位(GIE)在eeprom_write(程序中重新恢復(寫EEPROM時需要關閉EVAL_POLY#includedoubleeval_poly(doublex,const#includedoubleeval_poly(doublex,constdouble*d,inty=x*x*d2+x*d1+例#include<stdio.h>#include<math.h>main{doublex=2.2;y=printf("Thepolynomialevaluatedat%fis}返回EXP#includedoubleexp(doubleexp(函數(shù)返回參數(shù)的指數(shù)函數(shù)值,即ef(f為函數(shù)參數(shù)例#include<math.h>#include<stdio.h>main{doublefor(f=0.0;f<=5;f+=printf("eto%1.0f=}參log(),log10(),pow(FABS#includedoublefabs(double例FABS#includedoublefabs(double例#include<stdio.h>#include<math.h>main{printf("%f%f\n",fabs(1.5),fabs(-}參abs(FLOOR#includedoublefloor(double例#include<stdio.h>#include<math.h>main{printf("%f\n",floor(1.5));printf("%f\n",floor(-1.5));}FREXP#includedoublefrexp(doublef,int*frexp()函數(shù)將一個浮點數(shù)分解成規(guī)格化小數(shù)和2的整數(shù)次冪兩部分,整數(shù)冪部分存于指pint單元中。本函數(shù)的返回值x或在區(qū)間(0.5,1.0)0;而且有f=x×2p。如果f為0,則分解出來的兩部分均為0。例#include<math.h>#include<stdio.h>main{例#include<math.h>#include<stdio.h>main{inti;f=frexp(23456.34,&i);printf("23456.34=%f*2^%d\n",f,i);}參ldexp(GET_CAL_DATA#includedoubleget_cal_data(constunsignedchar*訪問KREF、KBG、BHTHERM和KTC單元(32位浮點參數(shù)。由于FOSC和TWDT例main{doublex;xget_cal_data(KREF);/*獲得參考斜率(slopereferenceratio)*/y=TWDT;/*獲得WDT溢出時間*/}返回注意:本函數(shù)僅用于PICGMTIME#includestructtm*gmtime(time_t*t所指的時間分解,并且存于結構體中,精確度為秒。其中,tt所指的時間分解,并且存于結構體中,精確度為秒。其中,t例#include<stdio.h>#include<time.h>main{time_tclock;structtm*tp;tp=printf("It’s%dinLondon\n",tp-}參ctime(),asctime(),time(),localtime(返回注意:由于編譯器不會提供time(程序,故它需要由用戶給定。詳情請參閱time(數(shù)據(jù)類structtm{inttm_sec;inttm_min;inttm_hour;inttm_mon;inttm_year;inttm_wday;inttm_yday;inttm_isdst;等函intisalnum(charc)intisalpha(charc)intisascii(charc)intiscntrl(charintisdigit(charc)intislower(charintisdigit(charc)intislower(charc)intisprint(charc)intisgraph(charc)intispunct(charc)intisspace(charc)intisupper(charintisxdigit(charisalpha(c)isascii(c)iscntrl(c)isdigit(c)islower(c)isprint(c)isgraph(c)ispunct(c)isspace(c)isupper(c)isxdigit3.例#include<ctype.h>#include<stdio.h>main{inti;i=c在0~9、a~f或A~F范圍內(nèi)buf[i]=printf("’%s’isthe}4.參toupper(),tolower(),toascii( KBHIT#includebitkbhit KBHIT#includebitkbhit)例main{inti;cputs("I’mfor(i=0;i!=1000;i++)}}參getch(),getche(返回注意:程序的主體需由用戶實現(xiàn),其主要框架可以從sources目錄下直接獲得LDEXP#includedoubleldexp(doublef,intldexp函數(shù)是frexp(的反函數(shù)。它先進行浮點數(shù)3.#include<math.h>#include<stdio.h>main{doublef=printf("1.0*2^10=f的指數(shù)部分與整i}參frexp(返回f}參frexp(返回fiLDIV#includeldiv_tldiv(longnumber,long例#include<stdlib.h>#include<stdio.h>main{ldiv_tlt=printf("Quotient=%ld,remainder=}參閱div()數(shù)據(jù)結typedef{longquot/*longrem/*余數(shù) LOCALTIME#includestructtm*localtime(time_t*數(shù)的返回值將與gmtime()的相同。3.例#include<stdio.h>#include<time.h>char*wday[]={main{time_tclock;structtm*tp;tp=printf("Todayis%s\n",wday[tp-}參ctime(),asctime(),time(返回注意:由于編譯器不會提供time(程序,故它需要由用戶給定。詳情請參閱time(數(shù)據(jù)結structtm{inttm_sec;inttm_min;inttm_hour;inttm_mon;inttm_year;inttm_wday;inttm_yday;inttm_isdst;LOG、LOG10#include<math.h>double#include<math.h>doublelog10(double))例#include<math.h>#include<stdio.h>main{doublefor(f=1.0;f<=10.0;f+=1.0)}參exp(),pow(返回如果函數(shù)參數(shù)為負,返回值為0MEMCHR#include/*constvoid*memchr(constvoid*block,intval,size_t/*高級系列單片機void*memchr(constvoid*block,intval,size_t3.#include<stdio.h>unsignedintary[]={1,5,0x6789,0x23};main{char*cp=memchr(ary,0x89,sizeofprintf("Foundprintf("Foundatoffset%u\n",cp-(char}參strchr(返回NULLMEMCMP#includeintmemcmp(constvoid*s1,constvoid*s2,size_tmemcmp()函數(shù)的功能是比較兩塊長度為n的內(nèi)存中變量的大小,類似strncmp()函例#include<stdio.h>main{buf[0]=1;buf[2]=cow[0]=cow[2]=buf[1]=cow[1]=if(i<0)printf("lessthan\n");elseif(i>0)}參strncpy(),strncmp(),strchr(),memset()參strncpy(),strncmp(),strchr(),memset(),返回,1。MEMCPY#include/*void*memcpy(void*d,constvoid*s,size_t/*farvoid*memcpy(farvoid*d,constvoid*s,size_t,例#include<stdio.h>main{charmemset(buf,0,sizeofbuf);memcpy(buf,"apartialstring",10);printf("buf=’%s’\n",buf);}參strncpy(),strncmp(),strchr(),返回MEMMOVE#include/*void*memmove(void*s1,constvoid*s2,size_t/*farvoid*memmove(farvoid*s1,constvoid*s2,size_t參參返回MEMSET#include/**/void*memset(void*sintc,size_t/*farvoid*memset(farvoid*s,intc,size_tmemset()函數(shù)將字節(jié)c存儲到指針s指向的,內(nèi)存開始的n個內(nèi)存字節(jié)例#include<stdio.h>main{charstrcpy(abuf,"Thisisastring");memset(abuf,’x’,5);printf("buf=’%s’\n",}參MODF#includedoublemodf(doublevalue,double*0.173.#includemain{doublei_val,f_valmain{doublei_val,f_val=modf(-3.17,}4.回PERSIST_CHECK,PERSIST_VALIDATE#includeintpersist_check(intflag)voidpersist_validate(void)例#include<sys.h>main{printf("Resetcountinvalid-zeroed\n");reset_count++;/*updatecount*/persist_validate();/*andchecksum*/continue;/*sleepuntilnextreset}返回,(非零POW#includedoublepow(doublef,POW#includedoublepow(doublef,doublepow()函數(shù)表示第一個參數(shù)f的p次冪例#include<math.h>#include<stdio.h>main{doublefor(f=1.0;f<=10.0;f+=printf("pow(2,%1.0f)=%f\n",f,pow(2,}參log(),log10(),返回fpPRINTF#includeunsignedcharprintf(constchar*fmt,(staoutoxXu 例輸出為’Total=23%’printf("Sizeis%lx",size)printf("Name=%.8s",’Nameprintf("xx%*d",3,輸出為’xx/*vprintf*/#includestdio.h>error(char*s,{va_listap;va_start(ap,s);vprintf(s,ap);}main{inti;i=error("testing12%d",}參見sprintf()函數(shù)返回關PICC–L的描述以及有關PICC–L的描述以及有關HPDPIC長整型格式在printf的菜單選項RAND#includeintrand每次被調(diào)用后,以隨機數(shù)據(jù)形式出現(xiàn)。這一運算規(guī)則將產(chǎn)生一個從同一起點開始的確定3.#include<stdio.h>#include<time.h>main{time_ttoc;inti;for(i=0;i!=10;i++)printf("%d\t",rand());}參見srand()函數(shù)SIN#includedoublesin(double3.程#include<math.h>#include<stdio.h>main{doublefor(i=0;main{doublefor(i=0;i<=180.0;i+=printf("sin(%3.0f)=%f,cos=%f\n",i,sin(i*C),}參cos(),tan(),asin(),acos(),atan(),返回SPRINTF#include/*unsignedcharsprintf(char*buf,constchar*fmt,/*unsignedcharsprintf(farchar*buf,constchar*fmt,;被放到buf緩沖器。字符串帶有空格結束符,buf緩沖器中的數(shù)據(jù)被返回。參見printf()返回sprintf()函數(shù)的返回值為被放入緩沖器中的數(shù)據(jù)。注意,返回值為字符型而非整型SQRT#includedoublesqrt(doublesqrt()函數(shù)利用牛頓法得到參數(shù)的近似平例#include<math.h>#include<stdio.h>main{doublefor(i=0;i<=20.0;i+=printf("squarerootof%.1f=%f\n",i,}參見exp()4.回SRAND參見exp()4.回SRAND#includevoidsrand(unsignedintsrand()函數(shù)是在調(diào)用rand()函數(shù)時被用來初始化隨機數(shù)據(jù)發(fā)生器的。它為rand()函數(shù)產(chǎn)3.#include<stdio.h>#include<time.h>main{time_ttoc;inti;for(i=0;i!=10;i++)printf("%d\t",rand());}STRCAT#include/*char*strcat(char*s1,constchar*/*farchar*strcat(farchar*s1,constchar*針型參數(shù)s1指向的字符數(shù)組必須保證大于結果字符串。3.#include<stdio.h>main{#include<stdio.h>main{char*s1,*s2;s1=buffer;s2="...endofline";strcat(s1,s2);printf("string=\"%s\"\n",buffer);}參strcpy(),strcmp(),strncat(),返回STRCHR,STRICHR#include/*constchar*strchr(constchar*s,intc)constchar*strichr(constchar*s,int/*char*strchr(constchar*s,intchar*strichr(constchar*s,intstrchr()函數(shù)查找sc。如果找到了3.#include<stdio.h>main{staticchartemp[]="Hereitis...";charc=’s’;if(strchr(temp,printf("Character%cwasfoundinstring\n",printf("Nocharacterwasfoundin}參printf("Nocharacterwasfoundin}參strrchr(),strlen(),返回STRCMP,STRICMP#includeintstrcmp(constchar*s1,constchar*s2)intstricmp(constchar*s1,constchar*是否小于、等于或大于字符串s2,返回一個有符號整數(shù)。比較是字母的順例#include<stdio.h>main{intif((i=strcmp("ABC","ABc"))<0)printf("ABCislessthanABc\n");elseif(i>0)printf("ABCisequalto}參strlen(),strncmp(),strcpy(),返回-11STRCPY#include/*char*strcpy#include/*char*strcpy(char*s1,constchar*/*farchar*strcpy(farchar*s1,constchar*例#include<stdio.h>main{char*s1,*s2;s1=buffer;s2="...endofline";strcat(s1,s2);printf("Length=%d\n",printf("string=\"%s\"\n",}參strncpy(),strlen(),strcat(),返回STRCSPN#includesize_tstrcspn(constchar*s1,constchar*例#include<stdio.h>main{staticcharset[]=printf("%d\n",strcspn("xxxbcadefs",set));printf("%d\n",strcspn("1234567890",}printf("%d\n",strcspn("xxxbcadefs",set));printf("%d\n",strcspn("1234567890",}參見strspn()函數(shù)4.回STRLEN#includesize_tstrlen(constchar*例#include<stdio.h>main{char*s1,*s2;s1=buffer;s2="...endofline";strcat(s1,s2);printf("string=\"%s\"\n",buffer);}返回STRNCAT#include/*char*strncat(char*s1,constchar*s2,size_t/*farchar*strncat(farchar*s1,constchar*s2,size_t例例#include<stdio.h>main{char*s1,*s2;s1=buffer;s2="...endofline";strncat(s1,s2,5);printf("string=\"%s\"\n",buffer);}參strcpy(),strcmp(),strcat(),返回,#includeintstrncmp(constchar*s1,constchar*s2,size_tn)intstrnicmp(constchar*s1,constchar*s2,size_t3.#include<stdio.h>main{intif(i==0)printf("Bothstringsareelseif(i>printf("String2elseif(i>printf("String2lessthanstring1\n");printf("String2isgreaterthanstring}參strlen(),strcmp(),strcpy(),返回注意:其它C應用函數(shù)可以采用不同的字母順序。返回值為負、零或正,并不一定是-1或1STRNCPY#include/*char*strncpy(char*s1,constchar*s2,size_t/*farchar*strncpy(farchar*s1,constchar*s2,size_t這個函數(shù)將帶結束符的字符串s2拷貝到字符指針s1指向的字符數(shù)組。最多有n例#include<stdio.h>main{char*s1,*s2;s1=buffer;s2="...endofline";strcat(s1,s2);printf("string=\"%s\"\n",buffer);}參strcpy(),strcat(),strlen(),返回指針s1指向的目的STRPBRK#include/*constSTRPBRK#include/*constchar*strpbrk(constchar*s1,constchar*/*char*strpbrk(constchar*s1,constchar*例#include<stdio.h>main{char*str="Thisisastring.";while(str!=NULL){printf("%s\n",strstr=strpbrk(str+1,"aeiou"}}返回#include/*中級和低級系列單片機*/constchar*strrchr(char*sintc)constchar*strrichr(char*sint/*char*strrchr(char*s,intchar*strrichr(char*s,intstrrchr()函數(shù)完全一樣。3.#include<stdio.h>#includemain{char*str=main{char*str="Thisisastring.";while(str!=NULL){printf("%s\n",strstr=strrchr(str+1,}}參strchr(),strlen(),strcmp(),strcpy(),返回STRSPN#includesize_tstrspn(constchar*s1,constchar*strspn()函數(shù)返回字符串s1中包含的、完全由字符串s2組成的字符的例#include<stdio.h>main{printf("%d\n",strspn("Thisisastring","This"));printf("%d\n",strspn("Thisisastring",}參見strcspn()函數(shù)4.回 STRSTR,STRISTR#include/*constchar*strstr(constchar*s1,constchar*s2)constchar*stristr(constchar*s1,constchar*/*char*strstr(constchar*s1,constchar*char*strist

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論