Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
275 johboh 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-03-01 14:38:06 +0100 (Thu, 01 Mar 2007) $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 347 $
10
 ********************************************************************/
11
#include <blinds.h>
12
 
13
#ifdef USE_BLINDS
14
 
347 johboh 15
static WORD timerVal = 50536;
16
static WORD turnOffIO = 0;
339 johboh 17
static signed char currentPrecent = 0;
347 johboh 18
static BYTE dutyOffCounter = 0;
275 johboh 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;
277 johboh 34
    TMR1H=((timerVal&0xFF00)>>8);
35
    TMR1L=(timerVal&0xFF);
275 johboh 36
    PIR1bits.TMR1IF = 0;
37
    PIE1bits.TMR1IE = 1;
339 johboh 38
    IPR1bits.TMR1IP = 0; // Low prior
275 johboh 39
}
40
 
41
/*
339 johboh 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
/*
276 johboh 57
*   Function: blindsTurn
275 johboh 58
*
277 johboh 59
*   Input:  Angle to turn to.
275 johboh 60
*   Output: none.
61
*   Pre-conditions: none
62
*   Affects: none.
63
*   Depends: none.
64
*/
339 johboh 65
void blindsTurn(BYTE precent)
275 johboh 66
{
277 johboh 67
    // 65536-TIME/0,0000001
68
    // -90 0.5 ms = 60536 (5000)
69
    // 0   1.5 ms = 50536 (15000)
70
    // 90  2.5 ms = 40536 (25000)
275 johboh 71
 
339 johboh 72
    BLINDS0P_IO=1;
73
    turnOffIO = IO_TIMEOUT;
74
 
347 johboh 75
    currentPrecent = precent;
76
    if (precent>100) { currentPrecent = 100; }
77
    if (precent<0)   { currentPrecent =   0; }
78
    dutyOffCounter = 0;
339 johboh 79
 
80
    timerVal=60536-(unsigned int)((90+(-1.05*(signed int)currentPrecent+60))*111.111);
275 johboh 81
}
82
 
276 johboh 83
 
275 johboh 84
/*
276 johboh 85
*   Function: blindsISR
275 johboh 86
*
276 johboh 87
*   Input:  none.
275 johboh 88
*   Output: none.
89
*   Pre-conditions: none
90
*   Affects: none.
91
*   Depends: none.
92
*/
93
void blindsISR(void)
94
{
95
    if (PIR1bits.TMR1IF == 1 && PIE1bits.TMR1IE == 1)
96
    {
347 johboh 97
        WORD timerValBuffer = timerVal;
275 johboh 98
 
347 johboh 99
        dutyOffCounter++;
275 johboh 100
 
347 johboh 101
        if (BLINDS0_IO==0 && dutyOffCounter>4)
102
        {
103
            BLINDS0_IO=1;
104
            dutyOffCounter=0;  
105
        }
106
        else if (BLINDS0_IO==0 && dutyOffCounter<=4)
107
        {
108
            timerValBuffer = DUTY_OFF_DELAY;
109
        }
110
        else if (BLINDS0_IO==1)
111
        {
112
            BLINDS0_IO=0;
113
        }
114
 
115
        TMR1H=((timerValBuffer&0xFF00)>>8);
116
        TMR1L=(timerValBuffer&0xFF);
117
 
339 johboh 118
        if (turnOffIO--<1)
119
        {
120
            BLINDS0P_IO=0;
121
        }
122
 
275 johboh 123
        PIR1bits.TMR1IF = 0;
124
    }
125
}
126
 
127
#endif