#include <reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit YELLOW=P0^7;
sbit GREEN=P2^0;
sbit RED=P2^1;
sbit BLUE =P2^2;
sbit ENA=P2^7;
sbit ENB=P3^0;
sbit IN1 = P2^3;
sbit IN2 = P2^4;
sbit IN3 = P2^5;
sbit IN4 = P2^6;
uchar cycle=100;
uchar speed=80;
void Delay(uint t) //延時函數
{
int i,j;
for(i=110;i>0;i--)
for(j=t;j>0;j--);
}
void t0(void) interrupt 1
{
if(cycle>100)
cycle=0;
if(speed<cycle)
ENA=ENB=0;
else
ENA=ENB=1;
cycle++;
}
void main() //主函數
{
EA=1;
ET0=1;
TMOD=0X02;
TH0=0XA3;
TL0=0XA3;
TR0=1;
Delay(5);
P1=0XFF;
while(1)
{
if(P1==0XFE) //前進
{
GREEN=0;
IN1 = 0; //電機轉動函數
IN2 = 1;
IN3 = 0;
IN4 = 1;
GREEN=1
}
if(P1==0XFD) //后退
{
RED=0;
IN1 = 1; //電機轉動函數
IN2 = 0;
IN3 = 1;
IN4 = 0;
RED=1;
}
if(P1==0XFB) //左轉
{
BLUE =0;
IN1 = 0; //電機轉動函數
IN2 = 0;
IN3 = 0;
IN4 = 1;
BLUE =1;
}
if(P1==0XF7) //右轉
{
YELLOW=0;
IN1 = 0; //電機轉動函數
IN2 = 1;
IN3 = 0;
IN4 = 0;
YELLOW=1;
}
if(P1==0XEF) //停止
{
IN1 = 0; //電機轉動函數
IN2 = 0;
IN3 = 0;
IN4 = 0;
}
}
}
|