#include <reg52.h>
#include <intrins.h>
#define uchar unsigned uchar
#define uint unsigned uint
sbit trig=P1^2;
sbit echo=P1^0;
sbit LCD_RS=P1^0;
sbit LCD_RW=P1^1;
sbit LCD_EN=P1^2;
sbit LCD_PSB=P1^6;
bit flag;
void delay(uchar x )
{
uchar i,j;
for(j=0;j<x;j++)
for(i=0;i<110;i++);
}
void write_cmd(uchar cmd)
{
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
P0=cmd;
delay(5);
LCD_EN=1;
delay(5);
LCD_EN=0;
}
void write_dat(uchar dat)
{
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
P0=dat;
delay(5);
LCD_EN=1;
delay(5);
LCD_EN=0;
}
void lcd_init()
{
LCD_PSB=1;
write_cmd(0x30);
delay(5);
write_cmd(0x0c);
delay(5);
write_cmd(0x01);
delay(5);
}
void init()
{
TMOD=0x01;
TH0=0;
TL0=0;
TR0=0;
IT0=1;
EX0=1;
EA=1;
lcd_init();
}
uint test_distance(void)
{
uint value;
TH0=0;
TL0=0;
trig=0;
_nop()_;
trig=1;
_nop_();_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();_nop_();
trig=0;
while(!echo);
TR0=1;
while(echo);
TR0=0;
value=TH0*256+TL0;
return value;
}
void timer() interrupt 0;
{
flag=1;
}
void lcd_pos(uchar x, uchar y)
{
uchar pos;
if(x == 0) // 第一行的地址是80H
{
x = 0x80;
}
else if(x == 1) //第二行的地址是90H
{
x = 0x90;
}
else if(x == 2) //第三行的地址是88H
{
x = 0x88;
}
else if(x == 3)
{
x = 0x98;
}
pos = x + y;
write_cmd(pos);
}
void main()
{
uchar table[4];
uint value;
uint a,b,c,d,e,i;
init();
while(1)
{
if(flag)
{
flag=0;
value=test_distance();
a=value/100;
b=a/2;
c=b*340;
e=c/10;
table[0]=e/1000;
table[1]=(e%1000)/100;
table[2]=((e%1000)%100)/10;
table[3]=e%10;
delay(5);
EA=1;
}
lcd_pos(0,0);
for(i=0;i<4;i++)
{
write_dat(table[i]+0x30);
}
}
}
SS.C(16): error C141: syntax error near 'x', expected ')'
SS.C(18): error C231: 'uchar': redefinition
SS.C(18): error C141: syntax error near 'i'
SS.C(19): error C202: 'x': undefined identifier
SS.C(22): error C141: syntax error near 'cmd', expected ')'
SS.C(27): error C202: 'cmd': undefined identifier
SS.C(33): error C141: syntax error near 'dat', expected ')'
SS.C(38): error C202: 'dat': undefined identifier
SS.C(66): error C141: syntax error near ';'
SS.C(67): error C129: missing ';' before 'test_distance'
Target not created
|