赤外線デバイスを制御し、次にステッピングモーターを制御する51個のシングルチップマイクロコンピューターを含む科学技術プロジェクトを行う場合、実装コードは次のとおりです。
/*
红外接收装置引脚连P3^2
IN1连P1^0,IN2连P1^1,IN3连P1^3,IN4连P4^1,
CH停止
CH-关闭 CH+开启
-为一直反转 +为一直正转
*/
#include <reg52.h>
typedef unsigned char INT8U;
typedef unsigned char uchar;
typedef unsigned int INT16U;
typedef unsigned int uint;
sbit A1 = P1^0;
sbit B1 = P1^1;
sbit C1 = P1^2;
sbit D1 = P1^3;
sbit IR = P3^2;
uchar IRtime;
uchar IRcord[4];
uchar IRdata[33];
bit IRpro_ok, IRok;
unsigned char pwm_left_val = 250;
unsigned char pwm_t;//??
void delay(unsigned int z)
{
unsigned int x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
void time0() interrupt 1
{
IRtime++;
}
void timer1() interrupt 3
{
pwm_t++;
if(pwm_t == 250)
pwm_t = P1 = 0;
if(pwm_left_val == pwm_t)
P1 = 0xff;
}
void int0() interrupt 0
{
static uchar i;
static bit startflag;
if(startflag)
{
if( (IRtime < 53) && (IRtime >= 32) )
i = 0;
IRdata[i] = IRtime;
IRtime = 0;
i++;
if(i == 33)
{
IRok = 1;
i = 0;
}
}
else
{
IRtime = 0;
startflag = 1;
}
}
void IRcordpro()
{
uchar i, j, k, cord, value;
k = 1;
for(i = 0; i < 4; i++)
{
for(j = 0; j < 8; j++)
{
cord = IRdata[k];
if(cord > 5)
value = value | 0x80;
if(j < 7)
{
value = value >> 1;
}
k++;
}
IRcord[i] = value;
value = 0;
}
IRpro_ok = 1;
}
//dian ji mo kuai
unsigned int delaydj=12;
void ALL_OFF()
{
A1=0;B1=0;C1=0;D1=0;}
void A_ON()
{
A1=1;B1=0;C1=0;D1=0;}
void B_ON()
{
A1=0;B1=1;C1=0;D1=0;}
void C_ON()
{
A1=0;B1=0;C1=1;D1=0;}
void D_ON()
{
A1=0;B1=0;C1=0;D1=1;}
void Delaydj(unsigned int n)
{
while(n>0)
n--;
}
void shun()
{
D_ON();
Delaydj(delaydj);
C_ON();
Delaydj(delaydj);
B_ON();
Delaydj(delaydj);
A_ON();
Delaydj(delaydj);
}
void ni()
{
A_ON();
Delaydj(delaydj);
B_ON();
Delaydj(delaydj);
C_ON();
Delaydj(delaydj);
D_ON();
Delaydj(delaydj);
}
int shunforever()
{
unsigned char i = 0;
while(1)
{
shun();
TMOD |= 0x20;//T1
TH1 = 245;
TL1 = 245;
TR1 = 1;
ET1 = 1;
EA = 1;
TMOD |= 0x02;
TH0 = 0x00;
TL0 = 0x00;
ET0 = 1;
TR0 = 1;
IT0 = 1;
EX0 = 1;
if(IRok)
{
IRcordpro();
IRok = 0;
if(IRpro_ok)
{
if(IRcord[2]==0x46)
return 0;
if(IRcord[2]==0x07)
if(delaydj<18)
delaydj = delaydj+5;
if(IRcord[2]==0x15)
if(delaydj>8)
delaydj = delaydj-5;
IRpro_ok = 0;
}
}
}
}
int niforever()
{
unsigned char i = 0;
while(1)
{
ni();
TMOD |= 0x20;//T1
TH1 = 245;
TL1 = 245;
TR1 = 1;
ET1 = 1;
EA = 1;
TMOD |= 0x02;
TH0 = 0x00;
TL0 = 0x00;
ET0 = 1;
TR0 = 1;
IT0 = 1;
EX0 = 1;
if(IRok)
{
IRcordpro();
IRok = 0;
if(IRpro_ok)
{
if(IRcord[2]==0x46)
return 0;
if(IRcord[2]==0x07)
if(delaydj<18)
delaydj = delaydj+5;
if(IRcord[2]==0x15)
if(delaydj>8)
delaydj = delaydj-5;
IRpro_ok = 0;
}
}
}
}
int on()
{
unsigned char i = 0;
unsigned char j = 0;
delaydj = 7;
for(i=0; i<200; i++)
{
for(j=0; j<10; j++)
{
shun();
TMOD |= 0x20;//T1
TH1 = 245;
TL1 = 245;
TR1 = 1;
ET1 = 1;
EA = 1;
TMOD |= 0x02;
TH0 = 0x00;
TL0 = 0x00;
ET0 = 1;
TR0 = 1;
IT0 = 1;
EX0 = 1;
if(IRok)
{
IRcordpro();
IRok = 0;
if(IRpro_ok)
{
if(IRcord[2]==0x46)
return 0;
if(IRcord[2]==0x07)
if(delaydj<18)
delaydj = delaydj+5;
if(IRcord[2]==0x15)
if(delaydj>8)
delaydj = delaydj-5;
IRpro_ok = 0;
}
}
}
}
}
int off()
{
unsigned char i = 0;
unsigned char j = 0;
delaydj = 7;
for(i=0; i<200; i++)
{
for(j=0; j<10; j++)
{
ni();
TMOD |= 0x20;//T1
TH1 = 245;
TL1 = 245;
TR1 = 1;
ET1 = 1;
EA = 1;
TMOD |= 0x02;
TH0 = 0x00;
TL0 = 0x00;
ET0 = 1;
TR0 = 1;
IT0 = 1;
EX0 = 1;
if(IRok)
{
IRcordpro();
IRok = 0;
if(IRpro_ok)
{
if(IRcord[2]==0x46)
return 0;
if(IRcord[2]==0x07)
if(delaydj<18)
delaydj = delaydj+5;
if(IRcord[2]==0x15)
if(delaydj>8)
delaydj = delaydj-5;
IRpro_ok = 0;
}
}
}
}
}
void main()
{
unsigned char i = 0;
TMOD |= 0x20;
TH1 = 245;
TL1 = 245;
TR1 = 1;
ET1 = 1;
EA = 1;
TMOD |= 0x02;
TH0 = 0x00;
TL0 = 0x00;
ET0 = 1;
TR0 = 1;
IT0 = 1;
EX0 = 1;
while(1)
{
if(IRok)
{
IRcordpro();
IRok = 0;
if(IRpro_ok)
{
switch(IRcord[2])
{
case 0x40: i = shunforever(); break;
case 0x44: i = niforever(); break;
case 0x47: i = on(); break;
case 0x45: i = off(); break;
}
IRpro_ok = 0;
}
}
}
}