Subversion Repositories HomeAutomation

Rev

Rev 359 | 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/branches/modules/EmbeddedSoftware/PIC/canFreezer/Source/Main.c $
  7.  * Last changed:    $LastChangedDate: 2007-03-04 15:13:16 +0100 (Sun, 04 Mar 2007) $
  8.  * By:              $LastChangedBy: johboh $
  9.  * Revision:        $Revision: 359 $
  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. #ifdef USE_ADC
  21.     #include <adc.h>
  22. #endif
  23.  
  24. static void mainInit(void);
  25.  
  26.  
  27. void main()
  28. {
  29.     // Inits
  30.     mainInit();
  31.     canInit();
  32.  
  33.     #ifdef USE_ADC
  34.         adcInit(TRUE,0b1011);
  35.     #endif
  36.  
  37.     while(1)
  38.     {
  39.         TICK currentTick = tickGet();
  40.         static TICK tick_led = 0;
  41.         static TICK tick_temperature = 0;
  42.         static TICK tick_FreezerDoor = 0;
  43.         static TICK tick_RefrigeratorDoor = 0;
  44.         static TICK bounce_FreezerDoor = 0;
  45.         static TICK bounce_RefrigeratorDoor = 0;
  46.  
  47.         static BYTE lastTemperature = TEMPERATURE_FREEZER;
  48.         static BYTE lastFreezerDoor = DOOR_CLOSED;
  49.         static BYTE lastRefrigeratorDoor = DOOR_CLOSED;
  50.  
  51.         static BOOL getNewTemperature = TRUE;
  52.  
  53.  
  54.         #ifdef USE_DOOR_SW
  55.         if ((currentTick-bounce_FreezerDoor)>(3*TICK_100MS) && lastFreezerDoor != DOOR_FREEZER_IO)
  56.         {
  57.             lastFreezerDoor = DOOR_FREEZER_IO;
  58.             tick_FreezerDoor = 0;
  59.             bounce_FreezerDoor = currentTick;
  60.         }
  61.  
  62.         if ((currentTick-bounce_RefrigeratorDoor)>(3*TICK_100MS) && lastRefrigeratorDoor != DOOR_REFRIGERATOR_IO)
  63.         {
  64.             lastRefrigeratorDoor = DOOR_REFRIGERATOR_IO;
  65.             tick_RefrigeratorDoor = 0;
  66.             bounce_RefrigeratorDoor = currentTick;
  67.         }
  68.  
  69.         if ((currentTick-tick_FreezerDoor)>(3*TICK_1S))
  70.         {
  71.             CAN_PACKET outCp;  
  72.             outCp.type      = ptPGM;
  73.             outCp.pgm.class = pcSENSOR;
  74.             outCp.pgm.id    = pstDOOR_FREEZER;
  75.             outCp.length    = 1;
  76.             outCp.data[0]   = (DOOR_FREEZER_IO==DOOR_CLOSED?0:1);
  77.             while(!canSendMessage(outCp,PRIO_HIGH));
  78.  
  79.             tick_FreezerDoor = currentTick;
  80.         }
  81.  
  82.         if ((currentTick-tick_RefrigeratorDoor)>(3*TICK_1S))
  83.         {
  84.             CAN_PACKET outCp;  
  85.             outCp.type      = ptPGM;
  86.             outCp.pgm.class = pcSENSOR;
  87.             outCp.pgm.id    = pstDOOR_REFRIGERATOR;
  88.             outCp.length    = 1;
  89.             outCp.data[0]   = (DOOR_REFRIGERATOR_IO==DOOR_CLOSED?0:1);
  90.             while(!canSendMessage(outCp,PRIO_HIGH));
  91.  
  92.             tick_RefrigeratorDoor = currentTick;
  93.         }
  94.         #endif
  95.  
  96.         if ((currentTick-tick_led)>TICK_1S)
  97.         {
  98.             LED0_IO=~LED0_IO;
  99.             tick_led = currentTick;
  100.         }
  101.  
  102.         #ifdef USE_ADC
  103.         if ((currentTick-tick_temperature)>(2*TICK_1S) && getNewTemperature==TRUE) // Each 2 second, read temperature.
  104.         {
  105.             if (lastTemperature==TEMPERATURE_FREEZER) lastTemperature=TEMPERATURE_REFRIGERATOR; else lastTemperature=TEMPERATURE_FREEZER;
  106.             adcConvert(lastTemperature);
  107.             tick_temperature = currentTick;
  108.         }
  109.         #endif
  110.  
  111.  
  112.         #ifdef USE_ADC
  113.         if (adcDone()) // Has temperature, send it to the bus
  114.         {
  115.             CAN_PACKET outCp;
  116.             BYTE decimal;
  117.             signed char tenth;
  118.             temperatureRead(&tenth,&decimal);
  119.            
  120.             outCp.type      = ptPGM;
  121.             outCp.pgm.class = pcSENSOR;
  122.             outCp.pgm.id    = (lastTemperature==TEMPERATURE_FREEZER?pstTEMP_FREEZER:pstTEMP_REFRIGERATOR);
  123.             outCp.length    = 2;
  124.             outCp.data[1]   = tenth; //  signed 10 an 1 decimal.
  125.             outCp.data[0]   = decimal; //  Decimal value, 0-9
  126.             while(!canSendMessage(outCp,PRIO_HIGH));
  127.            
  128.             tick_temperature = currentTick;
  129.             getNewTemperature = TRUE;
  130.         }
  131.         #endif
  132.  
  133.     }
  134. }
  135.  
  136.  
  137. #pragma interrupt high_isr
  138. void high_isr(void)
  139. {
  140.     canISR();
  141.     #ifdef USE_ADC
  142.         adcISR();
  143.     #endif
  144. }
  145.  
  146.  
  147. #pragma interrupt low_isr
  148. void low_isr(void)
  149. {
  150.  
  151. }
  152.  
  153.  
  154.  
  155. /*
  156. *   Function: mainInit
  157. *
  158. *   Input:  none
  159. *   Output: none
  160. *   Pre-conditions: none
  161. *   Affects: Se code
  162. *   Depends: none.
  163. */
  164. void mainInit()
  165. {
  166.     LED0_TRIS=0;   
  167.  
  168.     DOOR_FREEZER_TRIS=1;
  169.     DOOR_REFRIGERATOR_TRIS=1;
  170.  
  171.     // Enable Interrupts
  172.     INTCONbits.GIEH = 1;
  173.     INTCONbits.GIEL = 1;
  174.  
  175.     // Enable interrupt prirority
  176.     RCONbits.IPEN=1;
  177.    
  178.  
  179. }
  180.  
  181. /*
  182. *   Function: canParse
  183. *
  184. *   Input:  Received can message
  185. *   Output: none
  186. *   Pre-conditions: canInit and received packet.
  187. *   Affects: Sensors/actuators/etc. See code.
  188. *   Depends: none.
  189. */
  190. void canParse(CAN_PACKET cp)
  191. {
  192.  
  193. }
  194.  
  195.  
  196. // Below are mandantory when using bootloader
  197. // define DEBUG_MODE to use without bootloader.
  198.  
  199. #ifndef DEBUG_MODE
  200. extern void _startup (void);
  201. #pragma code _RESET_INTERRUPT_VECTOR = RM_RESET_VECTOR
  202. void _reset (void)
  203. {
  204.     _asm goto _startup _endasm
  205. }
  206. #pragma code
  207. #endif
  208.  
  209. #pragma code _HIGH_INTERRUPT_VECTOR = RM_HIGH_INTERRUPT_VECTOR
  210. void _high_ISR (void)
  211. {
  212.     _asm GOTO high_isr _endasm
  213. }
  214. #pragma code
  215.  
  216. #pragma code _LOW_INTERRUPT_VECTOR = RM_LOW_INTERRUPT_VECTOR
  217. void _low_ISR (void)
  218. {
  219.     _asm GOTO low_isr _endasm
  220. }
  221. #pragma code
  222.