標(biāo)題:
51單片機(jī)尋跡小車代碼資料
[打印本頁(yè)]
作者:
18223313969
時(shí)間:
2019-6-19 16:35
標(biāo)題:
51單片機(jī)尋跡小車代碼資料
基于51單片機(jī)循跡小車的代碼,傳感器為7個(gè)光電對(duì)管,前輪采用舵機(jī)轉(zhuǎn)向,后輪為驅(qū)動(dòng)輪,同時(shí)采用差速輔助前輪轉(zhuǎn)向
單片機(jī)源程序如下:
#include<reg52.h>
#define u8 unsigned char
#define u16 unsigned int
/*************電機(jī)速度設(shè)定***********/
#define speed_Left3 0
#define speed_Left2 20
#define speed_Left1 20
#define speed_Middle 20
#define speed_Right1 20
#define speed_Right2 20
#define speed_Right3 0
/*************舵機(jī)轉(zhuǎn)彎角度設(shè)定***********/
#define Turn_Left3 13
#define Turn_Left2 14
#define Turn_Left1 17
#define Middle 18
#define Turn_Right1 19
#define Turn_Right2 22
#define Turn_Right3 23
/***********電機(jī)引腳定義***************/
sbit left_pwm=P1^2; //左電機(jī)pwm
sbit left_motor0=P1^3; //左電機(jī)負(fù)極
sbit left_motor1=P1^4; //左電機(jī)正極
sbit right_pwm=P1^5; //右電機(jī)pwm
sbit right_motor1=P1^6; //右電機(jī)負(fù)極
sbit right_motor0=P1^7; //右電機(jī)正極
/***********舵機(jī)引腳定義***************/
sbit servo=P0^7;
/***********光電對(duì)管引腳定義***************/
sbit right_led3=P2^0; //最右側(cè)光電對(duì)管
sbit right_led2=P2^1; //次右側(cè)光電對(duì)管
sbit right_led1=P2^2; //內(nèi)右側(cè)光電對(duì)管
sbit middle_led=P2^3; //中間光電對(duì)管
sbit left_led1 =P2^4; //內(nèi)左側(cè)光電對(duì)管
sbit left_led2 =P2^5; //次左側(cè)光電對(duì)管
sbit left_led3 =P2^6; //最左側(cè)光電對(duì)管
sbit restart=P1^0;
void motor_pwm(u8 left_compare,u8 right_compare);
void servo_pwm(u8 sevor_compare);
void T0_init();
void led_scan();
u16 flag_num=0;
u8 servo_num=0;
u8 motor_num=0; //定時(shí)器計(jì)數(shù)
u8 direction;
u8 left_speed;
u8 right_speed;
/********************主函數(shù)***************************/
void main()
{
u8 n;
T0_init();
left_motor0=right_motor0=0;
left_motor1=right_motor1=1; //電機(jī)正轉(zhuǎn)
restart=0;
while(1)
{
led_scan();
for(servo_num=0,motor_num=0,n=0;n<1;)
{
servo_pwm(direction);
motor_pwm(left_speed,right_speed);
if(servo_num>=200) n++;
}
}
}
/*********************定時(shí)器0和1初始化函數(shù)*********************************/
void T0_init()
{
TMOD=0X11; //定時(shí)器0和1方式1:16位定時(shí)器/計(jì)數(shù)器
TH0=(65536-100)/256; //設(shè)置計(jì)數(shù)器0的高8位
TL0=(65536-100)%256; //設(shè)置計(jì)數(shù)器0的低8位
TH1=(65536-10000)/256; //設(shè)置計(jì)數(shù)器1的高8位
TL1=(65536-10000)%256; //設(shè)置計(jì)數(shù)器1的低8位
EA=1; //全局中斷使能
ET0=1; //定時(shí)器0中斷使能
ET1=1; //定時(shí)器1中斷使能
TR0=1; //定時(shí)器0使能
TR1=0; //定時(shí)器1失能
}
/*********************定時(shí)器0中斷服務(wù)函數(shù)*******************************/
void T0_time() interrupt 1 //每0.1ms加1
{
TH0=(65536-100)/256;
TL0=(65536-100)%256;
servo_num++;
motor_num++;
}
/*********************定時(shí)器1中斷服務(wù)函數(shù)*******************************/
void T1_time() interrupt 3 //每10ms加1
{
TH1=(65536-10000)/256;
TL1=(65536-10000)%256;
flag_num++;
}
/*****************舵機(jī)PWM函數(shù)*************************/
void servo_pwm(u8 compare1)
{
if(servo_num<compare1) servo=1; //高電平持續(xù)時(shí)間t=compare1*0.1 ms
else if(servo_num<200) servo=0;
}
/*****************電機(jī)PWM函數(shù)*************************/
void motor_pwm(u8 left_compare,u8 right_compare)
{
if(motor_num<left_compare) left_pwm=1;
else if(motor_num<100) left_pwm=0; //左電機(jī)占空比
if(motor_num<right_compare) right_pwm=1;
else if(motor_num<100) right_pwm=0; //右電機(jī)占空比
if(motor_num>=100) motor_num=0;
}
/*************光電對(duì)管掃描函數(shù)***************************/
void led_scan()
{
u8 n=0;
static u8 white_num=0;
static u8 stop=0;
if(restart==1) stop=0;
if(stop==0)
{
/********************中間光電對(duì)管檢測(cè)到黑線則直行**********************/
if(middle_led==1)
{ direction=Middle; left_speed=speed_Middle; right_speed=speed_Middle; n++;
left_motor0=right_motor0=0;
left_motor1=right_motor1=1;
}
/**************最右(左)邊光電對(duì)管檢測(cè)到黑線則大左轉(zhuǎn)*********************/
else if(right_led3==1)
{ direction=Turn_Right3; left_speed=speed_Middle; right_speed=speed_Right3; n++;
left_motor0=right_motor1=0;
left_motor1=right_motor0=1;
}
else if(left_led3==1)
{ direction=Turn_Left3; left_speed=speed_Left3; right_speed=speed_Middle; n++;
left_motor1=right_motor0=0;
left_motor0=right_motor1=1;
}
/**************次右(左)邊光電對(duì)管檢測(cè)到黑線則次左轉(zhuǎn)*********************/
else if(right_led2==1)
{ direction=Turn_Right2; left_speed=speed_Middle; right_speed=speed_Right2; n++;
left_motor0=right_motor0=0;
left_motor1=right_motor1=1;
}
else if(left_led2==1)
{ direction=Turn_Left2; left_speed=speed_Left2; right_speed=speed_Middle; n++;
left_motor0=right_motor0=0;
left_motor1=right_motor1=1;
}
/**************內(nèi)右(左)邊光電對(duì)管檢測(cè)到黑線則小左轉(zhuǎn)*********************/
else if(right_led1==1)
{ direction=Turn_Right1; left_speed=speed_Middle; right_speed=speed_Right1; n++;
left_motor0=right_motor0=0;
left_motor1=right_motor1=1;
}
else if(left_led1==1)
{ direction=Turn_Left1; left_speed=speed_Left1; right_speed=speed_Middle; n++;
left_motor0=right_motor0=0;
left_motor1=right_motor1=1;
}
}
///**************************檢測(cè)是否應(yīng)該停車********************************/
// if(n>=4) //如果3個(gè)以上的光電對(duì)管檢測(cè)到黑線
// {
// TR1=1; //定時(shí)器1使能
//
// }
// if(flag_num>2&&flag_num<12)
// {
// if(n!=0) white_num=0;
// if(n==0)
// {
// white_num++;
// if(white_num>=3) { left_speed=left_speed=0; stop=1; }
// }
// }
// else if(flag_num>=12)
// {
// TR1=0; //定時(shí)器1失能
// flag_num=0;
// white_num=0;
// }
// /*************************************************/
}
復(fù)制代碼
所有資料51hei提供下載:
尋跡小車代碼.zip
(101.46 KB, 下載次數(shù): 48)
2019-6-19 16:33 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
admin
時(shí)間:
2019-6-20 04:16
本帖需要重新編輯補(bǔ)全電路原理圖,源碼,詳細(xì)說明與圖片即可獲得100+黑幣(帖子下方有編輯按鈕)
作者:
tieq1952
時(shí)間:
2019-6-20 07:05
謝謝分享!!!
作者:
等溫好撒旦
時(shí)間:
2022-6-9 19:54
666正是我需要的
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1