Subversion Repositories HomeAutomation

Rev

Rev 275 | Rev 277 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*********************************************************************
  2.  *
  3.  *                  Main application
  4.  *
  5.  *********************************************************************
  6.  * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canTemp/Source/Main.c $
  7.  * Last changed:    $LastChangedDate: 2007-01-30 12:53:25 +0100 (Tue, 30 Jan 2007) $
  8.  * By:              $LastChangedBy: johboh $
  9.  * Revision:        $Revision: 276 $
  10.  ********************************************************************/
  11.  
  12.  
  13. #include <compiler.h>
  14. #include <stackTasks.h>
  15. #include <Tick.h>
  16. #include <funcdefs.h>
  17. #include <CAN.h>
  18.  
  19.  
  20. #ifdef USE_ADC
  21.     #include <adc.h>
  22. #endif
  23.  
  24. #ifdef USE_BLINDS
  25.     #include <blinds.h>
  26. #endif
  27.  
  28. static void mainInit(void);
  29.  
  30. void main()
  31. {
  32.  
  33.     static TICK t = 0;
  34.     CAN_MESSAGE outCm;
  35.  
  36.  
  37.     // Inits
  38.     mainInit();
  39.  
  40.     canInit();
  41.  
  42.     #ifdef USE_ADC
  43.         adcInit(TRUE,0b1011);
  44.     #endif
  45.  
  46.     #ifdef USE_BLINDS
  47.         blindsInit();
  48.     #endif
  49.  
  50.  
  51.     while(1)
  52.     {
  53.         static TICK t = 0;
  54.         static TICK temperature = 0;
  55.         static BYTE lastTemperature = TEMPERATURE_INSIDE;
  56.  
  57.         static TICK blinds = 0;
  58.        
  59.         if ((tickGet()-t)>TICK_SECOND)
  60.         {
  61.             LED0_IO=~LED0_IO;
  62.             t = tickGet();
  63.         }
  64.  
  65.         if ((tickGet()-blinds)>2*TICK_SECOND)
  66.         {
  67.             //blindsTurn(MIDDLE,3000);
  68.             blinds = tickGet();
  69.         }
  70.    
  71.  
  72.  
  73.         #ifdef USE_ADC
  74.         if ((tickGet()-temperature)>TICK_SECOND*2) // Each 2 second, read temperature.
  75.         {
  76.             if (lastTemperature==TEMPERATURE_INSIDE) lastTemperature=TEMPERATURE_OUTSIDE; else lastTemperature=TEMPERATURE_INSIDE;
  77.             adcConvert(lastTemperature);
  78.             temperature = tickGet();
  79.         }
  80.         #endif
  81.  
  82.  
  83.         #ifdef USE_ADC
  84.         if (adcDone()) // Has temperature, send it to the bus
  85.         {
  86.             BYTE decimal;
  87.             signed char tenth;
  88.             temperatureRead(&tenth,&decimal);
  89.  
  90.             outCm.funct                     = FUNCT_SENSORS;
  91.             outCm.funcc                     = (lastTemperature==TEMPERATURE_INSIDE?FUNCC_SENSORS_TEMPERATURE_INSIDE:FUNCC_SENSORS_TEMPERATURE_OUTSIDE);
  92.             outCm.data_length               = 2;
  93.             outCm.data[1]                   = tenth; //  signed 10 an 1 decimal.
  94.             outCm.data[0]                   = decimal; //  Decimal value, 0-9
  95.             while(!canSendMessage(outCm,PRIO_HIGH));
  96.         }
  97.         #endif
  98.  
  99.     }
  100. }
  101.  
  102.  
  103. #pragma interrupt high_isr
  104. void high_isr(void)
  105. {
  106.     canISR();
  107.  
  108.     #ifdef USE_BLINDS
  109.         blindsISR();
  110.     #endif
  111. }
  112.  
  113.  
  114. #pragma interrupt low_isr
  115. void low_isr(void)
  116. {
  117.     #ifdef USE_ADC
  118.         adcISR();
  119.     #endif
  120. }
  121.  
  122.  
  123.  
  124. /*
  125. *   Function: mainInit
  126. *
  127. *   Input:  none
  128. *   Output: none
  129. *   Pre-conditions: none
  130. *   Affects: Se code
  131. *   Depends: none.
  132. */
  133. void mainInit()
  134. {
  135.     LED0_TRIS=0;
  136.     BLINDS0_TRIS=0;
  137.  
  138.     // Enable Interrupts
  139.     INTCONbits.GIEH = 1;
  140.     INTCONbits.GIEL = 1;
  141.  
  142.     // Enable interrupt prirority
  143.     RCONbits.IPEN=1;
  144.    
  145.  
  146. }
  147.  
  148. /*
  149. *   Function: canParse
  150. *
  151. *   Input:  Received can message
  152. *   Output: none
  153. *   Pre-conditions: canInit and received packet.
  154. *   Affects: Sensors/actuators/etc. See code.
  155. *   Depends: none.
  156. */
  157.  
  158. void canParse(CAN_MESSAGE cm)
  159. {
  160.     CAN_MESSAGE outCm;
  161.  
  162. }
  163.  
  164.  
  165. // Below are mandantory when using bootloader
  166. // define DEBUG_MODE to use without bootloader.
  167.  
  168. #ifndef DEBUG_MODE
  169. extern void _startup (void);
  170. #pragma code _RESET_INTERRUPT_VECTOR = 0x001000
  171. void _reset (void)
  172. {
  173.     _asm goto _startup _endasm
  174. }
  175. #pragma code
  176. #endif
  177.  
  178. #pragma code _HIGH_INTERRUPT_VECTOR = 0x001008
  179. void _high_ISR (void)
  180. {
  181.     _asm GOTO high_isr _endasm
  182. }
  183. #pragma code
  184.  
  185. #pragma code _LOW_INTERRUPT_VECTOR = 0x001018
  186. void _low_ISR (void)
  187. {
  188.     _asm GOTO low_isr _endasm
  189. }
  190. #pragma code
  191.