本小车是从网上买的,7.2v电车供电,L298N电机驱动模块。
系统的硬件包括以下几个部分: (1)MCU:STC89C52; (2)电源:7.2V、1600mah的可充电镍镉电池; (3)红外光电传感器; (4)超声波传感器:HC-SR04 (5)两个直流电机; (6)电机驱动芯片:L298N。
单片机源程序如下:
- #include <reg52.h>
- #include <intrins.h>
- #define somenop _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_(); _nop_();
- #define DIS_DATA P0
- unsigned char code DIS_TABLE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
- 0x88,0x83,0xc6,0xa1,0x86,0x8e};
- sbit cs0 = P2^0;
- sbit cs1 = P2^1;
- sbit cs2 = P2^2;
- sbit cs3 = P2^3;
- sbit MOTO1_IN1 = P1^0;//左轮电机
- sbit MOTO1_IN2 = P1^1;
- sbit MOTO2_IN1 = P1^2;//右轮电机
- sbit MOTO2_IN2 = P1^3;
- sbit MOTO1_PWM = P1^4;
- sbit MOTO2_PWM = P1^5;
- sbit Tx = P3^3;
- sbit Rx = P3^2;
- sbit Key_1 = P3^5;
- sbit Key_2 = P3^4;
- sbit Led = P1^7;
- void RED_SCAN(void);
- void Turn_Forward(void);
- void Turn_Right(void);
- void Turn_Left(void);
- void Turn_Stop(void);
- void Turn_SRight(void);
- void Turn_SLeft(void);
- void Test(void);
- void Key_Scan(void);
- void send_wave(void);
- void Delay10us(void);
-
- unsigned char i = 0,j = 0;
- unsigned char a = 0,b = 0;
- unsigned char flag = 0;
- unsigned char flag1 = 0;
- unsigned char timeH = 0,timeL = 0,time = 0;
- unsigned char mode;
- unsigned char flag_csb_juli=0;
- unsigned char iCnt;
- unsigned int displayvalue;
- unsigned int scan_status;
- float distance;
- int main()
- {
- Tx = 0;
- EA = 1;
- TMOD = 0x11;
- TR0=0;
- TR1=0;
- MOTO1_PWM = 0;
- MOTO2_PWM = 0;
- TH0 = 65517/256;
- TL0 = 65517%256;
-
- while(1)
- {
- Key_Scan();
- if(mode == 1)
- {
- ET0 = 1;
- TR0 = 1;
- }
- if(mode == 2)
- {
- ET0 = 0;
- TR0 = 0;
- MOTO1_PWM = 1;
- MOTO2_PWM = 1;
- send_wave();
-
- }
- }
-
- }
- void RED_SCAN(void)
- {
- switch(P2&0xf0)
- {
-
- case 0x60: Turn_Forward();flag = 0;scan_status=110;
- break;
-
- case 0xC0: Turn_Right();flag = 0;scan_status=1100;
- break;
-
- case 0x30: Turn_Left();flag = 0;scan_status=11;
- break;
-
- case 0x80: Turn_SRight();flag = 2;scan_status=1000;
- break;
-
- case 0x10: Turn_SLeft(); flag = 1;scan_status=1;
- break;
-
- case 0xE0: Turn_Right();flag = 0;scan_status=1110;
- break;
-
- case 0x70: Turn_Left();flag = 0;scan_status=111;
- break;
-
- case 0x20: Turn_Right();flag = 0;scan_status=10;
- break;
-
- case 0x40: Turn_Left();flag = 0;scan_status=100;
- break;
-
- default: Test(); scan_status=0;
- break;
- }
- }
- void Turn_Forward(void)
- {
- MOTO1_IN1=0;
- MOTO1_IN2=1;
- MOTO2_IN1=0;
- MOTO2_IN2=1;
- a=80;
- b=70;
- }
- void Turn_Right(void)
- {
- MOTO1_IN1=0;
- MOTO1_IN2=1;
- MOTO2_IN1=0;
- MOTO2_IN2=1;
- a=70;
- b=85;
- }
- void Turn_Left(void)
- {
- MOTO1_IN1=0;
- MOTO1_IN2=1;
- MOTO2_IN1=0;
- MOTO2_IN2=1;
- a=85;
- b=70;
- }
- void Turn_SRight(void)
- {
- MOTO1_IN1=0;
- MOTO1_IN2=1;
- MOTO2_IN1=0;
- MOTO2_IN2=0;
- // TR0=1;
- a=80;
- b=70;
- }
- void Turn_SLeft(void)
- {
- MOTO1_IN1=0;
- MOTO1_IN2=0;
- MOTO2_IN1=0;
- MOTO2_IN2=1;
- a=80;
- b=70;
- }
- void Turn_Stop(void)
- {
- MOTO1_IN1=0;
- MOTO1_IN2=0;
- MOTO2_IN1=0;
- MOTO2_IN2=0;
- }
- void Test(void)
- {
- if(flag==0)
- Turn_Stop();
- if(flag==1)
- Turn_SLeft();
- if(flag==2)
- Turn_SRight();
- }
- void timer0() interrupt 1
- {
-
- TR0 = 0;
-
- i++;
- TH0=65517/256;
- TL0=65517%256;
-
- iCnt++;
- if(mode == 1)
- {
- displayvalue=scan_status;
- //displayvalue=1234;
- }
- else
- {
- displayvalue=(int)distance;
-
- }
-
- if(iCnt == 10)
- {
- DIS_DATA = 0xff; //消影
- cs0 = 1;
- cs1 = 1;
- cs2 = 1;
- cs3 = 0;
- DIS_DATA = DIS_TABLE[displayvalue % 10];
- }
- if(iCnt == 20)
- {
- DIS_DATA = 0xff; //消影
- cs0 = 1;
- cs1 = 1;
- cs2 = 0;
- cs3 = 1;
- DIS_DATA = DIS_TABLE[displayvalue /10 % 10];
-
- }
- if(iCnt == 30)
- {
- DIS_DATA = 0xff; //消影
- cs0 = 1;
- cs1 = 0;
- cs2 = 1;
- cs3 = 1;
- DIS_DATA = DIS_TABLE[displayvalue /100 % 10];
- }
- if(iCnt == 40)
- {
- // iCnt == 0;
- DIS_DATA = 0xff; //消影
- cs0 = 0;
- cs1 = 1;
- cs2 = 1;
- cs3 = 1;
- DIS_DATA = DIS_TABLE[displayvalue /1000 % 10];
- }
- if(iCnt == 50)
- {
- iCnt == 0;
- DIS_DATA = 0xff; //消影
- }
- if(i==a)
- {
- MOTO1_PWM = 1;
- }
- if(i==b)
- {
- MOTO2_PWM = 1;
- }
-
- if(i==100)
- {
- i=0;
- MOTO1_PWM = 0;
- MOTO2_PWM = 0;
- RED_SCAN();
-
- }
- TR0 = 1;
- }
- void Key_Scan(void)
- {
- if(Key_1 == 0)
- {
- mode = 1;
- while(!Key_1);
- }
- if(Key_2 == 0)
- {
- mode = 2;
- while(!Key_2);
- }
- }
- void send_wave()
- {
- Tx = 1; //10us的高电平触发
- Delay10us();
- Tx = 0;
- flag_csb_juli = 0;
- TH1 = 0; //给定时器0清零
- TL1 = 0;
- TR1 = 0; //关定时器0定时
- while(Rx==0); //当Echo为零时等待
- TR1=1;
- while(Rx==1) //当Echo为1计数并等待
- {
- flag_csb_juli = 1;
- }
- if(flag_csb_juli == 1)
- {
- TR1=0; //关定时器0定时
- distance =TH1 * 256 + TL1; //读出定时器1的定时时间
- distance *= 0.017; // 0.017 = 340M / 2 = 170M = 0.017cM 算出来是c米
- // distance单位是厘米
- if((distance < 20)) //距离 = 速度 * 时间
- {
- Turn_SLeft(); //如果大于3.8m就超出超声波的量程
-
- }
- if((distance > 20)) //距离 = 速度 * 时间
- {
- Turn_Forward(); //如果大于3.8m就超出超声波的量程
- }
- }
- }
- void Delay10us() //@11.0592MHz
- {
- unsigned char i;
- _nop_();
- i = 25;
- while (--i);
- }
复制代码
所有资料51hei提供下载:
共享程序
循迹加数码管.rar
(1.6 KB, 下载次数: 154)
【Realplay】HC-SR04 超声波模块 测距模块 超声波 传感器 送资料.rar
(1.67 MB, 下载次数: 163)
|