Subversion Repositories HomeAutomation

Rev

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

Rev 396 Rev 402
Line 4... Line 4...
4
 * A general application for using multiple sensors.
4
 * A general application for using multiple sensors.
5
 * DS18S20, TC1047...
5
 * DS18S20, TC1047...
6
 *
6
 *
7
 * @target  AVR
7
 * @target  AVR
8
 * @date    2007-01-16
8
 * @date    2007-01-16
9
 * @author  Jimmy Myhrman, Anders Runeson, Erik Larsson
9
 * @author  Anders Runeson, Erik Larsson
10
 *
10
 *
11
 * TODO: SHTxx
11
 * TODO: SHTxx
12
 */
12
 */
13
 
13
 
14
/*-----------------------------------------------------------------------------
14
/*-----------------------------------------------------------------------------
15
 * Defines
15
 * Defines
16
 * --------------------------------------------------------------------------*/
16
 * --------------------------------------------------------------------------*/
17
#define USE_DS18S20
17
#define USE_DS18S20
18
//#define USE_TC1047
18
//#define USE_TC1047
-
 
19
#define USE_LDR
19
 
20
 
20
#define DS_SEND_PERIOD  10000
21
#define DS_SEND_PERIOD  10000
21
#define DS_SEND_DELAY   2000
22
#define DS_SEND_DELAY   2000
22
#define TC_SEND_PERIOD  10000
23
#define TC_SEND_PERIOD  10000
-
 
24
#define LDR_SEND_PERIOD 10000
23
 
25
 
24
#include <funcdefs.h>
26
#include <funcdefs/funcdefs.h>
25
 
27
 
26
/*-----------------------------------------------------------------------------
28
/*-----------------------------------------------------------------------------
27
 * Includes
29
 * Includes
28
 *---------------------------------------------------------------------------*/
30
 *---------------------------------------------------------------------------*/
29
/* system files */
31
/* system files */
30
#include <avr/io.h>
32
#include <avr/io.h>
31
#include <avr/interrupt.h>
33
#include <avr/interrupt.h>
32
#include <avr/wdt.h>
-
 
33
#include <stdio.h>
34
#include <stdio.h>
34
/* lib files */
35
/* lib files */
35
#include <bios.h>
36
#include <bios.h>
-
 
37
#include <config.h>
-
 
38
#include <timebase.h>
36
 
39
 
37
#if defined(USE_DS18S20)
40
#if defined(USE_DS18S20)
38
#include <ds18x20.h>
41
#include <ds18x20.h>
39
#include <onewire.h>
42
#include <onewire.h>
40
#include <delay.h>
43
#include <delay.h>
41
#endif
44
#endif
42
 
45
 
43
#if defined(USE_TC1047)
46
#if defined(USE_TC1047)
44
#include <tc1047.h>
47
#include <tc1047.h>
-
 
48
#endif
-
 
49
 
-
 
50
#if defined(USE_LDR)
-
 
51
#include <ldr.h>
45
#endif
52
#endif
46
 
53
 
47
/*-----------------------------------------------------------------------------
54
/*-----------------------------------------------------------------------------
48
 * Main Program
55
 * Main Program
49
 *---------------------------------------------------------------------------*/
56
 *---------------------------------------------------------------------------*/
Line 57... Line 64...
57
    uint16_t sensor_ds_id[] = {SNS_DS18S20_FREEZER, SNS_DS18S20_INSIDE1, SNS_DS18S20_OUTSIDE, SNS_DS18S20_INSIDE2};
64
    uint16_t sensor_ds_id[] = {SNS_DS18S20_FREEZER, SNS_DS18S20_INSIDE1, SNS_DS18S20_OUTSIDE, SNS_DS18S20_INSIDE2};
58
#endif
65
#endif
59
#if defined(USE_TC1047)
66
#if defined(USE_TC1047)
60
    uint32_t tcTemperature = 0, timeStamp_TC = 0;
67
    uint32_t tcTemperature = 0, timeStamp_TC = 0;
61
    adcTemperatureInit();
68
    adcTemperatureInit();
-
 
69
#endif
-
 
70
#if defined(USE_LDR)
-
 
71
    uint32_t timeStamp_LDR = 0;
-
 
72
    uint8_t ldr;
-
 
73
    LDR_SensorInit();
62
#endif
74
#endif
63
    sei();
75
    sei();
64
 
76
 
-
 
77
    Timebase_Init();
-
 
78
 
65
#if defined(USE_DS18S20)
79
#if defined(USE_DS18S20)
66
    /* Make sure there is no more then 4 DS-sensors. */
80
    /* Make sure there is no more then 4 DS-sensors. */
67
    nSensors = search_sensors();
81
    nSensors = search_sensors();
68
#endif
82
#endif
69
 
83
 
70
    Can_Message_t txMsg;
84
    Can_Message_t txMsg;
71
 
85
 
72
    txMsg.ExtendedFlag = 1;
86
    txMsg.ExtendedFlag = 1;
73
    txMsg.DataLength = 2;
87
    txMsg.DataLength = 2;
74
 
88
 
75
    /* Set class and sid (sid=NODE_ID from bios.h) */
89
    /* Set class and sid (sid=NODE_ID from bios.h) */
76
    txMsg.Id = (CLASS_SNS << CLASS_MASK_BITS)|(NODE_ID);
90
    txMsg.Id = (CLASS_SNS << CLASS_MASK_BITS)|(NODE_ID);
77
 
91
 
78
    /* main loop */
92
    /* main loop */
79
    while (1) {
93
    while (1) {
80
#if defined(USE_DS18S20)
94
#if defined(USE_DS18S20)
81
        /* check temperature and send on CAN */
95
        /* check temperature and send on CAN */
82
        if( bios->timebase_get() - timeStamp_DS >= DS_SEND_PERIOD ){
96
        if( Timebase_PassedTimeMillis(timeStamp_DS) >= DS_SEND_PERIOD ){
83
            timeStamp_DS = bios->timebase_get();
97
            timeStamp_DS = Timebase_CurrentTime();
84
 
98
 
85
            txMsg.Id &= ~TYPE_MASK; /* Clear TYPE */
99
            txMsg.Id &= ~TYPE_MASK; /* Clear TYPE */
86
            txMsg.Id |= (SNS_TEMP_DS18S20 << SNS_TYPE_BITS); /* Set SNS_TYPE */
100
            txMsg.Id |= (SNS_TEMP_DS18S20 << SNS_TYPE_BITS); /* Set SNS_TYPE */
87
 
101
 
88
            if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
102
            if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
89
                delay_ms(DS18B20_TCONV_12BIT);
103
                delay_ms(DS18B20_TCONV_12BIT);
90
                for ( i=0; i<nSensors; i++ ) {
104
                for ( i=0; i<nSensors; i++ ) {
Line 98... Line 112...
98
                            txMsg.Data.bytes[0] = cel;
112
                            txMsg.Data.bytes[0] = cel;
99
                            txMsg.Data.bytes[1] = (cel_frac_bits<<4);
113
                            txMsg.Data.bytes[1] = (cel_frac_bits<<4);
100
                        }
114
                        }
101
                    }
115
                    }
102
                    /* Delay */
116
                    /* Delay */
103
                    timeStamp_DS_del = bios->timebase_get();
117
                    timeStamp_DS_del = Timebase_CurrentTime();
104
                    while(bios->timebase_get() - timeStamp_DS_del < DS_SEND_DELAY){}
118
                    while(Timebase_PassedTimeMillis(timeStamp_DS_del) < DS_SEND_DELAY){}
105
 
119
 
106
                    /* send txMsg */
120
                    /* send txMsg */
107
                    txMsg.Id &= ~SNS_ID_MASK; /* Clear sensor id */
121
                    txMsg.Id &= ~SNS_ID_MASK; /* Clear sensor id */
108
                    txMsg.Id |= ( sensor_ds_id[i] << SNS_ID_BITS); /* Set sesor id */
122
                    txMsg.Id |= ( sensor_ds_id[i] << SNS_ID_BITS); /* Set sensor id */
109
                    bios->can_send(&txMsg);
123
                    BIOS_CanSend(&txMsg);
110
                }
124
                }
111
            }
125
            }
112
        }
126
        }
113
#endif
127
#endif
114
#if defined(USE_TC1047)
128
#if defined(USE_TC1047)
115
#error msg id is still not correct // FIXME
129
#error tc1047 id is still not correct // FIXME
116
            /* check temperature and send on CAN */
130
            /* check temperature and send on CAN */
117
        if( bios->timebase_get() - timeStamp_TC >= TC_SEND_PERIOD ){
131
        if( bios->timebase_get() - timeStamp_TC >= TC_SEND_PERIOD ){
118
            timeStamp_TC = bios->timebase_get();
132
            timeStamp_TC = bios->timebase_get();
119
 
133
 
120
            tcTemperature = getTC1047temperature();
134
            tcTemperature = getTC1047temperature();
Line 123... Line 137...
123
            txMsg.DataLength = 2;
137
            txMsg.DataLength = 2;
124
 
138
 
125
            txMsg.Id &= ~TYPE_MASK; // rensa type
139
            txMsg.Id &= ~TYPE_MASK; // rensa type
126
            txMsg.Id |= (SNS_TEMP_TC1047 << TYPE_MASK_BITS); // sätt korrekt type
140
            txMsg.Id |= (SNS_TEMP_TC1047 << TYPE_MASK_BITS); // sätt korrekt type
127
 
141
 
128
            //Can_Send(&txMsg);
142
            BIOS_CanSend(&txMsg);
-
 
143
        }
-
 
144
#endif
-
 
145
#if defined(USE_LDR)
-
 
146
            /* check light and send on CAN */
-
 
147
        if( Timebase_PassedTimeMillis(timeStamp_LDR) >= LDR_SEND_PERIOD ){
-
 
148
            timeStamp_LDR = Timebase_CurrentTime();
-
 
149
 
-
 
150
            ldr = (uint8_t)LDR_GetData(0);
-
 
151
            txMsg.Data.bytes[0] = ldr;
-
 
152
            txMsg.DataLength = 1;
-
 
153
 
-
 
154
            txMsg.Id &= ~TYPE_MASK; /* Clear TYPE */
-
 
155
            txMsg.Id |= (SNS_LIGHT_LDR << SNS_TYPE_BITS); /* Set SNS_TYPE */
-
 
156
            txMsg.Id &= ~SNS_ID_MASK; /* Clear sensor id */
-
 
157
            txMsg.Id |= ( SNS_LDR_TEST1 << SNS_ID_BITS); /* Set sensor id */
-
 
158
 
129
            bios->can_send(&txMsg);
159
            BIOS_CanSend(&txMsg);
130
        }
160
        }
131
#endif
161
#endif
132
    }
162
    }
133
 
163
 
134
    return 0;
164
    return 0;