超声波程序
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar flag0 = 0; //单片机发出10个脉冲标志位
uchar flag1 ; //测量标志位
long time; //记录定时器1时间
long distance = 0;//测得距离
code char tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //数码管显示0~9段选位
void SendSupersonic();
void display();
void dalay(uchar k);
void main()
{
EA = 1;
ET0 = 0;
ET1 = 0;
TMOD = 0x12; //定时器1方式1,定时器0工作在方式2;定时器0用于输出40KHZ的脉冲信号;定时器1用于记录超声波来回时间
TH0 = 0xF4;
TL0 = 0xF4;
IT0 = 1; //外部中断0电平触发方式,低电平引起中断
EX0 = 0; //外部中断0允许位
P0 = 0x00;
while(1)
{
flag1 = 0;
TH1 = 0x00;
TL1 = 0x00;
SendSupersonic();
while(!flag1);
display();
}
}
void timer_0(void) interrupt 1 //40KHZ脉冲
{
flag2 = (~flag2);
flag0++;
}
void intsvr0(void) interrupt 0
{
TR1 = 0;
EX1 = 0;
time = TL1;
time += (long)TH1*256;
flag1 = 1;
}
void SendSupersonic()
{
TR0 = 1;
ET0 = 1; //打开定时器0开始发送40KHZ脉冲信号
ET1 = 1;
TR1 = 1; //打开定时器1,开始计时
while(flag0 < 18) ; //等待发送完10个脉冲信号
flag0 = 0;
ET0 = 0;
TR0 = 0;
}
void display()
{
distance = time*170;
distance = distance/1000000;
P2 = 0x7f;
P0 = tab[distance/10];
dalay(1);
P2 = 0xbf;
P0 = (tab[distance/1])&0x80;
dalay(1);
P2 = 0xdf;
P0 = tab[(distance*10)/10];
dalay(1);
P2 = 0xef;
P0 = tab[(((distance*100)%100)%10)/1];
dalay(1);
}
void dalay(uchar k)
{
uchar i,j;
for(i = 0;i <= k;i ++)
for(j = 0; j <= 110;j ++)
{
;
}
}
此帖出自
小平头技术问答
#include <intrins.h>
#define RX P0_1
#define TX P0_2
unsigned int time=0;
unsigned int timer=0;
unsigned char posit=0;
unsigned long S=0;
bit flag =0;
unsigned char const discode[] ={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e/*-*/};
unsigned char const positon[3]={ 0x90,0x88,0x83};
unsigned char disbuff[4] ={ 0,0,0,0,};
/********************************************************/
void Display(void) //扫描数码管
{
if(posit==0)
{P1=(discode[disbuff[posit]])&0x7f;}
else
{P1=discode[disbuff[posit]];}
P2=positon[posit];
if(++posit>=3)
posit=0;
}
/********************************************************/
void Conut(void)
{
time=TH0*256+TL0;
TH0=0;
TL0=0;
S=(time*1.7)/100; //算出来是CM
if((S>=700)||flag==1) //超出测量范围显示
{
flag=0;
disbuff[0]=10; //“-”
disbuff[1]=10; //“-”
disbuff[2]=10; //“-”
}
else
{
disbuff[0]=S%1000/100;
disbuff[1]=S%1000%100/10;
disbuff[2]=S%1000%10 %10;
}
}
/********************************************************/
void zd0() interrupt 1 //T0中断用来计数器溢出,超过测距范围
{
flag=1; //中断溢出标志
}
/********************************************************/
void zd3() interrupt 3 //T1中断用来扫描数码管和计800MS启动模块
{
TH1=0xf8;
TL1=0x30;
Display();
timer++;
if(timer>=400)
{
timer=0;
TX=1; //800MS 启动一次模块
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
TX=0;
}
}
/*********************************************************/
void main( void )
{
TMOD=0x11; //设T0为方式1,GATE=1;
TH0=0;
TL0=0;
TH1=0xf8; //2MS定时
TL1=0x30;
ET0=1; //允许T0中断
ET1=1; //允许T1中断
TR1=1; //开启定时器
EA=1; //开启总中断
while(1)
{
while(!RX); //当RX为零时等待
TR0=1; //开启计数
while(RX); //当RX为1计数并等待
TR0=0; //关闭计数
Conut(); //计算
}
}
一周热门 更多>