Subversion Repositories HomeAutomation

Rev

Rev 282 | Blame | 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/trunk/EmbeddedSoftware/PIC/canTemp/Source/blinds.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. #include <blinds.h>
  12.  
  13. #ifdef USE_BLINDS
  14.  
  15. static unsigned int timerVal = 50536;
  16. static unsigned int turnOffIO = 0;
  17. static signed char currentPrecent = 0;
  18.  
  19. /*
  20. *   Function: blindsInit
  21. *
  22. *   Input:  none.
  23. *   Output: none.
  24. *   Pre-conditions: none
  25. *   Affects: none.
  26. *   Depends: none.
  27. */
  28. void blindsInit()
  29. {
  30.     // setup timer one on 0.5ms.
  31.     // 16 bit write, system clock, prescale 2 bit, oscillator off,
  32.     T1CON = 0b11000001;
  33.     TMR1H=((timerVal&0xFF00)>>8);
  34.     TMR1L=(timerVal&0xFF);
  35.     PIR1bits.TMR1IF = 0;
  36.     PIE1bits.TMR1IE = 1;
  37.     IPR1bits.TMR1IP = 0; // Low prior
  38. }
  39.  
  40. /*
  41. *   Function: blindsGetAngle
  42. *
  43. *   Input:  none.
  44. *   Output: angle.
  45. *   Pre-conditions: none
  46. *   Affects: none.
  47. *   Depends: none.
  48. */
  49. BYTE blindsGetPrecent()
  50. {
  51.     return currentPrecent;
  52. }
  53.  
  54.  
  55. /*
  56. *   Function: blindsTurn
  57. *
  58. *   Input:  Angle to turn to.
  59. *   Output: none.
  60. *   Pre-conditions: none
  61. *   Affects: none.
  62. *   Depends: none.
  63. */
  64. void blindsTurn(BYTE precent)
  65. {
  66.     // 65536-TIME/0,0000001
  67.     // -90 0.5 ms = 60536 (5000)
  68.     // 0   1.5 ms = 50536 (15000)
  69.     // 90  2.5 ms = 40536 (25000)
  70.  
  71.     BLINDS0P_IO=1;
  72.     turnOffIO = IO_TIMEOUT;
  73.     /*
  74.         if (angle<ANGLE_MIN) {timerVal=ANGLE_MIN_TIMER; currentAngle=ANGLE_MIN; }
  75.         else if (angle>ANGLE_MAX) {timerVal=ANGLE_MAX_TIMER; currentAngle=ANGLE_MAX; }
  76.         else
  77.         {
  78.             timerVal=60536-(unsigned int)((90+(signed int)angle)*111.111);
  79.             currentAngle=angle;
  80.         }
  81.     */
  82.  
  83.     /*
  84.     angle=precent*
  85.  
  86.     -50=k*100+m
  87.     60=k*0+m
  88.     angle=-1.1x+60
  89.     */
  90.  
  91.     currentPrecent=precent;
  92.     if (precent>100) { currentPrecent=100; }
  93.     if (precent<0) { currentPrecent=0; }
  94.  
  95.    
  96.     timerVal=60536-(unsigned int)((90+(-1.05*(signed int)currentPrecent+60))*111.111);
  97. }
  98.  
  99.  
  100. /*
  101. *   Function: blindsISR
  102. *
  103. *   Input:  none.
  104. *   Output: none.
  105. *   Pre-conditions: none
  106. *   Affects: none.
  107. *   Depends: none.
  108. */
  109. void blindsISR(void)
  110. {
  111.     if (PIR1bits.TMR1IF == 1 && PIE1bits.TMR1IE == 1)
  112.     {
  113.         TMR1H=((timerVal&0xFF00)>>8);
  114.         TMR1L=(timerVal&0xFF);
  115.  
  116.         BLINDS0_IO=~BLINDS0_IO;
  117.  
  118.         if (turnOffIO--<1)
  119.         {
  120.             BLINDS0P_IO=0;
  121.         }
  122.  
  123.         PIR1bits.TMR1IF = 0;
  124.     }
  125. }
  126.  
  127. #endif
  128.