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