单片机智能小车
项目完整效果:
mc6c遥控器控制
#include<reg52.h>
#include<intrins.h>
#define SIGNAL_THRESHOLD 1382
#define ERROR_RANGE 10
sbit signal=P3^2;
sbit IN1=P1^0;
sbit IN2=P1^1;
sbit IN3=P1^2;
sbit IN4=P1^3;
unsigned int signal_time;
void init_Timer0(void);
void Timer0_process(void);
void init_Interrupt0(void);
void Interrupt0_process(void);
void forward(void);
void backward(void);
void stop(void);
void main(void)
{
init_Timer0();
init_Interrupt0();
while(1)
{
if(signal==0)
TR0=1;
if((signal_time<SIGNAL_THRESHOLD+ERROR_RANGE)&&(signal_time>SIGNAL_THRESHOLD-ERROR_RANGE))
{
stop();
}
else
{
signal_time>SIGNAL_THRESHOLD+ERROR_RANGE? forward() : backward();
}
}
}
void init_Timer0(void)
{
TMOD=0x09;
TH0=0x00;
TL0=0x00;
ET0=1;
}
void init_Interrupt0(void)
{
EA=1;
EX0=1;
IT0=1;
}
void Timer0_process(void) interrupt 1
{
TH0=0x00;
TL0=0x00;
}
void Interrupt0_process(void) interrupt 0
{
signal_time=0;
signal_time|=TH0;
signal_time<<=8;
signal_time|=TL0;
TH0=0x00;
TL0=0x00;
}
void forward(void)
{
IN1=0;
IN2=1;
IN3=0;
IN4=0;
}
void backward(void)
{
IN1=1;
IN2=0;
IN3=0;
IN4=1;
}
void stop(void)
{
IN1=1;
IN2=1;
IN3=1;
IN4=1;
}
红外协议控制
#include<reg52.h>
#include<intrins.h>
sbit IN1=P1^0;
sbit IN2=P1^1;
sbit IN3=P1^2;
sbit IN4=P1^3;
sbit servo_port=P2^7;
#define INITIAL_angle 15
#define BIAS_ANGLE 2
#define LEFT_ANGLE INITIAL_angle-BIAS_ANGLE
#define RIGHT_ANGLE INITIAL_angle+BIAS_ANGLE
unsigned char status_flag;
unsigned char irtime;
bit irpro_ok,ir_ok;
unsigned char IRcode[4],IRdata[33];
void servo(unsigned char t);
void delay_servo(unsigned char _100us_count);
void delay(void);
void backward(void);
void forward(void);
void left(void);
void right(void);
void brake(void);
void TIME0init(void);
void EX0init();
void TIME0_interrupt(void);
void EX0_interrupt(void);
void Ir_work(void);
void Ircodepro(void);
void initial_status_test(void);
void main(void)
{
EX0init();
TIME0init();
initial_status_test();
while(1)
{
if(ir_ok)
{
Ircodepro();
ir_ok=0;
}
if(irpro_ok)
{
Ir_work();
irpro_ok=0;
}
}
}
void initial_status_test(void)
{
servo(INITIAL_angle);
servo(INITIAL_angle);
forward();
delay();
delay();
brake();
backward();
delay();
delay();
brake();
}
void TIME0init(void)
{
TMOD=0x02;
TH0=0x00;
TL0=0x00;
ET0=1;
TR0=1;
}
void EX0init()
{
IT0=1;
EX0=1;
EA=1;
}
void TIME0_interrupt(void) interrupt 1 using 1
{
irtime++;
}
void EX0_interrupt(void) interrupt 0
{
static unsigned char i;
static bit startflag;
if(startflag)
{
if(irtime>40&&irtime<60)
i=0;
IRdata[i]=irtime;
irtime=0;
i++;
if(i==33)
{
ir_ok=1;
startflag=0;
}
}
else
{
irtime=0;
startflag=1;
}
}
void Ir_work(void)
{
if (IRcode[0]==0x00&&IRcode[1]==0xFF)
{
switch(IRcode[2])
{
case 0x18:
{
switch (status_flag)
{
case 0x01:brake();forward();break;
case 0x04:forward();break;
default:servo(INITIAL_angle);forward();
}
break;
}
case 0x08:
{
switch (status_flag)
{
case 0x00:left();forward();break;
case 0x01:left();backward();break;
default:left();
}
break;
}
case 0x1C:
{
brake();break;
}
case 0x5A:
{
switch (status_flag)
{
case 0x00:right();forward();break;
case 0x01:right();backward();break;
default:right();
}
break;
}
case 0x52:
{
switch (status_flag)
{
case 0x00:brake();backward();break;
case 0x04:backward();break;
default:servo(INITIAL_angle);backward();
}
break;
}
default:break;
}
}
}
void Ircodepro(void)
{
unsigned char i, j, k;
unsigned char cord,value;
k=1;
for(i=0;i<4;i++)
{
for(j=1;j<=8;j++)
{
cord=IRdata[k];
if(cord>7)
value|=0x80;
if(j<8)
{
value>>=1;
}
k++;
}
IRcode[i]=value;
value=0;
}
irpro_ok=1;
}
void servo(unsigned char t)
{
unsigned char i;
for(i=5;i>0;i--)
{
servo_port=1;
delay_servo(t);
servo_port=0;
delay_servo(200-t);
}
}
void delay_servo(unsigned char _100us_count)
{
unsigned char a,b;
while(_100us_count)
{
for(b=1;b>0;b--)
for(a=43;a>0;a--);
_100us_count--;
}
}
void delay(void)
{
unsigned char a,b,c;
for(c=75;c>0;c--)
for(b=196;b>0;b--)
for(a=1;a>0;a--);
}
void forward(void)
{
status_flag=0x00;
IN1=1;
IN2=0;
IN3=0;
IN4=1;
}
void backward(void)
{
status_flag=0x01;
IN1=0;
IN2=1;
IN3=1;
IN4=0;
}
void left(void)
{
status_flag=0x02;
servo(LEFT_ANGLE);
}
void right(void)
{
status_flag=0x03;
servo(RIGHT_ANGLE);
}
void brake(void)
{
status_flag=0x04;
IN1=1;
IN2=1;
IN3=1;
IN4=1;
}
蓝牙协议控制 蓝牙串口助手app下载:我的Github主页
#include<reg52.h>
#include<intrins.h>
sbit IN1=P1^0;
sbit IN2=P1^1;
sbit IN3=P1^2;
sbit IN4=P1^3;
sbit servo_port=P2^7;
#define INITIAL_angle 15
#define BIAS_ANGLE 2
#define LEFT_ANGLE INITIAL_angle-BIAS_ANGLE
#define RIGHT_ANGLE INITIAL_angle+BIAS_ANGLE
unsigned char status_flag;
unsigned char irtime;
bit irpro_ok,ir_ok;
unsigned char IRcode[4],IRdata[33];
void servo(unsigned char t);
void delay_servo(unsigned char _100us_count);
void delay(void);
void backward(void);
void forward(void);
void left(void);
void right(void);
void brake(void);
void TIME0init(void);
void EX0init();
void TIME0_interrupt(void);
void EX0_interrupt(void);
void Ir_work(void);
void Ircodepro(void);
void initial_status_test(void);
void main(void)
{
EX0init();
TIME0init();
initial_status_test();
while(1)
{
if(ir_ok)
{
Ircodepro();
ir_ok=0;
}
if(irpro_ok)
{
Ir_work();
irpro_ok=0;
}
}
}
void initial_status_test(void)
{
servo(INITIAL_angle);
servo(INITIAL_angle);
forward();
delay();
delay();
brake();
backward();
delay();
delay();
brake();
}
void TIME0init(void)
{
TMOD=0x02;
TH0=0x00;
TL0=0x00;
ET0=1;
TR0=1;
}
void EX0init()
{
IT0=1;
EX0=1;
EA=1;
}
void TIME0_interrupt(void) interrupt 1 using 1
{
irtime++;
}
void EX0_interrupt(void) interrupt 0
{
static unsigned char i;
static bit startflag;
if(startflag)
{
if(irtime>40&&irtime<60)
i=0;
IRdata[i]=irtime;
irtime=0;
i++;
if(i==33)
{
ir_ok=1;
startflag=0;
}
}
else
{
irtime=0;
startflag=1;
}
}
void Ir_work(void)
{
if (IRcode[0]==0x00&&IRcode[1]==0xFF)
{
switch(IRcode[2])
{
case 0x18:
{
switch (status_flag)
{
case 0x01:brake();forward();break;
case 0x04:forward();break;
default:servo(INITIAL_angle);forward();
}
break;
}
case 0x08:
{
switch (status_flag)
{
case 0x00:left();forward();break;
case 0x01:left();backward();break;
default:left();
}
break;
}
case 0x1C:
{
brake();break;
}
case 0x5A:
{
switch (status_flag)
{
case 0x00:right();forward();break;
case 0x01:right();backward();break;
default:right();
}
break;
}
case 0x52:
{
switch (status_flag)
{
case 0x00:brake();backward();break;
case 0x04:backward();break;
default:servo(INITIAL_angle);backward();
}
break;
}
default:break;
}
}
}
void Ircodepro(void)
{
unsigned char i, j, k;
unsigned char cord,value;
k=1;
for(i=0;i<4;i++)
{
for(j=1;j<=8;j++)
{
cord=IRdata[k];
if(cord>7)
value|=0x80;
if(j<8)
{
value>>=1;
}
k++;
}
IRcode[i]=value;
value=0;
}
irpro_ok=1;
}
void servo(unsigned char t)
{
unsigned char i;
for(i=5;i>0;i--)
{
servo_port=1;
delay_servo(t);
servo_port=0;
delay_servo(200-t);
}
}
void delay_servo(unsigned char _100us_count)
{
unsigned char a,b;
while(_100us_count)
{
for(b=1;b>0;b--)
for(a=43;a>0;a--);
_100us_count--;
}
}
void delay(void)
{
unsigned char a,b,c;
for(c=75;c>0;c--)
for(b=196;b>0;b--)
for(a=1;a>0;a--);
}
void forward(void)
{
status_flag=0x00;
IN1=1;
IN2=0;
IN3=0;
IN4=1;
}
void backward(void)
{
status_flag=0x01;
IN1=0;
IN2=1;
IN3=1;
IN4=0;
}
void left(void)
{
status_flag=0x02;
servo(LEFT_ANGLE);
}
void right(void)
{
status_flag=0x03;
servo(RIGHT_ANGLE);
}
void brake(void)
{
status_flag=0x04;
IN1=1;
IN2=1;
IN3=1;
IN4=1;
}
超声波测距自动避障行驶
#include<reg52.h>
#include <intrins.h>
#define STRAIGHT_DISTANCE_SETTING 30
#define ROUND_DISTANCE_SETTING 15
#define INITIAL_angle 14
#define BIAS_ANGLE 5
#define LEFT_ANGLE INITIAL_angle+BIAS_ANGLE
#define RIGHT_ANGLE INITIAL_angle-BIAS_ANGLE
sbit servo_port=P2^7;
sbit ECHO=P3^2;
sbit TRIG=P1^4;
sbit IN1=P1^0;
sbit IN2=P1^1;
sbit IN3=P1^2;
sbit IN4=P1^3;
unsigned char time_H,time_L;
int distance,time,left_distance,right_distance;
void delay(void);
void forward(void);
void backward(void);
void left(void);
void right(void);
void brake(void);
void compute_distance(void);
void DELAY100ms(void);
void DELAY15us(void);
void detect_distance(void);
void initTIMER0(void);
void servo(unsigned char t);
void delay_servo(unsigned char _100us_count);
void detect_left_distance(void);
void detect_right_distance(void);
void main(void)
{
initTIMER0();
servo(INITIAL_angle);
while(1)
{
detect_distance();
while(distance < STRAIGHT_DISTANCE_SETTING)
{
brake();
backward();
brake();
detect_left_distance();
left_distance=distance;
detect_right_distance();
right_distance=distance;
servo(INITIAL_angle);
if((left_distance>ROUND_DISTANCE_SETTING)||(right_distance>ROUND_DISTANCE_SETTING))
{
left_distance>right_distance? left() : right();
}
else
{
backward();
brake();
brake();
left();
}
detect_distance();
}
forward();
}
}
void servo(unsigned char t)
{
unsigned char i;
for(i=25;i>0;i--)
{
servo_port=1;
delay_servo(t);
servo_port=0;
delay_servo(200-t);
}
}
void delay_servo(unsigned char _100us_count)
{
unsigned char a,b;
while(_100us_count)
{
for(b=1;b>0;b--)
for(a=89;a>0;a--);
_100us_count--;
}
}
void detect_distance(void)
{
EX0=1;
TR0=1;
TRIG=1;
DELAY15us();
TRIG=0;
DELAY100ms();
EX0=0;
compute_distance();
}
void detect_left_distance(void)
{
servo(LEFT_ANGLE);
EX0=1;
TR0=1;
TRIG=1;
DELAY15us();
TRIG=0;
servo(LEFT_ANGLE);
servo(LEFT_ANGLE);
EX0=0;
compute_distance();
}
void detect_right_distance(void)
{
servo(RIGHT_ANGLE);
EX0=1;
TR0=1;
TRIG=1;
DELAY15us();
TRIG=0;
servo(RIGHT_ANGLE);
servo(RIGHT_ANGLE);
EX0=0;
compute_distance();
}
void INTER0_process(void) interrupt 0
{
TR0=0;
time_H=TH0;
time_L=TL0;
TH0=0;
TL0=0;
}
void initTIMER0(void)
{
TMOD=0x09;
TH0=0x00;
TL0=0x00;
EA=1;
IT0=1;
}
void DELAY15us(void)
{
unsigned char d;
for(d=12;d>0;d--);
}
void DELAY100ms(void)
{
unsigned char a,b,c;
for(c=131;c>0;c--)
for(b=156;b>0;b--)
for(a=3;a>0;a--);
}
void compute_distance(void)
{
time=0;
time|=time_H;
time<<=8;
time|=time_L;
distance=time*0.0093;
}
void delay(void)
{
unsigned char a,b,c;
for(c=75;c>0;c--)
for(b=196;b>0;b--)
for(a=1;a>0;a--);
}
void forward(void)
{
IN1=1;
IN2=0;
IN3=1;
IN4=0;
delay();
}
void backward(void)
{
IN1=0;
IN2=1;
IN3=0;
IN4=1;
delay();
delay();
delay();
delay();
}
void left(void)
{
IN1=1;
IN2=0;
IN3=0;
IN4=1;
delay();
delay();
delay();
brake();
}
void right(void)
{
IN1=0;
IN2=1;
IN3=1;
IN4=0;
delay();
delay();
delay();
brake();
}
void brake(void)
{
IN1=1;
IN2=1;
IN3=1;
IN4=1;
delay();
delay();
delay();
}
蓝牙协议控制小车机械臂
#include<reg52.h>
#include <intrins.h>
#include <stdio.h>
#include <math.h>
typedef unsigned char uchar;
typedef unsigned int uint;
sbit scl=P3^6;
sbit sda=P3^7;
#define PCA9685_adrr 0x80
#define PCA9685_SUBADR1 0x2
#define PCA9685_SUBADR2 0x3
#define PCA9685_SUBADR3 0x4
#define PCA9685_MODE1 0x00
#define PCA9685_PRESCALE 0xFE
#define LED0_ON_L 0x06
#define LED0_ON_H 0x07
#define LED0_OFF_L 0x08
#define LED0_OFF_H 0x09
#define ALLLED_ON_L 0xFA
#define ALLLED_ON_H 0xFB
#define ALLLED_OFF_L 0xFC
#define ALLLED_OFF_H 0xFD
unsigned char remote_command[7];
unsigned char data_index=0;
bit command_flag;
void delayms(uint z);
void delayus();
void init();
void start();
void stop();
void ACK();
void write_byte(uchar byte);
uchar PCA9685_read(uchar address);
uchar read_byte();
void PCA9685_write(uchar address,uchar date);
void reset(void) ;
void setPWMFreq(float freq);
void servo_control(uchar num, uchar angle);
void command_process(void);
void init_bluetooth_timer0(void);
void bluetooth_interrupt_process(void);
void begin(void);
void servo_angle(void);
uchar servo1=90;
uchar servo2=90;
uchar servo3=90;
uchar servo4=90;
void main()
{
init();
begin();
setPWMFreq(50);
init_bluetooth_timer0();
servo_angle();
while(1)
{
if(command_flag)
{
command_process();
servo_angle();
}
}
}
void servo_angle(void)
{
servo_control(0,servo1);
delayms(2);
servo_control(1,servo2);
delayms(2);
servo_control(2,servo3);
delayms(2);
servo_control(3,servo4);
}
void command_process(void)
{
if(remote_command[0]==0xA5)
{
servo1=remote_command[1]+90;
servo2=remote_command[2]+90;
servo3=remote_command[3]+90;
servo4=remote_command[4]+90;
}
command_flag=0;
}
void init_bluetooth_timer0(void)
{
TMOD=0x20;
TH1=253;
TL1=TH1;
SCON=0x50;
TR1=1;
EA=1;
ES=1;
}
void bluetooth_interrupt_process(void) interrupt 4
{
if(RI)
{
remote_command[data_index]=SBUF;
data_index++;
if(data_index==7)
{
data_index=0;
command_flag=1;
}
RI=0;
}
}
void delayms(uint z)
{
unsigned char a,b;
while(z)
{
for(b=102;b>0;b--)
for(a=3;a>0;a--);
z--;
}
}
void delayus()
{
_nop_();
_nop_();
}
void init()
{
sda=1;
delayus();
scl=1;
delayus();
}
void start()
{
sda=1;
delayus();
scl=1;
delayus();
sda=0;
delayus();
scl=0;
delayus();
}
void stop()
{
sda=0;
delayus();
scl=1;
delayus();
sda=1;
delayus();
}
void ACK()
{
uint i;
scl=1;
delayus();
while((sda=1)&&(i<255))
i++;
scl=0;
delayus();
}
void write_byte(uchar byte)
{
uchar i,temp;
temp=byte;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delayus();
sda=CY;
delayus();
scl=1;
delayus();
}
scl=0;
delayus();
sda=1;
delayus();
}
uchar read_byte()
{
uchar i,j,k;
scl=0;
delayus();
sda=1;
delayus();
for(i=0;i<8;i++)
{
delayus();
scl=1;
delayus();
if(sda==1)
{
j=1;
}
else j=0;
k=(k<< 1)|j;
scl=0;
}
delayus();
return k;
}
void PCA9685_write(uchar address,uchar date)
{
start();
write_byte(PCA9685_adrr);
ACK();
write_byte(address);
ACK();
write_byte(date);
ACK();
stop();
}
uchar PCA9685_read(uchar address)
{
uchar date;
start();
write_byte(PCA9685_adrr);
ACK();
write_byte(address);
ACK();
start();
write_byte(PCA9685_adrr|0x01);
ACK();
date=read_byte();
stop();
return date;
}
void reset(void)
{
PCA9685_write(PCA9685_MODE1,0x0);
}
void begin(void)
{
reset();
}
void setPWMFreq(float freq)
{
uint prescale,oldmode,newmode;
float prescaleval;
freq *= 0.92;
prescaleval = 25000000;
prescaleval /= 4096;
prescaleval /= freq;
prescaleval -= 1;
prescale = floor(prescaleval + 0.5);
oldmode = PCA9685_read(PCA9685_MODE1);
newmode = (oldmode&0x7F) | 0x10;
PCA9685_write(PCA9685_MODE1, newmode);
PCA9685_write(PCA9685_PRESCALE, prescale);
PCA9685_write(PCA9685_MODE1, oldmode);
delayms(2);
PCA9685_write(PCA9685_MODE1, oldmode | 0xa1);
}
void servo_control(uchar num, uchar angle)
{
uint off = 102.4+2.275555556*angle;
PCA9685_write(LED0_ON_L+4*num,0);
PCA9685_write(LED0_ON_H+4*num,0);
PCA9685_write(LED0_OFF_L+4*num,off);
PCA9685_write(LED0_OFF_H+4*num,off>>8);
}
|