Subversion Repositories HomeAutomation

Rev

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

  1. /*********************************************************************
  2.  *
  3.  *                  Main application
  4.  *
  5.  *********************************************************************
  6.  * FileName:        Main.c
  7.  *
  8.  * Author               Date    Comment
  9.  *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10.  * Johan Böhlin     21-10-06    Original        (Rev 1.0)
  11.  ********************************************************************/
  12.  
  13. #include <compiler.h>
  14. #include <stackTasks.h>
  15. #include <Tick.h>
  16.  
  17. #ifdef USE_CAN
  18.     #include <CAN.h>
  19. #endif
  20.  
  21. #ifdef USE_UART
  22.     #include <uart.h>
  23. #endif
  24.  
  25.  
  26.  
  27. static void mainInit(void);
  28.  
  29.  
  30. void main()
  31. {
  32.     static TICK t = 0;
  33.  
  34.     // Inits
  35.  
  36.     mainInit();
  37.  
  38.     #ifdef USE_CAN
  39.         canInit();
  40.     #endif
  41.  
  42.     #ifdef USE_UART
  43.         uartInit();
  44.     #endif
  45.  
  46.     tickInit();
  47.  
  48.     while(1)
  49.     {
  50.     }
  51.  
  52. }
  53.  
  54.  
  55. #pragma interruptlow HighISR
  56. void HighISR(void)
  57. {
  58.  
  59.  
  60.     #ifdef USE_UART
  61.         uartISR();
  62.     #endif
  63.  
  64.     #ifdef USE_CAN
  65.         canISR();
  66.     #endif
  67.  
  68.  
  69.     tickUpdate();
  70. }
  71. #pragma code highVector=0x08
  72. void HighVector (void)
  73. {
  74.     _asm goto HighISR _endasm
  75. }
  76. #pragma code // Return to default code section
  77.  
  78.  
  79.  
  80. #pragma interruptlow LowISR
  81. void LowISR(void)
  82. {
  83.  
  84. }
  85. #pragma code lowVector=0x18
  86. void LowVector (void)
  87. {
  88.     _asm goto LowISR _endasm
  89. }
  90. #pragma code // Return to default code section
  91.  
  92.  
  93. /*
  94. *   Function: mainInit
  95. *
  96. *   Input:  none
  97. *   Output: none
  98. *   Pre-conditions: none
  99. *   Affects: Se code
  100. *   Depends: none.
  101. */
  102. void mainInit()
  103. {
  104.     LED0_TRIS=0;   
  105.  
  106.     // Enable internal PORTB pull-ups
  107.     INTCON2bits.RBPU = 0;
  108.    
  109.     // Enable Interrupts
  110.     INTCONbits.GIEH = 1;
  111.     INTCONbits.GIEL = 1;
  112.  
  113.  
  114.     // Enable interrupt prirority
  115.     RCONbits.IPEN=1;
  116.    
  117.  
  118. }
  119.  
  120. /*
  121. *   Function: canParse
  122. *
  123. *   Input:  Received can message
  124. *   Output: none
  125. *   Pre-conditions: canInit and received packet.
  126. *   Affects: Sensors/actuators/etc. See code.
  127. *   Depends: none.
  128. */
  129. #ifdef USE_CAN
  130. void canParse(CAN_MESSAGE cm)
  131. {
  132.     int i=0;
  133.    
  134.     uartPutc(UART_START_BYTE);
  135.     uartPutc((BYTE)(cm.ident));
  136.     uartPutc((BYTE)(cm.ident>>8));
  137.     uartPutc((BYTE)(cm.ident>>16));
  138.     uartPutc((BYTE)(cm.ident>>24));
  139.     uartPutc(cm.extended);
  140.     uartPutc(cm.data_length);
  141.     for(i=0;i<8;i++) uartPutc(cm.data[i]);
  142.     uartPutc(UART_END_BYTE);
  143.  
  144. }
  145. #endif
  146.  
  147.  
  148. /*
  149. *   Function: uartParse
  150. *
  151. *   Input:  Received uart message
  152. *   Output: none
  153. *   Pre-conditions: uartInit and received byte.
  154. *   Affects: Sensors/actuators/etc. See code.
  155. *   Depends: none.
  156. */
  157. #ifdef USE_UART
  158. void uartParse(BYTE c)
  159. {
  160.     BYTE i;
  161.     unsigned int wait = 0;
  162.     CAN_MESSAGE cm;
  163.     static BOOL waitingMessage = FALSE;
  164.     static TICK timeout=0;
  165.     static BYTE count=0;
  166.  
  167.  
  168.     if (waitingMessage==TRUE && (tickGet()-timeout)>TICK_SECOND/20)
  169.     {
  170.         waitingMessage=FALSE;
  171.     }
  172.  
  173.  
  174.     if (waitingMessage==TRUE)
  175.     {
  176.  
  177.         timeout=tickGet();
  178.  
  179.         // UART END
  180.         if(count>=14)
  181.         {
  182.             if (c==UART_END_BYTE)
  183.             {
  184.                 while(!canSendMessage(cm));
  185.             }
  186.             waitingMessage=FALSE;
  187.             return;
  188.         }
  189.  
  190.         // data
  191.         if(count>=6)
  192.         {
  193.             cm.data[count-6]=c;
  194.             count++;
  195.             return;
  196.         }
  197.  
  198.         // data length
  199.         if(count>=5)
  200.         {
  201.             cm.data_length=c;
  202.             count++;
  203.             return;
  204.         }
  205.  
  206.         // extended
  207.         if(count>=4)
  208.         {
  209.             cm.extended=c;
  210.             count++;
  211.             return;
  212.         }
  213.  
  214.         // ident
  215.         if(count>=0)
  216.         {
  217.             cm.ident+=((DWORD)c<<(count*8));
  218.             count++;
  219.             return;
  220.         }
  221.    
  222.     }
  223.  
  224.  
  225.     if (c==UART_START_BYTE && waitingMessage==FALSE)
  226.     {
  227.             waitingMessage=TRUE;
  228.             timeout=tickGet();
  229.             count=0;
  230.             cm.ident=0;
  231.             return;
  232.     }
  233.  
  234.  
  235. }
  236. #endif
  237.