#include "reg52.h"
#include "nokia5110.h"
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned int
#define MotorData P1 //步进电机控制接口定义
//#define WR P3^6
//define RD P3^7
#define Key_Data P3_3 //定义Keyboard引脚
#define Key_CLK P3_2
sbit led=P2^1;
#define Busy 0x80 //用于检测LCM状态字中的Busy标识
void LCMInit(void);
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData);
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData);
void Delay5Ms(void);
void Delay400Ms(void);
void Decode(unsigned char ScanCode);
void WriteDataLCM(unsigned char WDLCM);
void WriteCommandLCM(unsigned char WCLCM,BuysC);
unsigned char ReadDataLCM(void);
unsigned char ReadStatusLCM(void);
unsigned char code mcustudio[] ={"==Range Finder=="};
unsigned char code email[] = {"heyaodz@163.com "};
unsigned char code Cls[] = {" "};
unsigned char code ASCII[15] = {'0','1','2','3','4','5','6','7','8','9','.','-','M'};
static unsigned char DisNum = 0; //显示用指针
unsigned int time=0;
unsigned long S=0;
bit flag =0;
unsigned char disbuff[4] ={ 0,0,0,0,};
uint valA;
uint TempCyc;
uchar phasecw[4] ={0x08,0x04,0x02,0x01};//正转 电机导通相序 D-C-B-A
uchar phaseccw[4]={0x01,0x02,0x04,0x08};//反转 电机导通相序 A-B-C-D
void show_5110();
void num_baishige();
void jianpan(void);
void MotorCW(void);
void MotorCCW(void);
void MotorStop(void);
void delayms(uint xms);
uint ideal_height=0; //理想高度
uint current_height=0; //当前高度
uint ih_bai=0,ih_shi=0,ih_ge=0, //理想高度的百,十,个位,用于数字分割,作5110显示使用。
ch_bai=0,ch_shi=0,ch_ge=0, //当前高度的百,十,个位,用于数字分割,作5110显示使用。
signal_bai=0,signal_shi=0,signal_ge=0,signal_all=0;
uchar enter=0;
void Conut(void);
void StartModule();
//5ms延时
void Delay5Ms(void)
{
unsigned int TempCyc = 5552;
while(TempCyc--);
}
//400ms延时
void Delay400Ms(void)
{
unsigned char TempCycA = 5;
unsigned int TempCycB;
while(TempCycA--)
{
TempCycB=7269;
while(TempCycB--);
};
}
void Timer_Count(void)
{
TR0=1; //开启计数
led=0;
while(RD); //当RX为1计数并等待
TR0=0; //关闭计数
Conut(); //计算
}
void StartModule() //启动模块
{
RD=1; //启动一次模块
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
RD=0;
}
void zd0() interrupt 1 //T0中断用来计数器溢出,超过测距范围
{
flag=1; //中断溢出标志
WR=0;
}
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=(time*1.8)/10; //算出来是MM
if((S>=7000)||flag==1) //超出测量范围显示“-”
{
flag=0;
// ch_bai=S/100+48;
// ch_shi=S/10%10+48;
// ch_ge=S%10+48;
// delay_ms(1);
// show_5110();
}
else
{
ch_bai=S/100+48;
ch_shi=S/10%10+48;
ch_ge=S%10+48;
delay_ms(1);
show_5110();
}
}
void main(void)
{
LCD_init(); //初始化
LCD_clear();
num_baishige();
show_5110();
for (TempCyc=0; TempCyc<10; TempCyc++)
Delay400Ms(); //延时
TMOD=0x01; //设T0为方式1,GATE=1;
TH0=0;
TL0=0;
ET0=1; //允许T0中断
EA=1; //开启总中断
while(1)
{
delayms(60);
WR=1;
StartModule();
for(valA=7510;valA>0;valA--)
{
if(WR==1)
{
Timer_Count();
}
}
}
}
//--------------------------------------------------
void num_baishige(void)
{
ih_bai=ideal_height/100+48;
ih_shi=ideal_height/10%10+48;
ih_ge=ideal_height%10+48;
ch_bai=current_height/100+48;
ch_shi=current_height/10%10+48;
ch_ge=current_height%10+48;
}
//--------------------------------------------------
void show_5110(void)
{
LCD_write_english_sendbyte(62,0,ih_bai);
LCD_write_english_sendbyte(69,0,ih_shi);
LCD_write_english_sendbyte(76,0,ih_ge);
// LCD_write_english_sendstr(62,0,"??? ");
delay_ms(1); //高度调节至后面的
LCD_write_english_sendstr(60,1," mm");
delay_ms(1);
LCD_write_english_sendbyte(48,2,ch_bai);
LCD_write_english_sendbyte(55,2,ch_shi);
LCD_write_english_sendbyte(62,2,ch_ge);
LCD_write_english_sendstr(69,2,"mm");
delay_ms(1);
LCD_write_english_sendstr(48,3,"+-3mm"); //当前高度后面的 超声波误差为3mm
LCD_Write12CnChar(0, 0, "高度调节至");
// delay_ms(1);
LCD_Write12CnChar(0, 18, "当前高度");
// delay_ms(1);
if(enter==0)
LCD_Write12CnChar(0, 36, "未确认");
else if(enter==1)
LCD_Write12CnChar(0, 36, "已确认");
// delay_ms(2);
LCD_Write12CnChar(10, 36,"重置");
// delay_ms(2);
}
//--------------------------------------------------
void jianpan()
{
uchar temp;
P0=0xfe;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P0;
switch(temp)
{
case 0xee:
if(signal_all==0)
ideal_height=ideal_height+100;
if(signal_all==1)
ideal_height=ideal_height+10;
if(signal_all==2)
ideal_height=ideal_height+1;
++signal_all;
break;
case 0xde:
if(signal_all==0)
ideal_height=ideal_height+200;
if(signal_all==1)
ideal_height=ideal_height+20;
if(signal_all==2)
ideal_height=ideal_height+2;
++signal_all;
break;
case 0xbe:
if(signal_all==0)
ideal_height=ideal_height+300;
if(signal_all==1)
ideal_height=ideal_height+30;
if(signal_all==2)
ideal_height=ideal_height+3;
++signal_all;
break;
// case 0x7e: break;
}
while(temp!=0xf0)
{
temp=P0;
temp=temp&0xf0;
}
//display(key);
}
}
/*----------------------------------------------------------------*/
P0=0xfd;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P0;
switch(temp)
{
case 0xed:
if(signal_all==0)
ideal_height=ideal_height+400;
if(signal_all==1)
ideal_height=ideal_height+40;
if(signal_all==2)
ideal_height=ideal_height+4;
++signal_all;
break;
case 0xdd:
if(signal_all==0)
ideal_height=ideal_height+500;
if(signal_all==1)
ideal_height=ideal_height+50;
if(signal_all==2)
ideal_height=ideal_height+5;
++signal_all;
break;
case 0xbd:
if(signal_all==0)
ideal_height=ideal_height+600;
if(signal_all==1)
ideal_height=ideal_height+60;
if(signal_all==2)
ideal_height=ideal_height+6;
++signal_all;
break;
// case 0x7d: break;
}
while(temp!=0xf0)
{
temp=P0;
temp=temp&0xf0;
}
//display(key);
}
}
/*----------------------------------------------------------------*/
P0=0xfb;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P0;
switch(temp)
{
case 0xeb:
if(signal_all==0)
ideal_height=ideal_height+700;
if(signal_all==1)
ideal_height=ideal_height+70;
if(signal_all==2)
ideal_height=ideal_height+7;
++signal_all;
break;
case 0xdb:
if(signal_all==0)
ideal_height=ideal_height+800;
if(signal_all==1)
ideal_height=ideal_height+80;
if(signal_all==2)
ideal_height=ideal_height+8;
++signal_all;
break;
case 0xbb:
if(signal_all==0)
ideal_height=ideal_height+900;
if(signal_all==1)
ideal_height=ideal_height+900;
if(signal_all==2)
ideal_height=ideal_height+9;
++signal_all;
break;
// case 0x7b: break;
}
while(temp!=0xf0)
{
temp=P0;
temp=temp&0xf0;
}
//display(key);
}
}
/*----------------------------------------------------------------*/
P0=0xf7;
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
delayms(10);
temp=P0;
temp=temp&0xf0;
if(temp!=0xf0)
{
temp=P0;
switch(temp)
{
case 0xe7:
enter=1;
break;
case 0xd7:
++signal_all;
break;
case 0xb7:
signal_all=0;
ideal_height=0;
enter=0;
break;
// case 0x77: break;
}
while(temp!=0xf0)
{
temp=P0;
temp=temp&0xf0;
}
// display(key);
}
}
}
//顺时针转动
void MotorCW(void)
{
uchar i;
for(i=0;i<4;i++)
{
MotorData=phasecw;
delayms(4);//转速调节
}
}
//逆时针转动
void MotorCCW(void)
{
uchar i;
for(i=0;i<4;i++)
{
MotorData=phaseccw;
delayms(4);//转速调节
}
}
//停止转动
void MotorStop(void)
{
MotorData=0x00;
}
void delayms(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
本程序
这个部分就不执行了,所以超声波不传数回来,求大神帮忙看一下哪里出了问题,怎么样才能解决
|
|