/*****************************************************************************/
/* 超声波测距 */
/* CPU:AT89S52 */
/* 完成时间:2009年8月21日 */
/*****************************************************************************/
#include <AT89X51.h>
#include <intrins.h>
/*~~~~~~~~~~~~~~宏定义~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
/*~~~~~~~~~~~~~~位定义~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
sbit OUT = P1^0;
sbit led = P1^1;
bit f,a;
/*~~~~~~~~~~~~~~存储器定义~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
uchar count[8]={0,0,0,0,0,0,0,0};
//uchar code tab_led[10]={0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0xcb,0x08,0x0b};
uchar code tab_led[10]={0x77,0x14,0xb3,0xb6,0xd4,0xe6,0xe7,0x34,0xf7,0xf6};
void tx38k(void);
void display(void);
void delayms(uchar a);
void delay10us(ulong i);
void init(void);
void main(void)
{
init();
led=1;
f=1;
while(1)
{
if(f==1)
{
f=0;
tx38k();
if(a==1)
{
a=0;
display();
}
delayms(100);
}
_nop_();
}
}
void init(void)
{
TMOD=0x11; //T1定时
TH0 =0x00;
TL0 =0x00;
TH1 =0x00;
TL1 =0x00;
IT0 =1; //下降沿触发
//IT0=0; //低电平触发
EX0 =1; //能中断0
//EA =1;
}
void tx38k(void)
{
uchar i;
EA = 0;
TH0 = 0x00;
TL0 = 0x00;
TH1 = 0x00;
TL1 = 0x00;
EA =1;
_nop_();
_nop_();
TR0 = 1;
TR1 = 1;
for(i=0;i<4;i++)
{
OUT=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
OUT=0;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
}
f=1;
}
void display(void)
{
uchar i;
SCON=0;
for(i=0;i<8;i++)
{
SBUF=tab_led[count[i]];
while(TI==0);
TI=0;
}
}
/******************************************/
/* 延时子程序 */
/******************************************/
void delayms(uchar a)
{
uchar i,j;
for(i=0;i<a;i++)
for(j=0;j<200;j++);
}
//void delay10us(ulong i)
//{
// while(i--);
//}
//******************************************************************
//外部中断0服务程序
void int0_isr(void) interrupt 0//reset P3.2
{
uint HZ;
EA =0;
TR0=0;
TR1=0;
HZ =TH0<<8|TL0;
HZ=HZ*0.17;
if(HZ>60)
{
count[7]=HZ/1000%10;
count[6]=HZ/100%10;
count[5]=HZ/10%10;
count[4]=HZ%10;
}
HZ =TH1<<8|TL1;
HZ=HZ*0.17;
if(HZ>60)
{
count[3]=HZ/1000%10;
count[2]=HZ/100%10;
count[1]=HZ/10%10;
count[0]=HZ%10;
}
//delayms(50);
led=~led;
a=1;
}
chaoshengboceju.rar_超声波模块
版权申诉
28 浏览量
2022-09-14
18:26:59
上传
评论
收藏 46KB RAR 举报
小贝德罗
- 粉丝: 71
- 资源: 1万+
最新资源
- 基于C++的二叉树的创建与遍历(免费提供源码)
- AEB算法模型matlab版本
- Screenshot_2024-05-28-00-07-11-51_a935a6faaffa07c582f2c3acde62ae6b.jpg
- 1-230201111535.zip
- 《DAX 底层计算过程演示-ADDCOLUMNS + 复杂上下文环境》配套数据文件
- ruc-波兰表达式c语言递归demo笔记
- Java.md
- credit-visualstudio2022笔记
- SystemServer启动阶段思维示意图
- 168832737737857��Ϊ�˶�����-14.1.4.171-wearBeta-390-lspatched.apk
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈