Subversion Repositories HomeAutomation

Rev

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

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