基于單片機(jī)的紅外發(fā)送程序
本程序適用于Infineon--XC866的單片機(jī),載波為38KHZ,通過P3_0作為發(fā)送端口,其中:0由0.56m的高電平和0.56ms的低電平組成,1由0.56的高電平和1.68ms的電平組成。稍加修改可應(yīng)用于C51。
發(fā)送程序如下:
//****************************************************************************// @Module Project Settings// @Filename MAIN.C// @Project 20W.dav//----------------------------------------------------------------------------// @Controller Infineon XC866-2FR//// @Compiler Keil//// @Codegenerator 2.0//// @Description This file contains the Project initialization function.////----------------------------------------------------------------------------// @Date 2010-11-4 15:54:56////****************************************************************************// USER CODE BEGIN (MAIN_General,1)// USER CODE END//****************************************************************************// @Project Includes// 完整代碼下載地址:點(diǎn)這里// 與本程序?qū)?yīng)的紅外接收程序代碼:http://www.51hei.com/mcu/1096.html//****************************************************************************#include "MAIN.H"http:// USER CODE BEGIN (MAIN_General,2)#includeintrins.h>#define uint unsigned int //宏定義#define uchar unsigned char#define HWTx P3_0 //位聲明:紅外發(fā)射管腳 bit HWTx_Out; //紅外發(fā)射管腳的狀態(tài)bit Key_Flag,Flag; //分別是:按鍵按下的標(biāo)志位,定時(shí)器開始的標(biāo)志位uint Count,Set_Count; //控制定時(shí)時(shí)間的變量uchar Table_Tx[4]={0x40,0,0,0};//待發(fā)送的緩沖區(qū)uchar HWTx_Code,HWTx_data;// USER CODE END//****************************************************************************// @Macros//****************************************************************************// USER CODE BEGIN (MAIN_General,3)// USER CODE END//****************************************************************************// @Defines//****************************************************************************// USER CODE BEGIN (MAIN_General,4)// USER CODE END//****************************************************************************// @Typedefs//****************************************************************************// USER CODE BEGIN (MAIN_General,5)// USER CODE END//****************************************************************************// @Imported Global Variables//****************************************************************************// USER CODE BEGIN (MAIN_General,6)// USER CODE END//****************************************************************************// @Global Variables//****************************************************************************// USER CODE BEGIN (MAIN_General,7)// USER CODE END//****************************************************************************// @External Prototypes//****************************************************************************// USER CODE BEGIN (MAIN_General,8)// USER CODE END//****************************************************************************// @Prototypes Of Local Functions//****************************************************************************// USER CODE BEGIN (MAIN_General,9)// USER CODE END//****************************************************************************// @Function void MAIN_vInit(void) ////----------------------------------------------------------------------------// @Description This function initializes the microcontroller. It is // assumed that the SFRs are in their reset state.////----------------------------------------------------------------------------// @Returnvalue None////----------------------------------------------------------------------------// @Parameters None////----------------------------------------------------------------------------// @Date 2010-11-4////****************************************************************************// USER CODE BEGIN (MAIN_Init,1)// USER CODE ENDvoid MAIN_vInit(void){// USER CODE BEGIN (MAIN_Init,2)// USER CODE END/// -----------------------------------------------------------------------/// Configuration of the System Clock:/// -----------------------------------------------------------------------/// - On Chip Osc is Selected/// - PLL Mode, NDIV = 2/// - input frequency is 10 MHz/// *********************************************************************************/// Note : All peripheral related IO configurations are done in the /// respective peripheral modules (alternate functions selection)/// *********************************************************************************/// Initialization of module 'GPIO'IO_vInit();// Interrupt PriorityIP = 0x00; // load Interrupt Priority RegisterIPH = 0x00; // load Interrupt Priority High RegisterIP1 = 0x00; // load Interrupt Priority 1 RegisterIPH1 = 0x00; // load Interrupt Priority 1 High Register// USER CODE BEGIN (MAIN_Init,3)// USER CODE END// globally enable interruptsEA = 1; } // End of function MAIN_vInit//****************************************************************************// @Function void main(void) ////----------------------------------------------------------------------------// @Description This is the main function.////----------------------------------------------------------------------------// @Returnvalue None////----------------------------------------------------------------------------// @Parameters None////----------------------------------------------------------------------------// @Date 2010-11-4////****************************************************************************// USER CODE BEGIN (MAIN_Main,1)void delay(uint z) //延時(shí)時(shí)間約為 1ms*X 晶振為12M{ uint x=0,y=0;for(x=z;x>0;x--)for(y=54;y>0;y--);}void Key_Scan(){uchar Key_Temp=0,i=0;if(P3_1!=1) { delay(10); if(P3_1!=1) { while(!P3_1); Key_Flag=1; for(i=1;i4;i++)Table_Tx[i]=i+0xaa;}} }void Send_Code8(){uchar i=0; //循環(huán)變量for(i=0;i8;i++) //8位數(shù)據(jù),顧循環(huán)8次{Set_Count=0x002b;//準(zhǔn)備發(fā)送長(zhǎng)達(dá)0.56ms(13*43=560us)的方波Flag=1; //置標(biāo)志位(通過該標(biāo)志位控制高低電平的轉(zhuǎn)換),因?yàn)楦唠娖狡骷?8K的波形不會(huì)改變 Count=0; //清零,準(zhǔn)備計(jì)數(shù) TR0=1; //啟動(dòng)定時(shí)器 while(CountSet_Count); //定時(shí)0.56ms,先發(fā)送0.56ms的高電平 TR0=0; //關(guān)閉定時(shí)器 if(HWTx_Code0x01) //判斷待發(fā)送的數(shù)據(jù)的最低位為1或0, { //將決定低電平時(shí)間的長(zhǎng)短,便于接收裝置的接收 Set_Count=130; //如果是1,則產(chǎn)生1.69ms(13*130=1690us)的低電平 } else { Set_Count=43; //如果是0,則產(chǎn)生0.56ms(13*43=560us)的低電平 } Flag=0; //用于產(chǎn)生低電平的標(biāo)志位 Count=0; //清零,準(zhǔn)備計(jì)數(shù) TR0=1; //啟動(dòng)定時(shí)器 while(CountSet_Count);//產(chǎn)生長(zhǎng)達(dá)對(duì)應(yīng)時(shí)間的低電平 TR0=0; //關(guān)閉定時(shí)器 HWTx_Code=HWTx_Code>>1;//右移一位,準(zhǔn)備發(fā)送 }}void Send_Code(){ uchar i=0;Set_Count=690;//準(zhǔn)備產(chǎn)生9ms(13*690=9000)的高電平 Flag=1; //置標(biāo)志位,取反(該標(biāo)志位控制高低電平的轉(zhuǎn)換)Count=0; //定時(shí)器計(jì)數(shù)TR0=1; //啟動(dòng)定時(shí)器while(CountSet_Count);//定時(shí)9ms,產(chǎn)生9ms的高電平TR0=0; //關(guān)閉定時(shí)器Set_Count=340;//準(zhǔn)備產(chǎn)生4.5ms(13*340=4500)的低電平Flag=0; //置位,取反(該標(biāo)志位控制高低電平的轉(zhuǎn)換) Count=0; //清零,準(zhǔn)備計(jì)數(shù)TR0=1; //啟動(dòng)定時(shí)器T0while(CountSet_Count); //產(chǎn)生4.5ms的低電平TR0=0;for(i=0;i4;i++) //發(fā)送緩沖區(qū)里的數(shù)據(jù){HWTx_Code=Table_Tx[i]; //賦值,等待發(fā)送Send_Code8(); //發(fā)送}Set_Count=43;//準(zhǔn)備產(chǎn)生4.5ms(13*340=4500)的低電平Flag=1; //置位,取反(該標(biāo)志位控制高低電平的轉(zhuǎn)換)Count=0; //清零,準(zhǔn)備計(jì)數(shù)TR0=1; //啟動(dòng)定時(shí)器T0while(CountSet_Count); //產(chǎn)生4.5ms的低電平TR0=0;HWTx=1; //置發(fā)射引腳的狀態(tài)為高電平 delay(23);//延時(shí)23msSet_Count=690; //準(zhǔn)備產(chǎn)生9.12ms的低電平,作為結(jié)束標(biāo)志Flag=1;Count=0;TR0=1;while(CountSet_Count);//產(chǎn)生時(shí)間長(zhǎng)達(dá)9.12ms的低電平TR0=0; //關(guān)閉定時(shí)器Set_Count=340;//準(zhǔn)備產(chǎn)生4.5ms的高電平,作為結(jié)束標(biāo)志Flag=0;Count=0;TR0=1;while(CountSet_Count);TR0=0;HWTx=1; //輸出引腳拉高}void init_timer0(){EA=1;TMOD=0x02;//工作在方式2,8位自動(dòng)重裝模式ET0=1; //開分控TH0=0x52; //定時(shí)時(shí)間為13us,晶振12MTL0=0x52;}// USER CODE ENDvoid main(void){// USER CODE BEGIN (MAIN_Main,2)// USER CODE ENDMAIN_vInit();// USER CODE BEGIN (MAIN_Main,3)init_timer0(); //定時(shí)器T0的初始化函數(shù)Count=0; //定時(shí)器計(jì)數(shù)值清零Flag=0; //高低電平變化標(biāo)志位HWTx_Out=1; //發(fā)射引腳的狀態(tài)// USER CODE ENDwhile(1){// USER CODE BEGIN (MAIN_Main,4)Key_Scan(); //按鍵掃描函數(shù)if(Key_Flag==1) //按鍵按下的標(biāo)志,是否置位 { Send_Code(); //發(fā)送數(shù)據(jù) delay(100); //延時(shí)5s Key_Flag=0; //按鍵按下標(biāo)志位清零}// USER CODE END}} // End of function main// USER CODE BEGIN (MAIN_General,10)void timer0() interrupt 1 //定時(shí)器T0{Count++; //變量計(jì)數(shù),可得出進(jìn)入T0的次數(shù),便可得出定時(shí)時(shí)間if(Flag==1){HWTx_Out=~HWTx_Out; //取反,不斷輸出周期為26us的方波}else{ HWTx_Out=0; //這里決定在接受程序,是以高電平還是低電平作為判斷1 和0的依據(jù)}HWTx=HWTx_Out; }// USER CODE END
評(píng)論