Subversion Repositories HomeAutomation

Rev

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

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