Subversion Repositories HomeAutomation

Rev

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

Rev Author Line No. Line
239 eqlazer 1
/**
2
 * CanSensors.
3
 *
4
 * A general application for using multiple sensors.
255 eqlazer 5
 * DS18S20, TC1047...
239 eqlazer 6
 *
7
 * @target  AVR
255 eqlazer 8
 * @date    2007-01-16
239 eqlazer 9
 * @author  Jimmy Myhrman, Anders Runeson, Erik Larsson
255 eqlazer 10
 *
11
 * TODO: SHTxx
239 eqlazer 12
 */
13
 
255 eqlazer 14
/*-----------------------------------------------------------------------------
15
 * Defines
16
 * --------------------------------------------------------------------------*/
239 eqlazer 17
#define USE_DS18S20
255 eqlazer 18
//#define USE_TC1047
239 eqlazer 19
 
255 eqlazer 20
#define SENSOR_SEND 0x1500
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
27
 
239 eqlazer 28
/*-----------------------------------------------------------------------------
29
 * Includes
30
 *---------------------------------------------------------------------------*/
31
/* system files */
32
#include <avr/io.h>
33
#include <avr/interrupt.h>
34
#include <avr/wdt.h>
35
#include <stdio.h>
36
/* lib files */
37
#include <can.h>
38
#include <timebase.h>
39
 
255 eqlazer 40
#if defined(USE_DS18S20)
239 eqlazer 41
#include <onewire.h>
42
#include <ds18x20.h>
43
#include <delay.h>
44
#endif
45
 
255 eqlazer 46
#if defined(USE_TC1047)
239 eqlazer 47
#include <tc1047.h>
48
#endif
49
 
50
/*-----------------------------------------------------------------------------
51
 * Defines
52
 * --------------------------------------------------------------------------*/
53
 
54
 
55
/*-----------------------------------------------------------------------------
56
 * Main Program
57
 *---------------------------------------------------------------------------*/
58
int main(void) {
255 eqlazer 59
#if defined(USE_DS18S20)
60
    uint8_t nSensors, i;
61
    uint8_t subzero, cel, cel_frac_bits;
62
    uint32_t timeStamp_DS = 0;
239 eqlazer 63
#endif
255 eqlazer 64
#if defined(USE_TC1047)
65
    uint32_t tcTemperature = 0, timeStamp_TC = 0;
239 eqlazer 66
    adcTemperatureInit();
67
#endif
255 eqlazer 68
    Timebase_Init();
69
    sei();
70
 
71
#if defined(USE_DS18S20)
72
    /* Make sure there is no more then 4 DS-sensors. */
73
    nSensors = search_sensors();
239 eqlazer 74
#endif
255 eqlazer 75
    Can_Init();
239 eqlazer 76
 
255 eqlazer 77
    Can_Message_t txMsg;
78
    Can_Message_t rxMsg;
239 eqlazer 79
 
255 eqlazer 80
    txMsg.ExtendedFlag = 1;
239 eqlazer 81
 
255 eqlazer 82
    /* main loop */
83
    while (1) {
84
        /* service the CAN routines */
85
       Can_Service();
239 eqlazer 86
 
255 eqlazer 87
        /* check if any messages have been received */
88
        while (Can_Receive(&rxMsg) == CAN_OK) {
89
 
90
        }
91
 
92
#if defined(USE_DS18S20)
93
        /* check temperature and send on CAN */
94
        if (Timebase_PassedTimeMillis(timeStamp_DS) >= DS_SEND_PERIOD) {
95
            timeStamp_DS = Timebase_CurrentTime();
96
 
97
            if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
98
                delay_ms(DS18B20_TCONV_12BIT);
99
                for ( i=0; i<nSensors; i++ ) {
100
                    if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero,
101
                        &cel, &cel_frac_bits) == DS18X20_OK ) {
102
 
103
                        //txMsg.Data.bytes[0] = subzero;
104
                        if (subzero) {
105
                            txMsg.Data.bytes[i*2] = -cel;
106
                            txMsg.Data.bytes[i*2+1] = ~(cel_frac_bits<<4);
107
                        }else{
108
                            txMsg.Data.bytes[i*2] = cel;
109
                            txMsg.Data.bytes[i*2+1] = (cel_frac_bits<<4);
110
                        }
111
                    }
112
                }
113
                txMsg.DataLength = nSensors*2;
114
                /* send txMsg */
115
                txMsg.Id = SENSOR_SEND_DS;
116
                Can_Send(&txMsg);
117
            }
118
        }
239 eqlazer 119
#endif
255 eqlazer 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
 
125
            tcTemperature = getTC1047temperature();
126
            txMsg.Data.bytes[0] = tcTemperature & 0x00FF;
127
            txMsg.Data.bytes[1] = (tcTemperature & 0xFF00)>>8;
128
            txMsg.DataLength = 2;
129
            txMsg.Id = SENSOR_SEND_TC;
130
 
131
            Can_Send(&txMsg);
132
        }
239 eqlazer 133
#endif
255 eqlazer 134
    }
239 eqlazer 135
 
255 eqlazer 136
    return 0;
239 eqlazer 137
}