Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

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