?? ht1621.c
字號:
/*------------------------------------------------------------------*/
/* --- STC MCU International Limited -------------------------------*/
/* --- STC 1T Series MCU RC Demo -----------------------------------*/
/* --- Mobile: (86)13922805190 -------------------------------------*/
/* --- Fax: 86-755-82944243 ----------------------------------------*/
/* --- Tel: 86-755-82948412 ----------------------------------------*/
/* --- Web: www.STCMCU.com -----------------------------------------*/
/* If you want to use the program or the program referenced in the */
/* article, please specify in which data and procedures from STC */
/*------------------------------------------------------------------*/
/************* 功能說明 **************
紅外接收程序。適用于市場上用量最大的HT6121/6122及其兼容IC的編碼。
對于用戶碼與User_code定義不同的遙控器,程序會將用戶碼一起從串口輸出。
使用模擬串口發送監控顯示編碼,顯示內容為ASCII碼和中文。
本接收程序基于狀態機的方式,占用CPU的時間非常少。
HEX文件在本目錄的/list里面。
******************************************/
/************* 用戶系統配置 **************/
#define MAIN_Fosc 12000000L //定義主時鐘, 模擬串口和紅外接收會自動適應。5~36MHZ
#define D_TIMER0 125 //選擇定時器時間, us, 紅外接收要求在60us~250us之間
#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(); //初始化Timer
PrintString("****** STC系列MCU紅外接收程序 2010-12-10 ******\r\n"); //上電后串口發送一條提示信息
while(1)
{
if(B_IR_Press) //有IR鍵按下
{
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; //清除IR鍵按下標志
}
}
}
/********************* 十六進制轉ASCII函數 *************************/
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結束
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -