|
這是我的代碼
#include <STC15F2K60S2.H>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
sbit led_sel=P2^3;
uchar code duanxuan[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
sbit Trig=P1^0;
sbit Echo=P1^1;
uchar data_H,data_L;
uint distance;
void Init(){
P0M0=0XFF;
P0M1=0X00;
P2M0=0XFF;
P2M1=0X00;
TR1=1;
TR0=0;
TMOD=0x11;
AUXR=0xC0;
EA=1;
ET0=0;
ET1=1;
TH1=(65536-60000)/256;
TL1=(65535-60000)%256;
}
void S_Init(){
Trig=0;
Echo=0;
}
void Delay20us()//@11.0592MHz
{
unsigned char i;
_nop_();
_nop_();
_nop_();
i = 52;
while (--i);
}
void Timer1() interrupt 3{
EA=0;
TH1=(65536-60000)/256;
TL1=(65536-60000)%256;
Trig=1;
Delay20us();
Trig=0;
while(Echo==0);
TH0=0;
TL0=0;
TR0=1;
while(Echo==1);
data_H=TH0;
data_L=TL0;
TH0=0;
TL0=0;
distance=data_H;
distance=distance<<8;
distance=distance|data_L;
distance=distance/58;
EA=1;
}
void Delay10us()//@11.0592MHz
{
unsigned char i;
_nop_();
i = 25;
while (--i);
}
void seg_display(){
uchar ge_data,shi_data,bai_data;
bai_data=distance/100;
shi_data=distance%100/10;
ge_data=distance%100/10%10;
led_sel=0;
P0=0x00;
P2=0x00;
Delay10us();
P2=0x00;
P0=duanxuan[bai_data];
Delay10us();
P2=0x01;
P0=duanxuan[shi_data];
Delay10us();
P2=0x01;
P0=duanxuan[ge_data];
Delay10us();
}
void main(){
Init();
S_Init();
while(1){
seg_display();
}
} |
|