NEC红外接收程序,实际应用效果良好,请大家帮忙看看还有无其它问题。
#include <STC_NEW_8051.H>
#include "PWM.H"
sbit IR_PIN = P3^3;
sbit WHITE_PIN = P1^0;
bit bUpdateFlag = 0, bLeaderFlag = 0;
long gReceiveByte;
void SystemInit()
{
IT1 = 1;
EX1 = 1;
EA = 1;
AUXR &= 0x7f;
ET0 = 1;
TMOD = 0x01;
TH0 = 0x00;
TL0 = 0x00;
}
void timer0() interrupt 1 using 1
{
TR0 = 0;
}
void exinit() interrupt 2 using 0
{
WORD RT1, RT2;
static BYTE BitCount;
if(bLeaderFlag)
{
RT1 = TH0 << 8;
RT2 = RT1 + TL0;
TH0 = 0;
TL0 = 0;
if((3600 < RT2) && (RT2 < 5400))
{
BitCount = 0;
gReceiveByte = 0;
}
else if((300 < RT2) && (RT2 < 450))
{
BitCount++;
gReceiveByte = (gReceiveByte << 1);
}
else if((600 < RT2) && (RT2 < 900))
{
BitCount++;
gReceiveByte = (gReceiveByte << 1) + 1;
}
else
{
TR0 = 0;
bLeaderFlag = 0;
}
if(BitCount == 32)
{
TR0 = 0;
bLeaderFlag = 0;
bUpdateFlag = 1;
}
}
else
{
TH0 = 0;
TL0 = 0;
TR0 = 1;
bLeaderFlag = 1;
}
}
void main()
{
const BYTE DimData[11] = {0, 25, 50, 75, 100, 125, 150, 175, 200, 225, 255};
BYTE DataCode, NotDataCode, i, j;
SystemInit();
InitPWM();
for(;;)
{
if(bUpdateFlag)
{
bUpdateFlag = 0;
DataCode = gReceiveByte >> 8;
NotDataCode = gReceiveByte;
if(~DataCode == NotDataCode)
{
switch(DataCode)
{
case 0x78: ChangeDutyCycle(0xff); WHITE_PIN = ~WHITE_PIN; break;
case 0x50: ChangeDutyCycle(0xff); break;
case 0x20: if(i < 10) ChangeDutyCycle(DimData[++i]); break;
case 0x40: if(i > 0) ChangeDutyCycle(DimData[--i]); break;
case 0x60: if(j < 255) ChangeDutyCycle(++j); break;
case 0x10: if(j > 0) ChangeDutyCycle(--j); break;
default: break;
}
}
}
}
}
#include "STC_NEW_8051.H"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define CPS0 0
#define CPS1 2
#define CPS2 4
#define CPS3 6
#define CPS4 8
#define CPS5 10
#define CPS6 12
#define CPS7 14
void InitPWM()
{
CCON = 0x00;
CMOD |= CPS0;
CCAPM0 = 0x42;
PCA_PWM0 = 0x00;
CCAP0L = 0x00;
CCAP0H = 0x00;
CL = 0x00;
CH = 0x00;
CR = 1;
}
void ChangeDutyCycle(BYTE duty)
{
if(duty == 0xff)
{
PCA_PWM0 = 0x03;
}
else
{
PCA_PWM0 = 0x00;
}
CCAP0L = CCAP0H = duty;
} |