Subversion Repositories HomeAutomation

Rev

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