Subversion Repositories HomeAutomation

Rev

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

Rev 129 Rev 141
Line 2... Line 2...
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: 2006-11-20 17:54:03 +0100 (Mon, 20 Nov 2006) $
7
 * Last changed:    $LastChangedDate: 2006-11-21 10:57:15 +0100 (Tue, 21 Nov 2006) $
8
 * By:              $LastChangedBy: johboh $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 129 $
9
 * Revision:        $Revision: 141 $
10
 ********************************************************************/
10
 ********************************************************************/
11
 
11
 
12
 
12
 
13
#include <compiler.h>
13
#include <compiler.h>
14
#include <stackTasks.h>
14
#include <stackTasks.h>
Line 18... Line 18...
18
 
18
 
19
#ifdef USE_CAN
19
#ifdef USE_CAN
20
    #include <CAN.h>
20
    #include <CAN.h>
21
#endif
21
#endif
22
 
22
 
-
 
23
#ifdef USE_ADC
-
 
24
    #include <adc.h>
-
 
25
#endif
-
 
26
 
23
static void mainInit(void);
27
static void mainInit(void);
24
 
28
 
25
 
29
 
26
static int MY_ID = DEFAULT_ID;
30
static int MY_ID = DEFAULT_ID;
27
static BYTE MY_NID = DEFAULT_NID;
31
static BYTE MY_NID = DEFAULT_NID;
28
 
32
 
Line 30... Line 34...
30
{
34
{
31
    static TICK t = 0;
35
    static TICK t = 0;
32
    CAN_MESSAGE outCm;
36
    CAN_MESSAGE outCm;
33
 
37
 
34
    // Inits
38
    // Inits
35
 
39
 
36
    mainInit();
40
    mainInit();
37
 
41
 
38
    #ifdef USE_CAN
42
    #ifdef USE_CAN
39
        canInit();
43
        canInit();
-
 
44
    #endif
-
 
45
 
-
 
46
    #ifdef USE_ADC
-
 
47
        adcInit(TRUE,0b1011);
40
    #endif
48
    #endif
41
 
49
 
42
    tickInit();
50
    tickInit();
43
 
51
 
44
    // Read ID and NID if exist.
52
    // Read ID and NID if exist.
Line 53... Line 61...
53
    outCm.funcc                     = FUNCC_BOOT_HEARTBEAT;
61
    outCm.funcc                     = FUNCC_BOOT_HEARTBEAT;
54
    outCm.nid                       = MY_NID;
62
    outCm.nid                       = MY_NID;
55
    outCm.sid                       = MY_ID;
63
    outCm.sid                       = MY_ID;
56
    outCm.data_length               = 1;
64
    outCm.data_length               = 1;
57
    outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_USER_STARTUP;
65
    outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_USER_STARTUP;
58
    while(!canSendMessage(outCm,PRIO_HIGH));
66
    while(!canSendMessage(outCm,PRIO_HIGH));
59
 
67
 
60
 
68
 
61
    while(1)
69
    while(1)
62
    {
70
    {
63
        static TICK t = 0;
71
        static TICK t = 0;
64
        static TICK heartbeat = 0;
72
        static TICK heartbeat = 0;
-
 
73
        static TICK temperature = 0;
-
 
74
        static BYTE lastTemperature = TEMPERATURE_FREEZER;
-
 
75
        static BYTE lastFreezerDoor = DOOR_CLOSED;
-
 
76
        static BYTE lastRefrigeratorDoor = DOOR_CLOSED;
-
 
77
 
-
 
78
 
-
 
79
        //Poll doors freezer door
-
 
80
        if (lastFreezerDoor!=DOOR_FREEZER_IO)
-
 
81
        {  
-
 
82
            lastFreezerDoor=DOOR_FREEZER_IO;
-
 
83
            // Send door status change
-
 
84
            outCm.funct                     = FUNCT_SENSORS;
-
 
85
            outCm.funcc                     = FUNCC_SENSORS_DOORS_FREEZER;
-
 
86
            outCm.nid                       = MY_NID;
-
 
87
            outCm.sid                       = MY_ID;
-
 
88
            outCm.data_length               = 1;
-
 
89
            outCm.data[0]                   = (lastFreezerDoor==DOOR_OPEN?0:1);
-
 
90
            while(!canSendMessage(outCm,PRIO_HIGH));
-
 
91
        }
-
 
92
 
-
 
93
        //Poll doors refrigerator door
-
 
94
        if (lastRefrigeratorDoor!=DOOR_REFRIGERATOR_IO)
-
 
95
        {  
-
 
96
            lastRefrigeratorDoor=DOOR_REFRIGERATOR_IO;
-
 
97
            // Send door status change
-
 
98
            outCm.funct                     = FUNCT_SENSORS;
-
 
99
            outCm.funcc                     = FUNCC_SENSORS_DOORS_REFRIGERATOR;
-
 
100
            outCm.nid                       = MY_NID;
-
 
101
            outCm.sid                       = MY_ID;
-
 
102
            outCm.data_length               = 1;
-
 
103
            outCm.data[0]                   = (lastRefrigeratorDoor==DOOR_OPEN?0:1);
-
 
104
            while(!canSendMessage(outCm,PRIO_HIGH));
-
 
105
        }
-
 
106
 
65
 
107
       
66
       
-
 
67
        if ((tickGet()-heartbeat)>TICK_SECOND*5)
108
        if ((tickGet()-heartbeat)>TICK_SECOND*5)
68
        {
109
        {
69
            // Send alive heartbeat
110
            // Send alive heartbeat
70
            outCm.funct                     = FUNCT_BOOTLOADER;
111
            outCm.funct                     = FUNCT_BOOTLOADER;
71
            outCm.funcc                     = FUNCC_BOOT_HEARTBEAT;
112
            outCm.funcc                     = FUNCC_BOOT_HEARTBEAT;
72
            outCm.nid                       = MY_NID;
113
            outCm.nid                       = MY_NID;
73
            outCm.sid                       = MY_ID;
114
            outCm.sid                       = MY_ID;
74
            outCm.data_length               = 1;
115
            outCm.data_length               = 1;
75
            outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_ALIVE;
116
            outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_ALIVE;
76
            while(!canSendMessage(outCm,PRIO_HIGH));
117
            while(!canSendMessage(outCm,PRIO_HIGH));
77
           
118
           
78
            heartbeat = tickGet();
119
            heartbeat = tickGet();
79
        }
120
        }
-
 
121
 
80
        if ((tickGet()-t)>TICK_SECOND)
122
        if ((tickGet()-t)>TICK_SECOND)
81
        {
123
        {
82
            LED0_IO=~LED0_IO;
124
            LED0_IO=~LED0_IO;
83
            t = tickGet();
125
            t = tickGet();
84
        }
126
        }
-
 
127
 
-
 
128
        #ifdef USE_ADC
-
 
129
        if ((tickGet()-temperature)>TICK_SECOND*2) // Each 2 second, read temperature.
-
 
130
        {
-
 
131
            if (lastTemperature==TEMPERATURE_FREEZER) lastTemperature=TEMPERATURE_REFRIGERATOR; else lastTemperature=TEMPERATURE_FREEZER;
-
 
132
            adcConvert(lastTemperature);
-
 
133
            temperature = tickGet();
-
 
134
        }
-
 
135
        #endif
-
 
136
 
-
 
137
 
-
 
138
        #ifdef USE_ADC
-
 
139
        if (adcDone()) // Has temperature, send it to the bus
-
 
140
        {
-
 
141
            BYTE decimal;
-
 
142
            signed char tenth;
-
 
143
            temperatureRead(&tenth,&decimal);
-
 
144
 
-
 
145
            outCm.funct                     = FUNCT_SENSORS;
-
 
146
            outCm.funcc                     = (lastTemperature==TEMPERATURE_FREEZER?FUNCC_SENSORS_TEMPERATURE_FREEZER:FUNCC_SENSORS_TEMPERATURE_REFRIGERATOR);
-
 
147
            outCm.nid                       = MY_NID;
-
 
148
            outCm.sid                       = MY_ID;
-
 
149
            outCm.data_length               = 2;
-
 
150
            outCm.data[1]                   = tenth; //  signed 10 an 1 decimal.
-
 
151
            outCm.data[0]                   = decimal; //  Decimal value, 0-9
-
 
152
            while(!canSendMessage(outCm,PRIO_HIGH));
-
 
153
        }
-
 
154
        #endif
-
 
155
 
85
    }
156
    }
86
}
157
}
87
 
158
 
88
 
159
 
89
#pragma interrupt high_isr
160
#pragma interrupt high_isr
90
void high_isr(void)
161
void high_isr(void)
91
{
162
{
92
    ClrWdt();
163
    ClrWdt();
93
 
164
 
94
    #ifdef USE_CAN
165
    #ifdef USE_CAN
95
        canISR();
166
        canISR();
96
    #endif
167
    #endif
97
 
-
 
98
    tickUpdate();
-
 
99
 
168
 
-
 
169
    tickUpdate();
-
 
170
 
100
}
171
}
101
 
172
 
102
 
173
 
103
#pragma interrupt low_isr
174
#pragma interrupt low_isr
104
void low_isr(void)
175
void low_isr(void)
105
{
176
{
-
 
177
    #ifdef USE_ADC
-
 
178
        adcISR();
106
 
179
    #endif
107
}
180
}
108
 
181
 
109
 
182
 
110
 
183
 
111
/*
184
/*
112
*   Function: mainInit
185
*   Function: mainInit
113
*
186
*
114
*   Input:  none
187
*   Input:  none
115
*   Output: none
188
*   Output: none
Line 118... Line 191...
118
*   Depends: none.
191
*   Depends: none.
119
*/
192
*/
120
void mainInit()
193
void mainInit()
121
{
194
{
122
    LED0_TRIS=0;   
195
    LED0_TRIS=0;   
-
 
196
 
-
 
197
    DOOR_FREEZER_TRIS=1;
-
 
198
    DOOR_REFRIGERATOR_TRIS=1;
123
 
199
 
124
    // Enable Interrupts
200
    // Enable Interrupts
125
    INTCONbits.GIEH = 1;
201
    INTCONbits.GIEH = 1;
126
    INTCONbits.GIEL = 1;
202
    INTCONbits.GIEL = 1;
127
 
203