#include "nokia_5110.h"
#include <stc12c5a60s2.h>
#include <intrins.h>
#include "delay.h"
#include "GPIO.h"
sbit Trig=P4^4;
sbit Echo=P4^5;
unsigned long time;
unsigned long distance;
void Display(void);
void main()
{
GPIO_init_pin(44,0);//双向口
GPIO_init_pin(45,0);
LCD_init();
LCD_clear();
EA=1;
TMOD=0x01;
TR0=0;
TH0=0;
TL0=0;
while(1)
{
Trig=1;
exact_delay(1,2,12);
Trig=0;
while(Echo==1);
TR0=1;
while(Echo==0);
TR0=0;
time=TH0*256+TL0;
distance=(time*340)/2;
delay_X_ms(100);
Dispaly();
}
}
void Display()
{
unsigned char unit,ten,hundred,thousand,shifenwei,baifenwei;
unit=distance/ % 10;
ten=distance/ 10 % 10;
hundred = distance/ 100 % 10;
thousand = distance / 1000 % 10;
shifenwei= (distance*10)%10;
baifenwei= (distance*100)%10;
if(time>8000)
{
LCD_write_english_string(3,2,"chao guo");
}
else
{
LCD_set_XY(4,3);
LCD_write_number(thousand);
LCD_set_XY(5,3);
LCD_write_number(hundred);
LCD_set_XY(6,3);
LCD_write_number(ten);
LCD_set_XY(7,3);
LCD_write_number(unit);
LCD_set_XY(8,3);
LCD_write_number();
LCD_set_XY(9,3);
LCD_write_number(shifenwei);
LCD_set_XY(10,3);
LCD_write_number(baifenwei);
}
}
|