|
|
400黑幣
樓主單片機(jī)小白一名。正在學(xué)習(xí)stc15w4k32s4這一款單片機(jī)。目前想利用超聲波傳感器做一個(gè)俯臥撐計(jì)數(shù)的裝置。但是遇到了種種問(wèn)題,經(jīng)過(guò)長(zhǎng)時(shí)間的調(diào)試仍未好轉(zhuǎn),誠(chéng)心求大佬解答!
項(xiàng)目具體描述如下:stc15單片機(jī)與兩個(gè)HC-SR04超聲波連接,通電后超聲波測(cè)距并在12864液晶上實(shí)時(shí)顯示,通過(guò)監(jiān)測(cè)超聲波返回的距離變化來(lái)統(tǒng)計(jì)做了俯臥撐的個(gè)數(shù)。當(dāng)距離超出一定范圍時(shí)蜂鳴器就會(huì)報(bào)警,提示別人姿勢(shì)錯(cuò)誤。測(cè)量的距離同時(shí)通過(guò)串口返回給電腦。
遇到的問(wèn)題:目前給單片機(jī)上電后在絕大多數(shù)情況下單片機(jī)無(wú)法正常工作,液晶只亮不顯示,串口助手無(wú)數(shù)據(jù)返回,蜂鳴器不響。像是沒(méi)有進(jìn)入到主函數(shù)中,單片機(jī)像是“僵死”了。樓主不知道是代碼的問(wèn)題還是硬件焊接的問(wèn)題實(shí)物圖如下:
51hei圖片20201115150335.jpg (517.26 KB, 下載次數(shù): 67)
下載附件
DAP下載器
2020-11-15 15:18 上傳
51hei圖片20201115150327.jpg (371.92 KB, 下載次數(shù): 72)
下載附件
用杜邦線連接
2020-11-15 15:18 上傳
51hei圖片20201115150320.jpg (228.48 KB, 下載次數(shù): 63)
下載附件
液晶不顯示
2020-11-15 15:18 上傳
單片機(jī)代碼如下:
- #include<stc15w.h>
- #include<intrins.h>
- #include<stdio.h>
- #include<math.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define Part P1 //P0接8位數(shù)據(jù)線
- sbit LCD_RS=P2^6; //定義12864液晶RS端,寄存器選擇信號(hào) H:數(shù)據(jù)寄存器 L:指令寄存器
- sbit LCD_RW=P2^5; //定義12864液晶RW端,讀/寫信號(hào) H:讀 L:寫
- sbit LCD_EN=P2^4; //定義12864液晶LCDEN端, 片選信號(hào) 下降沿觸發(fā),鎖存數(shù)據(jù)
- sbit LCD_PSB=P2^3; //定義12864液晶PSB端, H:并行 L:串行
- sbit Echo1=P0^0;//超聲波測(cè)距模塊Echo1連接的IO
- sbit Trig1=P0^1;//超聲波測(cè)距模塊Trig1連接的IO
- sbit Echo2=P0^2;//超聲波測(cè)距模塊Echo2連接的IO
- sbit Trig2=P0^3;//超聲波測(cè)距模塊Trig2連接的IO
- sbit Beep=P2^7;//蜂鳴器連接的IO口
- uchar show1[]="chest:0000mm";
- uchar show2[]="waist:0000mm";
- uchar show3[]="gap:000mm";
- uchar show4[]="times:000";
- uint xdata distance1=600,distance2=600,time,num=0;
- uchar xdata urdat,k=1;
- float xdata ratio;
- char xdata m=1;
- int xdata gap;
- void SendData(unsigned char dat);
- void delayms(uint xms)
- {
- uint i,j;
- for(i=xms;i>0;i--)
- for(j=110;j>0;j--);
- }
- void Delay20us() //@11.0592MHz
- {
- unsigned char i;
- _nop_();
- _nop_();
- _nop_();
- i = 52;
- while (--i);
- }
- //====================================
- //LCD忙檢測(cè)函數(shù)
- bit lcd_busy()
- {
- bit result;
- LCD_RS=0;
- LCD_RW=1;
- LCD_EN= 1;
- result=(bit)(Part & 0x80);
- LCD_EN=0;
- return result;
- }
- //=====================================
- //液晶寫命令函數(shù)
- void lcd_write_cmd(uchar com)
- {
- while(lcd_busy());
- LCD_RS=0; //選擇指令寄存器
- LCD_RW=0; //寫
- LCD_EN=0;
- Part=com; //指令值賦給P1口
- delayms(5);
- LCD_EN=1;
- delayms(5);
- LCD_EN=0;
- }
- //=====================================
- //液晶寫一個(gè)字符數(shù)據(jù)函數(shù)
- void lcd_write_dat(uchar date)
- {
- while(lcd_busy());
- LCD_RS=1; //選擇數(shù)據(jù)寄存器
- LCD_RW=0; //寫
- LCD_EN=0;
- P1=date; //數(shù)據(jù)值賦給P1口
- delayms(5);
- LCD_EN=1;
- delayms(5);
- LCD_EN=0;
- }
- //=====================================
- //液晶寫一個(gè)字符串函數(shù)
- //void lcd_write_string(uchar *str)
- //{
- //while(*str!='\0') //未結(jié)束
- // {
- // lcd_write_dat(*str++);
- // delayms(5);
- // }
- //}
- //=====================================
- //液晶顯示位置函數(shù)
- void lcd_pos(uchar x,uchar y) //從第X行的第Y位置開始顯示
- {
- uchar pos;
- if(x==1) //第一行
- { x=0x80;}
- else if(x==2) //第二行
- { x=0x90;}
- else if(x==3) //第三行
- { x=0x88;}
- else if(x==4) //第四行
- { x=0x98;}
- pos=x+y-1; //首地址為0X80
- lcd_write_cmd(pos);
- }
- //=====================================
- //液晶初始化函數(shù)
- void lcd_init()
- {
- LCD_PSB=1; //并行方式
- lcd_write_cmd(0x30);
- delayms(5);
- lcd_write_cmd(0x0c); //開顯示,不顯示光標(biāo)
- delayms(5);
- lcd_write_cmd(0x06); //寫一個(gè)字符后地址指針自動(dòng)加1
- delayms(5);
- lcd_write_cmd(0x01); //清屏
- delayms(5);
- }
- //=====================================
- void Display1()
- {
- uint xdata i;
- lcd_pos(1,1);
- for(i=0;i<13;i++)
- {lcd_write_dat(show1[i]); }
- }
- void Display2()
- {
- uint xdata i;
- lcd_pos(2,1);
- for(i=0;i<13;i++)
- {lcd_write_dat(show2[i]); }
- }
- void Display3()
- {
- uint xdata i;
- lcd_pos(3,1);
- for(i=0;i<10;i++)
- {lcd_write_dat(show3[i]); }
- }
- void Display4()
- {
- uint xdata i;
- lcd_pos(4,1);
- for(i=0;i<9;i++)
- {lcd_write_dat(show4[i]); }
- }
- void UartInit(void)
- {
- SCON=0x50;//工作方式選擇:8位UART波特率可變
- AUXR&=0x80;
- TMOD=0x00;
- TL1=0xe8;
- TH1=0xff;
- TR1=1;
- ES=1;
- EA=1;
- }//配置波特率,對(duì)數(shù)據(jù)采樣
- void Uart() interrupt 4 using 1//定時(shí)器1,16位自動(dòng)重裝模式
- {
- if(RI)
- {
- urdat=SBUF;//從緩沖區(qū)讀取數(shù)據(jù)
- RI=0;//響應(yīng)中斷請(qǐng)求后清零
- SendData(urdat);
- }
- }
- void SendData(unsigned char dat)
- {
- SBUF=dat;//將數(shù)據(jù)傳入PC機(jī)
- while(TI==0);//等待發(fā)送中斷請(qǐng)求
- TI=0;//響應(yīng)中斷后TI清零
- }
- char putchar(char c)
- {
- SendData(c);
- return c;
- }//輸出成對(duì)象
- void INT_init1 (void)
- {
- TMOD&=0x01;//定時(shí)器0,16位不可重裝載工作方式
- TH0=0;//定時(shí)器0清零
- TL0=0;//定時(shí)器0清零
- EA = 1;//總中斷開
- Trig1=0;//拉低為下次觸發(fā)做準(zhǔn)備
- Trig2=0;
- }
- void dist (void)//測(cè)量距離程序
- {
- if(m==1)
- {
- Trig1=1;
- Delay20us(); //延時(shí)20us
- Trig1=0; //Trig輸出20us高電平觸發(fā)測(cè)距
- while(Echo1==0); //等待Echo回波引腳變高電平
- TR0=1;//程序運(yùn)行到此處時(shí)說(shuō)明Echo腳變成了高電平,此時(shí)啟動(dòng)T0開始計(jì)時(shí)
- while(Echo1==1); //等待Echo回波引腳高電平結(jié)束
- TR0=0;//程序運(yùn)行到此處時(shí)說(shuō)明Echo腳變成了低電平,此時(shí)T0停止計(jì)時(shí)
- time=TH0*256+TL0;
- distance1=time*0.272; //測(cè)量距離
- TH0 = 0; //重置計(jì)時(shí)器
- TL0 = 0;
- }
- if(m==-1)
- {
- Trig2=1;
- Delay20us(); //延時(shí)20us
- Trig2=0; //Trig輸出20us高電平觸發(fā)測(cè)距
- while(Echo2==0); //等待Echo回波引腳變高電平
- TR0=1;//程序運(yùn)行到此處時(shí)說(shuō)明Echo腳變成了高電平,此時(shí)啟動(dòng)T0開始計(jì)時(shí)
- while(Echo2==1); //等待Echo回波引腳高電平結(jié)束
- TR0=0;//程序運(yùn)行到此處時(shí)說(shuō)明Echo腳變成了低電平,此時(shí)T0停止計(jì)時(shí)
- time=TH0*256+TL0;
- distance2=time*0.24; //測(cè)量距離
- TH0 = 0; //重置計(jì)時(shí)器
- TL0 = 0;
- }
- m=-m;//輪流測(cè)量距離12
- }
- void main()//主函數(shù)
- {
- P0M0=0x00;P0M1=0x00;
- P1M0=0x00;P1M1=0x00;
- P2M0=0x00;P2M1=0x00;
- P3M0=0x00;P3M1=0x00;//單片機(jī)IO口工作方式初始化
- INT_init1();//T0初始化
- UartInit();//串口初始化
- lcd_init();//液晶初始化
- while(1)
- {
- dist();//定時(shí)器測(cè)量距離
- printf("dis1=%d\n",distance1);
- printf("dis2=%d\n",distance2);
- gap=distance1-distance2;
- gap=abs(gap);
- ratio=distance1/distance2;
- printf("gap=%d",gap);
- if(gap<=200)
- {
- if(distance1<190&&distance2<180)
- {
- k=0;
- Beep=0;
- delayms(50);
- Beep=1;
- }
- else if(distance1>200&&distance2>250&&k==0)
- {
- k=1;
- Beep=0;
- delayms(50);
- Beep=1;
- num++;
- }
- // else continue;
- }
- else if(gap>200&&gap<400)
- {
- Beep=0;
- delayms(120);
- Beep=1;
- }
- // else
- // {
- // continue;
- // }
- printf("times=%d\n",num);
- printf(" ");
- show1[6]=distance1/1000+0x30;
- show1[7]=distance1%1000/100+0x30;
- show1[8]=distance1%1000%100/10+0x30;
- show1[9]=distance1%10+0x30;
- show2[6]=distance2/1000+0x30;
- show2[7]=distance2%1000/100+0x30;
- show2[8]=distance2%1000%100/10+0x30;
- show2[9]=distance2%10+0x30;
- show3[4]=abs(gap)/100+0x30;
- show3[5]=abs(gap)%100/10+0x30;
- show3[6]=abs(gap)%10+0x30;
- show4[7]=num/100+0x30;
- show4[8]=num%100/10+0x30;
- show4[9]=num%10+0x30;
- Display1();
- Display2();
- Display3();
- Display4();
- }
- }
復(fù)制代碼
樓主立志做一名工程師,努力學(xué)習(xí)電子制作,雖然在項(xiàng)目的制作中屢屢受挫,但是仍不氣餒,我相信總有好心人為我指點(diǎn)迷津,是否可以大致指出我的項(xiàng)目哪里有什么硬傷?出現(xiàn)上述問(wèn)題的可能原因是什么?如有則不勝感激!
|
最佳答案
查看完整內(nèi)容
1,你要用printf的話,串口初始化要加上 TI = 1;
2,while(Echo1==0);這里最好改成 while(Echo1==0 && TR0 == 1);超時(shí)判斷,因?yàn)槿绻暷K接收不到回聲,就會(huì)一直卡在while(Echo1==0);循環(huán)里面導(dǎo)致單片機(jī)卡死。
3,建議先單獨(dú)測(cè)試LCD屏幕能不能正常工作,再加上其他代碼,同理其他模塊得先確保能單獨(dú)工作的情況下,再把所有程序集成在一起。
4,超聲波測(cè)量周期建議100MS以上,否則會(huì)影響超聲波的下次測(cè)量結(jié)果,建議開個(gè)定時(shí) ...
|