Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
129 johboh 1
/*********************************************************************
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-01-28 14:58:11 +0100 (Sun, 28 Jan 2007) $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 260 $
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>
129 johboh 18
 
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
    static TICK t = 0;
30
    CAN_MESSAGE outCm;
31
 
32
    // Inits
33
    mainInit();
260 johboh 34
    canInit();
129 johboh 35
 
141 johboh 36
    #ifdef USE_ADC
37
        adcInit(TRUE,0b1011);
38
    #endif
39
 
129 johboh 40
    while(1)
41
    {
42
        static TICK t = 0;
141 johboh 43
        static TICK temperature = 0;
44
        static BYTE lastTemperature = TEMPERATURE_FREEZER;
45
        static BYTE lastFreezerDoor = DOOR_CLOSED;
46
        static BYTE lastRefrigeratorDoor = DOOR_CLOSED;
129 johboh 47
 
141 johboh 48
 
49
        //Poll doors freezer door
50
        if (lastFreezerDoor!=DOOR_FREEZER_IO)
51
        {  
52
            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;
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;
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
        }
72
 
129 johboh 73
        if ((tickGet()-t)>TICK_SECOND)
74
        {
75
            LED0_IO=~LED0_IO;
76
            t = tickGet();
77
        }
141 johboh 78
 
79
        #ifdef USE_ADC
80
        if ((tickGet()-temperature)>TICK_SECOND*2) // Each 2 second, read temperature.
81
        {
82
            if (lastTemperature==TEMPERATURE_FREEZER) lastTemperature=TEMPERATURE_REFRIGERATOR; else lastTemperature=TEMPERATURE_FREEZER;
83
            adcConvert(lastTemperature);
84
            temperature = tickGet();
85
        }
86
        #endif
87
 
88
 
89
        #ifdef USE_ADC
90
        if (adcDone()) // Has temperature, send it to the bus
91
        {
92
            BYTE decimal;
93
            signed char tenth;
94
            temperatureRead(&tenth,&decimal);
95
 
96
            outCm.funct                     = FUNCT_SENSORS;
97
            outCm.funcc                     = (lastTemperature==TEMPERATURE_FREEZER?FUNCC_SENSORS_TEMPERATURE_FREEZER:FUNCC_SENSORS_TEMPERATURE_REFRIGERATOR);
98
            outCm.data_length               = 2;
99
            outCm.data[1]                   = tenth; //  signed 10 an 1 decimal.
100
            outCm.data[0]                   = decimal; //  Decimal value, 0-9
101
            while(!canSendMessage(outCm,PRIO_HIGH));
102
        }
103
        #endif
104
 
129 johboh 105
    }
106
}
107
 
108
 
109
#pragma interrupt high_isr
110
void high_isr(void)
111
{
260 johboh 112
    canISR();
129 johboh 113
}
114
 
115
 
116
#pragma interrupt low_isr
117
void low_isr(void)
118
{
141 johboh 119
    #ifdef USE_ADC
120
        adcISR();
121
    #endif
129 johboh 122
}
123
 
124
 
125
 
126
/*
127
*   Function: mainInit
128
*
129
*   Input:  none
130
*   Output: none
131
*   Pre-conditions: none
132
*   Affects: Se code
133
*   Depends: none.
134
*/
135
void mainInit()
136
{
137
    LED0_TRIS=0;   
138
 
141 johboh 139
    DOOR_FREEZER_TRIS=1;
140
    DOOR_REFRIGERATOR_TRIS=1;
141
 
129 johboh 142
    // Enable Interrupts
143
    INTCONbits.GIEH = 1;
144
    INTCONbits.GIEL = 1;
145
 
146
    // Enable interrupt prirority
147
    RCONbits.IPEN=1;
148
 
149
 
150
}
151
 
152
/*
153
*   Function: canParse
154
*
155
*   Input:  Received can message
156
*   Output: none
157
*   Pre-conditions: canInit and received packet.
158
*   Affects: Sensors/actuators/etc. See code.
159
*   Depends: none.
160
*/
161
void canParse(CAN_MESSAGE cm)
162
{
163
    CAN_MESSAGE outCm;
164
}
165
 
166
 
167
// Below are mandantory when using bootloader
168
// define DEBUG_MODE to use without bootloader.
169
 
170
#ifndef DEBUG_MODE
171
extern void _startup (void);
172
#pragma code _RESET_INTERRUPT_VECTOR = 0x001000
173
void _reset (void)
174
{
175
    _asm goto _startup _endasm
176
}
177
#pragma code
178
#endif
179
 
180
#pragma code _HIGH_INTERRUPT_VECTOR = 0x001008
181
void _high_ISR (void)
182
{
183
    _asm GOTO high_isr _endasm
184
}
185
#pragma code
186
 
187
#pragma code _LOW_INTERRUPT_VECTOR = 0x001018
188
void _low_ISR (void)
189
{
190
    _asm GOTO low_isr _endasm
191
}
192
#pragma code