/***********************头文件**************************/
#include <regx52.h>
/************************位声明*************************/
/*红外信号位*/
sbit Inf_Headr = P0^5;
sbit Inf_Right_1 = P0^4;
sbit Inf_Right_2 = P0^3;
sbit Inf_Center = P0^2;
sbit Inf_Left_1 = P0^1;
sbit Inf_Left_2 = P0^0;
/*寻路舵机*/
sbit Motor = P3^5;
/*测距模块信号源*/
sbit Trig = P2^1;
sbit Echo = P2^2;
/*µ电机驱动模块*/
sbit EN_Right = P1^5;
sbit EN_Left = P1^0;
sbit in_Right1 = P1^4;
sbit in_Right2 = P1^3;
sbit in_Left1 = P1^2;
sbit in_Left2 = P1^1;
/*LED*/
sbit LED = P2^0;
/***********蓝牙模块串口*********/
void HC_06_Init(); //串口初始化
void HC_06_SendData(unsigned char Data); //发送
void HC_06_Interrupt(); //串口中断
/*************************************/
/***********Pwm调速**********/
void Turn_Right(unsigned char pwm); //右转
void Turn_Left(unsigned char pwm); //左转
void Go_Forword(unsigned char speed); //前进
void Go_Back(unsigned char speed); //后退
void Stop(); //停
void Timer0_PWM_init(); //PWM
void Timer0_Interrupt(); //
/**************************************/
/**************舵机偏转角度*************/
void Mot_Angle(unsigned char _angle);
/***************************************/
/**************LED**************/
void Control_LED(unsigned char led_switch);
/*************红外寻迹模式***************/
void Inf_mode();
/***************************************/
/*****************变量声明**************/
unsigned char PWM=10;
unsigned char pwm_count; //PWM计时
unsigned char motor_count; //舵机计时
unsigned char angle;
bit mode;
/*************************/
/********************************************************/
/*************************主程序*************************/
/********************************************************/
void main()
{
//初始化
HC_06_Init();
Timer0_PWM_init();
while(1) //主循环判断红外状态
{
if(mode==1)
Inf_mode();
}
}
//函数定义
//蓝牙串口init
void HC_06_Init()
{
PCON |= 0x80;
SCON = 0x50;
TMOD &= 0x0F;
TMOD |= 0x20;
TL1 = 0xFA;
TH1 = 0xFA;
ET1 = 0;
TR1 = 1;
ES=1;
EA=1;
}
//发送
void HC_06_SendData(unsigned char Data)
{
TI=1;
SBUF=Data;
while(TI==0);
TI=0;
}
//接收---控制
void HC_06_Interrupt() interrupt 4
{
if(RI==1)
{
if(SBUF==0x41)
Go_Forword(PWM); //前进
else if(SBUF==0x42)
Turn_Left(PWM); //左转
else if(SBUF==0x43)
Stop(); //停
else if(SBUF==0x44)
Turn_Right(PWM); //右转
else if(SBUF==0x45)
Go_Back(PWM); //后退
else if(SBUF==0x76)
Control_LED(0); //LED开
else if(SBUF==0x2d)
{
PWM-=5; //减速
HC_06_SendData(PWM);
}
else if(SBUF==0x2b)
{
PWM+=5; //加速
HC_06_SendData(PWM);
}
else if(SBUF==0x51) //红外模式
mode=1;
else if(SBUF==0x57) //蓝牙模式
mode=0;
RI=0;
}
}
void Turn_Right(unsigned char pwm)
{
PWM=pwm;
in_Right1=1;
in_Right2=0;
in_Left1=1;
in_Left2=0;
}
void Turn_Left(unsigned char pwm)
{
PWM=pwm;
in_Right1=0;
in_Right2=1;
in_Left1=0;
in_Left2=1;
}
void Go_Forword(unsigned char speed)
{
PWM=speed;
in_Right1=0;
in_Right2=1;
in_Left1=1;
in_Left2=0;
}
void Go_Back(unsigned char speed)
{
PWM=speed;
in_Right1=1;
in_Right2=0;
in_Left1=0;
in_Left2=1;
}
void Stop()
{
in_Left1=0;
in_Left2=0;
in_Right1=0;
in_Right2=0;
}
void Timer0_PWM_init()
{
TMOD &= 0xF0;
TL0 = 0xF5;
TH0 = 0xF5;
TF0 = 0;
TR0 = 1;
ET0=1;
EA=1;
PT0=0;
}
void Timer0_Interrupt() interrupt 1
{
TL0 = 0xF5;
TH0 = 0xF5;
pwm_count++;
if(pwm_count<PWM)
{
EN_Right=1;
EN_Left=1;
}
else
{
EN_Right=0;
EN_Left=0;
}
//
if(pwm_count>=100)
pwm_count=0;
}
void Mot_Angle(unsigned char _angle)
{
angle=_angle;
}
void Control_LED(unsigned char led_switch)
{
switch(led_switch)
{
case 0:
LED=0;
case 1:
LED=1;
}
}
void Inf_mode()
{
if(Inf_Center==1&&Inf_Right_1==0&&Inf_Right_2==0&& Inf_Left_1==0&&Inf_Left_2==0)
Go_Forword(16);
else if(Inf_Center==0&& Inf_Right_1==0&&Inf_Right_2==0&&Inf_Left_1==0&&Inf_Left_2==0)
Go_Forword(14);
else if(Inf_Center==1&& Inf_Right_1==1&&Inf_Right_2==0&&Inf_Left_1==0&&Inf_Left_2==0)
Turn_Right(30);
else if(Inf_Center==1&& Inf_Right_1==1&&Inf_Right_2==1&&Inf_Left_1==0&&Inf_Left_2==0)
Turn_Right(35);
else if(Inf_Center==1&& Inf_Right_1==0&&Inf_Right_2==1&&Inf_Left_1==0&&Inf_Left_2==0)
Turn_Right(35);
else if(Inf_Center==0&& Inf_Right_1==1&&Inf_Right_2==0&&Inf_Left_1==0&&Inf_Left_2==0)
Turn_Right(30);
else if(Inf_Center==0&& Inf_Right_1==1&&Inf_Right_2==1&&Inf_Left_1==0&&Inf_Left_2==0)
Turn_Right(35);
else if(Inf_Center==0&& Inf_Right_1==0&&Inf_Right_2==1&&Inf_Left_1==0&&Inf_Left_2==0)
Turn_Right(35);
else if(Inf_Center==1&& Inf_Right_1==0&&Inf_Right_2==0&&Inf_Left_1==1&&Inf_Left_2==0)
Turn_Left(30);
else if(Inf_Center==1&& Inf_Right_1==0&&Inf_Right_2==0&&Inf_Left_1==0&&Inf_Left_2==1)
Turn_Left(35);
else if(Inf_Center==1&& Inf_Right_1==0&&Inf_Right_2==0&&Inf_Left_1==1&&Inf_Left_2==1)
Turn_Left(35);
else if(Inf_Center==0&& Inf_Right_1==0&&Inf_Right_2==0&&Inf_Left_1==1&&Inf_Left_2==0)
Turn_Left(30);
else if(Inf_Center==0&& Inf_Right_1==0&&Inf_Right_2==0&&Inf_Left_1==0&&Inf_Left_2==1)
Turn_Left(35);
else if(Inf_Center==0&& Inf_Right_1==0&&Inf_Right_2==0&&Inf_Left_1==1&&Inf_Left_2==1)
Turn_Left(35);
else if(Inf_Center==1&& Inf_Right_1==1&&Inf_Right_2==1&&Inf_Left_1==1&&Inf_Left_2==1)
Stop();
if(Inf_Headr==0)
{
Turn_Left(35);
}
}
C51蓝牙串口&循迹小车代码实现
最新推荐文章于 2024-08-14 06:55:19 发布