- /*此程序是針對(duì)uPD6121系列遙控器的取碼程序,解碼值在Im[2]中,當(dāng)IrOK=1時(shí)解碼有效*/
- /******************************************************************************/
- /* 項(xiàng)目名稱 : 51hei擴(kuò)展板 紅外遙控器控制直流電機(jī) */
- /* 主控芯片 : STC89C52 */
- /* 文件名稱 : Inf_DcMotor */
- /* 文件功能 : 主函數(shù) */
- /* 文件版權(quán) : 北京?酥莿(dòng)科技開(kāi)發(fā)有限公司 */
- /* 文件版本 : */
- /******************************************************************************/
- /**********************************包含頭文件**********************************/
- #include <reg52.h>
- /************************************宏定義************************************/
- #define uchar unsigned char
- #define Imax 14000 //此處為晶振為11.0592時(shí)的取值,
- #define Imin 8000 //如用其它頻率的晶振時(shí),
- #define Inum1 1450 //要改變相應(yīng)的取值。
- #define Inum2 700
- #define Inum3 3000
- /************************************位定義************************************/
- sbit dula = P2^6;
- sbit wela = P2^7;
- sbit pwm = P1^7 ; //PWM信號(hào)輸出
- /********************************定義變量和數(shù)組********************************/
- uchar code table[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71}; //數(shù)碼管段碼
- uchar Im[4] = {0x00,0x00,0x00,0x00};
- uchar f;
- uchar IrOK;
- uchar PulseWidth = 0; //定義脈寬值
- uchar StartFlag = 0; //定義啟動(dòng)標(biāo)志位 0為停止 1為啟動(dòng)
- uchar count; //0.5ms次數(shù)標(biāo)識(shí)
- unsigned long m,Tc;
- /******************************************************************************/
- /* 函數(shù)名稱 : delay */
- /* 函數(shù)描述 : 延時(shí)函數(shù) */
- /* 輸入?yún)?shù) : 無(wú) */
- /* 參數(shù)描述 : 無(wú) */
- /* 返回值 : 無(wú) */
- /******************************************************************************/
- void delay(uchar i)
- {
- uchar j,k;
- for(j = i;j > 0;j--)
- {
- for(k = 125;k > 0;k--);
- }
- }
- /******************************************************************************/
- /* 函數(shù)名稱 : display */
- /* 函數(shù)描述 : 顯示函數(shù)函數(shù) */
- /* 輸入?yún)?shù) : 無(wú) */
- /* 參數(shù)描述 : 無(wú) */
- /* 返回值 : 無(wú) */
- /******************************************************************************/
- void display(void)
- {
- dula = 0;
- P0 = table[PulseWidth/10]; //顯示脈寬值高位
- dula = 1;
- dula = 0;
- wela = 0;
- P0 = 0xfe;
- wela = 1;
- wela = 0;
- delay(5);
- }
- /******************************************************************************/
- /* 函數(shù)名稱 : intersvr1 */
- /* 函數(shù)描述 : 外部中斷解碼程序 */
- /* 輸入?yún)?shù) : 無(wú) */
- /* 參數(shù)描述 : 無(wú) */
- /* 返回值 : 無(wú) */
- /******************************************************************************/
- void intersvr1(void) interrupt 2 using 1
- {
- Tc = TH0 * 256 + TL0; //提取中斷時(shí)間間隔時(shí)長(zhǎng)
- TH0 = 0;
- TL0 = 0; //定時(shí)中斷重新置零
- if((Tc > Imin) && (Tc < Imax))
- {
- m = 0;
- f = 1;
- return;
- } //找到啟始碼
- if(f == 1)
- {
- if(Tc > Inum1 && Tc < Inum3)
- {
- Im[m / 8] = Im[m / 8] >> 1 | 0x80;
- m++;
- }
- if(Tc > Inum2 && Tc < Inum1)
- {
- Im[m / 8] = Im[m / 8] >> 1;
- m++; //取碼
- }
- if(m == 32)
- {
- m = 0;
- f = 0;
- if(Im[2] ==~ Im[3])
- {
- IrOK =1 ;
- }
- else
- {
- IrOK = 0; //取碼完成后判斷讀碼是否正確
- }
- }
- }
- }
- /******************************************************************************/
- /* 函數(shù)名稱 : Inf_Dispose */
- /* 函數(shù)描述 : 紅外接收處理函數(shù) */
- /* 輸入?yún)?shù) : 無(wú) */
- /* 參數(shù)描述 : 無(wú) */
- /* 返回值 : 無(wú) */
- /******************************************************************************/
- void Inf_Dispose(void)
- {
- if(IrOK == 1)
- {
- switch(Im[2])
- {
- case 0x44: //遙控器 開(kāi)始/停止 鍵按下 啟動(dòng)停止控制
- StartFlag =~ StartFlag;
- if(StartFlag) //啟動(dòng) 打開(kāi)定時(shí)器中斷
- {
- if(PulseWidth) //只有當(dāng)脈寬值不為0時(shí)才可以啟動(dòng)
- {
- ET0 = 1;
- }
- }
- else //停止 關(guān)閉定時(shí)器中斷
- {
- ET0 = 0;
- pwm = 0;
- }
- break;
- case 0x09: //遙控器 - 鍵按下 脈寬值加10
- PulseWidth += 10;
- count=0;
- if(PulseWidth==60)
- {
- PulseWidth = 50;
- }
- break;
- case 0x15: //遙控器 + 鍵按下 脈寬值減10
- PulseWidth -= 10;
- count=0;
- if(PulseWidth == 0)
- {
- PulseWidth = 10;
- }
- if(PulseWidth == 246)
- {
- PulseWidth = 0;
- }
- break;
- default:
- break;
- }
- IrOK = 0;
- }
- }
- /******************************************************************************/
- /* 函數(shù)名稱 : Time0_Init */
- /* 函數(shù)描述 : 定時(shí)器0初始化函數(shù) */
- /* 輸入?yún)?shù) : 無(wú) */
- /* 參數(shù)描述 : 無(wú) */
- /* 返回值 : 無(wú) */
- /******************************************************************************/
- void Time0_Init() //定時(shí)器初始化
- {
- TMOD |= 0x01; //定時(shí)器0工作在方式1
- IE |= 0x82;
- TH0 = 0xfe;
- TL0 = 0x33; //11.0592MZ晶振,0.5ms
- TR0 = 1; //定時(shí)器開(kāi)始
- ET0 = 0;
- }
- /******************************************************************************/
- /* 函數(shù)名稱 : Time0_Int */
- /* 函數(shù)描述 : 定時(shí)器0中斷函數(shù) */
- /* 輸入?yún)?shù) : 無(wú) */
- /* 參數(shù)描述 : 無(wú) */
- /* 返回值 : 無(wú) */
- /******************************************************************************/
- void Time0_Int() interrupt 1 //中斷程序
- {
- TH0 = 0xfe; //重新賦值
- TL0 = 0x33;
-
- count++;
- if(count == PulseWidth) //對(duì)應(yīng)的脈寬值輸出低電平
- {
- pwm = 0;
- }
- if(count == 60)//30ms周期到達(dá)后
- {
- pwm = 1; //輸出高電平
- count = 0;
- }
- }
- /******************************************************************************/
- /* 函數(shù)名稱 : main */
- /* 函數(shù)描述 : 主函數(shù) */
- /* 輸入?yún)?shù) : 無(wú) */
- /* 參數(shù)描述 : 無(wú) */
- /* 返回值 : 無(wú) */
- /******************************************************************************/
- void main(void)
- {
- m = 0;
- f = 0;
- EA = 1;
-
- IT1 = 1;
- EX1 = 1;
-
- TMOD = 0x11;
-
- PulseWidth = 0;
- count = 0;
- pwm = 0;
- Time0_Init();
- while(1)
- {
- Inf_Dispose(); //紅外接收處理
- display(); //數(shù)碼管顯示
- }
- }
- /******************************************************************************/
復(fù)制代碼 |