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