Subversion Repositories HomeAutomation

Rev

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

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