Rev 406 | Rev 563 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 406 | Rev 515 | ||
|---|---|---|---|
| Line 12... | Line 12... | ||
| 12 | */ |
12 | */ |
| 13 | 13 | ||
| 14 | /*----------------------------------------------------------------------------- |
14 | /*----------------------------------------------------------------------------- |
| 15 | * Defines |
15 | * Defines |
| 16 | * --------------------------------------------------------------------------*/ |
16 | * --------------------------------------------------------------------------*/ |
| 17 | #define USE_DS18S20 |
- | |
| 18 | //#define USE_TC1047 |
- | |
| 19 | #define USE_LDR |
- | |
| 20 | - | ||
| 21 | #define DS_SEND_PERIOD 10000 |
- | |
| 22 | #define DS_SEND_DELAY 2000 |
- | |
| 23 | #define TC_SEND_PERIOD 10000 |
- | |
| 24 | #define LDR_SEND_PERIOD 10000 |
- | |
| 25 | - | ||
| 26 | #include <funcdefs/funcdefs.h> |
- | |
| 27 | 17 | ||
| 28 | /*----------------------------------------------------------------------------- |
18 | /*----------------------------------------------------------------------------- |
| 29 | * Includes |
19 | * Includes |
| 30 | *---------------------------------------------------------------------------*/ |
20 | *---------------------------------------------------------------------------*/ |
| 31 | /* system files */ |
21 | /* system files */ |
| Line 33... | Line 23... | ||
| 33 | #include <avr/interrupt.h> |
23 | #include <avr/interrupt.h> |
| 34 | #include <stdio.h> |
24 | #include <stdio.h> |
| 35 | /* lib files */ |
25 | /* lib files */ |
| 36 | #include <bios.h> |
26 | #include <bios.h> |
| 37 | #include <config.h> |
27 | #include <config.h> |
| 38 | #include < |
28 | #include <settings.h> |
| - | 29 | ||
| - | 30 | #include <drivers/timer/timebase.h> |
|
| 39 | 31 | ||
| 40 | #if defined(USE_DS18S20) |
32 | #if defined(USE_DS18S20) |
| 41 | #include <ds18x20.h> |
33 | #include <drivers/sensor/ds18s20/ds18x20.h> |
| 42 | #include <onewire.h> |
34 | #include <drivers/sensor/ds18s20/onewire.h> |
| 43 | #include <delay.h> |
35 | #include <drivers/sensor/ds18s20/delay.h> |
| 44 | #endif |
36 | #endif |
| 45 | 37 | ||
| 46 | #if defined(USE_TC1047) |
38 | #if defined(USE_TC1047) |
| 47 | #include <tc1047.h> |
39 | #include <drivers/sensor/tc1047/tc1047.h> |
| 48 | #endif |
40 | #endif |
| 49 | 41 | ||
| 50 | #if defined(USE_LDR) |
42 | #if defined(USE_LDR) |
| 51 | #include <ldr.h> |
43 | #include <drivers/sensor/ldr/ldr.h> |
| 52 | #endif |
44 | #endif |
| - | 45 | ||
| - | 46 | #define APP_TYPE CAN_APPTYPES_SENSOR |
|
| - | 47 | #define APP_VERSION 0x0001 |
|
| 53 | 48 | ||
| 54 | /*----------------------------------------------------------------------------- |
49 | /*----------------------------------------------------------------------------- |
| 55 | * Main Program |
50 | * Main Program |
| 56 | *---------------------------------------------------------------------------*/ |
51 | *---------------------------------------------------------------------------*/ |
| 57 | int main(void) { |
52 | int main(void) { |
| 58 | #if defined(USE_DS18S20) |
53 | #if defined(USE_DS18S20) |
| 59 | uint8_t nSensors, i; |
54 | uint8_t nSensors, i; |
| 60 | uint8_t subzero, cel, cel_frac_bits; |
55 | uint8_t subzero, cel, cel_frac_bits; |
| 61 | uint32_t timeStamp_DS = 0, timeStamp_DS_del = 0; |
56 | uint32_t timeStamp_DS = 0, timeStamp_DS_del = 0; |
| 62 | /* This is to identify and give the sensors correct ID |
57 | /* This is to identify and give the sensors correct ID |
| 63 | * TODO read ds18s20 serial id and that way give the "can id" */ |
58 | * TODO read ds18s20 serial id and that way give the "can id" */ |
| 64 | uint16_t sensor_ds_id[] = { |
59 | uint16_t sensor_ds_id[] = {TEMPSENSORID_1, TEMPSENSORID_2, TEMPSENSORID_3, TEMPSENSORID_4}; |
| 65 | #endif |
60 | #endif |
| 66 | #if defined(USE_TC1047) |
61 | #if defined(USE_TC1047) |
| 67 | uint32_t tcTemperature = 0, timeStamp_TC = 0; |
62 | uint32_t tcTemperature = 0, timeStamp_TC = 0; |
| 68 | adcTemperatureInit(); |
63 | adcTemperatureInit(); |
| 69 | #endif |
64 | #endif |
| Line 73... | Line 68... | ||
| 73 | LDR_SensorInit(); |
68 | LDR_SensorInit(); |
| 74 | #endif |
69 | #endif |
| 75 | sei(); |
70 | sei(); |
| 76 | 71 | ||
| 77 | Timebase_Init(); |
72 | Timebase_Init(); |
| 78 | 73 | ||
| - | 74 | Can_Message_t txMsg; |
|
| - | 75 | txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID); |
|
| - | 76 | txMsg.DataLength = 4; |
|
| - | 77 | txMsg.RemoteFlag = 0; |
|
| - | 78 | txMsg.ExtendedFlag = 1; |
|
| - | 79 | txMsg.Data.words[0] = APP_TYPE; |
|
| - | 80 | txMsg.Data.words[1] = APP_VERSION; |
|
| - | 81 | BIOS_CanSend(&txMsg); |
|
| - | 82 | ||
| 79 | #if defined(USE_DS18S20) |
83 | #if defined(USE_DS18S20) |
| 80 | /* Make sure there is no more then 4 DS-sensors. */ |
84 | /* Make sure there is no more then 4 DS-sensors. */ |
| 81 | nSensors = search_sensors(); |
85 | nSensors = search_sensors(); |
| 82 | #endif |
86 | #endif |
| 83 | 87 | ||
| 84 | Can_Message_t txMsg; |
- | |
| 85 | - | ||
| 86 | txMsg.ExtendedFlag = 1; |
- | |
| 87 | txMsg.DataLength = 2; |
88 | txMsg.DataLength = 2; |
| 88 | - | ||
| 89 | /* Set class and sid (sid=NODE_ID from bios.h) */ |
- | |
| 90 | txMsg.Id = (CLASS_SNS << CLASS_MASK_BITS)|(NODE_ID); |
- | |
| 91 | 89 | ||
| 92 | /* main loop */ |
90 | /* main loop */ |
| 93 | while (1) { |
91 | while (1) { |
| 94 | #if defined(USE_DS18S20) |
92 | #if defined(USE_DS18S20) |
| 95 | /* check temperature and send on CAN */ |
93 | /* check temperature and send on CAN */ |
| 96 | if( Timebase_PassedTimeMillis(timeStamp_DS) >= DS_SEND_PERIOD ){ |
94 | if( Timebase_PassedTimeMillis(timeStamp_DS) >= DS_SEND_PERIOD ){ |
| 97 | timeStamp_DS = Timebase_CurrentTime(); |
95 | timeStamp_DS = Timebase_CurrentTime(); |
| 98 | - | ||
| 99 | txMsg.Id &= ~TYPE_MASK; /* Clear TYPE */ |
- | |
| 100 | txMsg.Id |= (SNS_TEMP_DS18S20 << SNS_TYPE_BITS); /* Set SNS_TYPE */ |
- | |
| 101 | 96 | ||
| 102 | if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { |
97 | if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) { |
| 103 | delay_ms(DS18B20_TCONV_12BIT); |
98 | delay_ms(DS18B20_TCONV_12BIT); |
| 104 | for ( i=0; i<nSensors; i++ ) { |
99 | for ( i=0; i<nSensors; i++ ) { |
| 105 | if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero, |
100 | if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero, &cel, &cel_frac_bits) == DS18X20_OK ) { |
| 106 | &cel, &cel_frac_bits) == DS18X20_OK ) { |
- | |
| 107 | 101 | ||
| 108 | if (subzero) { |
102 | if (subzero) { |
| 109 | txMsg.Data.bytes[0] = -cel; |
103 | txMsg.Data.bytes[0] = -cel-1; |
| 110 | txMsg.Data.bytes[1] = ~(cel_frac_bits<<4); |
104 | txMsg.Data.bytes[1] = ~(cel_frac_bits<<4); |
| 111 | }else{ |
105 | }else{ |
| 112 | txMsg.Data.bytes[0] = cel; |
106 | txMsg.Data.bytes[0] = cel; |
| 113 | txMsg.Data.bytes[1] = (cel_frac_bits<<4); |
107 | txMsg.Data.bytes[1] = (cel_frac_bits<<4); |
| 114 | } |
108 | } |
| Line 116... | Line 110... | ||
| 116 | /* Delay */ |
110 | /* Delay */ |
| 117 | timeStamp_DS_del = Timebase_CurrentTime(); |
111 | timeStamp_DS_del = Timebase_CurrentTime(); |
| 118 | while(Timebase_PassedTimeMillis(timeStamp_DS_del) < DS_SEND_DELAY){} |
112 | while(Timebase_PassedTimeMillis(timeStamp_DS_del) < DS_SEND_DELAY){} |
| 119 | 113 | ||
| 120 | /* send txMsg */ |
114 | /* send txMsg */ |
| 121 | txMsg.Id |
115 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_TEMPERATURE << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
| 122 | txMsg.Id |= ( sensor_ds_id[i] << |
116 | txMsg.Id |= ( sensor_ds_id[i] << CAN_SHIFT_SNS_ID); /* Set sensor id */ |
| 123 | BIOS_CanSend(&txMsg); |
117 | BIOS_CanSend(&txMsg); |
| 124 | } |
118 | } |
| 125 | } |
119 | } |
| 126 | } |
120 | } |
| 127 | #endif |
121 | #endif |
| Line 134... | Line 128... | ||
| 134 | tcTemperature = getTC1047temperature(); |
128 | tcTemperature = getTC1047temperature(); |
| 135 | txMsg.Data.bytes[0] = tcTemperature & 0x00FF; |
129 | txMsg.Data.bytes[0] = tcTemperature & 0x00FF; |
| 136 | txMsg.Data.bytes[1] = (tcTemperature & 0xFF00)>>8; |
130 | txMsg.Data.bytes[1] = (tcTemperature & 0xFF00)>>8; |
| 137 | txMsg.DataLength = 2; |
131 | txMsg.DataLength = 2; |
| 138 | 132 | ||
| 139 | txMsg.Id |
133 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_TEMPERATURE << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
| 140 | txMsg.Id |= ( |
134 | txMsg.Id |= (TEMPSENSORID_5 << CAN_SHIFT_SNS_ID); // sätt korrekt sensorid |
| 141 | 135 | ||
| 142 | BIOS_CanSend(&txMsg); |
136 | BIOS_CanSend(&txMsg); |
| 143 | } |
137 | } |
| 144 | #endif |
138 | #endif |
| 145 | #if defined(USE_LDR) |
139 | #if defined(USE_LDR) |
| Line 149... | Line 143... | ||
| 149 | 143 | ||
| 150 | ldr = (uint8_t)LDR_GetData(0); |
144 | ldr = (uint8_t)LDR_GetData(0); |
| 151 | txMsg.Data.bytes[0] = ldr; |
145 | txMsg.Data.bytes[0] = ldr; |
| 152 | txMsg.DataLength = 1; |
146 | txMsg.DataLength = 1; |
| 153 | 147 | ||
| 154 | txMsg.Id &= ~TYPE_MASK; /* Clear TYPE */ |
- | |
| 155 | txMsg.Id |
148 | txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_LIGHT << CAN_SHIFT_SNS_TYPE) | (LIGHTSENSORID_1 << CAN_SHIFT_SNS_ID) | (NODE_ID << CAN_SHIFT_SNS_SID)); |
| 156 | txMsg.Id &= ~SNS_ID_MASK; /* Clear sensor id */ |
- | |
| 157 | txMsg.Id |= ( SNS_LDR_TEST1 << SNS_ID_BITS); /* Set sensor id */ |
- | |
| 158 | 149 | ||
| 159 | BIOS_CanSend(&txMsg); |
150 | BIOS_CanSend(&txMsg); |
| 160 | } |
151 | } |
| 161 | #endif |
152 | #endif |
| 162 | } |
153 | } |