Subversion Repositories HomeAutomation

Rev

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