#include <reg51.h> #include <intrins.h> typedef unsigned char uchar; typedef unsigned int uint; sbit LS138A = P2^2; sbit LS138B = P2^3; sbit LS138C = P2^4; unsigned int lednum[8]={0}; unsigned char code Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; uchar sta=0; uchar high=0; uchar low=0; int dist=0; void delay(unsigned int i) { char j; for(i; i > 0; i--) for(j = 200; j > 0; j--); } void InitUART(void) { TMOD = 0x20; SCON = 0x50; TH1 = 0xFD; TL1 = TH1; EA = 1; ES = 1; TR1 = 1; } void SendOneByte(unsigned char c) { SBUF = c; while(!TI); TI = 0; } void main(void) { uint i ; InitUART(); while(1) { SendOneByte(0x55); for( i=0; i<8; i++) { P0=Disp_Tab[lednum[i]]; LS138A = i &1; LS138B = (i >> 1) &1; LS138C = (i >> 2) &1; delay(100); } dist=256*high+low; lednum[0]=dist%100000000/100000; lednum[1]=dist%10000000/100000; lednum[2]=dist%1000000/100000; lednum[3]=dist%100000/10000; lednum[4]=dist%10000/1000; lednum[5]=dist%1000/100; lednum[6]=dist%100/10; lednum[7]=dist%10; } } void UARTInterrupt(void) interrupt 4 { if(RI) { RI = 0; sta++; if(sta==1) high=SBUF; else if(sta==2) { low=SBUF; sta=0; } else TI = 0; } }