main.zip

  • PUDN用户
    了解作者
  • C/C++
    开发工具
  • 2KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 4
    下载次数
  • 2012-04-26 17:41
    上传日期
利用51单片机来控制超声波的收发实现距离测量
main.zip
  • main.c
    3.6KB
内容介绍
#include <reg51.h> #include "intrins.h" //#include "Lcd1602.h" #define uchar unsigned char #define uint unsigned int sbit BEEP = P1^6; //报警提示口 sbit vout = P1^7; //脉冲发送口 sbit P3_2 = P3^2; //中断接收口 sbit P2_2 = P2^2; //sbit INT = P1^6; bit flag; static uchar ledcode[12] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xff,0x7f}; //unsigned char discol[4]={0xef,0xdf,0xbf,0x7f}; unsigned char code Anum_code[]={"0123456789"}; unsigned char disbuf[4]={0,1,2,3}; float high,low; unsigned char qian,bai,shi,ge; //unsigned char j=600; /*void delay_1ms(void) //12mhz delay 1.01ms { unsigned char x,y; x=3; while(x--) { y=40; while(y--); } } void delay_1us(unsigned char i) //i=1, 5us; i=2,7us; i=3,9us { while(--i); }*/ void delay_50us(unsigned int t) // t=100, 5ms; t=1000, 50ms { unsigned char j; for(;t>0;t--) for(j=19;j>0;j--); } /*void delay_50ms(unsigned int t) // t=10, 500ms; t=100, 5s; { unsigned int j; for(;t>0;t--) for(j=6245;j>0;j--); }*/ void display(void) { uint a,i,j; for(j=0;j<400;j++) { for(i=0;i<4;i++) // 四路扫描 { P0 = ledcode[disbuf[3-i]]; // 发送显示数据 P2 = ~(1<<i); // 确实显示位 for (a=0; a<40; a++); // 延时一段时间 P2 |= (1<<i); // 关闭显示 } P2_2 = 0; P0 = ledcode[11]; // 显示小数点 for (a=0; a<40; a++); P2_2 = 1; } } void work(void) { //unsigned char qian,bai,shi,ge; float value; value=(high*256+low)*17/100; qian=value/1000; bai=(value-qian*1000)/100; shi=(value-qian*1000-bai*100)/10; ge=value-qian*1000-bai*100-shi*10; if(value<=1000) { BEEP=0; } else { BEEP=1; } disbuf[0] = qian; disbuf[1] = bai; disbuf[2] = shi; disbuf[3] = ge; } main() { //unsigned char i; //unsigned int t; //Lcd_Init(); TMOD = 0x11; TH0 = 0; TL0 = 0; TH1 = 0; TL1 = 0; // P0 = 0xff; // P1 = 0xff; // P2 = 0xff; // P3 = 0xff; EX0 = 1; //允许外部中断0 ET1 = 1; //允许定时器1中断 EA = 1; TR1 = 1; while(1) { //display(); if(flag) { EA = 0; work(); //EA = 0; display(); //delay_50ms(100); //for(i=5;i>0;i--) //for(t=6245;t>0;t--); flag = 0; ET0 = 0; //禁止定时器0中断 TH0 = 0; TL0 = 0; ET1 = 1; TR1 = 1; EA = 1; } } } void INTT1() interrupt 3 { unsigned char i; EA = 0; TR1 = 0; EX0 = 0; //禁止外部中断 TH0 = 0; TL0 = 0; TH1 = 0; TL1 = 0; EA = 1; TR0 = 1; for(i=0;i<4;i++) { vout = ~vout; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } delay_50us(40); EX0 = 1; //开启外部中断 //ET0 = 1; //允许定时器0中断 /*if(j==600) { j=j+200; TR0 = 1; EX0 = 1; //开启外部中断 delay_50us(j); INT=0; } else { j=j-200; TR0 = 1; EX0 = 1; //开启外部中断 delay_50us(j); INT=0; }*/ } void PINT0() interrupt 0 { //_nop_(); TR0 = 0; //关闭定时器0 EX0 = 0; EA = 0; low = TL0; high = TH0; flag = 1; while(P3_2==0) { //TR0 = 0; //关闭定时器0 //EX0 = 0; //EA = 0; //low = TL0; //high = TH0; //low = 0x50; //high = 0xc3; //TH0 = 0; //TL0 = 0; //INT=1; //flag = 1; //Write_Comm(0x01); //清显示屏 //Write_Comm(0x8f); //Write_Data('k'); //delay_50ms(10); } }
评论
    相关推荐