Subversion Repositories HomeAutomation

Rev

Rev 276 | Rev 282 | 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-31 14:04:02 +0100 (Wed, 31 Jan 2007) $
  8.  * By:              $LastChangedBy: johboh $
  9.  * Revision:        $Revision: 277 $
  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.    
  52.  
  53.     while(1)
  54.     {
  55.         static TICK t = 0;
  56.         static TICK temperature = 0;
  57.         static BYTE lastTemperature = TEMPERATURE_INSIDE;
  58.         static signed int angle = 90;
  59.         static TICK blinds = 0;
  60.        
  61.         if ((tickGet()-t)>TICK_SECOND)
  62.         {
  63.             LED0_IO=~LED0_IO;
  64.             t = tickGet();
  65.         }
  66.  
  67.         #ifdef USE_BLINDS
  68.         if ((tickGet()-blinds)>2*TICK_SECOND)
  69.         {
  70.             angle = (angle>0?-45:45);
  71.             blindsTurn(angle);
  72.             blinds = tickGet();
  73.         }
  74.         #endif
  75.  
  76.  
  77.         #ifdef USE_ADC
  78.         if ((tickGet()-temperature)>TICK_SECOND*2) // Each 2 second, read temperature.
  79.         {
  80.             if (lastTemperature==TEMPERATURE_INSIDE) lastTemperature=TEMPERATURE_OUTSIDE; else lastTemperature=TEMPERATURE_INSIDE;
  81.             adcConvert(lastTemperature);
  82.             temperature = tickGet();
  83.         }
  84.         #endif
  85.  
  86.  
  87.         #ifdef USE_ADC
  88.         if (adcDone()) // Has temperature, send it to the bus
  89.         {
  90.             BYTE decimal;
  91.             signed char tenth;
  92.             temperatureRead(&tenth,&decimal);
  93.  
  94.             outCm.funct                     = FUNCT_SENSORS;
  95.             outCm.funcc                     = (lastTemperature==TEMPERATURE_INSIDE?FUNCC_SENSORS_TEMPERATURE_INSIDE:FUNCC_SENSORS_TEMPERATURE_OUTSIDE);
  96.             outCm.data_length               = 2;
  97.             outCm.data[1]                   = tenth; //  signed 10 an 1 decimal.
  98.             outCm.data[0]                   = decimal; //  Decimal value, 0-9
  99.             while(!canSendMessage(outCm,PRIO_HIGH));
  100.         }
  101.         #endif
  102.  
  103.     }
  104. }
  105.  
  106.  
  107. #pragma interrupt high_isr
  108. void high_isr(void)
  109. {
  110.     canISR();
  111.  
  112.     #ifdef USE_BLINDS
  113.         blindsISR();
  114.     #endif
  115. }
  116.  
  117.  
  118. #pragma interrupt low_isr
  119. void low_isr(void)
  120. {
  121.     #ifdef USE_ADC
  122.         adcISR();
  123.     #endif
  124. }
  125.  
  126.  
  127.  
  128. /*
  129. *   Function: mainInit
  130. *
  131. *   Input:  none
  132. *   Output: none
  133. *   Pre-conditions: none
  134. *   Affects: Se code
  135. *   Depends: none.
  136. */
  137. void mainInit()
  138. {
  139.     LED0_TRIS=0;
  140.     BLINDS0_TRIS=0;
  141.  
  142.     // Enable Interrupts
  143.     INTCONbits.GIEH = 1;
  144.     INTCONbits.GIEL = 1;
  145.  
  146.     // Enable interrupt prirority
  147.     RCONbits.IPEN=1;
  148.    
  149.  
  150. }
  151.  
  152. /*
  153. *   Function: canParse
  154. *
  155. *   Input:  Received can message
  156. *   Output: none
  157. *   Pre-conditions: canInit and received packet.
  158. *   Affects: Sensors/actuators/etc. See code.
  159. *   Depends: none.
  160. */
  161.  
  162. void canParse(CAN_MESSAGE cm)
  163. {
  164.     CAN_MESSAGE outCm;
  165.  
  166. }
  167.  
  168.  
  169. // Below are mandantory when using bootloader
  170. // define DEBUG_MODE to use without bootloader.
  171.  
  172. #ifndef DEBUG_MODE
  173. extern void _startup (void);
  174. #pragma code _RESET_INTERRUPT_VECTOR = 0x001000
  175. void _reset (void)
  176. {
  177.     _asm goto _startup _endasm
  178. }
  179. #pragma code
  180. #endif
  181.  
  182. #pragma code _HIGH_INTERRUPT_VECTOR = 0x001008
  183. void _high_ISR (void)
  184. {
  185.     _asm GOTO high_isr _endasm
  186. }
  187. #pragma code
  188.  
  189. #pragma code _LOW_INTERRUPT_VECTOR = 0x001018
  190. void _low_ISR (void)
  191. {
  192.     _asm GOTO low_isr _endasm
  193. }
  194. #pragma code
  195.