Subversion Repositories HomeAutomation

Rev

Rev 48 | Go to most recent revision | 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 "../Include/compiler.h"
  14. #include "../Include/stackTasks.h"
  15. #include "../Include/Tick.h"
  16.  
  17. #ifdef USE_CAN
  18.     #include "../Include/CAN.h"
  19. #endif
  20.  
  21. static void mainInit(void);
  22.  
  23. void main()
  24. {
  25.     static TICK t = 0;
  26.  
  27.     // Inits
  28.  
  29.     mainInit();
  30.  
  31.     #ifdef USE_CAN
  32.         canInit();
  33.     #endif
  34.  
  35.     tickInit();
  36.  
  37.     while(1)
  38.     {
  39.         #ifdef USE_CAN
  40.         if ((tickGet()-t)>=5*TICK_SECOND)
  41.         {
  42.             CAN_MESSAGE cm;
  43.  
  44.             t = tickGet();
  45.        
  46.             // Send heartbeat
  47.             cm.ident=0x00000666;
  48.             cm.extended=FALSE;
  49.             cm.data_length=1;
  50.             cm.data[0]='H';
  51.  
  52.             while(!canSendMessage(cm));
  53.         }
  54.         #endif
  55.     }
  56. }
  57.  
  58.  
  59. #pragma interruptlow HighISR
  60. void HighISR(void)
  61. {
  62.  
  63.  
  64.     #ifdef USE_CAN
  65.         canISR();
  66.     #endif
  67.  
  68.  
  69.     if (INTCONbits.INT0IE && INTCONbits.INT0IF)
  70.     {
  71.         static TICK t = 0;
  72.         CAN_MESSAGE cm;
  73.  
  74.         if ((tickGet()-t)>TICK_SECOND/2)
  75.         {
  76.  
  77.             LED0_IO=~LED0_IO;
  78.  
  79.             // Send heartbeat
  80.             cm.ident=0x1F910091;
  81.             cm.extended=TRUE;
  82.             cm.data_length=1;
  83.             cm.data[0]=0x01;
  84.             cm.data[1]=0x07;
  85.             cm.data[2]=0x09;
  86.             cm.data[3]=0x08;
  87.             cm.data[4]=0x03;
  88.  
  89.             while(!canSendMessage(cm));
  90.            
  91.             t=tickGet();
  92.         }
  93.  
  94.         INTCONbits.INT0IF=0;
  95.     }
  96.  
  97.  
  98.     tickUpdate();
  99. }
  100. #pragma code highVector=0x08
  101. void HighVector (void)
  102. {
  103.     _asm goto HighISR _endasm
  104. }
  105. #pragma code // Return to default code section
  106.  
  107.  
  108.  
  109. #pragma interruptlow LowISR
  110. void LowISR(void)
  111. {
  112.  
  113. }
  114. #pragma code lowVector=0x18
  115. void LowVector (void)
  116. {
  117.     _asm goto LowISR _endasm
  118. }
  119. #pragma code // Return to default code section
  120.  
  121.  
  122. /*
  123. *   Function: mainInit
  124. *
  125. *   Input:  none
  126. *   Output: none
  127. *   Pre-conditions: none
  128. *   Affects: Se code
  129. *   Depends: none.
  130. */
  131. void mainInit()
  132. {
  133.     LED0_TRIS=0;   
  134.  
  135.     // Enable internal PORTB pull-ups
  136.     INTCON2bits.RBPU = 0;
  137.    
  138.     // Enable Interrupts
  139.     INTCONbits.GIEH = 1;
  140.     INTCONbits.GIEL = 1;
  141.  
  142.  
  143.     // RB0 interrupt
  144.     INTCON2bits.INTEDG0 = 1; //rising edge
  145.     INTCONbits.INT0IE = 1; 
  146.  
  147.     // Enable interrupt prirority
  148.     RCONbits.IPEN=1;
  149.    
  150.  
  151. }
  152.  
  153. /*
  154. *   Function: canParse
  155. *
  156. *   Input:  Received can message
  157. *   Output: none
  158. *   Pre-conditions: canInit and received packet.
  159. *   Affects: Sensors/actuators/etc. See code.
  160. *   Depends: none.
  161. */
  162. #ifdef USE_CAN
  163. void canParse(CAN_MESSAGE cm)
  164. {
  165.         if (cm.ident==0x00000304)
  166.         {
  167.             if (cm.data[0]==0x20) LED0_IO=1;
  168.             if (cm.data[0]==0x40) LED0_IO=0;
  169.             if (cm.data[0]==0x80)
  170.             {
  171.                 cm.ident=0x00000303;
  172.                 cm.extended=FALSE;
  173.                 cm.data_length=1;
  174.                 cm.data[0]=LED0_IO;
  175.                 while(!canSendMessage(cm));
  176.  
  177.             }
  178.         }
  179.  
  180.  
  181. }
  182. #endif
  183.  
  184.