超聲波模塊單片機程序_第1頁
超聲波模塊單片機程序_第2頁
超聲波模塊單片機程序_第3頁
超聲波模塊單片機程序_第4頁
超聲波模塊單片機程序_第5頁
已閱讀5頁,還剩18頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1.超聲波測距〃晶振=8M//MCU=STC10F04XE//P0.0-P0.6共陽數(shù)碼管引腳//Trig=P1A0//Echo=P3A2#include<reg52.h> //包括一個52標準內(nèi)核的頭文件#defineucharunsignedchar//定義一下方便使用#defineuintunsignedint#defineulongunsignedlongsfrCLK_DIV=0x97;//為STC單片機定義,系統(tǒng)時鐘分頻〃為STC單片機的IO口設置地址定義sfrP0M1=0X93;sfrP0M0=0X94;sfrP1M1=0X91;sfrP1M0=0X92;sfrP2M1=0X95;sfrP2M0=0X96;sbitTrig=P1A0;//產(chǎn)生脈沖引腳sbitEcho=P3A2;//回波引腳sbittest=PIT;//測試用引腳ucharcodeSEG7[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};〃數(shù)碼管0-9uintdistance[4];//測距接收緩沖區(qū)ucharge,shi,bai,temp,flag,outcomeH,outcomeL,i;//自定義寄存器bitsucceed_flag;//測量成功標志voidconversion(uinttemp_data);voiddelay_20us();//函數(shù)聲明//函數(shù)聲明//voidpai_xu();voidmain(void)//主程序{uintdistance_data,a,b;ucharCONT_1;CLK_DIV=0X03;//系統(tǒng)時鐘為1/8晶振(pdf-45頁)P0M1=0; //將io口設置為推挽輸出P1M1=0;P2M1=0;P0M0=0XFF;P1M0=0XFF;P2M0=0XFF;i=0;flag=0;test=0;Trig=0; //首先拉低脈沖輸入引腳TMOD=0x11; //定時器0,定時器1,16位工作方式TR0=1; //啟動定時器0IT0=0; //由高電平變低電平,觸發(fā)外部中斷ET0=1; //打開定時器0中斷//ET1=1; //打開定時器1中斷EX0=0; //關閉外部中斷EA=1; //打開總中斷0while(1) //程序循環(huán){EA=0;Trig=1;delay_20us();Trig=O; 〃產(chǎn)生一個20us的脈沖,在Trig引腳while(Echo==0);//等待Echo回波引腳變高電平succeed_flag=0;//清測量成功標志EX0=1; //打開外部中斷TH1=0; //定時器1清零TL1=0; //定時器1清零TF1=0; //TR1=1; //啟動定時器1EA=1;while(TH1<30);//等待測量的結果,周期65.535毫秒(可用中斷實現(xiàn))TR1=0; //關閉定時器1EX0=0; //關閉外部中斷if(succeed_flag==1){distance_data=outcomeH; //測量結果的高8位distance_data<<=8; //放入16位的高8位distance_data=distance_data|outcomeL;〃與低8位合并成為16位結果數(shù)據(jù)distance_data*=12; //因為定時器默認為12分頻distance_data/=58; //微秒的單位除以58等于厘米} //為什么除以58等于厘米,Y米=(X秒*344)/2//X秒=(2*Y米)/344==》X秒=0.0058*Y米==》厘米=微秒/58if(succeed_flag==0){distance_data=0;test=!test;//沒有回波則清零//測試燈變化}/// distance[i]=distance_data;//將測量結果的數(shù)據(jù)放入緩沖區(qū)/// i++;///if(i==3)///{///distance_data=(distance[0]+distance[1]+distance[2]+distance[3])/4;/// pai_xu();/// distance_data=distance[1];a=distance_data;if(b==a)CONT_1=0;if(b!=a)CONT_1++;if(CONT_1>=3){CONT_1=0;///b=a;conversion(b);}i=0;///}}}////外部中斷0,用做判斷回波電平INTO_()interrupt0//外部中斷是0號{outcomeH=TH1; //取出定時器的值outcomeL=TL1; //取出定時器的值succeed_flag=1; //至成功測量的標志EX0=0; //關閉外部中斷}//定時器0中斷,用做顯示timer0()interrupt1//定時器0中斷是1號{TH0=0xfd;//寫入定時器0初始值TL0=0x77;switch(flag){case0x00:P0=ge;P2=0xfd;flag++;break;case0x01:P0=shi;P2=0xfe;flag++;break;case0x02:P0=bai;P2=0xfb;flag=0;break;}}/*//定時器1中斷,用做超聲波測距計時timer1()interrupt3//定時器0中斷是1號{TH1=0;TL1=0;}*///顯示數(shù)據(jù)轉換程序voidconversion(uinttemp_data){ucharge_data,shi_data,bai_data;bai_data=temp_data/100;temp_data=temp_data%100; //取余運算shi_data=temp_data/10;temp_data=temp_data%10; //取余運算ge_data=temp_data;bai_data=SEG7[bai_data];shi_data=SEG7[shi_data];ge_data=SEG7[ge_data];EA=0;bai=bai_data;shi=shi_data;ge=ge_data;EA=1;}voiddelay_20us(){ucharbt;for(bt=0;bt<100;bt++);}/*voidpai_xu(){uintt;if(distance[0]>distance[1]){t=distance[0];distance[0]=distance[1];distance[1]=t;}/*交換值if(distance[0]>distance[2]){t=distance[2];distance[2]=distance[0];distance[0]=t;}/*交換值if(distance[1]>distance[2]){t=distance[1];distance[1]=distance[2];distance[2]=t;}/*交換值}*/2?超聲波測距LCD1602顯示/*SMC1602A(16*2)模擬口線接線方式連接線圖:|LCM-----51| LCM 51 |LCM-----|--|---51|DB0-----P1.0|DB4- P1.4|RW-- P3.4||DB1-----P1.1|DB5- P1.5|RS ---P3.3||DB2-----P1.2|DB6- P1.6|E --P3.5||DB3-----P1.3|DB7- P1.7|VLCD接1K電阻至1」GND|接線:模塊TRIG接P3.7ECH0接P3.6本程序源碼只供學習參考,不得應用于商業(yè)用途,如有需要請聯(lián)系作者。[注:Stc89c52使用12M或11.0592M晶振實測使用11.0592M]=============================================================*/#include<AT89x51.H> //器件配置文件#include<intrinS.h>#defineRXP3_6#defineTXP3_7#defineLCM_RWP2_3//定義LCD引腳#defineLCM_RSP2_4#defineLCM_EP2_2#defineLCM_DataP1#defineKey_DataP3_3//定義Keyboard引腳#defineKey_CLKP3_2#defineBuSy 0x80//用于檢測LCM狀態(tài)字中的BuSy標識voidLCMInit(void);voidDiSplayOneChar(unSignedcharX,unSignedcharY,unSignedcharDData);voidDiSplayLiStChar(unSignedcharX,unSignedcharY,unSignedcharcode*DData);voidDelay5MS(void);voidDelay400MS(void);voidDecode(unsignedcharScanCode);voidWriteDataLCM(unsignedcharWDLCM);voidWriteCommandLCM(unsignedcharWCLCM,BuysC);unsignedcharReadDataLCM(void);unsignedcharReadStatusLCM(void);unsignedcharcodemcustudio[]={"==RangeFinder=="};unsignedcharcodeemail[]= {"heyaodz@163.com"};unsignedcharcodeCls[]= {""};unsignedcharcodeASCII[15]={'0','1','2','3','4','5','6','7','8','9','.','-','M'};staticunsignedcharDisNum=0;//顯示用指針unsignedinttime=0;unsignedlongS=0;bitflag=0;unsignedchardisbuff[4]={0,0,0,0,};//寫數(shù)據(jù)voidWriteDataLCM(unsignedcharWDLCM){ReadStatusLCM();//檢測忙LCM_Data=WDLCM;LCM_RS=1;LCM_RW=0;LCM_E=0;//若晶振速度太高可以在這后加小的延時LCM_E=0;//延時LCM_E=1;}//寫指令voidWriteCommandLCM(unsignedcharWCLCM,BuysC)//BuysC為0時忽略忙檢測{if(BuysC)ReadStatusLCM();//根據(jù)需要檢測忙LCM_Data=WCLCM;LCM_RS=0;LCM_RW=0;LCM_E=0;LCM_E=0;LCM_E=1;//讀數(shù)據(jù)unsignedcharReadDataLCM(void)LCM_RS=1;LCM_RW=1;LCM_E=0;LCM_E=0;LCM_E=1;return(LCM_Data);}//讀狀態(tài)unsignedcharReadStatusLCM(void){LCM_Data=0xFF;LCM_RS=0;LCM_RW=1;LCM_E=0;LCM_E=0;LCM_E=1;while(LCM_Data&Busy);//檢測忙信號return(LCM_Data);}voidLCMInit(void)//LCM初始化{LCM_Data=0;WriteCommandLCM(0x38,0);//三次顯示模式設置,不檢測忙信號Delay5Ms();WriteCommandLCM(0x38,0);Delay5Ms();WriteCommandLCM(0x38,0);Delay5Ms();WriteCommandLCM(0x38,1);//顯示模式設置,開始要求每次檢測忙信號WriteCommandLCM(0x08,1);//關閉顯示W(wǎng)riteCommandLCM(0x01,1);//顯示清屏WriteCommandLCM(0x06,1);//顯示光標移動設置WriteCommandLCM(0x0F,1);//顯示開及光標設置}//按指定位置顯示一個字符voidDisplayOneChar(unsignedcharX,unsignedcharY,unsignedcharDData){Y&=0x1;X&=0xF;//限制X不能大于15,Y不能大于1if(Y)X|=0x40;//當要顯示第二行時地址碼+0x40;X|=0x80;//算出指令碼WriteCommandLCM(X,1);//發(fā)命令字WriteDataLCM(DData);//發(fā)數(shù)據(jù)}//按指定位置顯示一串字符voidDisplayListChar(unsignedcharX,unsignedcharY,unsignedcharcode*DData){unsignedcharListLength;ListLength=0;Y&=0x1;X&=0xF;//限制X不能大于15,Y不能大于1while(DData[ListLength]>0x19)//若到達字串尾則退出{if(X<=0xF)//X坐標應小于0xF{DisplayOneChar(X,Y,DData[ListLength]);//顯示單個字符ListLength++;X++;}}}//5ms延時voidDelay5Ms(void){unsignedintTempCyc=5552;while(TempCyc--);}//400ms延時voidDelay400Ms(void){unsignedcharTempCycA=5;unsignedintTempCycB;while(TempCycA--){TempCycB=7269;while(TempCycB--);};voidConut(void){time=TH0*256+TL0;TH0=0;TL0=0;S=(time*1.8)/10; //算出來是MMif((S>=7000)||flag==1)//超出測量范圍顯示“-”{flag=0;DisplayOneChar(0,1,ASCII[11]);DisplayOneChar(1,1,ASCII[10]); //顯示點DisplayOneChar(2,1,ASCII[11]);DisplayOneChar(3,1,ASCII[11]);DisplayOneChar(4,1,ASCII[11]);DisplayOneChar(5,1,ASCII[12]); //顯示M}else{disbuff[0]=S/1000;disbuff[1]=S/100%10;disbuff[2]=S/10%10;disbuff[3]=S%10;DisplayOneChar(0,1,ASCII[disbuff[0]]);DisplayOneChar(1,1,ASCII[10]); //顯示點DisplayOneChar(2,1,ASCII[disbuff[1]]);DisplayOneChar(3,1,ASCII[disbuff[2]]);DisplayOneChar(4,1,ASCII[disbuff[3]]);DisplayOneChar(5,1,ASCII[12]); //顯示M}}voidzd0()interrupt1 //T0中斷用來計數(shù)器溢出,超過測距范圍{flag=1; //中斷溢出標志RX=0;}voidStartModule() //啟動模塊{TX=1; //啟動一次模塊_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();TX=0;}//開啟計數(shù)〃當//開啟計數(shù)〃當RX為1計數(shù)并等待//關閉計數(shù)//計算TR0=1;while(RX);TR0=0;Conut();voiddelayms(unsignedintms){unsignedchari=100,j;for(;ms;ms--){while(--i){j=10;while(--j);}*/*/voidmain(void){unsignedintvalA;unsignedcharTempCyc;Delay400Ms();//啟動等待,等LCM講入工作狀態(tài)LCMInit();//LCM初始化Delay5Ms();//延時片刻(可不要)DisplayListChar(0,0,mcustudio);DisplayListChar(0,1,email);ReadDataLCM();〃測試用句無意義for(TempCyc=0;TempCyc<10;TempCyc++)Delay400Ms();//延時DisplayListChar(0,1,Cls);TMOD=Ox01; 〃設TO為方式1,GATE=1;TH0=0;TL0=0;ETO=1; //允許TO中斷EA=1; //開啟總中斷while(1){delayms(6O);RX=1;StartModule();for(valA=751O;valA>O;valA--){if(RX==1){Timer_Count();}}}}3?超聲波測距LCD12864顯示//*Name:LCD12864(St7920/St7921)+超聲波測距模塊+STC89C52 *//*hy-SRFO5超聲波測距模塊DEMO程序*//*晶振:11.0592M *//*接線:模塊TRIG接P3.7ECHO接P3.6

*//*RS接P24;*//*RW接P23;*//*E接P22;*//*PSB接P27;*//#include<AT89X51.H>#include<intrins.h>//引腳定義sbit RX =P3A6;sbit TX =P3A7;sbitRS=P2A4;sbitRW=P2T;sbitE=P2A2;sbitRES=P2A5;sbitPSB=P2A7;sbitPAUSE=P2A6;#defineDataPortP1//MCUP1< >LCM#defineDataPortP1//MCUP1< >LCM//FunctionDefinition函數(shù)聲明voiddelayms(unsignedintms);voidDelay(intnum);voidInit_DS18B20(void);unsignedcharReadOneChar(void);voidWriteOneChar(unsignedchardat);unsignedintReadTemperature(void);voidclock_out(unsignedchardd);unsignedcharclock_in(void);unsignedcharread_clock(unsignedcharord);voidwrite_clock(unsignedcharord,unsignedchardd);voidDisp(void);voidid_case1_key(void);voidid_case2_key(void);voidSet_time(unsignedcharsel,bitsel_1);voidTimer0_Init(void);voidInit_1302(void);voidSet_Bell(unsignedcharsel,bitsel_1);voidBell(void);//12864voidWrite_char(bitstart,unsignedcharddata);voidSend_byte(unsignedcharbbyte);voidDelaynms(unsignedintdi);voidLcd_init(void);voidDisp_img(unsignedchar*img);voidLCD_Write_string(unsignedcharX,unsignedcharY,unsignedchar*s);voidLCD_set_xy(unsignedcharx,unsignedchary);unsignedcharcodenum[]={"0123456789:.-"};unsignedcharcodewaves[]={"超聲波測距系統(tǒng)"};unsignedcharcodeznwk[]={"智能微控工作室"};unsignedcharcodeCM[]={"M"};unsignedinttime=0;longS=0;bitflag=0;unsignedchardisbuff[4]={0,0,0,0,};unsignedcharcodelogo[]={/*--調(diào)入了一幅圖像:logo.bmp--*//*--寬度x高度=128x64歡迎使用本產(chǎn)品LOGO--*//*--調(diào)入了一幅圖像:C:\DocumentsandSettings\lwd\桌面\新建文件夾\X.bmp--*//*--寬度x高度=128x64--*/0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x0C,0x00,0x00,0x40,0x70,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,0x1E,0x00,0x00,0x60,0x70,0x00,0x00,0x0F,0xE0,0x00,0x00,0x00,0x01,0xC0,0x00,0x60,0x3C,0x00,0x00,0xE0,0x60,0x00,0x1F,0xFF,0xE0,0x00,0x00,0x00,0x03,0x80,0x00,0x70,0x70,0x40,0x00,0xE0,0x7E,0x00,0x1F,0xE0,0xC0,0x00,0x00,0x00,0x03,0x00,0x00,0x73,0xC3,0xF0,0x01,0xC3,0xFC,0x00,0x38,0xE1,0xC0,0x00,0x00,0x03,0xC6,0x3C,0x00,0x33,0x9F,0xF0,0x03,0x8F,0xC0,0x00,0x30,0xC1,0xC0,0x00,0x00,0x3F,0xEF,0xFE,0x00,0x03,0x1C,0xE0,0x07,0x01,0xCC,0x00,0x31,0xF1,0x80,0x00,0x00,0x79,0xDF,0xBC,0x00,0x06,0x18,0xC0,0x0E,0x01,0xFE,0x00,0x77,0xF9,0x80,0x00,0x00,0x01,0xB0,0x30,0x01,0xC6,0x19,0xC0,0x1E,0x7F,0xDE,0x00,0x6F,0x83,0x80,0x00,0x00,0x23,0xAC,0x60,0x0F,0xE6,0xF1,0x80,0x3E,0x7B,0x38,0x00,0x61,0x83,0x00,0x00,0x00,0x37,0x0E,0x00,0x0F,0xC7,0xB7,0x80,0xEC,0x63,0x30,0x00,0xE3,0x03,0x00,0x00,0x00,0x3E,0x0E,0x00,0x01,0x8F,0x37,0x01,0xCC,0x6F,0xF0,0x00,0xC7,0xF7,0x00,0x00,0x00,0x1E,0x0C,0x00,0x03,0x1E,0x66,0x03,0x0C,0x7F,0x00,0x01,0xFF,0xE6,0x00,0x00,0x00,0x1E,0x1C,0x00,0x03,0x1C,0x60,0x02,0x19,0xAC,0x00,0x01,0x96,0x06,0x00,0x00,0x00,0x3E,0x3E,0x00,0x03,0x18,0x60,0x00,0x19,0xFC,0x00,0x03,0x86,0x0E,0x00,0x00,0x00,0x7E,0x36,0x00,0x03,0x00,0xC0,0x00,0x38,0x78,0x00,0x03,0x06,0x0C,0x00,0x00,0x00,0xE6,0x67,0x00,0x03,0x00,0xC0,0x00,0x30,0x78,0x00,0x06,0x0C,0x0C,0x00,0x00,0x01,0xC6,0xC3,0x80,0x7F,0xF0,0xC0,0x00,0x30,0xFE,0x00,0x0E,0x0C,0x1C,0x00,0x00,0x03,0x81,0x83,0xC0,0x78,0xFF,0x80,0x00,0x61,0xCF,0x80,0x1C,0x08,0x18,0x00,0x00,0x06,0x07,0x03,0xE0,0x00,0x1F,0xFF,0xE0,0x67,0x07,0xE0,0x38,0x18,0xF8,0x00,0x00,0x08,0x0E,0x01,0xF8,0x00,0x07,0xFF,0x80,0xCE,0x03,0xF8,0x60,0x00,0x70,0x00,0x00,0x00,0x18,0x01,0xF8,0x00,0x01,0xFC,0x00,0xC0,0x01,0xFC,0xC0,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x00,0x00,0x00,0x0C,0xE0,0x00,0x01,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x00,0x00,0x3F,0xE0,0x00,0xFF,0xF0,0x00,0x38,0x00,0xE0,0x00,0x00,0x00,0x0C,0x00,0x00,0x0F,0xFF,0x80,0x00,0xF8,0xF0,0x00,0x38,0x00,0xE0,0x00,0x00,0x00,0x0C,0xF0,0x00,0x0F,0xC7,0x00,0x00,0x60,0xE0,0x00,0x38,0x00,0xE0,0x00,0x00,0x00,0x1F,0xF0,0x00,0x01,0x8E,0x00,0x00,0x60,0xC0,0x00,0x70,0x01,0xC0,0x00,0x00,0x07,0xFF,0x80,0x00,0x01,0x8C,0x00,0x00,0xE1,0x80,0x00,0x70,0x01,0xC0,0x00,0x00,0x07,0xF8,0x00,0x00,0x01,0x98,0x00,0x00,0xDF,0xC0,0x00,0x70,0x01,0xC0,0x00,0x00,0x00,0x78,0x00,0x00,0x01,0xBF,0xE0,0x00,0xFF,0x00,0x00,0x60,0x01,0x80,0x00,0x00,0x00,0xFC,0x00,0x00,0x7F,0xFF,0xE0,0x00,0xC0,0x00,0x00,0xE0,0x03,0x80,0x00,0x00,0x01,0xFE,0x00,0x00,0x7F,0xF8,0x00,0x00,0x00,0x00,0x00,0xC0,0x03,0x00,0x00,0x00,0x03,0xB7,0x00,0x00,0x60,0x00,0x00,0x00,0x00,0x3C,0x00,0xC0,0x03,0x00,0x00,0x00,0x07,0x67,0x00,0x00,0x60,0x00,0x00,0x03,0x9F,0xFE,0x00,0x80,0x02,0x00,0x00,0x00,0x0E,0x63,0x80,0x00,0xE0,0x00,0x01,0xFF,0xCF,0x1E,0x01,0x80,0x06,0x00,0x00,0x00,0x1C,0xE1,0xE0,0x00,0xC0,0x00,0x00,0xE3,0x9C,0x38,0x01,0x80,0x06,0x00,0x00,0x00,0x38,0xC1,0xF8,0x01,0xC0,0x00,0x00,0xC3,0x18,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0xE0,0xFC,0xFE,0x03,0x80,0x00,0x01,0xC7,0x18,0x60,0x00,0x00,0x00,0x00,0x00,0x01,0xDF,0xF8,0xFE,0x03,0x00,0x00,0x01,0x8E,0x1B,0xE0,0x07,0x00,0x1C,0x00,0x00,0x07,0x07,0x80,0x00,0x07,0x00,0x00,0x01,0xFF,0x3F,0xE0,0x07,0x00,0x1C,0x00,0x00,0x0C,0x03,0x80,0x00,0x0E,0x00,0x00,0x01,0xF0,0x38,0x00,0x0F,0x00,0x3C,0x00,0x00,0x00,0x03,0x00,0x00,0x1C,0x00,0x00,0x01,0x00,0x20,0x00,0x06,0x00,0x18,0x00,0x00,0x00,0x03,0x00,0x00,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x01,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};

/voidLcd_init(void)//初始化LCDdelayms(40);PSB=1;delayms(1);RES=0;delayms(1);RES=1;//大于40MS的延時程序//設置為8BIT并口工作模式//延時//復位//延時//復位置高delayms(1);Write_char(0,0x30); //選擇基本指令集delayms(1); //延時大于100usWrite_char(0,0x30); //選擇8bit數(shù)據(jù)流delayms(1); //延時大于37usWrite_char(0,0x0c); //開顯示(無游標、不反白)delayms(1); //延時大于100usWrite_char(0,0x01); //清除顯示,并且設定地址指針為00Hdelayms(1); //延時大于10msWrite_char(0,0x06); //指定在資料的讀取及寫入時,設定游標的移動方向及指定顯示的移位,光標從右向左加1位移動delayms(1);//delayms(1);//延時大于100us//* 檢測忙位 */voidCheck_Busy(){RS=0;RW=1;E=1;DataPort=0xff;while((DataPort&0x80)==0x80);〃忙則等待E=0;}voidWrite_char(bitstart,unsignedcharddata)//寫指令或數(shù)據(jù){//unsignedcharstart_data,Hdata,Ldata;if(start==0){Check_Busy();RS=0;RW=0;E=1;DataPort=ddata;//delayms(5);E=0;//delayms(5);}else{Check_Busy();RS=1;RW=0;E=1;DataPort=ddata;//delayms(5);E=0;//delayms(5);}}/voidDelaynms(unsignedintdi)//延時{unsignedintda,db;for(da=0;da<di;da++)for(db=0;db<10;db++);}/voidDisp_img(unsignedchar*img)//圖形方式12864顯示字模221橫向取膜{unsignedchari,j;unsignedintk=0;Write_char(0,0x36);//圖形方式for(i=0;i<32;i++){Write_char(0,0x80+i);Write_char(0,0x80);for(j=0;j<16;j++){Write_char(1,img[k++]);}}for(i=0;i<32;i++){Write_char(0,0x80+i);Write_char(0,0x88);for(j=0;j<16;j++){Write_char(1,img[k++]);}}}/voidClr_Scr(void)〃清屏函數(shù){Write_char(0,0x01);}voidLCD_set_xy(unsignedcharx,unsignedchary){ 〃設置LCD顯示的起始位置,X為行,Y為列unsignedcharaddress;switch(x){case0:address=0x80+y;break;case1:address=0x80+y;break;case2:address=0x90+y;break;case3:address=0x88+y;break;case4:address=0x98+y;break;default:address=0x80+y;break;}Write_char(0,address);}/voidLCD_Write_string(unsignedcharX,unsignedcharY,unsignedchar*s){//中英文字符串顯示函數(shù)LCD_set_xy(X,Y);while(*s){Write_char(1,*s);s++;Delaynms(1);}}/voidLCD_Write_number(unsignedchars)//數(shù)字顯示函數(shù){Write_char(1,num[s]);Delaynms(1);}voidLcd_Mark2(void){Clr_Scr();〃清屏LCD_Write_string(1,0,znwk);//LCD_Write_string(2,0,waves);//LCD_Write_string(3,7,CM);//}voidConut(void){time=TH0*256+TL0;TH0=0;TL0=0;S=(time*1.8)/10; //算出來是mMif((S>=7000)||flag==1)//超出測量范圍顯示“-”{flag=0;LCD_set_xy(3,3);LCD_Write_number(13);LCD_Write_number(12);LCD_set_xy(3,4);LCD_Write_number(13);LCD_Write_number(13);LCD_Write_number(13);}else{disbuff[0]=S/1000;disbuff[1]=S/100%10;disbuff[2]=S/10%10;disbuff[3]=S%10;LCD_set_xy(3,3);LCD_Write_number(disbuff[0]);LCD_Write_number(12);LCD_set_xy(3,4);LCD_Write_number(disbuff[1]);LCD_Write_number(disbuff[2]);LCD_Write_number(disbuff[3]);}}voiddelayms(unsignedintms){unsignedchari=100,j;for(;ms;ms--){while(--i){j=10;w

溫馨提示

  • 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

提交評論