/************* 用戶系統配置 **************/
#define MAIN_Fosc 12000000L //定義主時鐘, 模擬串口和紅外接收會自動適應。5~36MHZ
#define D_TIMER0 125 //選擇定時器時間, us, 紅外接收要求在60us~250us之間
#define User_code 0xFD02 //定義紅外接收用戶碼
/************* 以下宏定義用戶請勿修改 **************/
#include <STC15F2K60S2.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 = P3^2; //定義紅外接收輸入端口
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; //數據反碼
uchar Loop_data; //連發碼
bit B_IrUserErr; //用戶碼 flag
bit B_IR_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);
/********************* 十六進制轉ASCII函數 *************************/
uchar HEX2ASCII(uchar dat)
{
dat &= 0x0f;
if(dat <= 9)
return (dat + '0'); //數字0~9
return (dat - 10 + 'A'); //字母A~F
}
/******************** 紅外采樣時間宏定義*********************************/
#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
//*******************************************************************************************
//**************************** NEC協議6121解碼接收********************************************
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; //用戶嗎正確
else B_IrUserErr=1; //用戶碼錯誤
B_IR_Press=1; //數據有效
}
}
else if((IR_BitCnt&7)==0) //一個 byte 接收
{
IR_UserL=IR_UserH; //保存用戶碼高字節
IR_UserH=IR_data; //保存用戶碼低字節
IR_data=IR_DataShit; //保存數據 byte
}
}
}
}
}
/**************** Timer初始化函數 ******************************/
void InitTimer(void)
{
TMOD = 0; //for STC15W系列 Timer0 16位自動重載
TH0 = Timer0_Reload / 256;
TL0 = Timer0_Reload % 256;
ET0 = 1;
TR0 = 1;
EA = 1;
}
/********************** Timer0中斷函數************************/
void timer0 (void) interrupt 1
{
IR_RX_HT6121();
}
//串口發送函數---------------------------------------------------
void Tx1Send(uchar dat) //9600,N,8,1,發送一個字節
{
SBUF=dat; //發送數據
while(!TI); //等待前一幀數據發送完畢
TI=0; //復位標志位
}
/*------串口初始化函數------------------------------------------*/
void UartInit() //9600bps@11.0592MHz
{
SCON = 0x50; //8位數據,可變波特率
AUXR |= 0x04;
//AUXR |= 0x01; //串口1選擇定時器2為波特率發生器
T2L =0xC7; // 12M時為0XC7,11.0592M時為E0,65536-(11059200/4/9600); //設定定時初值
T2H =0xFE; // (65536-(11059200/4/9600))>>8; //設定定時初值
AUXR |= 0x10;
}
//----------串口發送字符串-----------------------------------------
void PrintString(unsigned char code *puts) //發送一串字符串
{
for (; *puts != 0;
puts++) Tx1Send(*puts); //遇到停止符0結束
}
/********************* 主函數 *************************/
void main(void)
{
InitTimer(); //初始化Timer
UartInit(); //串口初始化
while(1)
{
if(B_IR_Press) //有IR鍵按下
{
if(B_IrUserErr) //用戶碼錯誤,則發送用戶碼
{
PrintString("User:"); //提示用戶碼
Tx1Send(HEX2ASCII(IR_UserL >> 4)); //用戶碼低字節的高半字節
Tx1Send(HEX2ASCII(IR_UserL)); //用戶碼低字節的低半字節
PrintString(" "); //提示反碼
Tx1Send(HEX2ASCII(IR_UserH >> 4)); //用戶碼高字節的高半字節
Tx1Send(HEX2ASCII(IR_UserH)); //用戶碼高字節的低半字節
}
PrintString("; ");
PrintString("Data:"); //提示紅外鍵碼
Tx1Send(HEX2ASCII(IR_data >> 4)); //鍵碼高半字節
Tx1Send(HEX2ASCII(IR_data)); //鍵碼低半字節
PrintString(" "); //提示紅外鍵碼
Tx1Send(HEX2ASCII(IR_DataShit >> 4)); //鍵碼高半字節
Tx1Send(HEX2ASCII(IR_DataShit)); //鍵碼低半字節
PrintString("; ");
PrintString("Circuit:6121"); //提示適用電路6121
B_IR_Press = 0; //清除IR鍵按下標志
}
}//----------------------
}//--------------------
|