Subversion Repositories HomeAutomation

Rev

Rev 515 | Rev 571 | Go to most recent revision | 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 <bios.h>
  27. #include <config.h>
  28. //#include <settings.h> //Migrated to config.inc
  29.  
  30. #include <drivers/timer/timebase.h>
  31.  
  32. #if defined(USE_DS18S20)
  33. #include <drivers/sensor/ds18s20/ds18x20.h>
  34. #include <drivers/sensor/ds18s20/onewire.h>
  35. #include <drivers/sensor/ds18s20/delay.h>
  36. #endif
  37.  
  38. #if defined(USE_TC1047)
  39. #include <drivers/sensor/tc1047/tc1047.h>
  40. #endif
  41.  
  42. #if defined(USE_LDR)
  43. #include <drivers/sensor/ldr/ldr.h>
  44. #endif
  45.  
  46. #define APP_TYPE    CAN_APPTYPES_SENSOR
  47. #define APP_VERSION 0x0001
  48.  
  49. /*-----------------------------------------------------------------------------
  50.  * Main Program
  51.  *---------------------------------------------------------------------------*/
  52. int main(void) {
  53. #if defined(USE_DS18S20)
  54.     uint8_t nSensors, i;
  55.     uint8_t subzero, cel, cel_frac_bits;
  56.     uint32_t timeStamp_DS = 0, timeStamp_DS_del = 0;
  57.     /* This is to identify and give the sensors correct ID
  58.      * TODO read ds18s20 serial id and that way give the "can id" */
  59.     uint16_t sensor_ds_id[] = {TEMPSENSORID_1, TEMPSENSORID_2, TEMPSENSORID_3, TEMPSENSORID_4};
  60. #endif
  61. #if defined(USE_TC1047)
  62.     uint32_t tcTemperature = 0, timeStamp_TC = 0;
  63.     adcTemperatureInit();
  64. #endif
  65. #if defined(USE_LDR)
  66.     uint32_t timeStamp_LDR = 0;
  67.     uint8_t ldr;
  68.     LDR_SensorInit();
  69. #endif
  70.     sei();
  71.  
  72.     Timebase_Init();
  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.  
  83. #if defined(USE_DS18S20)
  84.     /* Make sure there is no more then 4 DS-sensors. */
  85.     nSensors = search_sensors();
  86. #endif
  87.  
  88.     txMsg.DataLength = 2;
  89.  
  90.     /* main loop */
  91.     while (1) {
  92. #if defined(USE_DS18S20)
  93.         /* check temperature and send on CAN */
  94.         if( Timebase_PassedTimeMillis(timeStamp_DS) >= DS_SEND_PERIOD ){
  95.             timeStamp_DS = Timebase_CurrentTime();
  96.  
  97.             if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
  98.                 delay_ms(DS18B20_TCONV_12BIT);
  99.                 for ( i=0; i<nSensors; i++ ) {
  100.                     if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero, &cel, &cel_frac_bits) == DS18X20_OK ) {
  101.  
  102.                         if (subzero) {
  103.                             txMsg.Data.bytes[0] = -cel-1;
  104.                             txMsg.Data.bytes[1] = ~(cel_frac_bits<<4);
  105.                         }else{
  106.                             txMsg.Data.bytes[0] = cel;
  107.                             txMsg.Data.bytes[1] = (cel_frac_bits<<4);
  108.                         }
  109.                     }
  110.                     /* Delay */
  111.                     timeStamp_DS_del = Timebase_CurrentTime();
  112.                     while(Timebase_PassedTimeMillis(timeStamp_DS_del) < DS_SEND_DELAY){}
  113.  
  114.                     /* send txMsg */
  115.                     txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_TEMPERATURE << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID));
  116.                     txMsg.Id |= ( sensor_ds_id[i] << CAN_SHIFT_SNS_ID); /* Set sensor id */
  117.                     BIOS_CanSend(&txMsg);
  118.                 }
  119.             }
  120.         }
  121. #endif
  122. #if defined(USE_TC1047)
  123. #error tc1047 id is still not correct // FIXME
  124.             /* check temperature and send on CAN */
  125.         if( bios->timebase_get() - timeStamp_TC >= TC_SEND_PERIOD ){
  126.             timeStamp_TC = bios->timebase_get();
  127.  
  128.             tcTemperature = getTC1047temperature();
  129.             txMsg.Data.bytes[0] = tcTemperature & 0x00FF;
  130.             txMsg.Data.bytes[1] = (tcTemperature & 0xFF00)>>8;
  131.             txMsg.DataLength = 2;
  132.  
  133.             txMsg.Id = ((CAN_SNS << CAN_SHIFT_CLASS) | (SNS_TYPE_TEMPERATURE << CAN_SHIFT_SNS_TYPE) | (NODE_ID << CAN_SHIFT_SNS_SID));
  134.             txMsg.Id |= (TEMPSENSORID_5 << CAN_SHIFT_SNS_ID); // sätt korrekt sensorid
  135.  
  136.             BIOS_CanSend(&txMsg);
  137.         }
  138. #endif
  139. #if defined(USE_LDR)
  140.             /* check light and send on CAN */
  141.         if( Timebase_PassedTimeMillis(timeStamp_LDR) >= LDR_SEND_PERIOD ){
  142.             timeStamp_LDR = Timebase_CurrentTime();
  143.  
  144.             ldr = (uint8_t)LDR_GetData(0);
  145.             txMsg.Data.bytes[0] = ldr;
  146.             txMsg.DataLength = 1;
  147.  
  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));
  149.  
  150.             BIOS_CanSend(&txMsg);
  151.         }
  152. #endif
  153.     }
  154.  
  155.     return 0;
  156. }
  157.