Subversion Repositories HomeAutomation

Rev

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

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