Subversion Repositories HomeAutomation

Rev

Rev 353 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*********************************************************************
  2.  *
  3.  *   Functions for controlling blinds
  4.  *
  5.  *********************************************************************
  6.  * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/branches/atomV2/EmbeddedSoftware/PIC/canTemp/Source/blinds.c $
  7.  * Last changed:    $LastChangedDate: 2007-03-03 19:07:14 +0100 (Sat, 03 Mar 2007) $
  8.  * By:              $LastChangedBy: johboh $
  9.  * Revision:        $Revision: 353 $
  10.  ********************************************************************/
  11. #include <blinds.h>
  12.  
  13. #ifdef USE_BLINDS
  14.  
  15. static WORD timerVal = 50536;
  16. static WORD turnOffIO = 0;
  17. static BYTE currentPrecent = 0;
  18. static BYTE dutyOffCounter = 0;
  19.  
  20. /*
  21. *   Function: blindsInit
  22. *
  23. *   Input:  none.
  24. *   Output: none.
  25. *   Pre-conditions: none
  26. *   Affects: none.
  27. *   Depends: none.
  28. */
  29. void blindsInit()
  30. {
  31.     // setup timer one on 0.5ms.
  32.     // 16 bit write, system clock, prescale 2 bit, oscillator off,
  33.     T1CON = 0b11000001;
  34.     TMR1H=(BYTE)((timerVal&0xFF00)>>8);
  35.     TMR1L=(timerVal&0xFF);
  36.     PIR1bits.TMR1IF = 0;
  37.     PIE1bits.TMR1IE = 1;
  38.     IPR1bits.TMR1IP = 1; // high prior
  39. }
  40.  
  41. /*
  42. *   Function: blindsGetAngle
  43. *
  44. *   Input:  none.
  45. *   Output: angle.
  46. *   Pre-conditions: none
  47. *   Affects: none.
  48. *   Depends: none.
  49. */
  50. BYTE blindsGetPrecent()
  51. {
  52.     return currentPrecent;
  53. }
  54.  
  55.  
  56. /*
  57. *   Function: blindsTurn
  58. *
  59. *   Input:  Angle to turn to.
  60. *   Output: none.
  61. *   Pre-conditions: none
  62. *   Affects: none.
  63. *   Depends: none.
  64. */
  65. void blindsTurn(BYTE precent)
  66. {
  67.    
  68.     // 65536-TIME/0,0000001
  69.     // -90 0.5 ms = 60536 (5000)
  70.     // 0   1.5 ms = 50536 (15000)
  71.     // 90  2.5 ms = 40536 (25000)
  72.  
  73.     BLINDS0P_IO=1;
  74.     turnOffIO = IO_TIMEOUT;
  75.  
  76.     currentPrecent = precent;
  77.     if (precent>100) { currentPrecent = 100; }
  78.     if (precent<0)   { currentPrecent =   0; }
  79.     dutyOffCounter = 0;
  80.  
  81.     timerVal=60536-(unsigned int)((90+(-1.05*(unsigned int)currentPrecent+60))*111.111);
  82.    
  83. }
  84.  
  85.  
  86. /*
  87. *   Function: blindsISR
  88. *
  89. *   Input:  none.
  90. *   Output: none.
  91. *   Pre-conditions: none
  92. *   Affects: none.
  93. *   Depends: none.
  94. */
  95. void blindsISR(void)
  96. {
  97.     if (PIR1bits.TMR1IF == 1 && PIE1bits.TMR1IE == 1)
  98.     {
  99.        
  100.         WORD timerValBuffer = timerVal;
  101.        
  102.         dutyOffCounter++;
  103.  
  104.         if (BLINDS0_IO==0 && dutyOffCounter>4)
  105.         {
  106.             BLINDS0_IO=1;
  107.             dutyOffCounter=0;  
  108.         }
  109.         else if (BLINDS0_IO==0 && dutyOffCounter<=4)
  110.         {
  111.             timerValBuffer = DUTY_OFF_DELAY;
  112.         }
  113.         else if (BLINDS0_IO==1)
  114.         {
  115.             BLINDS0_IO=0;
  116.         }
  117.        
  118.         TMR1H=(BYTE)((timerValBuffer&0xFF00)>>8);
  119.         TMR1L=(timerValBuffer&0xFF);
  120.  
  121.        
  122.         if (turnOffIO--<1)
  123.         {
  124.             BLINDS0P_IO=0;
  125.         }
  126.        
  127.  
  128.         PIR1bits.TMR1IF = 0;
  129.     }
  130. }
  131.  
  132. #endif
  133.