超声波自动蔽障小车代码

更新时间:2024-05-09 10:18:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

#include #include #include

#define RS_CLR RS=0 #define RS_SET RS=1

#define RW_CLR RW=0 #define RW_SET RW=1

#define EN_CLR EN=0 #define EN_SET EN=1 #define STOP 0 #define ADD 1 #define SUB 2 #define ZHENG 3 #define FAN 4

sbit RS = P2^6; //定义端口 sbit RW = P2^5; sbit EN = P2^7; sbit csb=P3^1; sbit csbint=P3^2;

unsigned char Line_One[]=\

unsigned char Line_Two[]=\ S %unsigned int T;//时间 unsigned int Dis;//距离

unsigned char EleMachine1[4]={0x01,0x02,0x04,0x08};//电机1正传参数 unsigned char EleMachine2[4]={0x10,0x20,0x40,0x80};//电机2正转参数 unsigned char Count;//电机动作步骤 unsigned int Speed;//转速

unsigned char Turn_Flag;//转弯标志

unsigned int Timer1_Count=0;//定时器1中断次数统计 unsigned int Sys_timer=0;//系统时间计时 float csbc=0;

unsigned char time_out;

unsigned char sucess_flag;//测试成功

/*------------------------------------------------

启动一次模块

------------------------------------------------*/ void start_module(void) { csb=0;

_nop_();_nop_();_nop_();_nop_();_nop_(); //空操作5次 _nop_();_nop_();_nop_();_nop_();_nop_(); //空操作5次 _nop_();_nop_();_nop_();_nop_();_nop_(); //空操作5次 _nop_();_nop_();_nop_();_nop_();_nop_(); //空操作5次 csb=1; }

/*------------------------------------------------

uS延时函数,含有输入参数 unsigned char t,无返回值 unsigned char 是定义无符号字符变量,其值的范围是

0~255 这里使用晶振12M,精确延时请使用汇编,大致延时 长度如下 T=tx2+5 uS

------------------------------------------------*/ void DelayUs2x(unsigned char t) { while(--t); }

/*------------------------------------------------

mS延时函数,含有输入参数 unsigned char t,无返回值 unsigned char 是定义无符号字符变量,其值的范围是 0~255 这里使用晶振12M,精确延时请使用汇编 ------------------------------------------------*/ void DelayMs(unsigned char t) { while(t--) //大致延时1mS { DelayUs2x(245); DelayUs2x(245); } }

void write_com(unsigned char com) //写命令 { RS_CLR; RW_CLR; P0=com; DelayMs(5); EN_SET; DelayMs(5);

EN_CLR; }

void write_data(unsigned char date) { RS_SET; RW_CLR; P0=date; DelayMs(5); EN_SET; DelayMs(5); EN_CLR; }

//写一个字符

void init() //初始化 { write_com(0x38); write_com(0x0c); write_com(0x06); write_com(0x01); }

/*------------------------------------------------ 写入字符串函数 ------------------------------------------------*/

void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) { if (y == 0) { write_com(0x80 + x); } else { write_com(0xC0 + x); } while (*s) { write_data( *s); s ++; } }

void LCD_Display(void) { Line_One[11]=Dis/100+'0';

Line_One[13]=Dis/10+'0'; Line_One[14]=Dis+'0'; Line_Two[2]=Sys_timer/10000+'0'; Line_Two[3]=Sys_timer000/1000+'0'; Line_Two[4]=Sys_timer00/100+'0'; Line_Two[5]=Sys_timer0/10+'0'; Line_Two[6]=Sys_timer+'0'; LCD_Write_String(0,0,Line_One); //显示 LCD_Write_String(8,0,Line_Two); }

main() { TH0=0; TL0=0;

Speed = 50000;//默认电机速度 TH1=(65536-Speed)/256; TL1=(65536-Speed)%6; TH2 = (65536-10000)/256; TL2 = (65536-10000)%6;

TMOD=0x11; //T1,T0为16位定时器 T2CON=0x00;//T2工作定时器方式 ET1 = 1;ET0 = 1;ET2=1;TF2=0; TR1 = 1;TR0 = 0;TR2=1; EA=1;EX0=0;IT0=0; init();

LCD_Display(); sucess_flag = 1; while(1) { if(sucess_flag == 1) { sucess_flag = 0; start_module(); while(csbint==0)start_module();//启动模块 ,避免卡死 EX0=1; TH0= 0; TL0 = 0; TR0=1;//启动计数器T0,用以计时 } csbc=0.0344; //声速344m/s(0.0344cm/us) csbc=csbc/2; //单位cm Dis=T*csbc; // 距离=(声速*时间)/2; if(Dis<30) //测量值小于下限 {

Speed = 5000; Turn_Flag = 1; } else { Speed = 5000; if(Turn_Flag == 1) { Count = 0; } Turn_Flag = 0; } LCD_Display(); DelayMs(20); } }

void T0_time() interrupt 1//定时器中断服务函数 { TH0=0; TL0=0; time_out =1 ; }

void T1_time() interrupt 3//定时器中断服务函数 { TH1=(65536-Speed)/256; TL1=(65536-Speed)%6; if(Turn_Flag==0) P1=EleMachine1[3-Count]|EleMachine2[Count];// 2个电机一起转,1反转,2正转 else P1=EleMachine1[3-Count]&0x0f;//转弯、第二个电机不转,第一个电机反转 Count++; if(Count>3) Count = 0; }

void T2_time() interrupt 5//定时器2中断服务函数 { TH2 = (65536-10000)/256; TL2 = (65536-10000)%6; TF2=0; Timer1_Count++; if(Timer1_Count>=100)// 1S计数 {

}

}

Sys_timer++;

Timer1_Count-=100;

void int0()interrupt 0//回响输出端高电平宽度测量(上升沿到来时候开始计时,下降沿到来进入外部中断,关闭计时器,停止计时) { }

TR0 = 0; //定时器停止计数

T=(TH0*256+TL0);//计算定时器内的测量值 TH0=0;//定时器清0 TL0=0;

EX0 = 0; //关闭外部中断 sucess_flag = 1;

}

}

Sys_timer++;

Timer1_Count-=100;

void int0()interrupt 0//回响输出端高电平宽度测量(上升沿到来时候开始计时,下降沿到来进入外部中断,关闭计时器,停止计时) { }

TR0 = 0; //定时器停止计数

T=(TH0*256+TL0);//计算定时器内的测量值 TH0=0;//定时器清0 TL0=0;

EX0 = 0; //关闭外部中断 sucess_flag = 1;

本文来源:https://www.bwwdw.com/article/j2ag.html

Top