Subversion Repositories HomeAutomation

Rev

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

Rev 245 Rev 255
Line 4... Line 4...
4
 * A general application for using multiple sensors.
4
 * A general application for using multiple sensors.
5
 * ie DS18S20, SHT71...
5
 * DS18S20, TC1047...
6
 *
6
 *
7
 * @target  AVR
7
 * @target  AVR
8
 * @date    2006-12-09
8
 * @date    2007-01-16
9
 * @author  Jimmy Myhrman, Anders Runeson, Erik Larsson
9
 * @author  Jimmy Myhrman, Anders Runeson, Erik Larsson
10
 *  
10
 *
11
 * Högst experimentell, decimaldel fel vid negativ temperatur?
-
 
12
 * negativ temperatur helt otestad!
-
 
13
 *
11
 * TODO: SHTxx
14
 */
12
 */
15
 
13
 
-
 
14
/*-----------------------------------------------------------------------------
16
//#define DEBUG
15
 * Defines
-
 
16
 * --------------------------------------------------------------------------*/
17
#define USE_DS18S20
17
#define USE_DS18S20
18
#define USE_SHT71
18
//#define USE_TC1047
-
 
19
 
-
 
20
#define SENSOR_SEND 0x1500
19
#define USE_TC1047
21
#define SENSOR_GET 0x1501
-
 
22
#define SENSOR_SEND_DS 0x1502
-
 
23
#define SENSOR_SEND_TC 0x1503
-
 
24
 
-
 
25
#define DS_SEND_PERIOD 5000
-
 
26
#define TC_SEND_PERIOD 2000
20
 
27
 
21
/*-----------------------------------------------------------------------------
28
/*-----------------------------------------------------------------------------
22
 * Includes
29
 * Includes
23
 *---------------------------------------------------------------------------*/
30
 *---------------------------------------------------------------------------*/
24
/* system files */
31
/* system files */
Line 28... Line 35...
28
#include <stdio.h>
35
#include <stdio.h>
29
/* lib files */
36
/* lib files */
30
#include <can.h>
37
#include <can.h>
31
#include <timebase.h>
38
#include <timebase.h>
32
 
39
 
33
#ifdef DEBUG
-
 
34
#include <uart.h>
-
 
35
#endif
-
 
36
 
-
 
37
#ifdef USE_DS18S20
40
#if defined(USE_DS18S20)
38
#include <onewire.h>
41
#include <onewire.h>
39
#include <ds18x20.h>
42
#include <ds18x20.h>
40
#include <delay.h>
43
#include <delay.h>
41
#endif
44
#endif
42
 
45
 
43
#ifdef USE_TC1047
46
#if defined(USE_TC1047)
44
#include <tc1047.h>
47
#include <tc1047.h>
45
#endif
48
#endif
46
 
49
 
47
#ifdef USE_SHT71
-
 
48
#include <sht7x.h>
-
 
49
#endif
-
 
50
/*-----------------------------------------------------------------------------
50
/*-----------------------------------------------------------------------------
51
 * Defines
51
 * Defines
52
 * --------------------------------------------------------------------------*/
52
 * --------------------------------------------------------------------------*/
53
 
53
 
54
 
54
 
55
/*-----------------------------------------------------------------------------
55
/*-----------------------------------------------------------------------------
56
 * Main Program
56
 * Main Program
57
 *---------------------------------------------------------------------------*/
57
 *---------------------------------------------------------------------------*/
58
int main(void) {
58
int main(void) {
59
#ifdef USE_DS18S20
59
#if defined(USE_DS18S20)
60
    uint8_t nSensors, i;
60
    uint8_t nSensors, i;
61
    uint8_t subzero, cel, cel_frac_bits;
61
    uint8_t subzero, cel, cel_frac_bits;
-
 
62
    uint32_t timeStamp_DS = 0;
62
#endif
63
#endif
63
    Mcu_Init();
-
 
64
    Timebase_Init();
-
 
65
#ifdef DEBUG
-
 
66
    Serial_Init();
-
 
67
#endif
-
 
68
#ifdef USE_TC1047
64
#if defined(USE_TC1047)
69
    uint32_t tcTemperature = 0;
65
    uint32_t tcTemperature = 0, timeStamp_TC = 0;
70
    adcTemperatureInit();
66
    adcTemperatureInit();
71
#endif
67
#endif
72
#ifdef USE_SHT71
-
 
73
    HumidityInit();
68
    Timebase_Init();
74
#endif
-
 
75
    sei();
69
    sei();
76
#ifdef DEBUG
70
 
77
#ifdef USE_DS18S20
71
#if defined(USE_DS18S20)
78
    printf( "\nDS18X20 1-Wire-Reader\n" );
72
    /* Make sure there is no more then 4 DS-sensors. */
79
    printf( "-----------------------" );
-
 
80
    nSensors = search_sensors();
73
    nSensors = search_sensors();
81
    printf( "%i DS18X20 Sensor(s) available:\n", (int) nSensors );
-
 
82
   
-
 
83
    for (i=0; i<nSensors; i++) {
-
 
84
        printf("Sensor# %i is a ", (int) i+1);
-
 
85
        if ( gSensorIDs[i][0] == DS18S20_ID)
-
 
86
            printf("DS18S20/DS1820");
-
 
87
        else printf("DS18B20");
-
 
88
        printf(" which is ");
-
 
89
        if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) ==
-
 
90
            DS18X20_POWER_PARASITE )
-
 
91
            printf( "parasite" );
-
 
92
        else printf( "externally" );
-
 
93
        printf( " powered\n" );
-
 
94
    }
-
 
95
   
-
 
96
    printf("CanInit...\n");
-
 
97
    if (Can_Init() != CAN_OK) {
-
 
98
        printf("FAILED!\n");
-
 
99
    }
-
 
100
    else {
-
 
101
        printf("OK!\n");
-
 
102
    }
-
 
103
#endif
74
#endif
104
#else
-
 
105
    Can_Init();
75
    Can_Init();
106
#endif
-
 
107
 
76
 
108
    uint32_t timeStamp = 0;
-
 
109
   
-
 
110
    Can_Message_t txMsg;
77
    Can_Message_t txMsg;
111
    Can_Message_t rxMsg;
78
    Can_Message_t rxMsg;
112
   
79
 
113
    txMsg.DataLength = 2;
-
 
114
    txMsg.Id = 0x450;
-
 
115
    txMsg.RemoteFlag = 0;
-
 
116
    txMsg.ExtendedFlag = 1;
80
    txMsg.ExtendedFlag = 1;
117
   
81
 
118
    /* main loop */
82
    /* main loop */
119
    while (1) {
83
    while (1) {
120
        /* service the CAN routines */
84
        /* service the CAN routines */
Line 123... Line 87...
123
        /* check if any messages have been received */
87
        /* check if any messages have been received */
124
        while (Can_Receive(&rxMsg) == CAN_OK) {
88
        while (Can_Receive(&rxMsg) == CAN_OK) {
125
           
89
 
126
        }
90
        }
127
 
91
 
-
 
92
#if defined(USE_DS18S20)
128
        /* check temperature and send on CAN once every other second */
93
        /* check temperature and send on CAN */
129
        if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
94
        if (Timebase_PassedTimeMillis(timeStamp_DS) >= DS_SEND_PERIOD) {
130
            timeStamp = Timebase_CurrentTime();
95
            timeStamp_DS = Timebase_CurrentTime();
131
 
96
 
132
#ifdef USE_DS18S20
-
 
133
            if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
97
            if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
134
#ifdef DEBUG
-
 
135
                printf("Measuring temperature... ");
-
 
136
#endif
-
 
137
                delay_ms(DS18B20_TCONV_12BIT);
98
                delay_ms(DS18B20_TCONV_12BIT);
138
                for ( i=0; i<nSensors; i++ ) {
99
                for ( i=0; i<nSensors; i++ ) {
139
                    if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero,
100
                    if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero,
140
                            &cel, &cel_frac_bits) == DS18X20_OK ) {
101
                        &cel, &cel_frac_bits) == DS18X20_OK ) {
141
                       
102
 
Line 145... Line 106...
145
                            txMsg.Data.bytes[i*2+1] = ~(cel_frac_bits<<4);
106
                            txMsg.Data.bytes[i*2+1] = ~(cel_frac_bits<<4);
146
                        } else {
107
                        }else{
147
                            txMsg.Data.bytes[i*2] = cel;
108
                            txMsg.Data.bytes[i*2] = cel;
148
                            txMsg.Data.bytes[i*2+1] = (cel_frac_bits<<4);
109
                            txMsg.Data.bytes[i*2+1] = (cel_frac_bits<<4);
149
                        }
110
                        }
150
                       
-
 
151
                    }
111
                    }
152
#ifdef DEBUG
112
                }
153
                    else printf("CRC Error (lost connection?)\n");
-
 
154
#endif
-
 
155
                }
-
 
156
               
-
 
157
                txMsg.DataLength = nSensors*2;
113
                txMsg.DataLength = nSensors*2;
158
#ifdef DEBUG
-
 
159
                printf("sending...\n");
-
 
160
#endif
-
 
161
                /* send txMsg */
114
                /* send txMsg */
-
 
115
                txMsg.Id = SENSOR_SEND_DS;
162
                Can_Send(&txMsg);
116
                Can_Send(&txMsg);
-
 
117
            }
163
            }
118
        }
164
#ifdef DEBUG
-
 
165
            else printf("Start meas. failed (short circuit?)\n");
-
 
166
#endif
-
 
167
#endif
119
#endif
168
#ifdef USE_TC1047
120
#if defined(USE_TC1047)
-
 
121
            /* check temperature and send on CAN */
-
 
122
        if (Timebase_PassedTimeMillis(timeStamp_TC) >= TC_SEND_PERIOD) {
-
 
123
            timeStamp_TC = Timebase_CurrentTime();
-
 
124
 
169
        tcTemperature = getTC1047temperature();
125
            tcTemperature = getTC1047temperature();
170
        txMsg.Data.bytes[0] = tcTemperature & 0x00FF;
126
            txMsg.Data.bytes[0] = tcTemperature & 0x00FF;
171
        txMsg.Data.bytes[1] = (tcTemperature & 0xFF00)>>8;
127
            txMsg.Data.bytes[1] = (tcTemperature & 0xFF00)>>8;
172
        txMsg.DataLength = 2;
128
            txMsg.DataLength = 2;
173
        txMsg.Id = 0x460;
129
            txMsg.Id = SENSOR_SEND_TC;
174
 
130
 
175
        Can_Send(&txMsg);
131
            Can_Send(&txMsg);
-
 
132
        }
176
#endif
133
#endif
177
#ifdef USE_SHT71
-
 
178
#endif
-
 
179
        }
-
 
180
    }
134
    }
181
   
135
 
182
    return 0;
136
    return 0;
183
}
137
}