在用紅外模塊TCRT5000使小車定點停車,變向,小車用l298n驅動,外加PT2262遙控調速,仿真已經實現了,為啥弄實物的時候電機不動呢?l298n的電機工作狀態一直顯示不工作,但電機驅動模塊是新的,沒問題,請大佬們指導一下,程序應該沒問題吧。
#include <reg52.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit in1= P2^0; //L298N引腳定義
sbit in2= P2^1;
sbit in3= P2^2;
sbit in4= P2^3;
sbit en1=P3^0;
sbit en2=P3^5;
//sbit key1=P3^1;//前進 ,遙控器引腳定義
//sbit key2=P3^2;//后退
sbit key3=P3^3;//加速
sbit key4=P3^4;//減速
sbit chetou=P2^4; //紅外傳感器引腳定義
sbit chewei=P2^5;
sbit cheshen=P2^6;
u16 M;//改變占空,一共有240
u16 Z;//用于中斷程序,實時紅外檢測
void delay_us(u16 aa) //延時函數
{
while(aa--);
}
void main()
{
u16 a;
a=120;//初始的速度
en1=0;
en2=0;
TMOD = 0x01; // T0定時器為工作方式一
TH0 = 0x0FF; //設置初值
TL0 = 0x09C; //設置初值
TR0 = 1; //開啟定時器T0
ET0 = 1;//開放定時器T0中斷
EA = 1;//開放總中斷
//key1 = 0;
//key2 = 0;
key3 = 1;//用于仿真工程,真正寫入程序板的時候要改為0
key4 = 1;
while(1)
{
if(Z==240)
{
Z=0;
if((chetou==0&&chewei==1&&cheshen==1)||(chewei==0&&chetou==1&&cheshen==1)||(chetou==1&&chewei==0&&cheshen==0)||(chetou==0&&chewei==1&&cheshen==0))
{
delay_us(2);
if((chetou==0&&chewei==1&&cheshen==1)||(chewei==0&&chetou==1&&cheshen==1)||(chetou==1&&chewei==0&&cheshen==0)||(chetou==0&&chewei==1&&cheshen==0))
{
en1=0;
en2=0;
in1=0;
in2=0;
in3=0;
in4=0;
}
}
if((chetou==0)&&(chewei==0)&&(cheshen==1))
{
delay_us(2);
if((chetou==0)&&(chewei==0)&&(cheshen==1))
{
in1=1;
in2=0;
in3=0;
in4=1;
}
}
if((chetou==0)&&(chewei==0)&&(cheshen==0))
{
delay_us(2);
if((chetou==0)&&(chewei==0)&&(cheshen==0))
{
in1=0;
in2=1;
in3=1;
in4=0;
}
}
}
if(key3==0)
{
delay_us(2);
if(key3==0)
{
a+=40;
if(a>=240)
a=240;
}
while(!key3);
delay_us(2);
while(!key3);
}
if(key4==0)
{
delay_us(2);
if(key4==0)
{
a-=40;
if(a<=40)
a=40;
}
while(!key4);
delay_us(2);
while(!key4);
}
if(M==0)
{
en1=1;
en2=1;
}
if(M==a)
{
en1=0;
en2=0;
}
}
}
void time0_int() interrupt 1 //T = 0.1ms * 240 = 24ms
{
TR0 = 0;
TH0 = 0x0FF;
TL0 = 0x09C; //0.1ms
M++;
Z++;
if(M>=240)
{
M=0;
}
TR0 = 1;
}
用的開關代替的PT2262個TCRT5000因為軟件里沒有這倆元件
|