Subversion Repositories HomeAutomation

Rev

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

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