Subversion Repositories HomeAutomation

Rev

Rev 260 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 260 Rev 359
Line 1... Line 1...
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/canFreezer/Source/Main.c $
6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canFreezer/Source/Main.c $
7
 * Last changed:    $LastChangedDate: 2007-01-28 14:58:11 +0100 (Sun, 28 Jan 2007) $
7
 * Last changed:    $LastChangedDate: 2007-03-04 15:13:16 +0100 (Sun, 04 Mar 2007) $
8
 * By:              $LastChangedBy: johboh $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 260 $
9
 * Revision:        $Revision: 359 $
10
 ********************************************************************/
10
 ********************************************************************/
11
 
11
 
12
 
12
 
13
#include <compiler.h>
13
#include <compiler.h>
14
#include <stackTasks.h>
14
#include <stackTasks.h>
15
#include <Tick.h>
15
#include <Tick.h>
16
#include <funcdefs.h>
16
#include <funcdefs.h>
17
#include <CAN.h>
17
#include <CAN.h>
18
 
-
 
-
 
18
#include "..\canBoot\Include\boot.h"
19
 
19
 
20
#ifdef USE_ADC
20
#ifdef USE_ADC
21
    #include <adc.h>
21
    #include <adc.h>
22
#endif
22
#endif
23
 
23
 
24
static void mainInit(void);
24
static void mainInit(void);
25
 
25
 
26
 
26
 
27
void main()
27
void main()
28
{
28
{
29
    static TICK t = 0;
-
 
30
    CAN_MESSAGE outCm;
-
 
31
 
-
 
32
    // Inits
29
    // Inits
33
    mainInit();
30
    mainInit();
34
    canInit();
31
    canInit();
35
 
32
 
36
    #ifdef USE_ADC
33
    #ifdef USE_ADC
37
        adcInit(TRUE,0b1011);
34
        adcInit(TRUE,0b1011);
38
    #endif
35
    #endif
39
 
36
 
40
    while(1)
37
    while(1)
41
    {
38
    {
-
 
39
        TICK currentTick = tickGet();
42
        static TICK t = 0;
40
        static TICK tick_led = 0;
43
        static TICK temperature = 0;
41
        static TICK tick_temperature = 0;
-
 
42
        static TICK tick_FreezerDoor = 0;
-
 
43
        static TICK tick_RefrigeratorDoor = 0;
-
 
44
        static TICK bounce_FreezerDoor = 0;
-
 
45
        static TICK bounce_RefrigeratorDoor = 0;
-
 
46
 
44
        static BYTE lastTemperature = TEMPERATURE_FREEZER;
47
        static BYTE lastTemperature = TEMPERATURE_FREEZER;
45
        static BYTE lastFreezerDoor = DOOR_CLOSED;
48
        static BYTE lastFreezerDoor = DOOR_CLOSED;
46
        static BYTE lastRefrigeratorDoor = DOOR_CLOSED;
49
        static BYTE lastRefrigeratorDoor = DOOR_CLOSED;
47
 
50
 
-
 
51
        static BOOL getNewTemperature = TRUE;
48
 
52
 
-
 
53
 
49
        //Poll doors freezer door
54
        #ifdef USE_DOOR_SW
50
        if (lastFreezerDoor!=DOOR_FREEZER_IO)
55
        if ((currentTick-bounce_FreezerDoor)>(3*TICK_100MS) && lastFreezerDoor != DOOR_FREEZER_IO)
51
        {  
56
        {
52
            lastFreezerDoor=DOOR_FREEZER_IO;
57
            lastFreezerDoor = DOOR_FREEZER_IO;
53
            // Send door status change
-
 
54
            outCm.funct                     = FUNCT_SENSORS;
-
 
55
            outCm.funcc                     = FUNCC_SENSORS_DOORS_FREEZER;
-
 
56
            outCm.data_length               = 1;
58
            tick_FreezerDoor = 0;
57
            outCm.data[0]                   = (lastFreezerDoor==DOOR_OPEN?0:1);
-
 
58
            while(!canSendMessage(outCm,PRIO_HIGH));
-
 
59
        }
-
 
60
 
-
 
61
        //Poll doors refrigerator door
-
 
62
        if (lastRefrigeratorDoor!=DOOR_REFRIGERATOR_IO)
-
 
63
        {  
-
 
64
            lastRefrigeratorDoor=DOOR_REFRIGERATOR_IO;
59
            bounce_FreezerDoor = currentTick;
65
            // Send door status change
-
 
66
            outCm.funct                     = FUNCT_SENSORS;
-
 
67
            outCm.funcc                     = FUNCC_SENSORS_DOORS_REFRIGERATOR;
-
 
68
            outCm.data_length               = 1;
-
 
69
            outCm.data[0]                   = (lastRefrigeratorDoor==DOOR_OPEN?0:1);
-
 
70
            while(!canSendMessage(outCm,PRIO_HIGH));
-
 
71
        }
60
        }
72
 
61
 
-
 
62
        if ((currentTick-bounce_RefrigeratorDoor)>(3*TICK_100MS) && lastRefrigeratorDoor != DOOR_REFRIGERATOR_IO)
-
 
63
        {
-
 
64
            lastRefrigeratorDoor = DOOR_REFRIGERATOR_IO;
-
 
65
            tick_RefrigeratorDoor = 0;
-
 
66
            bounce_RefrigeratorDoor = currentTick;
-
 
67
        }
-
 
68
 
-
 
69
        if ((currentTick-tick_FreezerDoor)>(3*TICK_1S))
-
 
70
        {
-
 
71
            CAN_PACKET outCp;  
-
 
72
            outCp.type      = ptPGM;
-
 
73
            outCp.pgm.class = pcSENSOR;
-
 
74
            outCp.pgm.id    = pstDOOR_FREEZER;
-
 
75
            outCp.length    = 1;
-
 
76
            outCp.data[0]   = (DOOR_FREEZER_IO==DOOR_CLOSED?0:1);
-
 
77
            while(!canSendMessage(outCp,PRIO_HIGH));
-
 
78
 
-
 
79
            tick_FreezerDoor = currentTick;
-
 
80
        }
-
 
81
 
-
 
82
        if ((currentTick-tick_RefrigeratorDoor)>(3*TICK_1S))
-
 
83
        {
-
 
84
            CAN_PACKET outCp;  
-
 
85
            outCp.type      = ptPGM;
-
 
86
            outCp.pgm.class = pcSENSOR;
-
 
87
            outCp.pgm.id    = pstDOOR_REFRIGERATOR;
-
 
88
            outCp.length    = 1;
-
 
89
            outCp.data[0]   = (DOOR_REFRIGERATOR_IO==DOOR_CLOSED?0:1);
-
 
90
            while(!canSendMessage(outCp,PRIO_HIGH));
-
 
91
 
-
 
92
            tick_RefrigeratorDoor = currentTick;
-
 
93
        }
-
 
94
        #endif
-
 
95
 
73
        if ((tickGet()-t)>TICK_SECOND)
96
        if ((currentTick-tick_led)>TICK_1S)
74
        {
97
        {
75
            LED0_IO=~LED0_IO;
98
            LED0_IO=~LED0_IO;
76
            t = tickGet();
99
            tick_led = currentTick;
77
        }
100
        }
78
 
101
 
79
        #ifdef USE_ADC
102
        #ifdef USE_ADC
80
        if ((tickGet()-temperature)>TICK_SECOND*2) // Each 2 second, read temperature.
103
        if ((currentTick-tick_temperature)>(2*TICK_1S) && getNewTemperature==TRUE) // Each 2 second, read temperature.
81
        {
104
        {
82
            if (lastTemperature==TEMPERATURE_FREEZER) lastTemperature=TEMPERATURE_REFRIGERATOR; else lastTemperature=TEMPERATURE_FREEZER;
105
            if (lastTemperature==TEMPERATURE_FREEZER) lastTemperature=TEMPERATURE_REFRIGERATOR; else lastTemperature=TEMPERATURE_FREEZER;
83
            adcConvert(lastTemperature);
106
            adcConvert(lastTemperature);
84
            temperature = tickGet();
107
            tick_temperature = currentTick;
85
        }
108
        }
86
        #endif
109
        #endif
87
 
110
 
88
 
111
 
89
        #ifdef USE_ADC
112
        #ifdef USE_ADC
90
        if (adcDone()) // Has temperature, send it to the bus
113
        if (adcDone()) // Has temperature, send it to the bus
91
        {
114
        {
-
 
115
            CAN_PACKET outCp;
92
            BYTE decimal;
116
            BYTE decimal;
93
            signed char tenth;
117
            signed char tenth;
94
            temperatureRead(&tenth,&decimal);
118
            temperatureRead(&tenth,&decimal);
95
 
119
           
-
 
120
            outCp.type      = ptPGM;
96
            outCm.funct                     = FUNCT_SENSORS;
121
            outCp.pgm.class = pcSENSOR;
97
            outCm.funcc                     = (lastTemperature==TEMPERATURE_FREEZER?FUNCC_SENSORS_TEMPERATURE_FREEZER:FUNCC_SENSORS_TEMPERATURE_REFRIGERATOR);
122
            outCp.pgm.id    = (lastTemperature==TEMPERATURE_FREEZER?pstTEMP_FREEZER:pstTEMP_REFRIGERATOR);
98
            outCm.data_length               = 2;
123
            outCp.length    = 2;
99
            outCm.data[1]                   = tenth; //  signed 10 an 1 decimal.
124
            outCp.data[1]   = tenth; //  signed 10 an 1 decimal.
100
            outCm.data[0]                   = decimal; //  Decimal value, 0-9
125
            outCp.data[0]   = decimal; //  Decimal value, 0-9
101
            while(!canSendMessage(outCm,PRIO_HIGH));
126
            while(!canSendMessage(outCp,PRIO_HIGH));
-
 
127
           
-
 
128
            tick_temperature = currentTick;
-
 
129
            getNewTemperature = TRUE;
102
        }
130
        }
103
        #endif
131
        #endif
104
 
132
 
105
    }
133
    }
106
}
134
}
107
 
135
 
108
 
136
 
109
#pragma interrupt high_isr
137
#pragma interrupt high_isr
110
void high_isr(void)
138
void high_isr(void)
111
{
139
{
112
    canISR();
140
    canISR();
113
}
-
 
114
 
-
 
115
 
-
 
116
#pragma interrupt low_isr
-
 
117
void low_isr(void)
-
 
118
{
-
 
119
    #ifdef USE_ADC
141
    #ifdef USE_ADC
120
        adcISR();
142
        adcISR();
121
    #endif
143
    #endif
-
 
144
}
-
 
145
 
-
 
146
 
-
 
147
#pragma interrupt low_isr
-
 
148
void low_isr(void)
-
 
149
{
-
 
150
 
122
}
151
}
123
 
152
 
124
 
153
 
125
 
154
 
126
/*
155
/*
Line 128... Line 157...
128
*
157
*
129
*   Input:  none
158
*   Input:  none
130
*   Output: none
159
*   Output: none
131
*   Pre-conditions: none
160
*   Pre-conditions: none
132
*   Affects: Se code
161
*   Affects: Se code
133
*   Depends: none.
162
*   Depends: none.
134
*/
163
*/
135
void mainInit()
164
void mainInit()
136
{
165
{
137
    LED0_TRIS=0;   
166
    LED0_TRIS=0;   
138
 
167
 
Line 156... Line 185...
156
*   Output: none
185
*   Output: none
157
*   Pre-conditions: canInit and received packet.
186
*   Pre-conditions: canInit and received packet.
158
*   Affects: Sensors/actuators/etc. See code.
187
*   Affects: Sensors/actuators/etc. See code.
159
*   Depends: none.
188
*   Depends: none.
160
*/
189
*/
161
void canParse(CAN_MESSAGE cm)
190
void canParse(CAN_PACKET cp)
162
{
191
{
163
    CAN_MESSAGE outCm;
-
 
-
 
192
 
164
}
193
}
165
 
194
 
166
 
195
 
167
// Below are mandantory when using bootloader
196
// Below are mandantory when using bootloader
168
// define DEBUG_MODE to use without bootloader.
197
// define DEBUG_MODE to use without bootloader.
169
 
198
 
170
#ifndef DEBUG_MODE
199
#ifndef DEBUG_MODE
171
extern void _startup (void);
200
extern void _startup (void);
172
#pragma code _RESET_INTERRUPT_VECTOR = 0x001000
201
#pragma code _RESET_INTERRUPT_VECTOR = RM_RESET_VECTOR
173
void _reset (void)
202
void _reset (void)
174
{
203
{
175
    _asm goto _startup _endasm
204
    _asm goto _startup _endasm
176
}
205
}
177
#pragma code
206
#pragma code
178
#endif
207
#endif
179
 
208
 
180
#pragma code _HIGH_INTERRUPT_VECTOR = 0x001008
209
#pragma code _HIGH_INTERRUPT_VECTOR = RM_HIGH_INTERRUPT_VECTOR
181
void _high_ISR (void)
210
void _high_ISR (void)
182
{
211
{
183
    _asm GOTO high_isr _endasm
212
    _asm GOTO high_isr _endasm
184
}
213
}
185
#pragma code
214
#pragma code
186
 
215
 
187
#pragma code _LOW_INTERRUPT_VECTOR = 0x001018
216
#pragma code _LOW_INTERRUPT_VECTOR = RM_LOW_INTERRUPT_VECTOR
188
void _low_ISR (void)
217
void _low_ISR (void)
189
{
218
{
190
    _asm GOTO low_isr _endasm
219
    _asm GOTO low_isr _endasm
191
}
220
}
192
#pragma code
221
#pragma code