標題:
智能尋跡小車CODE
[打印本頁]
作者:
風中的大表哥
時間:
2017-1-9 16:20
標題:
智能尋跡小車CODE
第一次發(fā)帖 給我點黑b吧
0.png
(57.23 KB, 下載次數(shù): 76)
下載附件
2017-1-12 02:54 上傳
資料下載:
智能尋跡小車CODE 11-14.rar
(32.13 KB, 下載次數(shù): 10)
2017-1-9 16:20 上傳
點擊文件名下載附件
這是一個用51做的紅外循跡小車
下載積分: 黑幣 -5
/*******************引腳定義****************/
#include "reg51.h"
typedef unsigned int uint;
typedef unsigned char uchar;
#define Duty_cycle 35 //占空比
#define Cycle 100 //PWM周期
sbit Sensor_1 = P1^1; //紅外檢測
sbit Sensor_2 = P1^2;
sbit Sensor_3 = P1^3;
sbit Motor_1H = P2^0; //電機驅(qū)動控制信號
sbit Motor_1L = P2^1;
sbit Motor_2H = P2^2;
sbit Motor_2L = P2^3;
sbit En_pwm1 = P2^4; //使能信號(PWM)
sbit En_pwm2 = P2^5;
uchar timer1; //定義變量
/*******************************************************************************
* 函 數(shù) 名 : Time1Config
* 函數(shù)功能 : 設(shè)置定時器
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
/**************設(shè)置定時器***********/
void Time1Config()
{
TMOD|= 0x10; //設(shè)置定時計數(shù)器T1工作在模式1
/***定時器賦初始值,12MHZ下定時0.5ms***/
TH1 = 0xFE;
TL1 = 0x0C;
ET1 = 1; //開啟定時器1中斷
EA = 1;
TR1 = 1; //開啟定時器
}
/************************************************
延時函數(shù)
總共延時
1ms
乘以
count
************************************************/
void DelayX1ms(uint count)
{
uint j;
while(count--!=0)
{
for(j=0;j<72;j++);
}
}
/************************************************
電機轉(zhuǎn)動函數(shù)定義
************************************************/
void Go_Straight( )
{
Motor_1H = 0;
Motor_1L = 0;
Motor_2H = 0;
Motor_2L = 0;
DelayX1ms(10);
Motor_1H = 1;
Motor_1L = 0;
Motor_2H = 1;
Motor_2L = 0;
DelayX1ms(20);
}
void Turn_Left( )
{
En_pwm1=0;
En_pwm2=0;
DelayX1ms(10);
Motor_1H = 0;
Motor_1L = 1;
Motor_2H = 1;
Motor_2L = 0;
DelayX1ms(20);
}
void Turn_Right( )
{
En_pwm1=0;
En_pwm2=0;
DelayX1ms(10);
Motor_1H = 1;
Motor_1L = 0;
Motor_2H = 0;
Motor_2L = 1;
DelayX1ms(20);
}
void Go_Back( )
{
Motor_1H = 0;
Motor_1L = 0;
Motor_2H = 0;
Motor_2L = 0;
DelayX1ms(10);
Motor_1H = 0;
Motor_1L = 1;
Motor_2H = 0;
Motor_2L = 1;
DelayX1ms(20);
}
/**************主函數(shù)**************************/
void main( )
{
Time1Config(); //定時器初始化
while(1)
{
if(Sensor_1==0 && Sensor_2==1 && Sensor_3==0) //狀態(tài):軌跡居中
Go_Straight(); //方向:前進
else if(Sensor_1==0 && Sensor_2==0 && Sensor_3==1) //狀態(tài):偏左
Turn_Right(); //方向:右轉(zhuǎn)
else if(Sensor_1==1 && Sensor_2==0 && Sensor_3==0) //狀態(tài):偏右
Turn_Left(); //方向:左轉(zhuǎn)
else if(Sensor_1==0 && Sensor_2==0 && Sensor_3==0) //狀態(tài):偏離軌道
Go_Back(); //方向:后退
else Go_Straight( ); //其他情況:前進
}
}
/*******************************************************************************
* 函 數(shù) 名 : Time1
* 函數(shù)功能 : 定時器1的中斷函數(shù)
* 輸 入 : 無
* 輸 出 : 無
* 備 注 : 3 為定時器1的中斷號 1 定時器0的中斷號 0 外部中斷1 2 外部中斷2 4 串口中斷
*******************************************************************************/
/********************定時器1的中斷函數(shù)********************/
void Time1(void) interrupt 3 //3 為定時器1的中斷號
{
timer1++;
if(timer1>Cycle) //PWM周期為100*0.5ms
{
timer1=0;
}
if(timer1 < Duty_cycle) //改變Duty_cycle這個值可以改變直流電機的速度
{
En_pwm1=1;
En_pwm2=1;
}
else
{
En_pwm1=0;
En_pwm2=0;
}
TH1 = 0xFE; //重新賦初值
TL1 = 0x0C;
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1