我做了快一个月的超声波设计,想在超声波的基础上再加一个按键的功能但一直调试不好,以下是源代码,请大家帮忙看看是哪里有问题
单片机源程序如下:
- #include <REGX52.H>
- #include "intrins.h"
- sbit buzz=P0^6;
- sbit TX = P1^0;
- sbit RX = P1^1;
- sbit S7 = P3^0;
- sfr P4 = 0xC0;
- unsigned char code SMG_duanma[] = {0xc0,0xf9,0xa4,0xb0,
- 0x99,0x92,0x82,0xf8,
- 0x80,0x90,0x88,0x83,
- 0xc6,0xa1,0x86,0x8e,
- 0xbf};
- unsigned char Segbuff[] = {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
- unsigned int distance = 0;
- bit flag = 0;
- void Delay12us(void)
- {
- unsigned char data i;
- _nop_();
- _nop_();
- i = 33;
- while (--i);
- }
- void DelayK(unsigned char t)
- {
- while(t--);
- }
- void SelectHC573(unsigned char channel)
- {
- switch(channel)
- {
- case 0 : P2 = (P2 & 0X1F) | 0X00;break;
- case 4 : P2 = (P2 & 0X1F) | 0X80;break;
- case 5 : P2 = (P2 & 0X1F) | 0Xa0;break;
- case 6 : P2 = (P2 & 0X1F) | 0Xc0;break;
- case 7 : P2 = (P2 & 0X1F) | 0Xe0;break;
- }
- }
- void InitSystem()
- {
- SelectHC573(5);
- P0 = 0x00;
- SelectHC573(4);
- P0 = 0xFF;
- SelectHC573(0);
- }
- void DispalySMG_Bit(unsigned char pos, unsigned char dat)
- {
- SelectHC573(7);
- P0 = 0xff;
- SelectHC573(6);
- P0 = 0x01 << pos;
- SelectHC573(0);
- P0 = 0xff;
- SelectHC573(7);
- P0 = dat;
- SelectHC573(0);
- }
- void Timer1_Init()
- {
- TMOD = 0X00;
- TH1 = (65536 - 1000) / 256;
- TL1 = (65536 - 1000) % 256;
- EA = 1;
- ET1 = 1;
- TR1 = 1;
- }
- void Measure_Distance()
- {
- unsigned char num = 10;
- unsigned int time = 0;
-
- TMOD &= 0xf0;
- TH0 = (65536 - 13) / 256;
- TL0 = (65536 - 13) % 256;
- TR0 = 1;
- TX = 0;
- while(num--)
- {
- while(TF0 == 0);
- TX = ~TX;
- TF0 = 0;
- }
- TR0 = 0;
- TL0 = 0;
- TH0 = 0;
- TR0 = 1;
- while((RX == 1) && (TF0 == 0));
- TR0 = 0;
- if(TF0 == 0)
- {
- time = TH0;
- time = (time << 8) | TL0;
- distance = time*0.017;
- }
- else
- {
- TF0 = 0;
- distance = 999;
- }
- }
- void Display_Distance()
- {
- if(distance == 999)
- {
- Segbuff[0] = 0xff;
- Segbuff[1] = 0xff;
- Segbuff[2] = 0xff;
- Segbuff[3] = 0xff;
- Segbuff[4] = 0xff;
- Segbuff[5] = SMG_duanma[9];
- Segbuff[6] = SMG_duanma[9];
- Segbuff[7] = SMG_duanma[9];
- P2=(P2&0x1f)|0xa0;
- buzz=1;
- }
- else
- {
- P2=(P2&0x1f)|0xa0;
- buzz=0;
- Segbuff[0] = 0xff;
- Segbuff[1] = 0xff;
- Segbuff[2] = 0xff;
- Segbuff[3] = 0xff;
- Segbuff[4] = 0xff;
- Segbuff[5] = SMG_duanma[distance / 100];
- Segbuff[6] = SMG_duanma[(distance % 100) / 10];
- Segbuff[7] = SMG_duanma[distance % 10];
-
- }
- }
- void ScanKeys_Alone()
- {
- if(S7==0)
- {
- DelayK(100);
- if(S7==0)
- {
- InitSystem();
- Timer1_Init();
- if(flag)
- {
- Measure_Distance();
- flag = 0;
- }
- Display_Distance();
- while(S7==0);
- TX = 1;
- }
- }
- }
- void Timer1_Service() interrupt 3
- {
-
- static unsigned char pos = 0;
- static unsigned char t_1ms = 0;
- t_1ms++;
- switch(pos)
- {
- case 0 : DispalySMG_Bit(pos, Segbuff[0]);pos++;P0 = 0XFF;break;
- case 1 : DispalySMG_Bit(pos, Segbuff[1]);pos++;P0 = 0XFF;break;
- case 2 : DispalySMG_Bit(pos, Segbuff[2]);pos++;P0 = 0XFF;break;
- case 3 : DispalySMG_Bit(pos, Segbuff[3]);pos++;P0 = 0XFF;break;
- case 4 : DispalySMG_Bit(pos, Segbuff[4]);pos++;P0 = 0XFF;break;
- case 5 : DispalySMG_Bit(pos, Segbuff[5]);pos++;P0 = 0XFF;break;
- case 6 : DispalySMG_Bit(pos, Segbuff[6]);pos++;P0 = 0XFF;break;
- case 7 : DispalySMG_Bit(pos, Segbuff[7]);pos = 0;P0 = 0XFF;break;
- }
- if(t_1ms == 200)
- {flag = 1;t_1ms = 0;}
- }
- void main()
- {
- P2=(P2&0x1f)|0xa0;
- buzz=0;
- while(1)
- {
- ScanKeys_Alone();
- }
- }
复制代码
|