標(biāo)題:
單片機(jī)紅外發(fā)射程序
[打印本頁]
作者:
51黑z
時(shí)間:
2016-9-11 22:20
標(biāo)題:
單片機(jī)紅外發(fā)射程序
0.png
(72.85 KB, 下載次數(shù): 148)
下載附件
2016-9-11 22:17 上傳
本例程可以實(shí)現(xiàn)用stc的51單片機(jī)來控制進(jìn)行紅外線的發(fā)射,
完整keil工程文件下載:
http://www.torrancerestoration.com/bbs/dpj-55490-1.html
下面是部分程序代碼的預(yù)覽:
主程序:
#include "IR_SendCode.h"
#include "IR_SendDisp.h"
#include "IR_Receive.h"
#include "ChannelShortcutDef.h"
#include "Nokia5110.h"
#include "delay.h"
ChannelKey_TypeDef Channel_ShortKey;//定義通道&快捷鍵結(jié)構(gòu)體
uint8_t IRSendOrRemote_Flag = Function_IRsend;//定義需要實(shí)現(xiàn)的是紅外發(fā)射還是紅外接收
void main(void)
{
IRsend_Init();
IRreceiver_Init();
Nokia5110_Init();
ReadE2P_ChannelShortKey();//讀取設(shè)置的通道和快捷鍵
Nokia5110_DispChannelKey();//顯示設(shè)置的通道和快捷鍵
while(1)
{
Funtion_KeyScan();
if(IRSendOrRemote_Flag == Function_IRsend) //紅外發(fā)送功能實(shí)現(xiàn)
{
Set_ChannelShortKey();
ShortKeyScan_IRSendCode();
}
if(IRSendOrRemote_Flag == Function_IRremote)//紅外接收功能實(shí)現(xiàn)
{
IRcode_Disp();
}
}
}
復(fù)制代碼
STC89Cx_it.c
#include "STC89Cx_it.h"
#include "IR_Receive.h"
//紅外解碼原理:把紅外中的載波解調(diào)成低電平,把紅外中的低電平解調(diào)成高電平輸出
//識(shí)別原理:識(shí)別一個(gè)完整周期的高低電平總時(shí)間來識(shí)別頭碼和"0"或"1"的值
#define Rev_Header 0x00 //數(shù)據(jù)接收狀態(tài):接收頭碼0xAA或oxAB
#define Rev_Length 0x01 //數(shù)據(jù)接收狀態(tài):接收頭碼數(shù)據(jù)包長度
#define Rev_DataPackage 0x02 //數(shù)據(jù)接收狀態(tài):接收數(shù)據(jù)包
extern uint8_t IRcode_Type; //聲明收到的紅外數(shù)據(jù)類型
extern uint8_t IRdataReceive_Flag;//聲明接收紅外數(shù)據(jù)狀態(tài),等待or接收中or接收完畢
extern uint32_t IRcode_Buffer; //紅外數(shù)據(jù)緩存
uint8_t CycleTime = 0; //存儲(chǔ)完整周期時(shí)間(高電平+低電平)
/**********************************************
函數(shù):void INT0_Handler(void);
功能:外部中斷0中斷處理函數(shù);
***********************************************/
void INT0_Handler(void) interrupt 0
{
}
/**********************************************
函數(shù):void TIME0_Handler(void);
功能:定時(shí)器0中斷處理函數(shù);
***********************************************/
void TIME0_Handler(void) interrupt 1
{
CycleTime++;
if(CycleTime==255)CycleTime=0;
}
/**********************************************
函數(shù):void INT1_Handler(void);
功能:外部中斷1中斷處理函數(shù);
***********************************************/
void INT1_Handler(void) interrupt 2
{
static uint8_t EnterINT0_CNT = 0;//存儲(chǔ)進(jìn)入外部中斷次數(shù)
static uint8_t Rev_State = Rev_Header;//定義接收狀態(tài)
uint8_t CycleTime_Buffer=0; //周期時(shí)間緩沖變量
EnterINT0_CNT++; //每進(jìn)入中斷一次加1
//第一次進(jìn)入中斷周期清零,打開定時(shí)器0準(zhǔn)備計(jì)時(shí)
if(EnterINT0_CNT==1)
{
CycleTime=0;
IRcode_Buffer=0;
TR0=1;
}
else//之后先讀時(shí)間,再進(jìn)行清零
{
CycleTime_Buffer=CycleTime;
CycleTime = 0;
}
//開始識(shí)別頭碼
if((Rev_State==Rev_Header)&&(EnterINT0_CNT==2))
{
Rev_State = Rev_DataPackage;
if((CycleTime_Buffer>60)&&(CycleTime_Buffer<100)) //頭碼時(shí)間位于6.0ms~10ms則為RCA碼(標(biāo)準(zhǔn)8ms)
{
IRcode_Type = IRcode_TypeRCA;
}
else if((CycleTime_Buffer>110)&&(CycleTime_Buffer<160))//頭碼時(shí)間位于11ms~16ms(必須是大于11ms)則為NEC碼(標(biāo)準(zhǔn)13.5ms)
{
IRcode_Type = IRcode_TypeNEC8;
}
else//如果都不是,則認(rèn)為沒有收到頭碼
{
TR0=0;
IRcode_Buffer = 0;
EnterINT0_CNT = 0;
Rev_State = Rev_Header;
}
}
if((Rev_State==Rev_DataPackage)&&(EnterINT0_CNT>2))//開始解碼
{
IRcode_Buffer <<= 1;
switch(IRcode_Type)
{
case IRcode_TypeRCA:
{
if((CycleTime_Buffer>10)&&(CycleTime_Buffer<20))//碼值時(shí)間位于1.0ms~2.0ms則為RCA'1'(標(biāo)準(zhǔn)1.5ms)
{
IRcode_Buffer |= (uint32_t)0x01;
}
else if((CycleTime_Buffer>20)&&(CycleTime_Buffer<30))//碼值時(shí)間位于1.0ms~2.0ms則為RCA'0'(標(biāo)準(zhǔn)2.5ms)
{
IRcode_Buffer |= (uint32_t)0x00;
}
else
{
TR0=0;
IRcode_Buffer = 0;
EnterINT0_CNT = 0;
Rev_State = Rev_Header;
}
break;
}
case IRcode_TypeNEC8:
{
if((CycleTime_Buffer>6)&&(CycleTime_Buffer<16))//碼值時(shí)間位于0.6ms~1.6ms則為NEC'0'(標(biāo)準(zhǔn)1.124ms)
{
IRcode_Buffer |= (uint32_t)0x00;
}
else if((CycleTime_Buffer>16)&&(CycleTime_Buffer<28))//碼值時(shí)間位于1.6ms~2.8ms則為NEC'1'(標(biāo)準(zhǔn)2.25ms)
{
IRcode_Buffer |= (uint32_t)0x01;
}
else
{
TR0=0;
IRcode_Buffer = 0;
EnterINT0_CNT = 0;
Rev_State = Rev_Header;
}
break;
}
}
if(((EnterINT0_CNT==26)&&((IRcode_Type==IRcode_TypeRCA)))||((EnterINT0_CNT==34)&&((IRcode_Type==IRcode_TypeNEC8))))//RCA碼加頭碼25,NEC碼加頭碼33位,解碼進(jìn)入中斷次數(shù)分別為26和34
{
TR0=0;//解碼完畢關(guān)閉定時(shí)器
EnterINT0_CNT=0;
IRdataReceive_Flag=IRdataReceive_Success;
Rev_State = Rev_Header;//準(zhǔn)備下一次接收
}
}
}
/**********************************************
函數(shù):void TIME1_Handler(void);
功能:定時(shí)器1中斷處理函數(shù);
***********************************************/
void TIME1_Handler(void) interrupt 3
{
}
/**********************************************
函數(shù):void USART_Handler(void);
功能:串口中斷處理函數(shù);
***********************************************/
void USART_Handler(void) interrupt 4
{
}
/**********************************************
函數(shù):void TIME2_Handler(void);
功能:定時(shí)器2中斷處理函數(shù);
***********************************************/
void TIME2_Handler(void) interrupt 5
{
}
復(fù)制代碼
#include "IR_SendCode.h"
/***********************************************************************************************
函數(shù)void IRsend_Init(void)
功能:定時(shí)器1初始化
**********************************************************************************************/
void IRsend_Init(void)
{
TMOD |= 0x10; //工作于方式1,16位定時(shí)器
TH1 = 0x00;
TL1 = 0x00;
TF1 = 0; //清楚定時(shí)器標(biāo)志位
TR1 = 0; //關(guān)閉定時(shí)器
}
/***********************************************************************************************
函數(shù)void Carrier_38KHz(void)
功能:產(chǎn)生38KHz載波,1/2占空比,12M晶振
**********************************************************************************************/
void Carrier_38KHz(void)
{
IR = 1;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
IR = 0;
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
/******************************************RCA編碼**********************************************/
/***********************************************************************************************
函數(shù)void RCA_LeadCode(void)
功能:產(chǎn)生RCA引導(dǎo)碼
**********************************************************************************************/
void RCA_LeadCode(void)
{
TH1 = (65536-TCarrier_RCALeadCode)/256;//定時(shí)載波時(shí)間
TL1 = (65536-TCarrier_RCALeadCode)%256;
TR1 = 1;//打開定時(shí)器0
IR_ON();
while(!TF1)
{
Carrier_38KHz();
}
TF1 = 0;TR1 = 0;
IR_OFF();
TH1 = (65536-TLowLevel_RCALeadCode)/256;//定時(shí)低電平時(shí)間
TL1 = (65536-TLowLevel_RCALeadCode)%256;
TR1 = 1;//打開定時(shí)器0
while(!TF1)IR_OFF();
TF1 = 0;TR1 = 0;
}
/***********************************************************************************************
函數(shù)void RCA_1(void)
功能:產(chǎn)生RCA"1"碼
**********************************************************************************************/
void RCA_1(void)
{
TH1 = (65536-TCarrier_RCA_1)/256;//定時(shí)載波時(shí)間
TL1 = (65536-TCarrier_RCA_1)%256;
TR1 = 1;//打開定時(shí)器0
IR_ON();
while(!TF1)
{
Carrier_38KHz();
}
TF1 = 0;TR1 = 0;
IR_OFF();
TH1 = (65536-TLowLevel_RCA_1)/256;//定時(shí)低電平時(shí)間
TL1 = (65536-TLowLevel_RCA_1)%256;
TR1 = 1;//打開定時(shí)器0
while(!TF1)IR_OFF();
TF1 = 0;TR1 = 0;
}
/***********************************************************************************************
函數(shù)void RCA_0(void)
功能:產(chǎn)生RCA"0"碼
**********************************************************************************************/
void RCA_0(void)
{
TH1 = (65536-TCarrier_RCA_0)/256;//定時(shí)載波時(shí)間
TL1 = (65536-TCarrier_RCA_0)%256;
TR1 = 1;//打開定時(shí)器0
IR_ON();
while(!TF1)
{
Carrier_38KHz();
}
TF1 = 0;TR1 = 0;
IR_OFF();
TH1 = (65536-TLowLevel_RCA_0)/256;//定時(shí)低電平時(shí)間
TL1 = (65536-TLowLevel_RCA_0)%256;
TR1 = 1;//打開定時(shí)器0
while(!TF1)IR_OFF();
TF1 = 0;TR1 = 0;
}
/***********************************************************************************************
函數(shù)void RCA_EndCode(void)
功能:結(jié)束碼
**********************************************************************************************/
void RCA_EndCode(void)
{
RCA_1();
}
/***********************************************************************************************
函數(shù)void IR_RCACode(uint8_t *pHex_RCA)
功能:RCA發(fā)碼(系統(tǒng)碼+用戶碼),高位在前發(fā)射,傳入的參數(shù)為RCA系統(tǒng)碼+用戶碼兩字節(jié)數(shù)組
**********************************************************************************************/
void IR_RCACode(uint8_t *pHex_RCA)
{
uint16_t i;
uint8_t SystemCode = pHex_RCA[0];//RCA系統(tǒng)碼
uint8_t UserCode = pHex_RCA[1];//RCA用戶碼
RCA_LeadCode();//RCA引導(dǎo)碼
for(i=0x0008;i!=0x0000;i=i>>1)//發(fā)送RCA系統(tǒng)碼
{
if(SystemCode&i)
{
RCA_1();
}
else
{
RCA_0();
}
}
for(i=0x0080;i!=0x0000;i=i>>1)//發(fā)送RCA用戶碼
{
if(UserCode&i)
{
RCA_1();
}
else
{
RCA_0();
}
}
SystemCode=~SystemCode;
for(i=0x0008;i!=0x0000;i=i>>1)//發(fā)送RCA系統(tǒng)反碼
{
if(SystemCode&i)
{
RCA_1();
}
else
{
RCA_0();
}
}
UserCode=~UserCode;
for(i=0x0080;i!=0x0000;i=i>>1)//發(fā)送RCA用戶反碼
{
if(UserCode&i)
{
RCA_1();
}
else
{
RCA_0();
}
}
RCA_EndCode();//停止碼
}
/******************************************NEC編碼**********************************************/
/***********************************************************************************************
函數(shù)void NEC_LeadCode(void)
功能:產(chǎn)生NEC引導(dǎo)碼
**********************************************************************************************/
void NEC_LeadCode(void)
{
TH1 = (65536-TCarrier_NECLeadCode)/256;//定時(shí)載波時(shí)間
TL1 = (65536-TCarrier_NECLeadCode)%256;
TR1 = 1;//打開定時(shí)器0
IR_ON();
while(!TF1)
{
Carrier_38KHz();
}
TF1 = 0;TR1 = 0;
IR_OFF();
TH1 = (65536-TLowLevel_NECLeadCode)/256;//定時(shí)低電平時(shí)間
TL1 = (65536-TLowLevel_NECLeadCode)%256;
TR1 = 1;//打開定時(shí)器0
while(!TF1)IR_OFF();
TF1 = 0;TR1 = 0;
}
/***********************************************************************************************
函數(shù)void NEC_1(void)
功能:產(chǎn)生NEC"1"碼
**********************************************************************************************/
void NEC_1(void)
{
TH1 = (65536-TCarrier_NEC_1)/256;//定時(shí)載波時(shí)間
TL1 = (65536-TCarrier_NEC_1)%256;
TR1 = 1;//打開定時(shí)器0
IR_ON();
while(!TF1)
{
Carrier_38KHz();
}
TF1 = 0;TR1 = 0;
IR_OFF();
TH1 = (65536-TLowLevel_NEC_1)/256;//定時(shí)低電平時(shí)間
TL1 = (65536-TLowLevel_NEC_1)%256;
TR1 = 1;//打開定時(shí)器0
while(!TF1)IR_OFF();
TF1 = 0;TR1 = 0;
}
/***********************************************************************************************
函數(shù)void NEC_0(void)
功能:產(chǎn)生NEC"0"碼
**********************************************************************************************/
void NEC_0(void)
{
TH1 = (65536-TCarrier_NEC_0)/256;//定時(shí)載波時(shí)間
TL1 = (65536-TCarrier_NEC_0)%256;
TR1 = 1;//打開定時(shí)器0
IR_ON();
while(!TF1)
{
Carrier_38KHz();
}
TF1 = 0;TR1 = 0;
IR_OFF();
TH1 = (65536-TLowLevel_NEC_0)/256;//定時(shí)低電平時(shí)間
TL1 = (65536-TLowLevel_NEC_0)%256;
TR1 = 1;//打開定時(shí)器0
while(!TF1)IR_OFF();
TF1 = 0;TR1 = 0;
}
/***********************************************************************************************
函數(shù)void NEC_EndCode(void)
功能:結(jié)束碼
**********************************************************************************************/
void NEC_EndCode(void)
{
NEC_1();
}
/******************************************NEC8編碼*******************************************/
/***********************************************************************************************
函數(shù)void IR_NEC8Code(uint8_t *pHex_NEC8)
功能:NEC8發(fā)碼(系統(tǒng)碼+用戶碼),低位在前發(fā)射,傳入?yún)?shù)為NEC8系統(tǒng)碼+用戶碼兩字節(jié)數(shù)組
**********************************************************************************************/
void IR_NEC8Code(uint8_t *pHex_NEC8)
{
uint16_t i;
uint8_t SystemCode = pHex_NEC8[0];//NEC8系統(tǒng)碼
uint8_t UserCode = pHex_NEC8[1];//NEC8用戶碼
NEC_LeadCode();//NEC8引導(dǎo)碼
for(i=0x0001;i!=0x0100;i=i<<1)//發(fā)送NEC8系統(tǒng)碼
{
if(SystemCode&i)
{
NEC_1();
}
else
{
NEC_0();
}
}
SystemCode=~SystemCode;
for(i=0x0001;i!=0x0100;i=i<<1)//發(fā)送NEC8系統(tǒng)反碼
{
if(SystemCode&i)
{
NEC_1();
}
else
{
NEC_0();
}
}
for(i=0x0001;i!=0x0100;i=i<<1)//發(fā)送NEC8用戶碼
{
if(UserCode&i)
{
NEC_1();
}
else
{
NEC_0();
}
}
UserCode=~UserCode;
for(i=0x0001;i!=0x0100;i=i<<1)//發(fā)送NEC8用戶反碼
{
if(UserCode&i)
{
NEC_1();
}
else
{
NEC_0();
}
}
NEC_EndCode();//停止碼
}
/******************************************NEC16編碼********************************************/
/***********************************************************************************************
函數(shù)void IR_NEC16Code(uint16_t *pHex_NEC16)
功能:NEC16發(fā)碼(系統(tǒng)碼+用戶碼),低位在前發(fā)射
**********************************************************************************************/
void IR_NEC16Code(uint16_t *pHex_NEC16)
{
uint16_t i;
uint16_t SystemCode = pHex_NEC16[0];//NEC16系統(tǒng)碼;
uint16_t UserCode = pHex_NEC16[1];//NEC16用戶碼;
uint16_t SystemCode_High=SystemCode/256;
uint16_t SystemCode_Low =SystemCode%256;
NEC_LeadCode();//NEC8引導(dǎo)碼
for(i=0x0001;i!=0x0100;i=i<<1)//發(fā)送NEC8系統(tǒng)碼高八位
{
if(SystemCode_High&i)
{
NEC_1();
}
else
{
NEC_0();
}
}
for(i=0x0001;i!=0x0100;i=i<<1)//發(fā)送NEC8系統(tǒng)碼低八位
{
if(SystemCode_Low&i)
{
NEC_1();
}
else
{
NEC_0();
}
}
for(i=0x0001;i!=0x0100;i=i<<1)//發(fā)送NEC8用戶碼
{
if(UserCode&i)
{
NEC_1();
}
else
{
NEC_0();
}
}
UserCode=~UserCode;
for(i=0x0001;i!=0x0100;i=i<<1)//發(fā)送NEC8用戶反碼
{
if(UserCode&i)
{
NEC_1();
}
else
{
NEC_0();
}
}
NEC_EndCode();//停止碼
}
復(fù)制代碼
作者:
vectorxu
時(shí)間:
2018-4-18 21:00
不錯(cuò),下載學(xué)習(xí),謝謝樓主!
作者:
127lo
時(shí)間:
2018-8-10 16:27
剛好需要,謝謝分享
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1