#define MAIN_Fosc
12000000L
#define D_TIMER0
125
#define
User_code
0xFD02
#include
"reg51.H"
#define
uchar
unsigned char
#define uint
unsigned int
#define freq_base
(MAIN_Fosc / 1200)
#define Timer0_Reload
(65536 - (D_TIMER0 * freq_base / 10000))
sbit
P_TXD1 = P3^1;
sbit
P_IR_RX = P0^1;
bit
P_IR_RX_temp;
//Last sample
bit
B_IR_Sync;
uchar
IR_SampleCnt;
uchar
IR_BitCnt;
uchar
IR_UserH;
uchar
IR_UserL;
uchar
IR_data;
uchar
IR_DataShit;
bit
B_IrUserErr;
//User code error flag
bit
B_IR_Press;
//Key press flag,include repeat key.
uchar
IR_code;
//IR code
void
Tx1Send(uchar dat);
uchar
HEX2ASCII(uchar dat);
void
InitTimer(void);
void
PrintString(unsigned char code *puts);
void main(void)
{
InitTimer();
PrintString("****** STC 2010-12-10 ******\r\n");
while(1)
{
if(B_IR_Press)
{
PrintString("ºìÍâÂë: 0x");
Tx1Send(HEX2ASCII(IR_code >> 4));
Tx1Send(HEX2ASCII(IR_code));
if(B_IrUserErr)
{
Tx1Send(' ');
Tx1Send(' ');
PrintString("Óû§Âë: 0x");
Tx1Send(HEX2ASCII(IR_UserH >> 4));
Tx1Send(HEX2ASCII(IR_UserH));
Tx1Send(HEX2ASCII(IR_UserL >> 4));
Tx1Send(HEX2ASCII(IR_UserL));
}
Tx1Send(0x0d);
Tx1Send(0x0a);
B_IR_Press = 0;
}
}
}
uchar
HEX2ASCII(uchar dat)
{
dat &= 0x0f;
if(dat <= 9)
return (dat + '0');
//Êý×Ö0~9
return (dat - 10 + 'A');
//×ÖĸA~F
}
//*******************************************************************
//*********************** IR Remote Module **************************
//*********************** IR Remote Module **************************
//this programme is used for Receive IR Remote (HT6121).
//data format: Synchro,AddressH,AddressL,data,/data, (total 32 bit).
//send a frame(85ms), pause 23ms, send synchro of another frame, pause 94ms
//data rate: 108ms/Frame
//Synchro:low=9ms,high=4.5/2.25ms,low=0.5626ms
//Bit0:high=0.5626ms,low=0.5626ms
//Bit1:high=1.6879ms,low=0.5626ms
//frame space = 23 ms or 96 ms
#if ((D_TIMER0 <= 250) && (D_TIMER0 >= 60))
#define
D_IR_sample
D_TIMER0
//¶¨Òå²ÉÑùʱ¼ä£¬ÔÚ60us~250usÖ®¼ä
#endif
#define D_IR_SYNC_MAX
(15000/D_IR_sample)
//SYNC max time
#define D_IR_SYNC_MIN
(9700 /D_IR_sample)
//SYNC min time
#define D_IR_SYNC_DIVIDE
(12375/D_IR_sample)
//decide data 0 or 1
#define D_IR_DATA_MAX
(3000 /D_IR_sample)
//data max time
#define D_IR_DATA_MIN
(600 /D_IR_sample)
//data min time
#define D_IR_DATA_DIVIDE
(1687 /D_IR_sample)
//decide data 0 or 1
#define D_IR_BIT_NUMBER
32
//bit number
//*******************************************************************************************
//**************************** IR RECEIVE MODULE ********************************************
void IR_RX_HT6121(void)
{
uchar
SampleTime;
IR_SampleCnt++;
//Sample + 1
F0 = P_IR_RX_temp;
//Save Last sample status
P_IR_RX_temp = P_IR_RX;
//Read current status
if(F0 && !P_IR_RX_temp)
//Last sample is high£¬and current sample is low, so is fall edge
{
SampleTime = IR_SampleCnt;
//get the sample time
IR_SampleCnt = 0;
//Clear the sample counter
if(SampleTime > D_IR_SYNC_MAX)
B_IR_Sync = 0;
//large the Maxim SYNC time, then error
else if(SampleTime >= D_IR_SYNC_MIN)
//SYNC
{
if(SampleTime >= D_IR_SYNC_DIVIDE)
{
B_IR_Sync = 1;
//has received SYNC
IR_BitCnt = D_IR_BIT_NUMBER;
//Load bit number
}
}
else if(B_IR_Sync)
//has received SYNC
{
if(SampleTime > D_IR_DATA_MAX)
B_IR_Sync=0;
//data samlpe time to large
else
{
IR_DataShit >>= 1;
//data shift right 1 bit
if(SampleTime >= D_IR_DATA_DIVIDE)
IR_DataShit |= 0x80;
//devide data 0 or 1
if(--IR_BitCnt == 0)
//bit number is over?
{
B_IR_Sync = 0;
//Clear SYNC
if(~IR_DataShit == IR_data)
//ÅжÏÊý¾ÝÕý·´Âë
{
if((IR_UserH == (User_code / 256)) &&
IR_UserL == (User_code % 256))
B_IrUserErr = 0;
//User code is righe
else
B_IrUserErr = 1;
//user code is wrong
IR_code = IR_data;
B_IR_Press = 1;
//Êý¾ÝÓÐЧ
}
}
else if((IR_BitCnt & 7)== 0)
//one byte receive
{
IR_UserL = IR_UserH;
//Save the User code high byte
IR_UserH = IR_data;
//Save the User code low byte
IR_data = IR_DataShit;
//Save the IR data byte
}
}
}
}
}
/**************** Timer³õʼ»¯º¯Êý ******************************/
void InitTimer(void)
{
TMOD = 0;
//for STC15FxxxϵÁÐ
Timer0 as 16bit reload timer.
TH0 = Timer0_Reload / 256;
TL0 = Timer0_Reload % 256;
ET0 = 1;
TR0 = 1;
EA = 1;
}
/********************** Timer0ÖжϺ¯Êý************************/
void timer0 (void) interrupt 1
{
IR_RX_HT6121();
}
/********************** Ä£Äâ´®¿ÚÏà¹Øº¯Êý************************/
void
BitTime(void)
//λʱ¼äº¯Êý
{
uint i;
i = ((MAIN_Fosc / 100) * 104) / 140000 - 1;
//¸ù¾ÝÖ÷ʱÖÓÀ´¼ÆËãλʱ¼ä
while(--i);
}
//Ä£Äâ´®¿Ú·¢ËÍ
void
Tx1Send(uchar dat)
//9600£¬N£¬8£¬1
·¢ËÍÒ»¸ö×Ö½Ú
{
uchar
i;
EA = 0;
P_TXD1 = 0;
BitTime();
for(i=0; i<8; i++)
{
if(dat & 1)
P_TXD1 = 1;
else
P_TXD1 = 0;
dat >>= 1;
BitTime();
}
P_TXD1 = 1;
EA = 1;
BitTime();
BitTime();
}
void PrintString(unsigned char code *puts)
//·¢ËÍÒ»´®×Ö·û´®
{
for (; *puts != 0;
puts++) Tx1Send(*puts);
//Óöµ½Í£Ö¹·û0½áÊø
}