Subversion Repositories HomeAutomation

Rev

Rev 282 | Rev 339 | 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-02-08 18:24:59 +0100 (Thu, 08 Feb 2007) $
  8.  * By:              $LastChangedBy: johboh $
  9.  * Revision:        $Revision: 297 $
  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.     BLINDS0P_IO=1;
  51.  
  52.     while(1)
  53.     {
  54.         static TICK t = 0;
  55.         static TICK temperature = 0;
  56.         static BYTE lastTemperature = TEMPERATURE_INSIDE;
  57.         static signed char angle = 70;
  58.         static TICK blinds = 0;
  59.        
  60.         if ((tickGet()-t)>TICK_SECOND)
  61.         {
  62.             LED0_IO=~LED0_IO;
  63.             t = tickGet();
  64.         }
  65.  
  66.         #ifdef USE_BLINDS
  67.         if ((tickGet()-blinds)>2*TICK_SECOND)
  68.         {
  69.             angle = (angle>0?-70:70);
  70.             blindsTurn(angle);
  71.             blinds = tickGet();
  72.         }
  73.         #endif
  74.  
  75.  
  76.         #ifdef USE_ADC
  77.         if ((tickGet()-temperature)>TICK_SECOND*2) // Each 2 second, read temperature.
  78.         {
  79.             if (lastTemperature==TEMPERATURE_INSIDE) lastTemperature=TEMPERATURE_OUTSIDE; else lastTemperature=TEMPERATURE_INSIDE;
  80.             adcConvert(lastTemperature);
  81.             temperature = tickGet();
  82.         }
  83.         #endif
  84.  
  85.  
  86.         #ifdef USE_ADC
  87.         if (adcDone()) // Has temperature, send it to the bus
  88.         {
  89.             BYTE decimal;
  90.             signed char tenth;
  91.             temperatureRead(&tenth,&decimal);
  92.  
  93.             outCm.funct                     = FUNCT_SENSORS;
  94.             outCm.funcc                     = (lastTemperature==TEMPERATURE_INSIDE?FUNCC_SENSORS_TEMPERATURE_INSIDE:FUNCC_SENSORS_TEMPERATURE_OUTSIDE);
  95.             outCm.data_length               = 2;
  96.             outCm.data[1]                   = tenth; //  signed 10 an 1 decimal.
  97.             outCm.data[0]                   = decimal; //  Decimal value, 0-9
  98.             while(!canSendMessage(outCm,PRIO_HIGH));
  99.         }
  100.         #endif
  101.  
  102.     }
  103. }
  104.  
  105.  
  106. #pragma interrupt high_isr
  107. void high_isr(void)
  108. {
  109.     canISR();
  110.  
  111.     #ifdef USE_BLINDS
  112.         blindsISR();
  113.     #endif
  114. }
  115.  
  116.  
  117. #pragma interrupt low_isr
  118. void low_isr(void)
  119. {
  120.     #ifdef USE_ADC
  121.         adcISR();
  122.     #endif
  123. }
  124.  
  125.  
  126.  
  127. /*
  128. *   Function: mainInit
  129. *
  130. *   Input:  none
  131. *   Output: none
  132. *   Pre-conditions: none
  133. *   Affects: Se code
  134. *   Depends: none.
  135. */
  136. void mainInit()
  137. {
  138.     LED0_TRIS=0;
  139.     BLINDS0_TRIS=0;
  140.     BLINDS0P_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.