Subversion Repositories HomeAutomation

Rev

Rev 297 | Blame | 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-25 20:51:08 +0100 (Sun, 25 Feb 2007) $
  8.  * By:              $LastChangedBy: johboh $
  9.  * Revision:        $Revision: 339 $
  10.  ********************************************************************/
  11.  
  12.  
  13. #include <compiler.h>
  14. #include <stackTasks.h>
  15. #include <Tick.h>
  16. #include <funcdefs.h>
  17. #include <CAN.h>
  18. #include "..\canBoot\Include\boot.h"
  19.  
  20.  
  21. #ifdef USE_ADC
  22.     #include <adc.h>
  23. #endif
  24.  
  25. #ifdef USE_BLINDS
  26.     #include <blinds.h>
  27. #endif
  28.  
  29. static void mainInit(void);
  30.  
  31. void main()
  32. {
  33.  
  34.     static TICK t = 0;
  35.     CAN_PACKET outCp;
  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.         blindsTurn(0);
  49.     #else
  50.         BLINDS0P_IO=0;
  51.     #endif
  52.  
  53.  
  54.     while(1)
  55.     {
  56.         static TICK t = 0;
  57.         static TICK temperature = 0;
  58.         static BOOL getNewTemperature = TRUE;
  59.         static BYTE lastTemperature = TEMPERATURE_INSIDE;
  60.         static TICK blinds = 0;
  61.        
  62.         if ((tickGet()-t)>TICK_SECOND)
  63.         {
  64.             LED0_IO=~LED0_IO;
  65.             t = tickGet();
  66.         }
  67.  
  68.        
  69.         #ifdef USE_BLINDS
  70.         if ((tickGet()-blinds)>3*TICK_SECOND)
  71.         {
  72.             outCp.type=ptPGM;
  73.             outCp.pgm.class=pcSENSOR;
  74.             outCp.pgm.id=pstBLIND_TR;
  75.             outCp.length=1;
  76.             outCp.data[0]=blindsGetPrecent();
  77.             while(!canSendMessage(outCp,PRIO_HIGH));
  78.             blinds = tickGet();
  79.         }
  80.         #endif
  81.        
  82.  
  83.         #ifdef USE_ADC
  84.         if ((tickGet()-temperature)>TICK_SECOND*2 && getNewTemperature==TRUE) // Each 2 second, read temperature.
  85.         {
  86.             if (lastTemperature==TEMPERATURE_INSIDE) lastTemperature=TEMPERATURE_OUTSIDE; else lastTemperature=TEMPERATURE_INSIDE;
  87.             adcConvert(lastTemperature);
  88.             temperature = tickGet();
  89.             getNewTemperature=FALSE;
  90.         }
  91.         #endif
  92.  
  93.  
  94.         #ifdef USE_ADC
  95.         if (adcDone()) // Has temperature, send it to the bus
  96.         {
  97.             BYTE decimal;
  98.             signed char tenth;
  99.             unsigned long q;
  100.             temperatureRead(&tenth,&decimal);
  101.             q = adcGetRaw();
  102.             getNewTemperature=TRUE;
  103.  
  104.             outCp.type=ptPGM;
  105.             outCp.pgm.class =pcSENSOR;
  106.             outCp.pgm.id    =(lastTemperature==TEMPERATURE_INSIDE?pstTEMP_INSIDE:pstTEMP_OUTSIDE);
  107.             outCp.length    =2;
  108.             outCm.data[1]   = tenth; //  signed 10 an 1 decimal.
  109.             outCm.data[0]   = decimal; //  Decimal value, 0-9
  110.             while(!canSendMessage(outCm,PRIO_HIGH));
  111.         }
  112.         #endif
  113.  
  114.     }
  115. }
  116.  
  117.  
  118. #pragma interrupt high_isr
  119. void high_isr(void)
  120. {
  121.     canISR();
  122. }
  123.  
  124.  
  125. #pragma interrupt low_isr
  126. void low_isr(void)
  127. {
  128.     #ifdef USE_ADC
  129.         adcISR();
  130.     #endif
  131.  
  132.     #ifdef USE_BLINDS
  133.         blindsISR();
  134.     #endif
  135. }
  136.  
  137.  
  138.  
  139. /*
  140. *   Function: mainInit
  141. *
  142. *   Input:  none
  143. *   Output: none
  144. *   Pre-conditions: none
  145. *   Affects: Se code
  146. *   Depends: none.
  147. */
  148. void mainInit()
  149. {
  150.     LED0_TRIS=0;
  151.     BLINDS0_TRIS=0;
  152.     BLINDS0P_TRIS=0;
  153.  
  154.     // Enable Interrupts
  155.     INTCONbits.GIEH = 1;
  156.     INTCONbits.GIEL = 1;
  157.  
  158.     // Enable interrupt prirority
  159.     RCONbits.IPEN=1;
  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.  
  172. void canParse(CAN_PACKET cp)
  173. {
  174.     #ifdef USE_BLINDS
  175.     if (cp.pgm.class==pcACTUATOR && cp.pgm.id==patBLIND_TR)
  176.     {
  177.         blindsTurn(cp.data[0]);
  178.     }
  179.     #endif
  180. }
  181.  
  182.  
  183. // Below are mandantory when using bootloader
  184. // define DEBUG_MODE to use without bootloader.
  185.  
  186. #ifndef DEBUG_MODE
  187. extern void _startup (void);
  188. #pragma code _RESET_INTERRUPT_VECTOR = RM_RESET_VECTOR
  189. void _reset (void)
  190. {
  191.     _asm goto _startup _endasm
  192. }
  193. #pragma code
  194. #endif
  195.  
  196. #pragma code _HIGH_INTERRUPT_VECTOR = RM_HIGH_INTERRUPT_VECTOR
  197. void _high_ISR (void)
  198. {
  199.     _asm GOTO high_isr _endasm
  200. }
  201. #pragma code
  202.  
  203. #pragma code _LOW_INTERRUPT_VECTOR = RM_LOW_INTERRUPT_VECTOR
  204. void _low_ISR (void)
  205. {
  206.     _asm GOTO low_isr _endasm
  207. }
  208. #pragma code
  209.