|
利用proteus進(jìn)行紅外仿真
MCU采用的是51單片機(jī)
供參考和學(xué)習(xí)使用
單片機(jī)源程序如下:
- //作用 紅外發(fā)射與接收一體 萬能遙控解碼
- #include"reg52.h"
- #define uchar unsigned char
- #define uint unsigned int
- //定時(shí)器寄存器設(shè)置
- #define THO_int0 0xfa
- #define TLO_int0 0x8d
- #define TH1_int1 0xff
- #define TL1_int1 177
-
- sbit LED1=P2^1;
- sbit LED2=P2^2;
- sbit LED3=P2^3;
- sbit LED4=P2^0;
- sbit Beep=P3^7;
- sbit KEY1=P1^4;
- sbit KEY2=P1^5;
- sbit KEY3=P1^6;
- sbit KEY4=P1^7;
- sbit infrared = P3^3;
- sbit infr_send = P3^4;
- sbit tiaoshi = P3^6;
- //表格存于ROM*************************************
- uchar code dsp_tap[16]={0xa0,0xbb,0x62,0x2a,0x39,0x2c,0x24,0xba,0x20,0x28,0x30,0x25,0xe4,0x23,0x64,0x74};
- uchar code dsp_cs[4]={0x70,0xd0,0xb0,0xe0};
- //uchar code dat_tap[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
- //uchar code dat_tap2[13]={0,31,29,31,30,31,30,31,31,30,31,30,31};
- //變量定義存于RAM**********************************
- uchar SimpleKeyState = 0; //簡單按鍵的狀態(tài)
- uchar SimpleKeyPressDelay = 7; //簡單按鍵按實(shí)的大概時(shí)間,單位:10毫秒
- uchar SimpleKeyCode = 0xFF; //簡單按鍵鍵碼
- uchar infr_key1_data[4] = {0};
- uint infr_send_cont = 0;
- uchar infrared_new_data[4] = {0};
- uchar infrared_old_data[4] = {0x80,0xff,0xc8,0x37};
- uchar infrared_byte = 0;
- uchar infrared_byte_cont = 0;
- uchar infrared_old_state = 0;
- uchar infrared_new_state = 0;
- uint infrared_cont = 0;
- uint infrared_cont_now = 0;
- uchar flg_infrared = 0;
- uchar flg_infrared_ok = 0;
- uchar seg_step = 0;
- uchar dsp_step;
- uchar cont_4ms;
- uint cont_1s;
- uchar cont_500ms;
- uchar cont_10ms;
- uchar sec;
- uchar beef_delay;
- uchar dis_play[4] = {0};
- uint dis_num = 0;
- uchar flg_display = 0;
- uchar flg_change = 0;
- uchar flg_send_start = 0;
- uchar aaa[8] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
- uchar flg_key = 0;
- void writ_send(uint tin)
- {
- uchar j;
- uint i;
- for(i=0;i<tin;i++)
- {
- for(j=0;j<4;j++);
- infr_send =~ infr_send;
- }
- infr_send = 1;
- }
- void delay(uint w)
- {
- uint i;
- for(i = 0;i<w;i++);
- }
- void infr_send_ctrol(void)
- {
- uchar cont;
- EA=0;
- infrared_byte_cont = 0;
- infrared_byte = 0;
- writ_send(660); //頭碼9ms
- delay(748); //4500us 高電平延時(shí)
- for(cont = 0;cont < 33; cont++)
- {
- writ_send(38); //560us低電平發(fā)射 42
- if(infrared_old_data[infrared_byte] & aaa[infrared_byte_cont])
- {
- delay(280); //1690us 高電平延時(shí) 1 269
- infrared_byte_cont++;
- }else
- {
- delay(93); //560us 高電平延時(shí) 0 89
- infrared_byte_cont++;
- }
- if(infrared_byte_cont == 8)
- {
- infrared_byte++;
- infrared_byte_cont = 0;
- }
- }
- EA=1;
- }
- /*============
- 簡單按鍵處理子函數(shù)
- ==============*/
- void SimpleKeyAction(void)
- {
- if(SimpleKeyCode == 1)
- {
- LED1 =~ LED1;
- infr_send_ctrol();
- }
- else if(SimpleKeyCode == 2)
- {
-
- LED2 =~ LED2;
- // infr_send =~ infr_send;
- }
- else if(SimpleKeyCode == 3)
- {
- //LED3 =~ LED3;
- }else if(SimpleKeyCode == 4)
- {
- Beep = 0;
- LED4 =~ LED4;
- }
- }
- /*============
- 簡單按鍵掃描子函數(shù)
- ==============*/
- void SimpleKeyScan(void)
- {
- P1 =P1|0xf0;
- if(SimpleKeyPressDelay > 0) SimpleKeyPressDelay--;
-
- if( (SimpleKeyState == 0) && ((KEY1 ==0) || (KEY2 ==0) || (KEY3 ==0) || (KEY4 ==0)) ) //某個(gè)按鈕被按下
- {
- SimpleKeyState = 1;
- SimpleKeyPressDelay = 7; //從接觸按鍵到把按鍵按下所需要的大概時(shí)間:70毫秒
- }
- if(SimpleKeyState == 1)
- {
- if(SimpleKeyPressDelay == 0) //按鍵按實(shí)
- {
- // Beep = 0;
- SimpleKeyState = 2;
- if(KEY1 ==0)
- {
- SimpleKeyCode = 1;
- }
- else if(KEY2 ==0)
- {
- SimpleKeyCode = 2;
- }
- else if(KEY3 ==0)
- {
- SimpleKeyCode = 3;
- }
- else if(KEY4 ==0)
- {
- SimpleKeyCode = 4;
- }
-
- SimpleKeyAction();//按鍵處理
- }
- }
- if(SimpleKeyState == 2)
- {
- if( (KEY1 == 1) && (KEY2 == 1) && (KEY3 == 1) && (KEY4 == 1) ) //所有的按鍵松開
- {
- SimpleKeyPressDelay = 7; //松開按鍵所需要的大概時(shí)間:70毫秒
- SimpleKeyState = 3;
- }
- }
- if(SimpleKeyState == 3)
- {
- if(SimpleKeyPressDelay == 0) //已松開按鍵
- {
- SimpleKeyState = 0;
- SimpleKeyCode = 0xFF;
- }
- }
- }
- //******************************************************************
- void T0_int(void)
- {
- //定時(shí)器0初始化設(shè)置
- TR0 = 1;
- TH0=THO_int0;
- TL0=TLO_int0;
- ET0=1;
- EA=1;
- }
- //*********************************************************************
- void MCU_init(void)
- {
- P1=0xff;
- P2=0xff;
- P3=0xff;
- T0_int();
- // Beep = 0;
- }
-
- //*******************************************************************
-
- void timer0_(void)interrupt 1 // 定時(shí)器溢出周期為100us
- {
- TR0=0;
- TH0=THO_int0;
- TL0=TLO_int0;
- TR0=1;
- // tiaoshi =~ tiaoshi;
- if(flg_send_start == 0)
- {
- //紅外接收代碼
- P3=(P3|0x08); //把紅外管腳的電平設(shè)為1
- infrared_old_state = infrared_new_state; //保留上一次的電平
- infrared_new_state = (P3&0x08); //讀取現(xiàn)在的管腳電平值
- if(infrared_new_state == infrared_old_state) infrared_cont++; //如果兩次的電平相同 變量infrared_cont 自加1
- else if((P3&0x08) == 0x00) //如果兩次的電平不相同 且 該管腳的電平為低電平
- {
- infrared_cont_now = infrared_cont; //保留infrared_cont的值
- infrared_cont = 0; //變量清零
- if(flg_infrared == 1)
- {
- if((infrared_cont_now >= 6)&&(infrared_cont_now <= 25)) //如果變量值在6到15這個(gè)范圍之內(nèi)
- {
- if(infrared_cont_now <= 14) //小于的話 設(shè)置為0
- {
- infrared_new_data[infrared_byte] = infrared_new_data[infrared_byte]<<1;
- infrared_new_data[infrared_byte] = (infrared_new_data[infrared_byte]&0xfe);
- infrared_byte_cont++;
- }
- else //大于的話 設(shè)置為1
- {
- infrared_new_data[infrared_byte] = infrared_new_data[infrared_byte]<<1;
- infrared_new_data[infrared_byte] = (infrared_new_data[infrared_byte]|0x01);
- infrared_byte_cont++;
- }
- if(infrared_byte_cont == 8) //當(dāng)存儲(chǔ)玩8位數(shù)據(jù)infrared_new_data[0]之后,再存儲(chǔ)infrared_new_data[1]
- {
-
- infrared_byte++;
- infrared_byte_cont = 0;
- if(infrared_byte == 4) //當(dāng)存儲(chǔ)完32位數(shù)據(jù)之后
- {
- infrared_byte = 0;
- flg_infrared = 0; //標(biāo)志位清零
- infrared_old_data[0] = infrared_new_data[0]; //保留數(shù)據(jù)
- infrared_old_data[1] = infrared_new_data[1];
- infrared_old_data[2] = infrared_new_data[2];
- infrared_old_data[3] = infrared_new_data[3];
- flg_infrared_ok = 1;
- }
-
- }
- }else flg_infrared = 0;
- }else
- {
- if((infrared_cont_now >= 80)&&(infrared_cont_now <= 150)) //頭嗎解讀
- {
- flg_infrared = 1; //標(biāo)志位置1
- infrared_byte = 0; //變量清零
- infrared_byte_cont = 0;
- }else flg_infrared = 0;
- }
- }
- }
- cont_4ms++;
- if(cont_4ms >= 40)
- {
- cont_4ms=0;
- flg_display = 1;
- //蜂鳴器控制****************************
- if(Beep == 0)
- {
- beef_delay++;
- if(beef_delay > 10) Beep = 1;
- }else beef_delay = 0;
- //**************************************
- }
- cont_1s++;
- if(cont_1s>=10000)
- {
- cont_1s=0;
- // dis_num++;
- flg_change =~ flg_change;
- }
- }
- void main(void)
- {
- MCU_init();
- tiaoshi = 0;
- for(;;)
- {
- SimpleKeyScan();
- if(flg_infrared_ok == 1)
- {
- flg_infrared_ok = 0;
- LED4 =~ LED4;
- Beep = 0;
- }
-
- if(flg_display)
- {
- flg_display = 0;
- P2 = (P2&0x0f);
- if(flg_change)
- {
- dis_play[3] = dsp_tap[infrared_old_data[2]%256/16];
- dis_play[2] = dsp_tap[infrared_old_data[2]%16];
- dis_play[1] = dsp_tap[infrared_old_data[3]%256/16];
- dis_play[0] = dsp_tap[infrared_old_data[3]%16];
- }else
- {
- dis_play[3] = dsp_tap[infrared_old_data[0]%256/16];
- dis_play[2] = dsp_tap[infrared_old_data[0]%16];
- dis_play[1] = dsp_tap[infrared_old_data[1]%256/16];
- dis_play[0] = dsp_tap[infrared_old_data[1]%16];
- }
- P0 = dis_play[seg_step];
- P2 = (P2|dsp_cs[seg_step]);
- seg_step++;
- if(seg_step>=4)seg_step = 0;
- }
- }
- }
- void EXT0_INT(void) interrupt 0 {;}
- void EXT1_INT(void) interrupt 2 {;}
- void timer1_(void) interrupt 3 {;}
- void serial_(void) interrupt 4 {;}
復(fù)制代碼
|
評分
-
查看全部評分
|