Rev 326 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 326 | Rev 350 | ||
|---|---|---|---|
| 1 | /** |
1 | /** |
| 2 | * CanSensors. |
2 | * CanSensors. |
| 3 | * |
3 | * |
| 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 Jimmy Myhrman, 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 | 19 | ||
| 20 | #define DS_SEND_PERIOD 10000 |
20 | #define DS_SEND_PERIOD 10000 |
| - | 21 | #define DS_SEND_DELAY 2000 |
|
| 21 | #define TC_SEND_PERIOD 10000 |
22 | #define TC_SEND_PERIOD 10000 |
| 22 | 23 | ||
| 23 | #include < |
24 | #include <funcdefs.h> |
| 24 | 25 | ||
| 25 | /*----------------------------------------------------------------------------- |
26 | /*----------------------------------------------------------------------------- |
| 26 | * Includes |
27 | * Includes |
| 27 | *---------------------------------------------------------------------------*/ |
28 | *---------------------------------------------------------------------------*/ |
| 28 | /* system files */ |
29 | /* system files */ |
| 29 | #include <avr/io.h> |
30 | #include <avr/io.h> |
| 30 | #include <avr/interrupt.h> |
31 | #include <avr/interrupt.h> |
| 31 | #include <avr/wdt.h> |
32 | #include <avr/wdt.h> |
| 32 | #include <stdio.h> |
33 | #include <stdio.h> |
| 33 | /* lib files */ |
34 | /* lib files */ |
| 34 | #include <bios.h> |
35 | #include <bios.h> |
| 35 | 36 | ||
| 36 | #if defined(USE_DS18S20) |
37 | #if defined(USE_DS18S20) |
| 37 | #include <ds18x20.h> |
38 | #include <ds18x20.h> |
| 38 | #include <onewire.h> |
39 | #include <onewire.h> |
| 39 | #include <delay.h> |
40 | #include <delay.h> |
| 40 | #endif |
41 | #endif |
| 41 | 42 | ||
| 42 | #if defined(USE_TC1047) |
43 | #if defined(USE_TC1047) |
| 43 | #include <tc1047.h> |
44 | #include <tc1047.h> |
| 44 | #endif |
45 | #endif |
| 45 | 46 | ||
| 46 | /*----------------------------------------------------------------------------- |
47 | /*----------------------------------------------------------------------------- |
| 47 | * Main Program |
48 | * Main Program |
| 48 | *---------------------------------------------------------------------------*/ |
49 | *---------------------------------------------------------------------------*/ |
| 49 | int main(void) { |
50 | int main(void) { |
| 50 | #if defined(USE_DS18S20) |
51 | #if defined(USE_DS18S20) |
| 51 | uint8_t nSensors, i; |
52 | uint8_t nSensors, i; |
| 52 | uint8_t subzero, cel, cel_frac_bits; |
53 | uint8_t subzero, cel, cel_frac_bits; |
| 53 | uint32_t timeStamp_DS = 0; |
54 | uint32_t timeStamp_DS = 0, timeStamp_DS_del = 0; |
| 54 | /* This is to identify and give the sensors correct ID (For eqlazers net) |
55 | /* This is to identify and give the sensors correct ID (For eqlazers net) |
| 55 | * TODO read ds18s20 serial id and that way give the "can id" */ |
56 | * TODO read ds18s20 serial id and that way give the "can id" */ |
| 56 | uint16_t sensor_ds_id[] = {SNS_DS18S20_FREEZER, SNS_DS18S20_INSIDE1, SNS_DS18S20_OUTSIDE, SNS_DS18S20_INSIDE2}; |
57 | uint16_t sensor_ds_id[] = {SNS_DS18S20_FREEZER, SNS_DS18S20_INSIDE1, SNS_DS18S20_OUTSIDE, SNS_DS18S20_INSIDE2}; |
| 57 | #endif |
58 | #endif |
| 58 | #if defined(USE_TC1047) |
59 | #if defined(USE_TC1047) |
| 59 | uint32_t tcTemperature = 0, timeStamp_TC = 0; |
60 | uint32_t tcTemperature = 0, timeStamp_TC = 0; |
| 60 | adcTemperatureInit(); |
61 | adcTemperatureInit(); |
| 61 | #endif |
62 | #endif |
| 62 | sei(); |
63 | sei(); |
| 63 | 64 | ||
| 64 | #if defined(USE_DS18S20) |
65 | #if defined(USE_DS18S20) |
| 65 | /* Make sure there is no more then 4 DS-sensors. */ |
66 | /* Make sure there is no more then 4 DS-sensors. */ |
| 66 | nSensors = search_sensors(); |
67 | nSensors = search_sensors(); |
| 67 | #endif |
68 | #endif |
| 68 | 69 | ||
| 69 | Can_Message_t txMsg; |
70 | Can_Message_t txMsg; |
| 70 | 71 | ||
| 71 | txMsg.ExtendedFlag = 1; |
72 | txMsg.ExtendedFlag = 1; |
| 72 | txMsg.DataLength = 2; |
73 | txMsg.DataLength = 2; |
| 73 | 74 | ||
| 74 | /* Set class and sid (sid=NODE_ID from bios.h) */ |
75 | /* Set class and sid (sid=NODE_ID from bios.h) */ |
| 75 | txMsg.Id = (CLASS_SNS << CLASS_MASK_BITS)|(NODE_ID); |
76 | txMsg.Id = (CLASS_SNS << CLASS_MASK_BITS)|(NODE_ID); |
| 76 | 77 | ||
| 77 | /* main loop */ |
78 | /* main loop */ |
| 78 | while (1) { |
79 | while (1) { |
| 79 | #if defined(USE_DS18S20) |
80 | #if defined(USE_DS18S20) |
| 80 | /* check temperature and send on CAN */ |
81 | /* check temperature and send on CAN */ |
| 81 | if( bios->timebase_get() - timeStamp_DS >= DS_SEND_PERIOD ){ |
82 | if( bios->timebase_get() - timeStamp_DS >= DS_SEND_PERIOD ){ |
| 82 | timeStamp_DS = bios->timebase_get(); |
83 | timeStamp_DS = bios->timebase_get(); |
| 83 | 84 | ||
| 84 | txMsg.Id &= ~TYPE_MASK; /* Clear TYPE */ |
85 | txMsg.Id &= ~TYPE_MASK; /* Clear TYPE */ |
| 85 | txMsg.Id |= (SNS_TEMP_DS18S20 << SNS_TYPE_BITS); /* Set SNS_TYPE */ |
86 | txMsg.Id |= (SNS_TEMP_DS18S20 << SNS_TYPE_BITS); /* Set SNS_TYPE */ |
| 86 | 87 | ||
| 87 | if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { |
88 | if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { |
| 88 | delay_ms(DS18B20_TCONV_12BIT); |
89 | delay_ms(DS18B20_TCONV_12BIT); |
| 89 | for ( i=0; i<nSensors; i++ ) { |
90 | for ( i=0; i<nSensors; i++ ) { |
| 90 | if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero, |
91 | if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero, |
| 91 | &cel, &cel_frac_bits) == DS18X20_OK ) { |
92 | &cel, &cel_frac_bits) == DS18X20_OK ) { |
| 92 | 93 | ||
| 93 | if (subzero) { |
94 | if (subzero) { |
| 94 | txMsg.Data.bytes[0] = -cel; |
95 | txMsg.Data.bytes[0] = -cel; |
| 95 | txMsg.Data.bytes[1] = ~(cel_frac_bits<<4); |
96 | txMsg.Data.bytes[1] = ~(cel_frac_bits<<4); |
| 96 | }else{ |
97 | }else{ |
| 97 | txMsg.Data.bytes[0] = cel; |
98 | txMsg.Data.bytes[0] = cel; |
| 98 | txMsg.Data.bytes[1] = (cel_frac_bits<<4); |
99 | txMsg.Data.bytes[1] = (cel_frac_bits<<4); |
| 99 | } |
100 | } |
| 100 | } |
101 | } |
| - | 102 | /* Delay */ |
|
| - | 103 | timeStamp_DS_del = bios->timebase_get(); |
|
| - | 104 | while(bios->timebase_get() - timeStamp_DS_del < DS_SEND_DELAY){} |
|
| - | 105 | ||
| 101 | /* send txMsg */ |
106 | /* send txMsg */ |
| 102 | txMsg.Id &= ~SNS_ID_MASK; /* Clear sensor id */ |
107 | txMsg.Id &= ~SNS_ID_MASK; /* Clear sensor id */ |
| 103 | txMsg.Id |= ( sensor_ds_id[i] << SNS_ID_BITS); /* Set sesor id */ |
108 | txMsg.Id |= ( sensor_ds_id[i] << SNS_ID_BITS); /* Set sesor id */ |
| 104 | bios->can_send(&txMsg); |
109 | bios->can_send(&txMsg); |
| 105 | } |
110 | } |
| 106 | } |
111 | } |
| 107 | } |
112 | } |
| 108 | #endif |
113 | #endif |
| 109 | #if defined(USE_TC1047) |
114 | #if defined(USE_TC1047) |
| 110 | #error msg id is still not correct // FIXME |
115 | #error msg id is still not correct // FIXME |
| 111 | /* check temperature and send on CAN */ |
116 | /* check temperature and send on CAN */ |
| 112 | if( bios->timebase_get() - timeStamp_TC >= TC_SEND_PERIOD ){ |
117 | if( bios->timebase_get() - timeStamp_TC >= TC_SEND_PERIOD ){ |
| 113 | timeStamp_TC = bios->timebase_get(); |
118 | timeStamp_TC = bios->timebase_get(); |
| 114 | 119 | ||
| 115 | tcTemperature = getTC1047temperature(); |
120 | tcTemperature = getTC1047temperature(); |
| 116 | txMsg.Data.bytes[0] = tcTemperature & 0x00FF; |
121 | txMsg.Data.bytes[0] = tcTemperature & 0x00FF; |
| 117 | txMsg.Data.bytes[1] = (tcTemperature & 0xFF00)>>8; |
122 | txMsg.Data.bytes[1] = (tcTemperature & 0xFF00)>>8; |
| 118 | txMsg.DataLength = 2; |
123 | txMsg.DataLength = 2; |
| 119 | 124 | ||
| 120 | txMsg.Id &= ~TYPE_MASK; // rensa type |
125 | txMsg.Id &= ~TYPE_MASK; // rensa type |
| 121 | txMsg.Id |= (SNS_TEMP_TC1047 << TYPE_MASK_BITS); // sätt korrekt type |
126 | txMsg.Id |= (SNS_TEMP_TC1047 << TYPE_MASK_BITS); // sätt korrekt type |
| 122 | 127 | ||
| 123 | //Can_Send(&txMsg); |
128 | //Can_Send(&txMsg); |
| 124 | bios->can_send(&txMsg); |
129 | bios->can_send(&txMsg); |
| 125 | } |
130 | } |
| 126 | #endif |
131 | #endif |
| 127 | } |
132 | } |
| 128 | 133 | ||
| 129 | return 0; |
134 | return 0; |
| 130 | } |
135 | } |
| 131 | 136 | ||