#include"reg52.h"
#include"intrins.h"
#define unint unsigned int
#define unchar unsigned char
#define PLAY_OUT P1
unchar sec=0;
unchar min=0;
unchar count=0;
bit gj=0; //光和遥控的切换
unchar r6,r7;
bit time0=1;
bit time1=1;
sbit PLAY_Q_Q=P3^0;
sbit PLAY_Z=P3^1;
sbit PLAY_Y=P3^4;
sbit PLAY_QH=P3^5;
sbit PLAY_Q_H=P3^6;
sbit PLAY_T=P3^7;
unchar r0,r1,r2=0x00,r3=0x00,r4=0x00,r5=0x00;
unchar code TAB_Q[4]={0xf9,0xf5,0xf6,0xfa};//前进
unchar code TAB_H[4]={0xfa,0xf6,0xf5,0xf9};//后退
unchar code TAB_Y[4]={0x9f,0x5f,0x6f,0xaf};//右转
unchar code TAB_Z[4]={0xaf,0x6f,0x5f,0x9f};//左转
/*************************/
void intiaton(void);
void int_time3(void);
void int_time0(void);
void fanzhuan_Y(void);
void fanzhuan_Z(void);
void zhenzhuan_Q(void);
void zhenzhuan_H(void);
void zhenzhuan_AQ(void);
void zhenzhuan_AH(void);
void cankey(void);
/*************************/
delay(unint t) //0.6ms
{unint i;
for(i=55;i>t;i--) ;
}
/**************************/
delayms(unint k) //k=1延迟0.884毫秒
{unint i;
for(i=80;i>k;i--)
;
}
/*************************/
void delay238(void) //2.38ms
{
unint i,j;
for(j=125;j>0;j--)
for(i=0;i<1;i++);
}
/************************/
void intiaton(void) //初始化
{
TMOD=0x11;
TH1=0XFA;
TL1=0X56;
ET1=1;
TR1=1;
EA=1;
}
/*********************/
void int_time3() interrupt 3 using 3
{
TH1=0XFA;
TL1=0X56;
sec++;
if(sec==2)
{
sec=0;
min++;
}
}
/*************************/ //光感与按键控制
void cankey()
{
if(gj)
{
if(!PLAY_Q_Q)
{
count=1;
}
if(!PLAY_T)
{
count=2;
}
if(!PLAY_Q_H)
{
count=3;
}
}
if((PLAY_Z==0)||(PLAY_Y==0))
{
delayms(1);
if(PLAY_Z==0)
{
fanzhuan_Z();count=1;
}
else
{
fanzhuan_Y();count=1;
}
}
if(PLAY_QH==0)
{
delayms(10);
if(PLAY_QH==0)
{
count++;
if(count==4)
{
count=0;
}
}
while(PLAY_QH==0);
}
switch(count)
{
case 0: PLAY_OUT=0xff;break;
case 1: zhenzhuan_AQ();break;
case 2: PLAY_OUT=0xff;break;
case 3: zhenzhuan_AH();break;
default :break;
}
}
/*******************************/ //按键前进
void zhenzhuan_AQ(void)
{
for(r6=0;r6<4;r6++)
{
PLAY_OUT=TAB_Q[r6];
while(sec==0);
sec=0;
}
}
/*******************/ //按键后退
void zhenzhuan_AH(void)
{
for(r6=0;r6<4;r6++)
{
PLAY_OUT=TAB_H[r6];
while(sec==0);
sec=0;
}
}
/**********************/ //左转
void fanzhuan_Z(void)
{
unchar i;
for(i=0;i<22;i++)
{
for(r7=0;r7<4;r7++)
{
PLAY_OUT=TAB_Z[r7];
while(sec==0);
sec=0;
}
}
cankey();
}
/**********************/ //右转
void fanzhuan_Y(void)
{
unchar i;
for(i=0;i<22;i++)
{
for(r7=0;r7<4;r7++)
{
PLAY_OUT=TAB_Y[r7];
while(sec==0);
sec=0;
}
}
cankey();
}
/***************************/
void main()
{
intiaton();
while(1)
{
cankey();
}
这是加了感光跟按键控制的智能小车,是读大一时候写的,现在看起来呵呵。。。垃圾代码一大堆,不过好象还可以用。
交换单片机输出端的高低电平应该可以的吧
你要有硬件电路才能写代码。不然谁知道你要怎么控制?