Subversion Repositories HomeAutomation

Rev

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

Rev 129 Rev 139
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: 2006-11-20 17:54:03 +0100 (Mon, 20 Nov 2006) $
7
 * Last changed:    $LastChangedDate: 2006-11-20 21:31:03 +0100 (Mon, 20 Nov 2006) $
8
 * By:              $LastChangedBy: johboh $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 129 $
9
 * Revision:        $Revision: 139 $
10
 ********************************************************************/
10
 ********************************************************************/
11
 
11
 
12
 
12
 
13
#include <compiler.h>
13
#include <compiler.h>
14
#include <stackTasks.h>
14
#include <stackTasks.h>
Line 18... Line 18...
18
 
18
 
19
#ifdef USE_CAN
19
#ifdef USE_CAN
20
    #include <CAN.h>
20
    #include <CAN.h>
21
#endif
21
#endif
22
 
22
 
-
 
23
#ifdef USE_ADC
-
 
24
    #include <adc.h>
-
 
25
#endif
-
 
26
 
23
static void mainInit(void);
27
static void mainInit(void);
24
 
28
 
25
 
29
 
26
static int MY_ID = DEFAULT_ID;
30
static int MY_ID = DEFAULT_ID;
27
static BYTE MY_NID = DEFAULT_NID;
31
static BYTE MY_NID = DEFAULT_NID;
28
 
32
 
29
void main()
33
void main()
30
{
34
{
31
    static TICK t = 0;
35
    static TICK t = 0;
32
    CAN_MESSAGE outCm;
36
    CAN_MESSAGE outCm;
33
 
37
 
34
    // Inits
38
    // Inits
35
 
39
 
36
    mainInit();
40
    mainInit();
37
 
41
 
38
    #ifdef USE_CAN
42
    #ifdef USE_CAN
39
        canInit();
43
        canInit();
-
 
44
    #endif
-
 
45
 
-
 
46
    #ifdef USE_ADC
-
 
47
        adcInit(TRUE,0b1011);
40
    #endif
48
    #endif
41
 
49
 
42
    tickInit();
50
    tickInit();
43
 
51
 
44
    // Read ID and NID if exist.
52
    // Read ID and NID if exist.
45
    if (EERead(NODE_ID_EE)==NODE_HAS_ID)
53
    if (EERead(NODE_ID_EE)==NODE_HAS_ID)
46
    {
54
    {
47
        MY_ID=(((WORD)EERead(NODE_ID_EE + 1))<<8)+EERead(NODE_ID_EE + 2);
55
        MY_ID=(((WORD)EERead(NODE_ID_EE + 1))<<8)+EERead(NODE_ID_EE + 2);
48
        MY_NID=EERead(NODE_ID_EE + 3);
56
        MY_NID=EERead(NODE_ID_EE + 3);
49
    }
57
    }
50
 
58
 
51
    // Send user program startup heatbeat
59
    // Send user program startup heatbeat
52
    outCm.funct                     = FUNCT_BOOTLOADER;
60
    outCm.funct                     = FUNCT_BOOTLOADER;
53
    outCm.funcc                     = FUNCC_BOOT_HEARTBEAT;
61
    outCm.funcc                     = FUNCC_BOOT_HEARTBEAT;
54
    outCm.nid                       = MY_NID;
62
    outCm.nid                       = MY_NID;
55
    outCm.sid                       = MY_ID;
63
    outCm.sid                       = MY_ID;
56
    outCm.data_length               = 1;
64
    outCm.data_length               = 1;
57
    outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_USER_STARTUP;
65
    outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_USER_STARTUP;
58
    while(!canSendMessage(outCm,PRIO_HIGH));
66
    while(!canSendMessage(outCm,PRIO_HIGH));
59
 
67
 
60
 
68
 
61
    while(1)
69
    while(1)
62
    {
70
    {
63
        static TICK t = 0;
71
        static TICK t = 0;
64
        static TICK heartbeat = 0;
72
        static TICK heartbeat = 0;
-
 
73
        static TICK temperature = 0;
-
 
74
        static BYTE lastTemperature = TEMPERATURE_INSIDE;
65
 
75
 
66
       
76
       
67
        if ((tickGet()-heartbeat)>TICK_SECOND*5)
77
        if ((tickGet()-heartbeat)>TICK_SECOND*5)
68
        {
78
        {
69
            // Send alive heartbeat
79
            // Send alive heartbeat
Line 75... Line 85...
75
            outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_ALIVE;
85
            outCm.data[BOOT_DATA_HEARTBEAT_INDEX] = HEARTBEAT_ALIVE;
76
            while(!canSendMessage(outCm,PRIO_HIGH));
86
            while(!canSendMessage(outCm,PRIO_HIGH));
77
           
87
           
78
            heartbeat = tickGet();
88
            heartbeat = tickGet();
79
        }
89
        }
-
 
90
 
80
        if ((tickGet()-t)>TICK_SECOND)
91
        if ((tickGet()-t)>TICK_SECOND)
81
        {
92
        {
82
            LED0_IO=~LED0_IO;
93
            LED0_IO=~LED0_IO;
83
            t = tickGet();
94
            t = tickGet();
84
        }
95
        }
-
 
96
 
-
 
97
        #ifdef USE_ADC
-
 
98
        if ((tickGet()-temperature)>TICK_SECOND*2) // Each 2 second, read temperature.
-
 
99
        {
-
 
100
            if (lastTemperature==TEMPERATURE_INSIDE) lastTemperature=TEMPERATURE_OUTSIDE; else lastTemperature=TEMPERATURE_INSIDE;
-
 
101
            adcConvert(lastTemperature);
-
 
102
            temperature = tickGet();
-
 
103
        }
-
 
104
        #endif
-
 
105
 
-
 
106
 
-
 
107
        #ifdef USE_ADC
-
 
108
        if (adcDone()) // Has temperature, send it to the bus
-
 
109
        {
-
 
110
            BYTE decimal;
-
 
111
            signed char tenth;
-
 
112
            temperatureRead(&tenth,&decimal);
-
 
113
 
-
 
114
            outCm.funct                     = FUNCT_SENSORS;
-
 
115
            outCm.funcc                     = (lastTemperature==TEMPERATURE_INSIDE?FUNCC_SENSORS_TEMPERATURE_INSIDE:FUNCC_SENSORS_TEMPERATURE_OUTSIDE);
-
 
116
            outCm.nid                       = MY_NID;
-
 
117
            outCm.sid                       = MY_ID;
-
 
118
            outCm.data_length               = 2;
-
 
119
            outCm.data[1]                   = tenth; //  signed 10 an 1 decimal.
-
 
120
            outCm.data[0]                   = decimal; //  Decimal value, 0-9
-
 
121
            while(!canSendMessage(outCm,PRIO_HIGH));
-
 
122
        }
-
 
123
        #endif
-
 
124
 
85
    }
125
    }
86
}
126
}
87
 
127
 
88
 
128
 
89
#pragma interrupt high_isr
129
#pragma interrupt high_isr
Line 91... Line 131...
91
{
131
{
92
    ClrWdt();
132
    ClrWdt();
93
 
133
 
94
    #ifdef USE_CAN
134
    #ifdef USE_CAN
95
        canISR();
135
        canISR();
96
    #endif
136
    #endif
97
 
137
 
98
    tickUpdate();
138
    tickUpdate();
99
 
139
 
100
}
140
}
101
 
141
 
102
 
142
 
103
#pragma interrupt low_isr
143
#pragma interrupt low_isr
104
void low_isr(void)
144
void low_isr(void)
105
{
145
{
-
 
146
    #ifdef USE_ADC
-
 
147
        adcISR();
106
 
148
    #endif
107
}
149
}
108
 
150
 
109
 
151
 
110
 
152
 
111
/*
153
/*