Rev 199 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 199 | Rev 202 | ||
|---|---|---|---|
| 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 | Timebase_Init(); |
80 | Timebase_Init(); |
| 81 | Serial_Init(); |
81 | Serial_Init(); |
| 82 |
|
82 | #if defined(__AVR_ATmega88__) |
| 83 | - | ||
| 84 | // set the clock speed to 8MHz |
83 | // set the clock speed to 8MHz |
| 85 | // set the clock prescaler. First write CLKPCE to enable setting of clock the |
84 | // set the clock prescaler. First write CLKPCE to enable setting of clock the |
| 86 | // next four instructions. |
85 | // next four instructions. |
| 87 | CLKPR=(1<<CLKPCE); |
86 | CLKPR=(1<<CLKPCE); |
| 88 | CLKPR=0; // 8 MHZ |
87 | CLKPR=0; // 8 MHZ |
| - | 88 | #endif |
|
| - | 89 | ||
| - | 90 | sei(); |
|
| 89 | 91 | ||
| 90 | printf( "\nDS18X20 1-Wire-Reader\n" ); |
92 | printf( "\nDS18X20 1-Wire-Reader\n" ); |
| 91 | printf( "-----------------------" ); |
93 | printf( "-----------------------" ); |
| 92 | nSensors = search_sensors(); |
94 | nSensors = search_sensors(); |
| 93 | printf( "%i DS18X20 Sensor(s) available:\n", (int) nSensors ); |
95 | printf( "%i DS18X20 Sensor(s) available:\n", (int) nSensors ); |
| 94 | 96 | ||
| 95 | for (i=0; i<nSensors; i++) { |
97 | for (i=0; i<nSensors; i++) { |
| 96 | printf("Sensor# %i is a ", (int) i+1); |
98 | printf("Sensor# %i is a ", (int) i+1); |
| 97 | if ( gSensorIDs[i][0] == DS18S20_ID) |
99 | if ( gSensorIDs[i][0] == DS18S20_ID) |
| 98 | printf("DS18S20/DS1820"); |
100 | printf("DS18S20/DS1820"); |
| 99 | else printf("DS18B20"); |
101 | else printf("DS18B20"); |
| 100 | printf(" which is "); |
102 | printf(" which is "); |
| 101 | if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) == |
103 | if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) == |
| 102 | DS18X20_POWER_PARASITE ) |
104 | DS18X20_POWER_PARASITE ) |
| 103 | printf( "parasite" ); |
105 | printf( "parasite" ); |
| 104 | else printf( "externally" ); |
106 | else printf( "externally" ); |
| 105 | printf( " powered\n" ); |
107 | printf( " powered\n" ); |
| 106 | } |
108 | } |
| 107 | 109 | ||
| 108 | printf("CanInit...\n"); |
110 | printf("CanInit...\n"); |
| 109 | if (Can_Init() != CAN_OK) { |
111 | if (Can_Init() != CAN_OK) { |
| 110 | printf("FAILED!\n"); |
112 | printf("FAILED!\n"); |
| 111 | } |
113 | } |
| 112 | else { |
114 | else { |
| 113 | printf("OK!\n"); |
115 | printf("OK!\n"); |
| 114 | } |
116 | } |
| 115 | 117 | ||
| 116 | uint32_t timeStamp = 0; |
118 | uint32_t timeStamp = 0; |
| 117 | 119 | ||
| 118 | Can_Message_t txMsg; |
120 | Can_Message_t txMsg; |
| 119 | Can_Message_t rxMsg; |
121 | Can_Message_t rxMsg; |
| 120 | 122 | ||
| 121 | txMsg.DataLength = 2; |
123 | txMsg.DataLength = 2; |
| 122 | txMsg.Id = 0; |
124 | txMsg.Id = 0; |
| 123 | txMsg.RemoteFlag = 0; |
125 | txMsg.RemoteFlag = 0; |
| 124 | txMsg.ExtendedFlag = 1; |
126 | txMsg.ExtendedFlag = 1; |
| 125 | 127 | ||
| 126 | /* main loop */ |
128 | /* main loop */ |
| 127 | while (1) { |
129 | while (1) { |
| 128 | /* service the CAN routines */ |
130 | /* service the CAN routines */ |
| 129 | Can_Service(); |
131 | Can_Service(); |
| 130 | 132 | ||
| 131 | /* check if any messages have been received */ |
133 | /* check if any messages have been received */ |
| 132 | while (Can_Receive(&rxMsg) == CAN_OK) { |
134 | while (Can_Receive(&rxMsg) == CAN_OK) { |
| 133 | 135 | ||
| 134 | } |
136 | } |
| 135 | 137 | ||
| 136 | /* check temperature and send on CAN once every other second */ |
138 | /* check temperature and send on CAN once every other second */ |
| 137 | if (Timebase_PassedTimeMillis(timeStamp) >= 2000) { |
139 | if (Timebase_PassedTimeMillis(timeStamp) >= 2000) { |
| 138 | timeStamp = Timebase_CurrentTime(); |
140 | timeStamp = Timebase_CurrentTime(); |
| 139 | 141 | ||
| 140 | if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { |
142 | if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { |
| 141 | printf("Measuring temperature... "); |
143 | printf("Measuring temperature... "); |
| 142 | delay_ms(DS18B20_TCONV_12BIT); |
144 | delay_ms(DS18B20_TCONV_12BIT); |
| 143 | for ( i=0; i<nSensors; i++ ) { |
145 | for ( i=0; i<nSensors; i++ ) { |
| 144 | if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero, |
146 | if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero, |
| 145 | &cel, &cel_frac_bits) == DS18X20_OK ) { |
147 | &cel, &cel_frac_bits) == DS18X20_OK ) { |
| 146 | 148 | ||
| 147 | //txMsg.Data.bytes[0] = subzero; |
149 | //txMsg.Data.bytes[0] = subzero; |
| 148 | if (subzero) { |
150 | if (subzero) { |
| 149 | txMsg.Data.bytes[i*2] = -cel; |
151 | txMsg.Data.bytes[i*2] = -cel; |
| 150 | txMsg.Data.bytes[i*2+1] = ~(cel_frac_bits<<4); |
152 | txMsg.Data.bytes[i*2+1] = ~(cel_frac_bits<<4); |
| 151 | } else { |
153 | } else { |
| 152 | txMsg.Data.bytes[i*2] = cel; |
154 | txMsg.Data.bytes[i*2] = cel; |
| 153 | txMsg.Data.bytes[i*2+1] = (cel_frac_bits<<4); |
155 | txMsg.Data.bytes[i*2+1] = (cel_frac_bits<<4); |
| 154 | } |
156 | } |
| 155 | 157 | ||
| 156 | } |
158 | } |
| 157 | else printf("CRC Error (lost connection?)\n"); |
159 | else printf("CRC Error (lost connection?)\n"); |
| 158 | } |
160 | } |
| 159 | 161 | ||
| 160 | txMsg.DataLength = nSensors*2; |
162 | txMsg.DataLength = nSensors*2; |
| 161 | printf("sending...\n"); |
163 | printf("sending...\n"); |
| 162 | /* send txMsg */ |
164 | /* send txMsg */ |
| 163 | Can_Send(&txMsg); |
165 | Can_Send(&txMsg); |
| 164 | } |
166 | } |
| 165 | else printf("Start meas. failed (short circuit?)\n"); |
167 | else printf("Start meas. failed (short circuit?)\n"); |
| 166 | 168 | ||
| 167 | } |
169 | } |
| 168 | } |
170 | } |
| 169 | 171 | ||
| 170 | return 0; |
172 | return 0; |
| 171 | } |
173 | } |
| 172 | 174 | ||