Rev 184 | Rev 187 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 184 | Rev 185 | ||
|---|---|---|---|
| Line 3... | Line 3... | ||
| 3 | * |
3 | * |
| 4 | * |
4 | * |
| 5 | * @date 2006-12-17 |
5 | * @date 2006-12-17 |
| 6 | * @author Erik Larsson |
6 | * @author Erik Larsson |
| 7 | * |
7 | * |
| 8 | * Observera! Applikationen är testad på labplatta med m88, pot och lysdiod |
8 | * Observera! Applikationen är endast testad på labplatta med m88, pot och lysdiod |
| 9 | * |
9 | * |
| 10 | * ADC=Vin*1024/Vref |
10 | * ADC=Vin*1024/Vref |
| 11 | * Vout=( 10 mV/C )( Temperature C ) + 500 mV |
11 | * Vout=( 10 mV/C )( Temperature C ) + 500 mV |
| 12 | * |
12 | * |
| 13 | * Transmitts: DATA: <temperature |
13 | * Transmitts: DATA: <temperature low byte> <temperature high byte> <relay status> |
| 14 | * |
14 | * |
| 15 | * TODO fixa så status/temp ligger i en funktion |
15 | * TODO fixa så status/temp ligger i en funktion |
| 16 | * temperaturomvandlingen fungerar |
16 | * temperaturomvandlingen fungerar fint |
| 17 | * fixa så adcn inte pollas hela tiden, det tar för mycket tid |
17 | * fixa så adcn inte pollas hela tiden, det tar för mycket tid |
| - | 18 | * kunna använda Vref = 1.1 |
|
| 18 | * |
19 | * |
| 19 | */ |
20 | */ |
| 20 | 21 | ||
| 21 | /* |
22 | /* |
| 22 | * For testing with ATmega88 PDIP enable this fetaure |
23 | * For testing with ATmega88 PDIP enable this fetaure |
| Line 36... | Line 37... | ||
| 36 | /* lib files */ |
37 | /* lib files */ |
| 37 | #include <can.h> |
38 | #include <can.h> |
| 38 | #include <serial.h> |
39 | #include <serial.h> |
| 39 | #include <timebase.h> |
40 | #include <timebase.h> |
| 40 | /* defines */ |
41 | /* defines */ |
| 41 | #define OFF |
42 | #define OFF 0x00 |
| 42 | #define ON |
43 | #define ON 0x01 |
| 43 | #define Vref 5 |
44 | #define Vref 5 /* For Aref use 5, internal ref 1.1 */ |
| 44 | 45 | ||
| 45 | /*----------------------------------------------------------------------------- |
46 | /*----------------------------------------------------------------------------- |
| 46 | * Main Program |
47 | * Main Program |
| 47 | *---------------------------------------------------------------------------*/ |
48 | *---------------------------------------------------------------------------*/ |
| 48 | int main(void) { |
49 | int main(void) { |
| 49 | 50 | ||
| 50 | uint8_t relayStatus; |
51 | uint8_t relayStatus; |
| 51 | uint32_t boardTemperature; |
52 | uint32_t boardTemperature; |
| 52 | 53 | ||
| 53 | Timebase_Init(); |
54 | Timebase_Init(); |
| - | 55 | #if DEBUG |
|
| 54 | Serial_Init(); |
56 | Serial_Init(); |
| - | 57 | #endif |
|
| 55 | sei(); |
58 | sei(); |
| 56 | 59 | ||
| 57 | /* Turn relay off */ |
60 | /* Turn relay off */ |
| 58 | PORTC &= ~(1<<PC1); |
61 | PORTC &= ~(1<<PC1); |
| 59 | DDRC |= (1<<DDC1); |
62 | DDRC |= (1<<DDC1); |
| 60 | 63 | ||
| 61 | relayStatus = OFF; |
64 | relayStatus = OFF; |
| 62 | 65 | ||
| 63 | /* Initiate ADC for reading temperature sensor */ |
66 | /* Initiate ADC for reading temperature sensor */ |
| 64 | 67 | ||
| 65 | /* Wake up ADC */ |
68 | /* Wake up ADC */ |
| 66 | PRR &= ~(1<<PRADC); |
69 | PRR &= ~(1<<PRADC); |
| 67 | #if PDIP |
70 | #if PDIP |
| 68 | /* Enable AREF and ADC0 (For PDIP package) */ |
71 | /* Enable AREF and ADC0 (For PDIP package) */ |
| 69 | ADMUX = 0x00; // TODO 0x00 för aref, 0xc0 för internal 1,1 |
72 | ADMUX = 0x00; // TODO 0x00 för aref, 0xc0 för internal 1,1 |
| 70 | #else |
73 | #else |
| 71 | /* Enable AREF and ADC7 (For TQFP package) */ |
74 | /* Enable AREF and ADC7 (For TQFP package) */ |
| 72 | ADMUX = 0x07; |
75 | ADMUX = 0x07; |
| 73 | #endif |
76 | #endif |
| 74 | ADCSRA |= (1<<ADEN); |
77 | ADCSRA |= (1<<ADEN); |
| 75 | 78 | ||
| 76 | #if DEBUG |
79 | #if DEBUG |
| 77 | printf("\n------------------------------------------------------------\n"); |
80 | printf("\n------------------------------------------------------------\n"); |
| 78 | printf( " CAN: Relay\n"); |
81 | printf( " CAN: Relay\n"); |
| 79 | printf( "------------------------------------------------------------\n"); |
82 | printf( "------------------------------------------------------------\n"); |
| 80 | 83 | ||
| Line 83... | Line 86... | ||
| 83 | if (Can_Init() != CAN_OK) { |
86 | if (Can_Init() != CAN_OK) { |
| 84 | printf("FAILED!\n"); |
87 | printf("FAILED!\n"); |
| 85 | }else{ |
88 | }else{ |
| 86 | printf("OK!\n"); |
89 | printf("OK!\n"); |
| 87 | } |
90 | } |
| - | 91 | #else |
|
| - | 92 | Can_Init(); |
|
| 88 | #endif |
93 | #endif |
| 89 | uint32_t timeStamp = 0; |
94 | uint32_t timeStamp = 0; |
| 90 | 95 | ||
| 91 | Can_Message_t txMsg; |
96 | Can_Message_t txMsg; |
| 92 | Can_Message_t rxMsg; |
97 | Can_Message_t rxMsg; |
| Line 156... | Line 161... | ||
| 156 | /* Wait for conversion to complete */ |
161 | /* Wait for conversion to complete */ |
| 157 | while( ADCSRA & (1<<ADSC) ){} |
162 | while( ADCSRA & (1<<ADSC) ){} |
| 158 | 163 | ||
| 159 | txMsg.Id = 0x1201; // temporary ID |
164 | txMsg.Id = 0x1201; // temporary ID |
| 160 | 165 | ||
| 161 | txMsg.Data.bytes[ |
166 | txMsg.Data.bytes[2] = relayStatus; |
| 162 | 167 | ||
| 163 | /* Convert voltage to temperature */ |
168 | /* Convert voltage to temperature */ |
| 164 | 169 | ||
| 165 | boardTemperature = ADCW; |
170 | boardTemperature = ADCW; |
| 166 | 171 | ||
| 167 | boardTemperature = boardTemperature*Vref; |
172 | boardTemperature = boardTemperature*Vref; |
| 168 | boardTemperature = (boardTemperature*100/1024)-50; |
173 | boardTemperature = (boardTemperature*100/1024)-50; |
| 169 | 174 | ||
| 170 | /* Save and send */ |
175 | /* Save and send */ |
| 171 | txMsg.Data.bytes[0]= |
176 | txMsg.Data.bytes[0]= boardTemperature & 0x00FF; |
| 172 | txMsg.Data.bytes[1]= ( |
177 | txMsg.Data.bytes[1]= (boardTemperature & 0xFF00)>>8; |
| 173 | 178 | ||
| 174 | txMsg.DataLength = 3; |
179 | txMsg.DataLength = 3; |
| 175 | 180 | ||
| 176 | Can_Send( &txMsg ); |
181 | Can_Send( &txMsg ); |
| 177 | } |
182 | } |
| Line 191... | Line 196... | ||
| 191 | /* Wait for conversion to complete */ |
196 | /* Wait for conversion to complete */ |
| 192 | while( ADCSRA & (1<<ADSC) ){} |
197 | while( ADCSRA & (1<<ADSC) ){} |
| 193 | 198 | ||
| 194 | txMsg.Id = 0x1201; // temporary ID |
199 | txMsg.Id = 0x1201; // temporary ID |
| 195 | 200 | ||
| 196 | txMsg.Data.bytes[ |
201 | txMsg.Data.bytes[2] = relayStatus; |
| 197 | 202 | ||
| 198 | /* Convert voltage to temperature */ |
203 | /* Convert voltage to temperature */ |
| 199 | 204 | ||
| 200 | boardTemperature = ADCW; |
205 | boardTemperature = ADCW; |
| 201 | 206 | ||
| 202 | boardTemperature = boardTemperature*Vref; |
207 | boardTemperature = boardTemperature*Vref; |
| 203 | boardTemperature = (boardTemperature*100/1024)-50; |
208 | boardTemperature = (boardTemperature*100/1024)-50; |
| 204 | 209 | ||
| 205 | /* Save and send */ |
210 | /* Save and send */ |
| 206 | txMsg.Data.bytes[0]= |
211 | txMsg.Data.bytes[0]= boardTemperature & 0x00FF; |
| 207 | txMsg.Data.bytes[1]= ( |
212 | txMsg.Data.bytes[1]= (boardTemperature & 0xFF00)>>8; |
| 208 | 213 | ||
| 209 | txMsg.DataLength = 3; |
214 | txMsg.DataLength = 3; |
| 210 | Can_Send( &txMsg ); |
215 | Can_Send( &txMsg ); |
| 211 | #if DEBUG |
216 | #if DEBUG |
| 212 | printf("Message sent...\n"); |
217 | printf("Message sent...\n"); |