Subversion Repositories HomeAutomation

Rev

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

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