Subversion Repositories HomeAutomation

Rev

Rev 797 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * CanSensors.
  3.  *
  4.  * A general application for using multiple sensors.
  5.  * DS18S20, TC1047...
  6.  *
  7.  * @target  AVR
  8.  * @date    2007-01-16
  9.  * @author  Anders Runeson, Erik Larsson
  10.  *
  11.  * TODO: SHTxx
  12.  */
  13.  
  14. /*-----------------------------------------------------------------------------
  15.  * Defines
  16.  * --------------------------------------------------------------------------*/
  17.  
  18. /*-----------------------------------------------------------------------------
  19.  * Includes
  20.  *---------------------------------------------------------------------------*/
  21. /* system files */
  22. #include <avr/io.h>
  23. #include <avr/interrupt.h>
  24. #include <stdio.h>
  25. /* lib files */
  26. #include <config.h>
  27. #include <bios.h>
  28.  
  29. #include <drivers/timer/timebase.h>
  30.  
  31. #if defined(USE_DS18S20)
  32. #include <drivers/sensor/ds18s20/ds18x20.h>
  33. #include <drivers/sensor/ds18s20/onewire.h>
  34. #include <drivers/sensor/ds18s20/delay.h>
  35. #endif
  36.  
  37. #if defined(USE_TC1047)
  38. #include <drivers/sensor/tc1047/tc1047.h>
  39. #endif
  40.  
  41. #if defined(USE_LDR)
  42. #include <drivers/sensor/ldr/ldr.h>
  43. #endif
  44.  
  45. #define APP_TYPE    CAN_APPTYPES_SENSOR
  46. #define APP_VERSION 0x0001
  47.  
  48. /*-----------------------------------------------------------------------------
  49.  * Main Program
  50.  *---------------------------------------------------------------------------*/
  51. int main(void) {
  52. #if defined(USE_DS18S20)
  53.     uint8_t nSensors, i;
  54.     uint8_t subzero, cel, cel_frac_bits;
  55.     uint32_t timeStamp_DS = 0, timeStamp_DS_del = 0;
  56.     /* This is to identify and give the sensors correct ID
  57.      * TODO read ds18s20 serial id and that way give the "can id" */
  58.     uint16_t sensor_ds_id[] = {TEMPSENSORID_1, TEMPSENSORID_2, TEMPSENSORID_3, TEMPSENSORID_4};
  59. #endif
  60. #if defined(USE_TC1047)
  61.     uint16_t tcTemperature = 0, timeStamp_TC = 0;
  62.     adcTemperatureInit();
  63. #endif
  64. #if defined(USE_LDR)
  65.     uint32_t timeStamp_LDR = 0;
  66.     uint8_t ldr;
  67.     LDR_SensorInit();
  68. #endif
  69.     sei();
  70.  
  71.     Timebase_Init();
  72.  
  73.     Can_Message_t txMsg;
  74.     txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
  75.     txMsg.DataLength = 4;
  76.     txMsg.RemoteFlag = 0;
  77.     txMsg.ExtendedFlag = 1;
  78.     txMsg.Data.words[0] = APP_TYPE;
  79.     txMsg.Data.words[1] = APP_VERSION;
  80.     BIOS_CanSend(&txMsg);
  81.  
  82. #if defined(USE_DS18S20)
  83.     /* Make sure there is no more then 4 DS-sensors. */
  84.     nSensors = search_sensors();
  85. #endif
  86.  
  87.     txMsg.DataLength = 2;
  88.  
  89.     /* main loop */
  90.     while (1) {
  91. #if defined(USE_DS18S20)
  92.         /* check temperature and send on CAN */
  93.         if( Timebase_PassedTimeMillis(timeStamp_DS) >= DS_SEND_PERIOD ){
  94.             timeStamp_DS = Timebase_CurrentTime();
  95.  
  96. /*          txMsg.Id = 0xffffffff;
  97.             txMsg.DataLength = 0;
  98.             BIOS_CanSend(&txMsg);
  99. */
  100.             if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
  101.                 delay_ms(DS18B20_TCONV_12BIT);
  102.                 for ( i=0; i<nSensors; i++ ) {
  103.                     if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero, &cel, &cel_frac_bits) == DS18X20_OK ) {
  104.  
  105.                         if (subzero) {
  106.                             txMsg.Data.bytes[0] = -cel-1;
  107.                             txMsg.Data.bytes[1] = ~(cel_frac_bits<<4);
  108.                         }else{
  109.                             txMsg.Data.bytes[0] = cel;
  110.                             txMsg.Data.bytes[1] = (cel_frac_bits<<4);
  111.                         }
  112.                     }
  113.                     /* Delay */
  114.                     timeStamp_DS_del = Timebase_CurrentTime();
  115.                     while(Timebase_PassedTimeMillis(timeStamp_DS_del) < DS_SEND_DELAY){}
  116.  
  117.                     /* send txMsg */
  118.                     txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_TEMPERATURE << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID));
  119.                     txMsg.Id |= ( sensor_ds_id[i] << CAN_SHIFT_SNS_ID); /* Set sensor id */
  120.                     BIOS_CanSend(&txMsg);
  121.                 }
  122.             }
  123.         }
  124. #endif
  125. #if defined(USE_TC1047)
  126. #error tc1047 id is still not correct // FIXME
  127.             /* check temperature and send on CAN */
  128.         if( bios->timebase_get() - timeStamp_TC >= TC_SEND_PERIOD ){
  129.             timeStamp_TC = bios->timebase_get();
  130.  
  131.             tcTemperature = getTC1047temperature(TC1047_AD);
  132.             txMsg.Data.bytes[0] = tcTemperature&0xff;
  133.             txMsg.Data.bytes[1] = (tcTemperature>>8)&0xff;
  134.             txMsg.DataLength = 2;
  135.  
  136.             txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_TEMPERATURE << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID));
  137.             txMsg.Id |= (TEMPSENSORID_5 << CAN_SHIFT_SNS_ID); // sätt korrekt sensorid
  138.  
  139.             BIOS_CanSend(&txMsg);
  140.         }
  141. #endif
  142. #if defined(USE_LDR)
  143.             /* check light and send on CAN */
  144.         if( Timebase_PassedTimeMillis(timeStamp_LDR) >= LDR_SEND_PERIOD ){
  145.             timeStamp_LDR = Timebase_CurrentTime();
  146.  
  147.             ldr = (LDR_GetData(LDR_AD)>>2);
  148.             txMsg.Data.bytes[0] = ldr&0xff;
  149.             txMsg.DataLength = 1;
  150.  
  151.             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));
  152.  
  153.             BIOS_CanSend(&txMsg);
  154.         }
  155. #endif
  156.     }
  157.  
  158.     return 0;
  159. }
  160.