Subversion Repositories HomeAutomation

Rev

Rev 844 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
406 arune 1
/**
2
 * CanSensors.
3
 *
4
 * A general application for using multiple sensors.
5
 * DS18S20, TC1047...
6
 *
7
 * @target  AVR
8
 * @date    2007-01-16
9
 * @author  Anders Runeson, Erik Larsson
10
 *
11
 * TODO: SHTxx
12
 */
13
 
14
/*-----------------------------------------------------------------------------
15
 * Defines
16
 * --------------------------------------------------------------------------*/
17
 
18
/*-----------------------------------------------------------------------------
19
 * Includes
20
 *---------------------------------------------------------------------------*/
578 nattgris 21
/* system files */
406 arune 22
#include <avr/io.h>
23
#include <avr/interrupt.h>
24
#include <stdio.h>
578 nattgris 25
/* lib files */
571 noddan 26
#include <config.h>
406 arune 27
#include <bios.h>
28
 
515 arune 29
#include <drivers/timer/timebase.h>
30
 
406 arune 31
#if defined(USE_DS18S20)
515 arune 32
#include <drivers/sensor/ds18s20/ds18x20.h>
33
#include <drivers/sensor/ds18s20/onewire.h>
34
#include <drivers/sensor/ds18s20/delay.h>
406 arune 35
#endif
36
 
37
#if defined(USE_TC1047)
515 arune 38
#include <drivers/sensor/tc1047/tc1047.h>
406 arune 39
#endif
40
 
41
#if defined(USE_LDR)
515 arune 42
#include <drivers/sensor/ldr/ldr.h>
406 arune 43
#endif
44
 
515 arune 45
#define APP_TYPE    CAN_APPTYPES_SENSOR
46
#define APP_VERSION 0x0001
47
 
406 arune 48
/*-----------------------------------------------------------------------------
49
 * Main Program
50
 *---------------------------------------------------------------------------*/
51
int main(void) {
52
#if defined(USE_DS18S20)
53
    uint8_t nSensors, i;
54
    uint8_t subzero, cel, cel_frac_bits;
55
    uint32_t timeStamp_DS = 0, timeStamp_DS_del = 0;
515 arune 56
    /* This is to identify and give the sensors correct ID
406 arune 57
     * TODO read ds18s20 serial id and that way give the "can id" */
515 arune 58
    uint16_t sensor_ds_id[] = {TEMPSENSORID_1, TEMPSENSORID_2, TEMPSENSORID_3, TEMPSENSORID_4};
406 arune 59
#endif
60
#if defined(USE_TC1047)
797 arune 61
    uint16_t tcTemperature = 0, timeStamp_TC = 0;
406 arune 62
    adcTemperatureInit();
63
#endif
64
#if defined(USE_LDR)
65
    uint32_t timeStamp_LDR = 0;
797 arune 66
    uint8_t ldr;
406 arune 67
    LDR_SensorInit();
68
#endif
69
    sei();
70
 
71
    Timebase_Init();
72
 
515 arune 73
    Can_Message_t txMsg;
74
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
75
    txMsg.DataLength = 4;
76
    txMsg.RemoteFlag = 0;
77
    txMsg.ExtendedFlag = 1;
78
    txMsg.Data.words[0] = APP_TYPE;
79
    txMsg.Data.words[1] = APP_VERSION;
80
    BIOS_CanSend(&txMsg);
81
 
406 arune 82
#if defined(USE_DS18S20)
83
    /* Make sure there is no more then 4 DS-sensors. */
84
    nSensors = search_sensors();
85
#endif
86
 
87
    txMsg.DataLength = 2;
88
 
89
    /* main loop */
90
    while (1) {
91
#if defined(USE_DS18S20)
92
        /* check temperature and send on CAN */
93
        if( Timebase_PassedTimeMillis(timeStamp_DS) >= DS_SEND_PERIOD ){
94
            timeStamp_DS = Timebase_CurrentTime();
95
 
620 arune 96
/*          txMsg.Id = 0xffffffff;
97
            txMsg.DataLength = 0;
98
            BIOS_CanSend(&txMsg);
99
*/
406 arune 100
            if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
101
                delay_ms(DS18B20_TCONV_12BIT);
102
                for ( i=0; i<nSensors; i++ ) {
515 arune 103
                    if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero, &cel, &cel_frac_bits) == DS18X20_OK ) {
406 arune 104
 
105
                        if (subzero) {
515 arune 106
                            txMsg.Data.bytes[0] = -cel-1;
686 arune 107
                            txMsg.Data.bytes[1] = ~(cel_frac_bits<<4);
406 arune 108
                        }else{
811 noddan 109
                            // txMsg.Data.bytes[0] = cel;
110
                            // txMsg.Data.bytes[1] = (cel_frac_bits<<4);
111
                            txMsg.Data.words[0] = (cel<<4 | cel_frac_bits); //Sends temperature compatible with our CAN standard.
112
 
406 arune 113
                        }
114
                    }
115
                    /* Delay */
116
                    timeStamp_DS_del = Timebase_CurrentTime();
117
                    while(Timebase_PassedTimeMillis(timeStamp_DS_del) < DS_SEND_DELAY){}
118
 
119
                    /* send txMsg */
515 arune 120
                    txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_TEMPERATURE << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID));
121
                    txMsg.Id |= ( sensor_ds_id[i] << CAN_SHIFT_SNS_ID); /* Set sensor id */
406 arune 122
                    BIOS_CanSend(&txMsg);
123
                }
124
            }
125
        }
126
#endif
127
#if defined(USE_TC1047)
844 arune 128
#warning tc1047 id is still not correct // FIXME
406 arune 129
            /* check temperature and send on CAN */
845 arune 130
        if( Timebase_PassedTimeMillis(timeStamp_TC) >= TC_SEND_PERIOD ){
131
            timeStamp_TC = Timebase_CurrentTime();
406 arune 132
 
797 arune 133
            tcTemperature = getTC1047temperature(TC1047_AD);
134
            txMsg.Data.bytes[0] = tcTemperature&0xff;
135
            txMsg.Data.bytes[1] = (tcTemperature>>8)&0xff;
406 arune 136
            txMsg.DataLength = 2;
137
 
515 arune 138
            txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_TEMPERATURE << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID));
139
            txMsg.Id |= (TEMPSENSORID_5 << CAN_SHIFT_SNS_ID); // sätt korrekt sensorid
406 arune 140
 
141
            BIOS_CanSend(&txMsg);
142
        }
143
#endif
144
#if defined(USE_LDR)
145
            /* check light and send on CAN */
146
        if( Timebase_PassedTimeMillis(timeStamp_LDR) >= LDR_SEND_PERIOD ){
147
            timeStamp_LDR = Timebase_CurrentTime();
148
 
797 arune 149
            ldr = (LDR_GetData(LDR_AD)>>2);
150
            txMsg.Data.bytes[0] = ldr&0xff;
406 arune 151
            txMsg.DataLength = 1;
152
 
515 arune 153
            txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_LIGHT << CAN_SHIFT_SNS_TYPE) | (LIGHTSENSORID_1 << CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID));
406 arune 154
 
155
            BIOS_CanSend(&txMsg);
156
        }
157
#endif
158
    }
159
 
160
    return 0;
161
}