Subversion Repositories HomeAutomation

Rev

Rev 339 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 339 Rev 342
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-02-25 20:51:08 +0100 (Sun, 25 Feb 2007) $
7
 * Last changed:    $LastChangedDate: 2007-02-26 21:38:37 +0100 (Mon, 26 Feb 2007) $
8
 * By:              $LastChangedBy: johboh $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 339 $
9
 * Revision:        $Revision: 342 $
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
#include "..\canBoot\Include\boot.h"
18
#include "..\canBoot\Include\boot.h"
19
 
19
 
20
 
20
 
21
#ifdef USE_ADC
21
#ifdef USE_ADC
22
    #include <adc.h>
22
    #include <adc.h>
23
#endif
23
#endif
24
 
24
 
25
#ifdef USE_BLINDS
25
#ifdef USE_BLINDS
26
    #include <blinds.h>
26
    #include <blinds.h>
27
#endif
27
#endif
28
 
28
 
29
static void mainInit(void);
29
static void mainInit(void);
30
 
30
 
31
void main()
31
void main()
32
{
32
{
33
 
33
 
34
    static TICK t = 0;
34
    static TICK t = 0;
35
    CAN_PACKET outCp;
35
    CAN_PACKET outCp;
36
 
36
 
37
    // Inits
37
    // Inits
38
    mainInit();
38
    mainInit();
39
 
39
 
40
    canInit();
40
    canInit();
41
 
41
 
42
    #ifdef USE_ADC
42
    #ifdef USE_ADC
43
        adcInit(TRUE,0b1011);
43
        adcInit(TRUE,0b1011);
44
    #endif
44
    #endif
45
 
45
 
46
    #ifdef USE_BLINDS
46
    #ifdef USE_BLINDS
47
        blindsInit();
47
        blindsInit();
48
        blindsTurn(0);
48
        blindsTurn(0);
49
    #else
49
    #else
50
        BLINDS0P_IO=0;
50
        BLINDS0P_IO=0;
51
    #endif
51
    #endif
52
 
52
 
53
 
53
 
54
    while(1)
54
    while(1)
55
    {
55
    {
56
        static TICK t = 0;
56
        static TICK t = 0;
57
        static TICK temperature = 0;
57
        static TICK temperature = 0;
58
        static BOOL getNewTemperature = TRUE;
58
        static BOOL getNewTemperature = TRUE;
59
        static BYTE lastTemperature = TEMPERATURE_INSIDE;
59
        static BYTE lastTemperature = TEMPERATURE_INSIDE;
60
        static TICK blinds = 0;
60
        static TICK blinds = 0;
61
       
61
       
62
        if ((tickGet()-t)>TICK_SECOND)
62
        if ((tickGet()-t)>TICK_SECOND)
63
        {
63
        {
64
            LED0_IO=~LED0_IO;
64
            LED0_IO=~LED0_IO;
65
            t = tickGet();
65
            t = tickGet();
66
        }
66
        }
67
 
67
 
68
       
68
       
69
        #ifdef USE_BLINDS
69
        #ifdef USE_BLINDS
70
        if ((tickGet()-blinds)>3*TICK_SECOND)
70
        if ((tickGet()-blinds)>3*TICK_SECOND)
71
        {
71
        {
72
            outCp.type=ptPGM;
72
            outCp.type=ptPGM;
73
            outCp.pgm.class=pcSENSOR;
73
            outCp.pgm.class=pcSENSOR;
74
            outCp.pgm.id=pstBLIND_TR;
74
            outCp.pgm.id=pstBLIND_TR;
75
            outCp.length=1;
75
            outCp.length=1;
76
            outCp.data[0]=blindsGetPrecent();
76
            outCp.data[0]=blindsGetPrecent();
77
            while(!canSendMessage(outCp,PRIO_HIGH));
77
            while(!canSendMessage(outCp,PRIO_HIGH));
78
            blinds = tickGet();
78
            blinds = tickGet();
79
        }
79
        }
80
        #endif
80
        #endif
81
       
81
       
82
 
82
 
83
        #ifdef USE_ADC
83
        #ifdef USE_ADC
84
        if ((tickGet()-temperature)>TICK_SECOND*2 && getNewTemperature==TRUE) // Each 2 second, read temperature.
84
        if ((tickGet()-temperature)>TICK_SECOND*2 && 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
            temperature = tickGet();
88
            temperature = tickGet();
89
            getNewTemperature=FALSE;
89
            getNewTemperature=FALSE;
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
            BYTE decimal;
97
            BYTE decimal;
98
            signed char tenth;
98
            signed char tenth;
99
            unsigned long q;
99
            unsigned long q;
100
            temperatureRead(&tenth,&decimal);
100
            temperatureRead(&tenth,&decimal);
101
            q = adcGetRaw();
101
            q = adcGetRaw();
102
            getNewTemperature=TRUE;
102
            getNewTemperature=TRUE;
103
 
103
 
104
            outCp.type=ptPGM;
104
            outCp.type=ptPGM;
105
            outCp.pgm.class =pcSENSOR;
105
            outCp.pgm.class =pcSENSOR;
106
            outCp.pgm.id    =(lastTemperature==TEMPERATURE_INSIDE?pstTEMP_INSIDE:pstTEMP_OUTSIDE);
106
            outCp.pgm.id    =(lastTemperature==TEMPERATURE_INSIDE?pstTEMP_INSIDE:pstTEMP_OUTSIDE);
107
            outCp.length    =2;
107
            outCp.length    =2;
108
            outCm.data[1]   = tenth; //  signed 10 an 1 decimal.
108
            outCp.data[1]   = tenth; //  signed 10 an 1 decimal.
109
            outCm.data[0]   = decimal; //  Decimal value, 0-9
109
            outCp.data[0]   = decimal; //  Decimal value, 0-9
110
            while(!canSendMessage(outCm,PRIO_HIGH));
110
            while(!canSendMessage(outCp,PRIO_HIGH));
111
        }
111
        }
112
        #endif
112
        #endif
113
 
113
 
114
    }
114
    }
115
}
115
}
116
 
116
 
117
 
117
 
118
#pragma interrupt high_isr
118
#pragma interrupt high_isr
119
void high_isr(void)
119
void high_isr(void)
120
{
120
{
121
    canISR();
121
    canISR();
122
}
122
}
123
 
123
 
124
 
124
 
125
#pragma interrupt low_isr
125
#pragma interrupt low_isr
126
void low_isr(void)
126
void low_isr(void)
127
{
127
{
128
    #ifdef USE_ADC
128
    #ifdef USE_ADC
129
        adcISR();
129
        adcISR();
130
    #endif
130
    #endif
131
 
131
 
132
    #ifdef USE_BLINDS
132
    #ifdef USE_BLINDS
133
        blindsISR();
133
        blindsISR();
134
    #endif
134
    #endif
135
}
135
}
136
 
136
 
137
 
137
 
138
 
138
 
139
/*
139
/*
140
*   Function: mainInit
140
*   Function: mainInit
141
*
141
*
142
*   Input:  none
142
*   Input:  none
143
*   Output: none
143
*   Output: none
144
*   Pre-conditions: none
144
*   Pre-conditions: none
145
*   Affects: Se code
145
*   Affects: Se code
146
*   Depends: none.
146
*   Depends: none.
147
*/
147
*/
148
void mainInit()
148
void mainInit()
149
{
149
{
150
    LED0_TRIS=0;
150
    LED0_TRIS=0;
151
    BLINDS0_TRIS=0;
151
    BLINDS0_TRIS=0;
152
    BLINDS0P_TRIS=0;
152
    BLINDS0P_TRIS=0;
153
 
153
 
154
    // Enable Interrupts
154
    // Enable Interrupts
155
    INTCONbits.GIEH = 1;
155
    INTCONbits.GIEH = 1;
156
    INTCONbits.GIEL = 1;
156
    INTCONbits.GIEL = 1;
157
 
157
 
158
    // Enable interrupt prirority
158
    // Enable interrupt prirority
159
    RCONbits.IPEN=1;
159
    RCONbits.IPEN=1;
160
}
160
}
161
 
161
 
162
/*
162
/*
163
*   Function: canParse
163
*   Function: canParse
164
*
164
*
165
*   Input:  Received can message
165
*   Input:  Received can message
166
*   Output: none
166
*   Output: none
167
*   Pre-conditions: canInit and received packet.
167
*   Pre-conditions: canInit and received packet.
168
*   Affects: Sensors/actuators/etc. See code.
168
*   Affects: Sensors/actuators/etc. See code.
169
*   Depends: none.
169
*   Depends: none.
170
*/
170
*/
171
 
171
 
172
void canParse(CAN_PACKET cp)
172
void canParse(CAN_PACKET cp)
173
{
173
{
174
    #ifdef USE_BLINDS
174
    #ifdef USE_BLINDS
175
    if (cp.pgm.class==pcACTUATOR && cp.pgm.id==patBLIND_TR)
175
    if (cp.pgm.class==pcACTUATOR && cp.pgm.id==patBLIND_TR)
176
    {
176
    {
177
        blindsTurn(cp.data[0]);
177
        blindsTurn(cp.data[0]);
178
    }
178
    }
179
    #endif
179
    #endif
180
}
180
}
181
 
181
 
182
 
182
 
183
// Below are mandantory when using bootloader
183
// Below are mandantory when using bootloader
184
// define DEBUG_MODE to use without bootloader.
184
// define DEBUG_MODE to use without bootloader.
185
 
185
 
186
#ifndef DEBUG_MODE
186
#ifndef DEBUG_MODE
187
extern void _startup (void);
187
extern void _startup (void);
188
#pragma code _RESET_INTERRUPT_VECTOR = RM_RESET_VECTOR
188
#pragma code _RESET_INTERRUPT_VECTOR = RM_RESET_VECTOR
189
void _reset (void)
189
void _reset (void)
190
{
190
{
191
    _asm goto _startup _endasm
191
    _asm goto _startup _endasm
192
}
192
}
193
#pragma code
193
#pragma code
194
#endif
194
#endif
195
 
195
 
196
#pragma code _HIGH_INTERRUPT_VECTOR = RM_HIGH_INTERRUPT_VECTOR
196
#pragma code _HIGH_INTERRUPT_VECTOR = RM_HIGH_INTERRUPT_VECTOR
197
void _high_ISR (void)
197
void _high_ISR (void)
198
{
198
{
199
    _asm GOTO high_isr _endasm
199
    _asm GOTO high_isr _endasm
200
}
200
}
201
#pragma code
201
#pragma code
202
 
202
 
203
#pragma code _LOW_INTERRUPT_VECTOR = RM_LOW_INTERRUPT_VECTOR
203
#pragma code _LOW_INTERRUPT_VECTOR = RM_LOW_INTERRUPT_VECTOR
204
void _low_ISR (void)
204
void _low_ISR (void)
205
{
205
{
206
    _asm GOTO low_isr _endasm
206
    _asm GOTO low_isr _endasm
207
}
207
}
208
#pragma code
208
#pragma code
209
 
209