Subversion Repositories HomeAutomation

Rev

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

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