51单片机红外控制舵机程序
#include "reg52.h"
#include <intrins.h>
typedef unsigned int u16;
typedef unsigned char u8;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
sbit pwm =P1^5;
sbit IRIN=P3^2; //红外传感器定义口
u8 jd;
u8 count;
u8 IrValue[6];
u8 Time;
u8 DisplayData[8];
u8 code smgduan[17]={
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0X76};
//extern controlcar(unsigned char contype);
u8 IRCOM[7];
/*******************************************************************************
延时函数
*******************************************************************************/
void delay(u16 i)
{
while(i--);
}
/*******************************************************************************
中断函数 定时器中断 0.25ms控制舵机
*******************************************************************************/
void Time0_Init() //??????????
{
TMOD = 0x01; //?????0????????1
IE = 0x82;
TH0 = 0xff;
TL0 = 0x06; //12MZ????0.25ms
TR0=1; //????????
EX1=1;
IT1=1;
EA=1;
}
void DigDisplay() //数码管显示函数
{
u8 i;
for(i=0;i<3;i++)
{
switch(i) //λ?????????????????
{
case(0):
LSA=0;LSB=0;LSC=0; break;//?????0λ
case(1):
LSA=1;LSB=0;LSC=0; break;//?????1λ
case(2):
LSA=0;LSB=1;LSC=0; break;//?????2λ
}
P0=DisplayData[2-i];//????????
delay(100); //????????????
P0=0x00;//????
}
}
//舵机定义函数
void zuozhuan()
{
delay(100);
jd=3;
count=0;
}
void guiwei()
{
delay(100);
jd=4;
count=0;
}
void youzhuan()
{
delay(100);
jd=5;
count=0;
}
void controlcar(unsigned char contype)
{
jd=4;
switch(contype)
{
case 1:
{
zuozhuan();
delay(100) ;
break;
}
case 2:
{
guiwei() ;
delay(100);
break;
}
case 3:
{
youzhuan();
delay(100);
break;
}
}
}
/*******************************************************************************
* ?????? : IrInit()
* ???????? : ??????????????
* ???? : ??
* ??? : ??
*******************************************************************************/
void IrInit()
{
IT0=1;//????????
EX0=1;//???ж?0????
EA=1; //?????ж?
IRIN=1;//????????
}
/*******************************************************************************
* ?? ?? ?? : main
* ???????? : ??????
* ?? ?? : ??
* ?? ?? : ??
*******************************************************************************/
void main()
{
IrInit();
Time0_Init();
// EX1=1; //????????ж?1
// IT1=1; //?趨???ж?1?????????????
// EA=1;
delay(10);
while(1)
{
DisplayData[0] = smgduan[IrValue[2]/16];
DisplayData[1] = smgduan[IrValue[2]%16];
DisplayData[2] = smgduan[16];
DigDisplay();
}
}
/*******************************************************************************
红外线解码函数
*******************************************************************************/
void IR_IN() interrupt 2 using 0 //????INT2???ж????
{
unsigned char j,k,N=0; //??????????????
EX1 = 0; //??????ж?,?????????????
delay(15); //?????????к???????
if (IRIN==1) //?ж?????????????
{
EX1 =1; //???ж??
return; //????
}
while (!IRIN) //??IR?????????????9ms?????????????
{
delay(1); //??????
}
for (j=0;j<4;j++) //????????????????
{
for (k=0;k<8;k++) //??β??8λ????
{
while (IRIN) //?? IR ?????????????4.5ms?????????????
{
delay(1); //??????
}
while (!IRIN) //?? IR ???????
{
delay(1); //??????
}
while (IRIN) //????IR???????
{
delay(1); //??????
N++; //?????????
if (N>=30) //?ж??????????
{
EX1=1; //?????ж????
return; //????
}
}
IRCOM[j]=IRCOM[j] >> 1; //????????λ??????????????
if (N>=8) //?ж????????
{
IRCOM[j] = IRCOM[j] | 0x80; //???????λ??1
}
N=0; //????λ???????
}
}
if (IRCOM[2]!=~IRCOM[3]) //?ж???????????
{
EX1=1; //?????ж?
return; //????
}
for(j=0;j<10;j++) //??????м??????
{
if(IRCOM[2]=smgduan[j]) //???м?λ???
{
controlcar(j); //???????????????
}
}
EX1 = 1; //???ж??
}
void Time0_Int() interrupt 1 //定时器中断函数
{
TH0 = 0xff; //??????
TL0 = 0x06;
if(count< jd) //?ж?0.5ms???????С??????
pwm=1; //??С???PWM???????
else
pwm=0; //?????????????
count=(count+1); //0.5ms??????1
count=count%40; //???????????40 ???????????20ms
}
|