我在写红外遥控的C语言解码程序,参考了一份代码,代码很短,不到300行,但是呢,对于这个数组:uchar IRCOM[7];
的值是如何得来的,我真搞不懂,没有任何地方有给它赋值过,导致我对这句话很不理解:IRCOM[j]=IRCOM[j] >> 1; //高位补0
按照程序的思路,解码后的值就存储在 IRCOM[2] 里面,然后跟键码表RecvData[] 进行对比:
for(j=0;j<10;j++)
{
if(IRCOM[2]==RecvData[j])
{
ControlCar(j);
}
}
从而得知按下哪个按键。请教大神 IRCOM[2] 的值是怎么得来的?
- #include
- #include
- #define uint unsigned int
- #define uchar unsigned char
- uchar code LedShowData[]={0x03,0x9F,0x25,0x0D,0x99,
- 0x49,0x41,0x1F,0x01,0x19};//0,1,2,3,4,5,6,7,8,9
- uchar code RecvData[]={0x19,0x46,0x15,0x44,0x43,0x40,0x0D,0x0E,0x00,0x0F};
- uchar IRCOM[7];
- static unsigned int LedFlash; //¶¨ÒåÉÁ¶¯ÆµÂʼÆÊý±äÁ¿
- unsigned char RunFlag=0; //¶¨ÒåÔËÐбê־λ
- bit EnableLight=0; //¶¨ÒåָʾµÆʹÄÜλ
- /************************/
- ***it S1State=P1^0;
- ***it S2State=P1^1;
- ***it B1State=P1^2;
- ***it IRState=P1^3;
- ***it RunStopState=P1^4;
- ***it FontIRState=P1^5;
- ***it LeftIRState=P1^6;
- ***it RightIRState=P1^7;
- /************************/
- ***it S1=P3^2;
- ***it S2=P3^4;
- /*************************/
- ***it LeftLed=P2^0;
- ***it RightLed=P0^7; /
- /**********************/
- ***it LeftIR=P3^5;
- ***it RightIR=P3^6;
- ***it FontIR=P3^7;
- /************************/
- ***it M1A=P0^0;
- ***it M1B=P0^1;
- ***it M2A=P0^2;
- ***it M2B=P0^3;
- /**************************/
- ***it B1=P0^4;
- ***it RL1=P0^5;
- ***it SB1=P0^6;
- /**********/
- ***it IRIN=P3^3;
- /**********************/
- #define ShowPort P2
- extern void ControlCar(uchar CarType);
- void delayms(unsigned char x) //0.14mSÑÓʱ³ÌÐò
- {
- unsigned char i; //¶¨ÒåÁÙʱ±äÁ¿
- while(x--) //ÑÓʱʱ¼äÑ»·
- {
- for (i = 0; i<13; i++) {} //14mSÑÓʱ
- }
- }
- void Delay1ms(unsigned int i)
- {
- unsigned char j,k;
- do{
- j = 10;
- do{
- k = 50;
- do{
- _nop_();
- }while(--k);
- }while(--j);
- }while(--i);
- }
- void Delay() //¶¨ÒåÑÓʱ×Ó³ÌÐò
- { uint DelayTime=30000; //¶¨ÒåÑÓʱʱ¼ä±äÁ¿
- while(DelayTime--); //¿ªÊ¼½øÐÐÑÓʱѻ·
- return; //×Ó³ÌÐò·µ»Ø
- }
- void tingzhi()
- {
- M1A=0; //½«M1µç»úA¶Ë³õʼ»¯Îª0
- M1B=0; //½«M1µç»úB¶Ë³õʼ»¯Îª0
- M2A=0; //½«M2µç»úA¶Ë³õʼ»¯Îª0
- M2B=0;
- }
- void qianjin()
- {
- M1A=1;
- M1B=0;
- M2A=1;
- M2B=0;
- }
- void houtui()
- {
- M1A=0;
- M1B=1;
- M2A=0;
- M2B=1;
- }
- void youzhuan()
- {
- M1A=0;
- M1B=1;
- M2A=1;
- M2B=0;
- }
- void zuozhuan()
- {
- M1A=1;
- M1B=0;
- M2A=0;
- M2B=1;
- }
- void ControlCar(unsigned char ConType) //¶¨Òåµç»ú¿ØÖÆ×Ó³ÌÐò
- {
-
- tingzhi();
- switch(ConType) //ÅжÏÓû§É趨µç»úÐÎʽ
- {
- case 1: //Ç°½ø //ÅжÏÓû§ÊÇ·ñÑ¡ÔñÐÎʽ1
- {
-
- tingzhi(); /
- Delay1ms(240);
- LeftLed = 0 ;
- qianjin();
- break;
- }
- case 2: //ºóÍË //ÅжÏÓû§ÊÇ·ñÑ¡ÔñÐÎʽ2
- {
- tingzhi();
- Delay1ms(240);
- LeftLed = 1 ;
- houtui(); //M2µç»ú·´×ª
- break;
- }
- case 3: //×óת //ÅжÏÓû§ÊÇ·ñÑ¡ÔñÐÎʽ3
- {
- tingzhi();
- Delay1ms(240);
- zuozhuan(); //M2µç»úÕýת
- break;
- }
- case 4: //ÓÒת //ÅжÏÓû§ÊÇ·ñÑ¡ÔñÐÎʽ4
- {
- tingzhi();
- Delay1ms(240);
- youzhuan(); //M1µç»úÕýת
- //M2µç»ú·´×ª
- break;
- }
- case 5: //Í£Ö¹ //ÅжÏÓû§ÊÇ·ñÑ¡ÔñÐÎʽ8
- {
- tingzhi();
- break; //Í˳öµ±Ç°Ñ¡Ôñ
- }
- }
- }
- void IR_IN() interrupt 2 using 0
- {
- unsigned char j,k,N=0; //¶¨ÒåÁÙʱ½ÓÊÕ±äÁ¿
-
- EX1 = 0; /
- delayms(15);
- if (IRIN==1) //ÅжϺìÍâÐźÅÊÇ·ñÏûʧ
- {
- EX1 =1; //ÍⲿÖжϿª
- return; //·µ»Ø
- }
-
- while (!IRIN)
- {
- delayms(1); //ÑÓʱµÈ´ý
- }
- for (j=0;j<4;j++) //²É¼¯ºìÍâÒ£¿ØÆ÷Êý¾Ý
- {
- for (k=0;k<8;k++) //·Ö´Î²É¼¯8λÊý¾Ý
- {
- while (IRIN) /
- {
- delayms(1); //ÑÓʱµÈ´ý
- }
-
- while (!IRIN) //µÈ IR ±äΪ¸ßµçƽ
- {
- delayms(1); //ÑÓʱµÈ´ý
- }
-
- while (IRIN) //¼ÆËãIR¸ßµçƽʱ³¤
- {
- delayms(1); //ÑÓʱµÈ´ý
- N++; //¼ÆÊýÆ÷¼Ó¼Ó
- if (N>=30) //ÅжϼÆÊýÆ÷ÀÛ¼ÓÖµ
- {
- EX1=1; //´ò¿ªÍⲿÖжϹ¦ÄÜ
- return; //·µ»Ø
- }
- }
-
- IRCOM[j]=IRCOM[j] >> 1; // 高位补0
-
- if (N>=8)
- {
- IRCOM[j] = IRCOM[j] | 0x80;
- }
- N=0;
- }
- }
-
- if (IRCOM[2]!=~IRCOM[3])
- {
- EX1=1; //´ò¿ªÍⲿÖжÏ
- return; //·µ»Ø
- }
- for(j=0;j<10;j++)
- {
- if(IRCOM[2]==RecvData[j]) //与键码表进行比对
- {
- ControlCar(j);
- }
- }
-
-
- EX1 = 1; //ÍⲿÖжϿª
- }
- void main(void)
- {
- bit ExeFlag=0;
-
- LedFlash=3000;
- EX1=1;
- IT1=1;
- EA=1;
- ShowPort=LedShowData[0];
- while(1) //³ÌÐòÖ÷Ñ»·
- {
-
- Delay();
- RunStopState=~RunStopState;
-
- }
- }
复制代码
0
|
8个回答
|
|
|