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