|
pic單片機(jī)的軟件編碼解碼程序:
- /******************************************************************************/
- /****************************** 遙控器接收程序 ********************************/
- /******************************************************************************/
- #include <pic.h>
- #include <pic1687x.h>
- #define remote_geshu 10
- /******************************************************************************/
- union BIT_16
- {
- int TIMER1_REG;
- unsigned char REG[2];
- }
- union BIT_32
- {
- unsigned long data_temp_long;
- unsigned char data_temp_byte[4];
- }
- /******************************************************************************/
- static union BIT_16 TIMER1_TEMP;//16位定時(shí)器1
- static union BIT_32 data_temp;
- /******************************************************************************/
- static volatile unsigned char rec_status @ 97;
- static unsigned char data_cout;//接收的遙控器碼位數(shù)
- static unsigned char data;//接收的4位數(shù)據(jù)
- static unsigned int h_pulse;//高電平寬度
- static unsigned int l_pulse;//低電平寬度
- static unsigned char remote_cout;//遙控器數(shù)量
- static unsigned char remote_numb;//遙控器編號(hào)
- /******************************************************************************/
- static unsigned char TIMER15S1;//清除學(xué)習(xí)碼按鍵長(zhǎng)按時(shí)間
- static unsigned char TIMER15S2;//學(xué)習(xí)等待時(shí)間
- static unsigned char TIMER15S3;//遙控器數(shù)據(jù)緩沖時(shí)間
- static unsigned char TIMER15S4;//LED顯示時(shí)間
- static unsigned char TIMER15S5;//
- static unsigned char TIMER15S6;//
- static unsigned char TIMER15S7;//
- static unsigned char TIMER15S8;//
- /******************************************************************************/
- static bit head @ ((unsigned)(&rec_status)*8+(0));//同步頭標(biāo)志位
- static bit learn @ ((unsigned)(&rec_status)*8+(1));//學(xué)習(xí)標(biāo)志位
- static bit recieved @((unsigned)(&rec_status)*8+(2));//接收完成標(biāo)志位
- static bit remote_button_status @((unsigned)(&rec_status)*8+(3));//遙控器按鍵標(biāo)志位
- static bit first_click_status @((unsigned)(&rec_status)*8+(4));//遙控器按鍵單擊標(biāo)志位
- /******************************************************************************/
- /********************************** 數(shù)據(jù)接收 **********************************/
- /******************************************************************************/
- unsigned char data_read(void)
- {
- if(h_pulse>l_pulse)
- {
- if((l_pulse>200)&&(l_pulse<1000))
- {
- if(h_pulse<(l_pulse<<2)) return 1;//數(shù)據(jù)為1
- }
- return 2;//無(wú)效的數(shù)據(jù)
- }
- else if(h_pulse<l_pulse)
- {
- if((h_pulse>200)&&(h_pulse<1000))
- {
- if(l_pulse<(h_pulse<<2)) return 0;//數(shù)據(jù)為0
- }
- return 2;//無(wú)效的數(shù)據(jù)
- }
- }
- /******************************************************************************/
- void clr_head(void)//清除寄存器
- {
- data_cout=0;
- head=0;
- }
- /******************************************************************************/
- #pragma interrupt_level 1
- void check_data(void)//檢測(cè)數(shù)據(jù)是否正確
- {
- if(head)
- {
- switch (data_read())
- {
- case 0:(data_temp.data_temp_long)<<=1;;data_cout++;break;
- case 1:(data_temp.data_temp_long)<<=1;(data_temp.data_temp_long)++;;data_cout++;break;
- default:clr_head();break;
- }
- if(data_cout>23)
- {
- INTE=0;
- recieved=1;
- clr_head();//
- }
- }
- }
- /******************************************************************************/
- #pragma interrupt_level 1
- void check_head(void)
- {
- if((!head)&&(!recieved))//
- {
- if ((h_pulse>300)&&(h_pulse<1000))
- {
- if((l_pulse>h_pulse*27)&&(l_pulse<h_pulse*35)) {
- head=1;
- }
- }
- }
- } //end******************>>
- /******************************************************************************/
- #pragma interrupt_level 1
- void interrupt level_h_l(void)
- {
- /* if(RAIF)//如果是電平中斷
- { PORTA=PORTA;
- RAIF=0;//
- TIMER1_TEMP.REG[0]=TMR1L;
- TIMER1_TEMP.REG[1]=TMR1H; TMR1H=0;
- TMR1L=0;
- if(RA4)//如果是低電平中斷
- { l_pulse=TIMER1_TEMP.TIMER1_REG;
- check_data();
- check_head();
- }
- else//如果是高電平中斷
- {
- h_pulse=TIMER1_TEMP.TIMER1_REG;
- }
- }*/
- if(INTF)
- {
- INTF=0;
- TIMER1_TEMP.REG[0]=TMR1L;
- TIMER1_TEMP.REG[1]=TMR1H;
- TMR1H=0;
- TMR1L=0;
- if(INTEDG)//低電平寬度
- {
- INTEDG=0;
- l_pulse=TIMER1_TEMP.TIMER1_REG;
- check_data();
- check_head();
- }
- else
- {
- INTEDG=1;
- h_pulse=TIMER1_TEMP.TIMER1_REG; }
- }
- if(T0IF)//
- {
- T0IF=0;
- TIMER15S1++;
- TIMER15S2++;
- TIMER15S3++; TIMER15S4++;
- TIMER15S5++;
- TIMER15S6++;
- TIMER15S7++;
- TIMER15S8++;
- } }
- /******************************************************************************/
- /********************************* 遙控器學(xué)習(xí)**********************************/
- /******************************************************************************/
- void clr_learn_reg(void)
- {
- unsigned char n;
- di();
- for (n=1;n==remote_cout*4;n++)
- {
- eeprom_write(n,0);
- }
- eeprom_write(70,0); eeprom_write(71,0);
- ei(); }
- /******************************************************************************/
- unsigned char compare_data(unsigned char eep_addr)
- {
- unsigned char n;
- union BIT_32 addr_data; for(n=0;n<4;n++)
- {
- addr_data.data_temp_byte[n]=eeprom_read(eep_addr+n);
- } if((data_temp.data_temp_long&0x00fffff0)==(addr_data.data_temp_long&0x00fffff0)) {
- return 1;//地址匹配返回1
- }
- return 0;//地址不匹配返回0 }
- /******************************************************************************/
- unsigned char compare_all_data(void)
- {
- unsigned char n;
- for (n=0;n<remote_cout;n++)//在已經(jīng)學(xué)習(xí)好的遙控器地址里比較有沒有相同的
- {
- if (compare_data(n*4+1))//如果有相同的遙控器 {
- return 1;
- }
- }
- return 0;//沒有一個(gè)地址是相同的 }
- /******************************************************************************/
- //讀遙控器的數(shù)量
- void read_remote_cout(void)
- {
- remote_cout=eeprom_read(71);//讀出已經(jīng)學(xué)習(xí)的遙控器總數(shù)量
- if(remote_cout>20)remote_cout=0; //如果EEPROM是空的則為0 }
- /******************************************************************************/
- void check_learn_pro(void)
- {
- unsigned char n;
- if(learn)
- {
- learn=0; remote_numb=eeprom_read(70);//讀出現(xiàn)在可以覆蓋掉哪個(gè)遙控器的編號(hào)
- read_remote_cout();////讀遙控器的數(shù)量
- if(remote_numb>(remote_geshu-1))remote_numb=0;//如果遙控器的編號(hào)已經(jīng)是最大的了 則從小開始
- if((remote_cout==0)||!compare_all_data())//如果還沒有遙控器學(xué)習(xí)或沒有相同地址的遙控器學(xué)習(xí)
- {
- di(); for(n=0;n<4;n++)
- {
- eeprom_write (remote_numb*4+n+1,data_temp.data_temp_byte[n]);
- } remote_numb++;
- if(remote_cout<remote_geshu+1)remote_cout++;//已經(jīng)學(xué)習(xí)好的遙控器數(shù)量
- eeprom_write(71,remote_cout);//保存已經(jīng)學(xué)習(xí)好的遙控器總數(shù)量 eeprom_write
- (70,remote_numb);//保存已學(xué)習(xí)的遙控起編號(hào)
- ei(); }
- } }
- /******************************************************************************/
- void check_out_pro(void)
- {
- read_remote_cout();//讀遙控器的數(shù)量
- if(compare_all_data())
- { data=data_temp.data_temp_byte[0]&0x0f;//
- TIMER15S3=0;
- TIMER15S4=0;
- RC4=1;
- } }
- /******************************************************************************/
- void decode_init(void)//接收初始化
- {
- OPTION=0x87;
- RBPU=0;
- TMR0=0;
- T0IE=1;//使能定時(shí)器0中斷 INTE=1;
- TMR1ON=1;//
- PORTC=0x00;//
- TRISC=0x00;//
- ei();//開放全局中斷 }
- /******************************************************************************/
- void check_remote_recieved(void)//檢測(cè)有無(wú)新的數(shù)據(jù)
- {
- if(!RB1)//
- {
- if(TIMER15S1>30)//是否長(zhǎng)按了3秒
- {
- learn=0;//
- clr_learn_reg();
- RC4=0;
- }
- else
- { learn=1;//
- TIMER15S2=0;
- TIMER15S4=0;
- RC4=1;
- }
- }
- else
- { TIMER15S1=0;
- if(TIMER15S2>60)
- {
- learn=0;//
- }
- }
- if(TIMER15S3>2)//數(shù)據(jù)保持時(shí)間
- {
- data=0; TIMER15S3=0;
- }
- if(TIMER15S4>3)//LED顯示時(shí)間
- {
- RC4=0;
- }
- if(recieved)
- {
- TIMER15S3=0;
- recieved=0;
- TIMER15S4=0;
- RC4=1;
- check_learn_pro(); check_out_pro();
- }
- INTE=1; }
- /******************************************************************************/
- /********************************* 控制部分程序 *******************************/
- /******************************************************************************/
- void control_init(void)
- {
- ; }
- /******************************************************************************//*********
- ************************聲音部分程序*********************************/
- /******************************************************************************/
- /******************************************************************************/
- unsigned char delay(unsigned int nus)
- {
- for(;nus>0;nus--)
- {
- if (recieved==1) return 0;
- asm("nop"); } }
- /******************************************************************************/
- unsigned char soud_one_fre(unsigned int cout,unsigned int delay_time)//發(fā)音程序
- {
- if(recieved==1) return 0;
- INTE=0;
- for (;cout>0;cout--)
- {
- RC6=!RC6;
- delay(delay_time);
- } RC6=0;
- INTE=1; }
- /******************************************************************************/
- /*********************************急促的聲音***********************************/
- /******************************************************************************/
- unsigned char soud_one_fre0(unsigned int time,unsigned int delay_time)
- {
- for (;time>0;time--)
- {
- if(recieved==1) return 0;
- soud_one_fre (1000,10);
- delay(delay_time);
- } }
- /******************************************************************************/
- /*********************************救護(hù)的聲音***********************************/
- /******************************************************************************/
- unsigned char soud_tow_fre0(unsigned int time)//
- {
- for (;time>0;time--)
- {
- if(recieved==1) return 0;
- soud_one_fre(900,10); soud_one_fre(800,30);
- } }
- /******************************************************************************//*********
- ***************************低-高音***********************************/
- /******************************************************************************/
- unsigned char soud_many_fre0(unsigned int time,unsigned int cout)
- {
- for (;time>0;time--)
- {
- unsigned int delay_time;
- for(delay_time=50;delay_time>10;delay_time--)
- {
- if(recieved==1) return 0; soud_one_fre(cout,delay_time);
- }
- } }
- /******************************************************************************/
- /************************************低-高音1**********************************/
- /******************************************************************************/
- unsigned char soud_many_fre2(unsigned int time,unsigned int cout)
- {
- for (;time>0;time--)
- {
- unsigned int delay_time;
- for(delay_time=30;delay_time>20;delay_time--)
- {
- if(recieved==1) return 0;
- soud_one_fre(cout,delay_time);
- }
- } }
- /******************************************************************************/
- /**********************************高—低音************************************/
- /******************************************************************************/
- unsigned char soud_many_fre1(unsigned int time,unsigned int cout)
- { for (;time>0;time--)
- {
- unsigned int delay_time;
- for (delay_time=10;delay_time<50;delay_time++)
- {
- if(recieved==1) return 0;
- soud_one_fre(cout,delay_time);
- }
- } }
- /******************************************************************************/
- /**********************************高—低音1***********************************/
- /******************************************************************************/
- unsigned char soud_many_fre3(unsigned int time,unsigned int cout)
- {
- for (;time>0;time--)
- {
- unsigned int delay_time;
- for(delay_time=20;delay_time<30;delay_time++)
- {
- if(recieved==1) return 0;
- soud_one_fre(cout,delay_time);
- }
- } }
- /******************************************************************************/
- /**********************************汽車報(bào)警聲**********************************/
- /******************************************************************************/
- unsigned char soud_low_high_low(unsigned int cout)
- {
- for (;cout>0;cout--)
- {
- if(recieved==1) return 0;
- soud_many_fre0(1,10);
- soud_many_fre2(1,10);
- } }
- /******************************************************************************/
- unsigned char remote_check(void)//遙控器按鍵檢測(cè)
- {
- if(data)//
- {
- if(remote_button_status) {
- return 0;
- }
- else
- { remote_button_status=1;
- return data;//遙控器數(shù)據(jù)有效
- }
- }
- else
- { remote_button_status=0;
- return 0;
- } }
- /******************************************************************************/
- void check_remote_command(void)
- {
- if(TIMER15S5>10)
- {
- first_click_status=0;//
- }
- switch (remote_check())
- {
- case 0x01:
- { RC0=!RC0; //
- soud_one_fre0(1,6000);
- soud_tow_fre0(5);
- delay(10000);
- soud_many_fre0(5,40);
- delay(10000);
- soud_many_fre2(10,100);
- delay(10000); soud_many_fre1(5,40);
- delay(10000);
- soud_many_fre3(10,150);
- delay(10000);
- soud_low_high_low(5);
- delay(10000);
- soud_one_fre0(2,10000); break;
- }
- case 0x02:RC1=!RC1;break;
- case 0x04:RC2=!RC2;break;
- case 0x08:
- {
- if(first_click_status==1)
- {
- RC3=!RC3;
- first_click_status=0;// break;
- }
- else
- {
- first_click_status=1;
- TIMER15S5=0; break;
- }
- }
- default:break;
- }
- }
- /******************************************************************************/
- void control_pro(void)
- {
- check_remote_command();
- }
復(fù)制代碼
以上代碼下載:
433和315無(wú)線模塊的軟件編解碼遙控程序.zip
(9.97 KB, 下載次數(shù): 1304)
2016-10-25 19:14 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|