Subversion Repositories HomeAutomation

Rev

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