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