Subversion Repositories HomeAutomation

Rev

Rev 121 | Blame | Last modification | View Log | SVN | RSS feed

  1. /*********************************************************************
  2.  *
  3.  *                  IR reciver module
  4.  *
  5.  *********************************************************************
  6.  * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canIR/Source/IRec.c $
  7.  * Last changed:    $LastChangedDate: 2006-11-18 21:08:42 +0100 (Sat, 18 Nov 2006) $
  8.  * By:              $LastChangedBy: johboh $
  9.  * Revision:        $Revision: 122 $
  10.  ********************************************************************/
  11.  
  12. #include <IRec.h>
  13.  
  14. #include <CAN.h>
  15. #include <funcdefs.h>
  16.  
  17. #ifdef USE_IREC
  18.  
  19. static BYTE toggle = 0;
  20. static BYTE addr = 0;
  21. static BYTE data = 0;
  22. static BYTE bitIndex = 0;
  23. static IR_TYPE type = IR_NONE;
  24.  
  25. /*
  26. *   Function: irecInit
  27. *
  28. *   Input:  none
  29. *   Output: none
  30. *   Pre-conditions: none
  31. *   Affects: none
  32. *   Depends: TIMER 3 is free
  33. */
  34. void irecInit()
  35. {
  36.  
  37.     // Setup timer 3 to be source for CCP1
  38.     T3CON = 0b11111001;
  39.    
  40.     // configure timer 3 interrupt (for overflow)
  41.     PIR2bits.TMR3IF = 0;
  42.     PIE2bits.TMR3IE = 1;
  43.     IPR2bits.TMR3IP = 1;
  44.  
  45.     // Setup CCP1 to use capture on falling edge
  46.     CCP1CON=0x04;
  47.  
  48.     // Enable CCP1 interrupt and set priority to high.
  49.     PIR1bits.CCP1IF=0;
  50.     PIE1bits.CCP1IE=1;
  51.     IPR1bits.CCP1IP=1;
  52.  
  53. }
  54.  
  55. /*
  56. *   Function: irecISR
  57. *
  58. *   Input:  none
  59. *   Output: none
  60. *   Pre-conditions: a call to irecInit()
  61. *   Affects: ...
  62. *   Depends: ...
  63. */
  64. void irecISR()
  65. {
  66.     static WORD timediff = 0;
  67.     static BOOL started = FALSE;
  68.     static BYTE lastbit = 1;
  69.     static BOOL second = FALSE;
  70.  
  71.     // If IR bit
  72.     if(PIR1bits.CCP1IF && PIE1bits.CCP1IE)
  73.     {
  74.         // Read timediff
  75.         timediff  = TMR3L;
  76.         timediff += (((WORD)TMR3H)<<8);
  77.  
  78.         // Clear timer.
  79.         TMR3H = 0;
  80.         TMR3L = 0;
  81.  
  82.         // Wait on inverse edge
  83.         CCP1CONbits.CCP1M0=~CCP1CONbits.CCP1M0;
  84.  
  85.         // Disable interrupt
  86.         PIR1bits.CCP1IF=0;
  87.  
  88.         if (started==TRUE && (type==IR_RC5 || type==IR_RC5X))
  89.         {
  90.  
  91.             if ((t2-250)<timediff && timediff<(t2+250) && second==FALSE)
  92.             {
  93.                 if (bitIndex==0) toggle=lastbit;
  94.                 if (bitIndex>=1 && bitIndex<=5) addr+=(lastbit<<(5-bitIndex));
  95.                 if (bitIndex>=6 && bitIndex<=11) data+=(lastbit<<(11-bitIndex));
  96.                 bitIndex++;
  97.    
  98.                 second = TRUE;
  99.                 return;
  100.             }
  101.    
  102.             if ((t4-250)<timediff && timediff<(t4+250) && second==FALSE)
  103.             {
  104.                 lastbit=(lastbit?0:1);
  105.    
  106.                 if (bitIndex==0) toggle=lastbit;
  107.                 if (bitIndex>=1 && bitIndex<=5) addr+=(lastbit<<(5-bitIndex));
  108.                 if (bitIndex>=6 && bitIndex<=11) data+=(lastbit<<(11-bitIndex));
  109.                 bitIndex++;
  110.    
  111.                 second = TRUE;
  112.                 return;
  113.             }
  114.    
  115.             if ((t2-250)<timediff && timediff<(t2+250) && second==TRUE)
  116.             {
  117.                 second = FALSE;
  118.                 return;
  119.             }
  120.    
  121.             if ((t4-250)<timediff && timediff<(t4+250) && second==TRUE)
  122.             {
  123.                 lastbit=(lastbit?0:1);
  124.    
  125.                 if (bitIndex==0) toggle=lastbit;
  126.                 if (bitIndex>=1 && bitIndex<=5) addr+=(lastbit<<(5-bitIndex));
  127.                 if (bitIndex>=6 && bitIndex<=11) data+=(lastbit<<(11-bitIndex));
  128.                 bitIndex++;
  129.    
  130.                 return;
  131.             }
  132.  
  133.         } // if started and type == RC5 or RC5X
  134.  
  135.  
  136.         if (started==TRUE && type==IR_RC6)
  137.         {
  138.  
  139.             if ((t1-250)<timediff && timediff<(t1+250) && second==FALSE)
  140.             {
  141.                 if (bitIndex==0) toggle=lastbit;
  142.                 if (bitIndex>=1 && bitIndex<=8) addr+=(lastbit<<(8-bitIndex));
  143.                 if (bitIndex>=9 && bitIndex<=16) data+=(lastbit<<(16-bitIndex));
  144.                 bitIndex++;
  145.    
  146.                 second = TRUE;
  147.                 return;
  148.             }
  149.    
  150.             if ((t2-250)<timediff && timediff<(t2+250) && second==FALSE)
  151.             {
  152.                 lastbit=(lastbit?0:1);
  153.    
  154.                 if (bitIndex==0) toggle=lastbit;
  155.                 if (bitIndex>=1 && bitIndex<=8) addr+=(lastbit<<(8-bitIndex));
  156.                 if (bitIndex>=9 && bitIndex<=16) data+=(lastbit<<(16-bitIndex));
  157.                 bitIndex++;
  158.    
  159.                 second = TRUE;
  160.                 return;
  161.             }
  162.    
  163.             if ((t1-250)<timediff && timediff<(t1+250) && second==TRUE)
  164.             {
  165.                 second = FALSE;
  166.                 return;
  167.             }
  168.    
  169.             if ((t2-250)<timediff && timediff<(t2+250) && second==TRUE)
  170.             {
  171.                 lastbit=(lastbit?0:1);
  172.    
  173.                 if (bitIndex==0) toggle=lastbit;
  174.                 if (bitIndex>=1 && bitIndex<=8) addr+=(lastbit<<(8-bitIndex));
  175.                 if (bitIndex>=9 && bitIndex<=16) data+=(lastbit<<(16-bitIndex));
  176.                 bitIndex++;
  177.    
  178.                 return;
  179.             }
  180.  
  181.         } // if started and type == RC6
  182.  
  183.  
  184.  
  185.         bitIndex++;
  186.  
  187.  
  188.         if (started==FALSE && type==IR_NONE && bitIndex==2 && (t2-250)<timediff && timediff<(t2+250))
  189.         {
  190.             type=IR_RC5;
  191.         }
  192.         else if (started==FALSE && type==IR_NONE && bitIndex==2 && (t4-250)<timediff && timediff<(t4+250))
  193.         {
  194.             started = TRUE;
  195.             bitIndex = 0;
  196.             lastbit = 1;
  197.             type=IR_RC5X;
  198.         }
  199.         else if (started==FALSE && type==IR_NONE && bitIndex==2 && (t6-250)<timediff && timediff<(t6+250))
  200.         {
  201.             type=IR_RC6;
  202.         }
  203.         else if (started==FALSE && type==IR_RC6 && bitIndex==9)
  204.         {  
  205.             started = TRUE;
  206.             bitIndex = 0;
  207.             lastbit = 0;
  208.             type=IR_RC6;
  209.             second = TRUE;
  210.         }
  211.         else if (started==FALSE && type==IR_RC5 && bitIndex==3 && (t2-250)<timediff && timediff<(t2+250))
  212.         {
  213.             started = TRUE;
  214.             bitIndex = 0;
  215.             lastbit = 1;
  216.             type=IR_RC5;
  217.         }
  218.         else if(bitIndex>2 && type!=IR_RC6)
  219.         {
  220.             type=IR_NONE;
  221.         }
  222.         else if(bitIndex>9)
  223.         {
  224.             type=IR_NONE;
  225.         }
  226.        
  227.     }
  228.  
  229.  
  230.  
  231.  
  232.     // If timer 3 overflow
  233.     if(PIR2bits.TMR3IF && PIE2bits.TMR3IE)
  234.     {  
  235.  
  236.         PIR2bits.TMR3IF=0;
  237.  
  238.         if (type!=IR_NONE)
  239.         {
  240.             irecParse(type,toggle,addr,data);
  241.         }
  242.  
  243.         started  = FALSE;
  244.         bitIndex = 0;
  245.         addr = 0;
  246.         toggle = 0;
  247.         data = 0;
  248.         type = IR_NONE;
  249.  
  250.         // wait in falling edge.
  251.         CCP1CONbits.CCP1M0=0;
  252.        
  253.     }
  254.  
  255. }
  256.  
  257.  
  258.  
  259. #endif
  260.