Subversion Repositories HomeAutomation

Rev

Rev 347 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 347 Rev 353
Line 2... Line 2...
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-03-01 14:38:06 +0100 (Thu, 01 Mar 2007) $
7
 * Last changed:    $LastChangedDate: 2007-03-03 19:07:14 +0100 (Sat, 03 Mar 2007) $
8
 * By:              $LastChangedBy: johboh $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 347 $
9
 * Revision:        $Revision: 353 $
10
 ********************************************************************/
10
 ********************************************************************/
11
 
-
 
12
 
11
 
13
#include <compiler.h>
12
#include <compiler.h>
14
#include <stackTasks.h>
13
#include <stackTasks.h>
15
#include <Tick.h>
14
#include <Tick.h>
16
#include <funcdefs.h>
15
#include <funcdefs.h>
17
#include <CAN.h>
16
#include <CAN.h>
18
#include "..\canBoot\Include\boot.h"
17
#include "..\canBoot\Include\boot.h"
19
 
-
 
20
 
18
 
21
#ifdef USE_ADC
19
#ifdef USE_ADC
22
    #include <adc.h>
20
    #include <adc.h>
23
#endif
21
#endif
24
 
22
 
25
#ifdef USE_BLINDS
23
#ifdef USE_BLINDS
Line 27... Line 25...
27
#endif
25
#endif
28
 
26
 
29
static void mainInit(void);
27
static void mainInit(void);
30
 
28
 
31
void main()
29
void main()
32
{
30
{
33
 
-
 
34
    static TICK t = 0;
-
 
35
    CAN_PACKET outCp;
-
 
36
 
-
 
37
    // Inits
31
    // Inits
38
    mainInit();
32
    mainInit();
39
 
33
 
40
    canInit();
34
    canInit();
41
 
35
 
42
    #ifdef USE_ADC
36
    #ifdef USE_ADC
43
        adcInit(TRUE,0b1011);
37
        adcInit(TRUE,0b1011);
44
    #endif
38
    #endif
45
 
39
 
46
    #ifdef USE_BLINDS
40
    #ifdef USE_BLINDS
47
        blindsInit();
41
        blindsInit();
48
        blindsTurn(0);
42
        blindsTurn(0);
49
    #else
43
    #else
50
        BLINDS0P_IO=0;
44
        BLINDS0P_IO=0;
51
    #endif
45
    #endif
52
 
46
 
53
 
47
 
54
    while(1)
48
    while(1)
55
    {
49
    {
-
 
50
        TICK currentTick = tickGet();
-
 
51
 
56
        static TICK t = 0;
52
        static TICK tick_led = 0;
57
        static TICK temperature = 0;
53
        static TICK tick_temperature = 0;
-
 
54
        static TICK tick_blinds = 0;   
-
 
55
 
58
        static BOOL getNewTemperature = TRUE;
56
        static BOOL getNewTemperature = TRUE;
59
        static BYTE lastTemperature = TEMPERATURE_INSIDE;
57
        static BYTE lastTemperature = TEMPERATURE_INSIDE;
60
        static TICK blinds = 0;
-
 
61
       
58
       
-
 
59
 
62
        if ((tickGet()-t)>TICK_SECOND)
60
        if ((currentTick-tick_led)>(1*TICK_1S))
63
        {
61
        {
64
            LED0_IO=~LED0_IO;
62
            LED0_IO=~LED0_IO;
65
            t = tickGet();
63
            tick_led = currentTick;
66
        }
64
        }
67
 
65
 
68
       
66
       
69
        #ifdef USE_BLINDS
67
        #ifdef USE_BLINDS
70
        if ((tickGet()-blinds)>3*TICK_SECOND)
68
        if ((currentTick-tick_blinds)>(3*TICK_1S))
71
        {
69
        {
-
 
70
            CAN_PACKET outCp;
72
            outCp.type=ptPGM;
71
            outCp.type=ptPGM;
73
            outCp.pgm.class=pcSENSOR;
72
            outCp.pgm.class=pcSENSOR;
74
            outCp.pgm.id=pstBLIND_TR;
73
            outCp.pgm.id=pstBLIND_TR;
75
            outCp.length=1;
74
            outCp.length=1;
76
            outCp.data[0]=blindsGetPrecent();
75
            outCp.data[0]=blindsGetPrecent();
77
            while(!canSendMessage(outCp,PRIO_HIGH));
76
            while(!canSendMessage(outCp,PRIO_HIGH));
-
 
77
 
78
            blinds = tickGet();
78
            tick_blinds = currentTick;         
79
        }
79
        }
80
        #endif
80
        #endif
81
       
81
       
82
 
82
 
83
        #ifdef USE_ADC
83
        #ifdef USE_ADC
84
        if ((tickGet()-temperature)>TICK_SECOND*2 && getNewTemperature==TRUE) // Each 2 second, read temperature.
84
        if ((currentTick-tick_temperature)>(2*TICK_1S) && getNewTemperature==TRUE) // Each 2 second, read temperature.
85
        {
85
        {
86
            if (lastTemperature==TEMPERATURE_INSIDE) lastTemperature=TEMPERATURE_OUTSIDE; else lastTemperature=TEMPERATURE_INSIDE;
86
            if (lastTemperature==TEMPERATURE_INSIDE) lastTemperature=TEMPERATURE_OUTSIDE; else lastTemperature=TEMPERATURE_INSIDE;
87
            adcConvert(lastTemperature);
87
            adcConvert(lastTemperature);
88
            temperature = tickGet();
-
 
89
            getNewTemperature=FALSE;
88
            getNewTemperature=FALSE;
-
 
89
            tick_temperature = currentTick;
90
        }
90
        }
91
        #endif
91
        #endif
92
 
92
 
93
 
93
 
94
        #ifdef USE_ADC
94
        #ifdef USE_ADC
95
        if (adcDone()) // Has temperature, send it to the bus
95
        if (adcDone()) // Has temperature, send it to the bus
96
        {
96
        {
-
 
97
            CAN_PACKET outCp;
97
            BYTE decimal;
98
            BYTE decimal;
98
            signed char tenth;
99
            signed char tenth;
99
            unsigned long q;
-
 
-
 
100
 
100
            temperatureRead(&tenth,&decimal);
101
            temperatureRead(&tenth,&decimal);
101
            q = adcGetRaw();
-
 
102
            getNewTemperature=TRUE;
-
 
103
 
102
           
104
            outCp.type=ptPGM;
103
            outCp.type=ptPGM;
105
            outCp.pgm.class =pcSENSOR;
104
            outCp.pgm.class =pcSENSOR;
106
            outCp.pgm.id    =(lastTemperature==TEMPERATURE_INSIDE?pstTEMP_INSIDE:pstTEMP_OUTSIDE);
105
            outCp.pgm.id    =(lastTemperature==TEMPERATURE_INSIDE?pstTEMP_INSIDE:pstTEMP_OUTSIDE);
107
            outCp.length    =2;
106
            outCp.length    =2;
108
            outCp.data[1]   = tenth; //  signed 10 an 1 decimal.
107
            outCp.data[1]   = tenth; //  signed 10 an 1 decimal.
109
            outCp.data[0]   = decimal; //  Decimal value, 0-9
108
            outCp.data[0]   = decimal; //  Decimal value, 0-9
110
            while(!canSendMessage(outCp,PRIO_HIGH));
109
            while(!canSendMessage(outCp,PRIO_HIGH));
-
 
110
 
-
 
111
            getNewTemperature=TRUE;
-
 
112
            tick_temperature = currentTick;
111
        }
113
        }
112
        #endif
114
        #endif
113
 
115
   
114
    }
116
    } // end while
115
}
117
   
-
 
118
} // end main
116
 
119
 
117
 
120
 
118
#pragma interrupt high_isr
121
#pragma interrupt high_isr
119
void high_isr(void)
122
void high_isr(void)
120
{
123
{
121
    canISR();
124
    canISR();
122
}
-
 
123
 
125
 
124
 
-
 
125
#pragma interrupt low_isr
126
    #ifdef USE_BLINDS
126
void low_isr(void)
127
        blindsISR();
-
 
128
    #endif
127
{
129
 
128
    #ifdef USE_ADC
130
    #ifdef USE_ADC
129
        adcISR();
131
        adcISR();
130
    #endif
132
    #endif
-
 
133
}
-
 
134
 
-
 
135
 
-
 
136
#pragma interrupt low_isr
-
 
137
void low_isr(void)
-
 
138
{
131
 
139
 
132
    #ifdef USE_BLINDS
-
 
133
        blindsISR();
-
 
134
    #endif
-
 
135
}
140
}
136
 
141
 
137
 
142
 
138
 
143
 
139
/*
144
/*
Line 148... Line 153...
148
void mainInit()
153
void mainInit()
149
{
154
{
150
    LED0_TRIS=0;
155
    LED0_TRIS=0;
151
    BLINDS0_TRIS=0;
156
    BLINDS0_TRIS=0;
152
    BLINDS0P_TRIS=0;
157
    BLINDS0P_TRIS=0;
-
 
158
 
-
 
159
    // Enable interrupt prirority
-
 
160
    RCONbits.IPEN=1;
153
 
161
 
154
    // Enable Interrupts
162
    // Enable Interrupts
155
    INTCONbits.GIEL = 1;
163
    INTCONbits.GIEL = 1;
156
    INTCONbits.GIEH = 1;
164
    INTCONbits.GIEH = 1;
157
   
-
 
158
 
-
 
159
    // Enable interrupt prirority
-
 
160
    RCONbits.IPEN=1;
-
 
161
}
165
}
162
 
166
 
163
/*
167
/*
164
*   Function: canParse
168
*   Function: canParse
165
*
169
*
Line 181... Line 185...
181
}
185
}
182
 
186
 
183
 
187
 
184
// Below are mandantory when using bootloader
188
// Below are mandantory when using bootloader
185
// define DEBUG_MODE to use without bootloader.
189
// define DEBUG_MODE to use without bootloader.
-
 
190
 
-
 
191
 
186
 
192
 
187
#ifndef DEBUG_MODE
193
#ifndef DEBUG_MODE
188
extern void _startup (void);
194
extern void _startup (void);
189
#pragma code _RESET_INTERRUPT_VECTOR = RM_RESET_VECTOR
195
#pragma code _RESET_INTERRUPT_VECTOR = RM_RESET_VECTOR
190
void _reset (void)
196
void _reset (void)