0
本帖最后由 马上有钱有对象 于 2014-3-28 21:40 编辑
这是一个超声波测速及霍尔元件测速,测出来的距离在lcd1602显示的程序,有点问题,超声波测速的中断接的是 I T 0,霍尔测速的中断接的是I T1,关键是中断这里我有点不会,求大神帮我改一下
#include
#include
#define uint unsigned int
#define uchar unsigned char
#define NOP() {_nop_();_nop_();_nop_();_nop_();}
//------LCD??-----
***it LCD_RS=P2^6;
***it LCD_RW=P2^5;
***it LCD_EN=P2^7;
//------?????-------
***it Tx=P3^7; //超声波测距发射
***it Rx=P3^2; //接收
uchar code table[]={"distance="}; //LCD?????
uchar temp_dis[]= {"000.0cm"}; //LCD???
uchar temp_dis2[]={"cm"};
uchar code table1[]={"rate="};
uchar temp_dis1[]= {"000.0cm/s"};
long int t,distance,rate;
uint i,m,n,z;
uint h=5, l=5;
uchar bai1,shi1,ge1,bai2,shi2,ge2,bai3,shi3,ge3;
long int s,s1,s2,M1,M2,f;
uchar cache[4]={0,0,0,0};
uchar su[4]={0,0,0,0};
//--------??-------
void delay(uint ms)
{
uint t;
while(ms--)
for(t=0;t<120;t++);
}
//-------?LCD??-------
uchar read_lcd_state()
{
uchar state;
LCD_RS=0;
LCD_RW=1;
LCD_EN=1;
_nop_();
state=P0;
LCD_EN=0;
_nop_();
return state;
}
//-------???------
void lcd_busy_wait()
{
while((read_lcd_state() & 0x80)==0x80);
NOP();
}
//----------LCD???----------
void lcd_write_com(uchar com)
{
lcd_busy_wait();
LCD_RS=0; //RS?0?,???,RS?1?,???
LCD_RW=0;
P0=com;
NOP();
LCD_EN=1;
NOP();
LCD_EN=0;
}
//----------LCD???----------
void lcd_write_data(uchar dat)
{
lcd_busy_wait();
LCD_RS=1;
LCD_RW=0;
P0=dat;
NOP();
LCD_EN=1;
NOP();
LCD_EN=0;
}
//-------LCD???-------
void lcd_init()
{
LCD_EN=0;
lcd_write_com(0x38); //LCD??????
lcd_write_com(0x0c); //LCD???/??????
lcd_write_com(0x06); //????????????1,????1
lcd_write_com(0x01); //????
}
//---------????????-----------
void set_lcd_pos(uchar p)
{
lcd_write_com(p|0x80);
}
//---------??????----------
void lcd_print(uchar p,uchar *s,uint low)
{
uint num;
set_lcd_pos(p);
for(num=0;num
{
lcd_write_data(s[num]);
delay(1);
}
}
void HC05_Init()
{
Tx=1; //触发脉冲发射口P3^7
NOP();NOP();NOP();NOP();
Tx=0;
distance=0.17*t; //距离计算
}
void T1_Init()
{
TMOD = 0x10; //定时器1设置为8位自动加载
ET1=1; //开定时器中断
EA=1; //开总中断
IP=0x01; //设置中断优先级
TH1 = (65535-50000)/256; //初始加载50毫秒
TL1 = (65535-50000)%256;
}
void INT_Init()
{
IT1 = 1; //设外部中断1为下降沿触发
//EX0 = 1; //?????0
EX1 = 1; //开外部中断1Ï11
EA = 1;
}
void distance_convert(long int dat)
{
cache[0]=dat/1000;
cache[1]=dat/100%10;
cache[2]=dat/10%10;
cache[3]=dat%10;
temp_dis[0]=cache[0]+'0';
temp_dis[1]=cache[1]+'0';
temp_dis[2]=cache[2]+'0';
temp_dis[4]=cache[3]+'0';
}
void rate_convert(long int dat)
{
su[0]=dat/1000;
su[1]=dat/100%10;
su[2]=dat/10%10;
su[3]=dat%10;
temp_dis1[0]=su[0]+'0';
temp_dis1[1]=su[1]+'0';
temp_dis1[2]=su[2]+'0';
temp_dis1[4]=su[3]+'0';
}
//------------???-----------
void main()
{ int i;
lcd_init();
delay(5);
TMOD=0x19;
EA=1; //¿ª×ÜÖжÏ
TR0=1; //启动时器
EX0=1; //设置下降沿中断方式
IT0=1;
//
while(1)
{
HC05_Init();
distance_convert(distance);
lcd_print(0x00,table,9);//显示distance=
lcd_print(0x09,temp_dis,8);//显示测出的的距离
//lcd_print(0x0e,temp_dis2,5);
//T1_Init(); //外部中断IT1初始化
INT_Init();//ÍⲿÖжÏIT1³õʼ»¯
//TR1=1;//Æô¶¯¶¨Ê±Æ÷1
rate_convert(rate);
lcd_print(0x40,table1,5);//lcd1602显示检测出来的速度
lcd_print(0x45,temp_dis1,8);
//lcd_print(0x4e,temp_dis2,5);
}
}
//????0
void int0() interrupt 0
{
t=(TH0*256+TL0); //计算高电平持续的时间,上升沿到来时开始计时,下降沿到来时借宿这是超声测距的中断
TH0=0;
TL0=0;
}
void myrate_INT0(void) interrupt 2
{
M1=M1++; //每中断一次变量+1
}
void timer1() interrupt 3
{ TR1=0; //Í停止T1 TL1 = (65535-50000)%256; //求整放在高8位 TL1 = (65535-50000)%256; //求余放在低8位
n=n+1; //n++
if(n==20) // //定时1秒(20个50ms为1秒) { s1=M1/100.00*3.14*65; //1???????s1??,??????65MM
bai3=(s1/100)+48; //???????
shi3=(s1%100/10)+48; //???????
ge3=(s1%100%10)+48; //???????
n=0;
rate=bai3*100+shi3*10+ge3; //计算速度的以上
s1=0; //????????s1??
s2=0; //????????s2??
M1=0; //????0??????
//????1??????
}
TR1=1; //启动T1}
|
|