Subversion Repositories HomeAutomation

Rev

Rev 221 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 221 Rev 389
1
/**
1
/**
2
 * DS18S20 over CAN program.
2
 * DS18S20 over CAN program.
3
 *
3
 *
4
 * @target  AVR
4
 * @target  AVR
5
 * @date    2006-12-09
5
 * @date    2006-12-09
6
 * @author  Jimmy Myhrman, Anders Runeson
6
 * @author  Jimmy Myhrman, Anders Runeson
7
 *  
7
 *  
8
 * Högst experimentell, decimaldel fel vid negativ temperatur?
8
 * Högst experimentell, decimaldel fel vid negativ temperatur?
9
 * negativ temperatur helt otestad!
9
 * negativ temperatur helt otestad!
10
 *
10
 *
11
 */
11
 */
12
 
12
 
13
/*-----------------------------------------------------------------------------
13
/*-----------------------------------------------------------------------------
14
 * Includes
14
 * Includes
15
 *---------------------------------------------------------------------------*/
15
 *---------------------------------------------------------------------------*/
16
/* system files */
16
/* system files */
17
#include <avr/io.h>
17
#include <avr/io.h>
18
#include <avr/interrupt.h>
18
#include <avr/interrupt.h>
19
#include <avr/wdt.h>
19
#include <avr/wdt.h>
20
#include <stdio.h>
20
#include <stdio.h>
21
/* lib files */
21
/* lib files */
22
#include <can.h>
22
#include <can.h>
23
#include <uart.h>
23
#include <uart.h>
24
#include <timebase.h>
24
#include <timebase.h>
25
 
25
 
26
#include <onewire.h>
26
#include <onewire.h>
27
#include <ds18x20.h>
27
#include <ds18x20.h>
28
 
28
 
29
#include <delay.h>
29
#include <delay.h>
30
 
30
 
31
 
31
 
32
 
32
 
33
#define MAXSENSORS 4
33
#define MAXSENSORS 4
34
 
34
 
35
uint8_t gSensorIDs[MAXSENSORS][OW_ROMCODE_SIZE];
35
uint8_t gSensorIDs[MAXSENSORS][OW_ROMCODE_SIZE];
36
 
36
 
37
uint8_t search_sensors(void)
37
uint8_t search_sensors(void)
38
{
38
{
39
    uint8_t i;
39
    uint8_t i;
40
    uint8_t id[OW_ROMCODE_SIZE];
40
    uint8_t id[OW_ROMCODE_SIZE];
41
    uint8_t diff, nSensors;
41
    uint8_t diff, nSensors;
42
   
42
   
43
    printf( "\nScanning Bus for DS18X20\n" );
43
    printf( "\nScanning Bus for DS18X20\n" );
44
   
44
   
45
    nSensors = 0;
45
    nSensors = 0;
46
   
46
   
47
    for( diff = OW_SEARCH_FIRST;
47
    for( diff = OW_SEARCH_FIRST;
48
        diff != OW_LAST_DEVICE && nSensors < MAXSENSORS ; )
48
        diff != OW_LAST_DEVICE && nSensors < MAXSENSORS ; )
49
    {
49
    {
50
        DS18X20_find_sensor( &diff, &id[0] );
50
        DS18X20_find_sensor( &diff, &id[0] );
51
       
51
       
52
        if( diff == OW_PRESENCE_ERR ) {
52
        if( diff == OW_PRESENCE_ERR ) {
53
            printf( "No Sensor found\n" );
53
            printf( "No Sensor found\n" );
54
            break;
54
            break;
55
        }
55
        }
56
       
56
       
57
        if( diff == OW_DATA_ERR ) {
57
        if( diff == OW_DATA_ERR ) {
58
            printf( "Bus Error\n" );
58
            printf( "Bus Error\n" );
59
            break;
59
            break;
60
        }
60
        }
61
       
61
       
62
        for (i=0;i<OW_ROMCODE_SIZE;i++)
62
        for (i=0;i<OW_ROMCODE_SIZE;i++)
63
            gSensorIDs[nSensors][i]=id[i];
63
            gSensorIDs[nSensors][i]=id[i];
64
       
64
       
65
        nSensors++;
65
        nSensors++;
66
    }
66
    }
67
   
67
   
68
    return nSensors;
68
    return nSensors;
69
}
69
}
70
 
70
 
71
 
71
 
72
 
72
 
73
/*-----------------------------------------------------------------------------
73
/*-----------------------------------------------------------------------------
74
 * Main Program
74
 * Main Program
75
 *---------------------------------------------------------------------------*/
75
 *---------------------------------------------------------------------------*/
76
int main(void) {
76
int main(void) {
77
    uint8_t nSensors, i;
77
    uint8_t nSensors, i;
78
    uint8_t subzero, cel, cel_frac_bits;
78
    uint8_t subzero, cel, cel_frac_bits;
79
   
79
   
80
    Mcu_Init();
80
    Mcu_Init();
81
    Timebase_Init();
81
    Timebase_Init();
82
    Serial_Init();
82
    Serial_Init();
83
   
83
   
84
    sei();
84
    sei();
85
   
85
   
86
    printf( "\nDS18X20 1-Wire-Reader\n" );
86
    printf( "\nDS18X20 1-Wire-Reader\n" );
87
    printf( "-----------------------" );
87
    printf( "-----------------------" );
88
    nSensors = search_sensors();
88
    nSensors = search_sensors();
89
    printf( "%i DS18X20 Sensor(s) available:\n", (int) nSensors );
89
    printf( "%i DS18X20 Sensor(s) available:\n", (int) nSensors );
90
   
90
   
91
    for (i=0; i<nSensors; i++) {
91
    for (i=0; i<nSensors; i++) {
92
        printf("Sensor# %i is a ", (int) i+1);
92
        printf("Sensor# %i is a ", (int) i+1);
93
        if ( gSensorIDs[i][0] == DS18S20_ID)
93
        if ( gSensorIDs[i][0] == DS18S20_ID)
94
            printf("DS18S20/DS1820");
94
            printf("DS18S20/DS1820");
95
        else printf("DS18B20");
95
        else printf("DS18B20");
96
        printf(" which is ");
96
        printf(" which is ");
97
        if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) ==
97
        if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) ==
98
            DS18X20_POWER_PARASITE )
98
            DS18X20_POWER_PARASITE )
99
            printf( "parasite" );
99
            printf( "parasite" );
100
        else printf( "externally" );
100
        else printf( "externally" );
101
        printf( " powered\n" );
101
        printf( " powered\n" );
102
    }
102
    }
103
   
103
   
104
    printf("CanInit...\n");
104
    printf("CanInit...\n");
105
    if (Can_Init() != CAN_OK) {
105
    if (Can_Init() != CAN_OK) {
106
        printf("FAILED!\n");
106
        printf("FAILED!\n");
107
    }
107
    }
108
    else {
108
    else {
109
        printf("OK!\n");
109
        printf("OK!\n");
110
    }
110
    }
111
   
111
   
112
    uint32_t timeStamp = 0;
112
    uint32_t timeStamp = 0;
113
   
113
   
114
    Can_Message_t txMsg;
114
    Can_Message_t txMsg;
115
    Can_Message_t rxMsg;
115
    Can_Message_t rxMsg;
116
   
116
   
117
    txMsg.DataLength = 2;
117
    txMsg.DataLength = 2;
118
    txMsg.Id = 0;
118
    txMsg.Id = 0;
119
    txMsg.RemoteFlag = 0;
119
    txMsg.RemoteFlag = 0;
120
    txMsg.ExtendedFlag = 1;
120
    txMsg.ExtendedFlag = 1;
121
   
121
   
122
    /* main loop */
122
    /* main loop */
123
    while (1) {
123
    while (1) {
124
        /* service the CAN routines */
124
        /* service the CAN routines */
125
        Can_Service();
125
        Can_Service();
126
 
126
 
127
        /* check if any messages have been received */
127
        /* check if any messages have been received */
128
        while (Can_Receive(&rxMsg) == CAN_OK) {
128
        while (Can_Receive(&rxMsg) == CAN_OK) {
129
           
129
           
130
        }
130
        }
131
 
131
 
132
        /* check temperature and send on CAN once every other second */
132
        /* check temperature and send on CAN once every other second */
133
        if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
133
        if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
134
            timeStamp = Timebase_CurrentTime();
134
            timeStamp = Timebase_CurrentTime();
135
           
135
           
136
            if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
136
            if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
137
                printf("Measuring temperature... ");
137
                printf("Measuring temperature... ");
138
                delay_ms(DS18B20_TCONV_12BIT);
138
                delay_ms(DS18B20_TCONV_12BIT);
139
                for ( i=0; i<nSensors; i++ ) {
139
                for ( i=0; i<nSensors; i++ ) {
140
                    if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero,
140
                    if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero,
141
                            &cel, &cel_frac_bits) == DS18X20_OK ) {
141
                            &cel, &cel_frac_bits) == DS18X20_OK ) {
142
                       
142
                       
143
                        //txMsg.Data.bytes[0] = subzero;
143
                        //txMsg.Data.bytes[0] = subzero;
144
                        if (subzero) {
144
                        if (subzero) {
145
                            txMsg.Data.bytes[i*2] = -cel;
145
                            txMsg.Data.bytes[i*2] = -cel;
146
                            txMsg.Data.bytes[i*2+1] = ~(cel_frac_bits<<4);
146
                            txMsg.Data.bytes[i*2+1] = ~(cel_frac_bits<<4);
147
                        } else {
147
                        } else {
148
                            txMsg.Data.bytes[i*2] = cel;
148
                            txMsg.Data.bytes[i*2] = cel;
149
                            txMsg.Data.bytes[i*2+1] = (cel_frac_bits<<4);
149
                            txMsg.Data.bytes[i*2+1] = (cel_frac_bits<<4);
150
                        }
150
                        }
151
                       
151
                       
152
                    }
152
                    }
153
                    else printf("CRC Error (lost connection?)\n");
153
                    else printf("CRC Error (lost connection?)\n");
154
                }
154
                }
155
               
155
               
156
                txMsg.DataLength = nSensors*2;
156
                txMsg.DataLength = nSensors*2;
157
                printf("sending...\n");
157
                printf("sending...\n");
158
                /* send txMsg */
158
                /* send txMsg */
159
                Can_Send(&txMsg);
159
                Can_Send(&txMsg);
160
            }
160
            }
161
            else printf("Start meas. failed (short circuit?)\n");
161
            else printf("Start meas. failed (short circuit?)\n");
162
 
162
 
163
        }
163
        }
164
    }
164
    }
165
   
165
   
166
    return 0;
166
    return 0;
167
}
167
}
168
 
168