Subversion Repositories HomeAutomation

Rev

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

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