全部资料51hei下载地址:
程序.zip
(130.97 KB, 下载次数: 8)
Project Logs for 电钢.zip
(5.68 KB, 下载次数: 6)
- /*******************************************************************/
- /*******************超声波测距仪************************************/
- /*******************(液晶屏显示)************************************/
- /*******************晶振 11.0592MHz********************************/
- #include <reg52.h>
- #include "12864.h"
- #include <l298.h>
- typedef unsigned char U8; /* defined for unsigned 8-bits integer variable 无符号8位整型变量 */
- typedef signed char S8; /* defined for signed 8-bits integer variable 有符号8位整型变量 */
- typedef unsigned int U16; /* defined for unsigned 16-bits integer variable 无符号16位整型变量 */
- typedef signed int S16; /* defined for signed 16-bits integer variable 有符号16位整型变量 */
- typedef unsigned long U32; /* defined for unsigned 32-bits integer variable 无符号32位整型变量 */
- //typedef signed long S32; /* defined for signed 32-bits integer variable 有符号32位整型变量 */
- //typedef float F32; /* single precision floating point variable (32bits) 单精度浮点数(32位长度) */
- //typedef double F64; /* double precision floating point variable (64bits) 双精度浮点数(64位长度) */
- //定时器0的定时值为1mS,即11059/12=922个时钟脉冲,其补为65536-922=64614
- #define SYSTEMCLK 921600 //11059200/12
- #define T0CLK 921600 //11059200/12
- #define T1CLK 921600 //11059200/12
- #define T1PERIOD 1000000/921600 //T1周期时间,以微秒为单位,约为1.085uS
- #define TIMER0H 0xFC //64614/256=252
- #define TIMER0L 0x66 //54447%256=102
- //管脚定义
- sbit Trig = P1^3;
- sbit Echo = P3^2; //回波必须接在外部中断引脚上
- sbit Beep = P2^6; //报警蜂鸣器
- sbit k1=P3^0;
- sbit k2=P3^1;
- sbit k3=P3^3;
- sbit k4=P3^4;
- sbit k5=P3^5;
- sbit k6=P2^5;
- sbit k7=P2^7;
- sbit hy=P1^1;
- unsigned char code tab[]="0123456789 :.%";
- uint h=0,v=0;
- uint V1=0,s=0;
- uchar time1,puk1=100,k=1,w=0,m=0,puk=0;
- //定义标志
- volatile bit FlagSucceed = 0; //测量成功标志
- volatile bit FlagDisplay = 0; //显示标志
- //定义全局变量
- U16 DisplayCount=0;
- U32 time=0;
- U32 distance=0;
- //函数声明
- void delay_20us();
- void Start_Module();
- void INT0_Init(void);
- void Data_Init();
- void Timer0_Init();
- void Timer1_Init();
- //20us延时程序,不一定很准
- void delay_20us()
- {
- U16 bt ;
- for(bt=0;bt<190;bt++); //8M晶振是100
- }
- void delayms(uchar t)
- {
- uchar a,b;
- for(a=t;a>0;a--)
- for(b=110;b>0;b--);
- }
- void speak()
- {
- Beep=0;
- delayms(200);
- Beep=1;
- }
- //数据初始化
- void Data_Init()
- {
- Trig = 0;
- distance = 0;
- DisplayCount = 0;
- }
- //外部中断初始化函数
- void INT0_Init(void)
- {
- IT0 = 0; //负边沿触发中断
- EX0=0; //关闭外部中断
- }
- //外部中断处理用做判断回波电平
- void INT0_ISR (void) interrupt 0
- {
- time =TH1*256+TL1; //取出定时器的值
- FlagSucceed = 1; //置成功测量的标志
- if(s==1)
- V1=3*puk1;
- else
- V1=0;
- co_display (3,5,V1/100,V1%100/10,tab);
-
- EX0=0; //关闭外部中断
- }
- /////////////初始化定时器///////////////////
- void InitTimer0(void)
- {
- TMOD = 0x11;
- TH0=0xff;
- TL0=0xfe;
- EA = 1;
- ET0 = 1;
- TR0 = 1;
- }
- //////////////////PWM产生函数//////////////////////////////
- void Timer0Interrupt(void) interrupt 1
- {
- TH0=0xff;
- TL0=0xfe; //从新赋值
- time1++;
- if(time1<puk1)pwm1=1; //time小于给的那个值时就是高电平
- else
- if((time1>=puk1)&&(time1<=100))pwm1=0; // 如果puk1到100的就是低电平
- else
- if(time1>=100)time1=0; //time大于100就清0
- }
- //定时器1初始化,16位计数模式,时钟为11059200/12=921600Hz
- //60ms计数为55296,即0xD800
- void Timer1_Init()
- {
- TMOD = 0x11; //定时器0和1工作在16位方式
- TH1 =0;
- TL1= 0;
- ET1 = 1;
- }
- //启动模块,Trig管脚20us正脉冲
- void Start_Module() //启动模块
- {
- Trig=1; //启动一次模块
- delay_20us();
- Trig=0;
- }
- /********************************************************************
- * 名称 : Main()
- * 功能 : 主函数
- ***********************************************************************/
- void key()
- {
- if(k1==0||hy==0)
- {
- pwm(w);
- // puk1=puk;
- cn_display(1,6,0,"启");
- s=1;
- }
- if(k2==0)
- {
- delay_20us();
- delay_20us();
- pwm(2);
- s=0;
- {
- if(k==1)
- cn_display(1,4,0,"伸"),w=1,k=0;
- else if(k==0)
- cn_display(1,4,0,"缩"),w=0,k=1;
- // else
- // cn_display(1,4,0,"停"),pwm(w),k=1;
- }
- }
- if(k3==0)
- {
- h++;
- if(h>=30)
- h=30;
- }
- if(k4==0)
- {
- h--;
- if(h==0)
- h=30;
- }
- if(k5==0)
- {
- v++;
- if(v>=30)
- v=30;
- }
- if(k6==0)
- {
- v--;
- if(v==0)
- v=30;
- }
- if(k7==0)
- {
- m++;
- if(m>6)
- m=0;
- }
- }
- void chaosheng()
- {
- EA= 0;
- //以下为一次检测过程:先发出Trig电平,打开外部中断,清零T1,
- //最后在外部中断下降沿触发时取出T1当前值,计算出Trig脉冲宽度。
- while (Echo == 1) ;
- Start_Module();
- while(Echo==0) ;//等待Echo回波引脚变高电平
- FlagSucceed = 0;
- EX0=1;
- TH1= 0;
- TL1= 0;
- TF1= 0;
- TR1=1; //启动定时器1开始计数
- EA = 1;
- while (TH1<83) ; //盲区
- TR1 = 0 ; //关闭定时器1
- EX0 = 0; //关闭外部中断
- if(FlagSucceed==1) //一次测试成功,则计算距离,单位为厘米
- {
- distance = time * 1.085 ; //计算得到脉冲时间(以微秒为单位)
- //将微秒时间转变成厘米距离的算法: Y米=(X秒*344)/2
- // X秒=( 2*Y米)/344 ==》X秒=0.0058*Y米 ==》厘米=微秒/58
- distance /=58;
- //如果距离小于20cm,则声光报警
- distance*=10;
- distance-=35;
- distance-=520;
- co_display (2,6,distance%1000/100,distance%100/10,tab);
- }
- }
- void init()
- {
- U16 i;
- init_lcd ();
- InitTimer0();
- for(i=0;i<8;i++)
- cn_display(1,i,i," 陕西首届高校 ");
- for(i=0;i<8;i++)
- cn_display(2,i,i,"教师电子设计竞赛");
- delayms(2000);
- clrram ();
- EA = 0;
- INT0_Init();
- Timer1_Init(); //定时器1初始化
- Data_Init();
- EA = 1;
- for(i=0;i<8;i++)
- cn_display(0,i,i,"同位伸缩控制系统");
- for(i=0;i<8;i++)
- cn_display(1,i,i," A状态: ");
- pwm(2);
- for(i=0;i<2;i++)
- cn_display(2,i,i,"SET:");
- cn_display(2,3,0,"cm");
- cn_display(2,4,0,"RU");
- cn_display(2,5,0,"N:");
- cn_display(2,7,0,"cm");
- cn_display(3,0,0,"V:");
- cn_display(3,2,0,"mm");
- cn_display(3,3,0,"/s");
- cn_display(3,4,0,"V:");
- cn_display(3,6,0,"mm");
- cn_display(3,7,0,"/s");
- }
- void main()
- {
- uint l=1,pt=1;
- init();
- while(1)
- {
- chaosheng();
- co_display (1,3,11,m%10,tab);
- co_display (2,2,h/10,h%10,tab);
- co_display (3,1,v/10,v%10,tab);
-
- if(m==0)
- {
- if((distance/10==h))
- {
- pwm(2),w=2;
- speak();
- }
- }
- if(m==1)
- {
- if((v*10)>V1)
- {
- puk1+=1;
- if(puk1>=100)
- puk1=100;
- }
- else
- if((v*10)<V1)
- {
- puk1-=1;
- if(puk1==40)
- puk1=40;
- }
- else
- {
- speak();
- }
- }
- if(m==2)
- {
- if(distance/10==20)
- pwm(0),pt=0;
- else if((distance/10<=1) &&pt==0)
- pwm(2),speak(),pt=1;
- }
- key();
- }
- }
复制代码
|