Subversion Repositories HomeAutomation

Rev

Rev 563 | 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. #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.  
  28. /*-----------------------------------------------------------------------------
  29.  * Includes
  30.  *---------------------------------------------------------------------------*/
  31. /* system files */
  32. #include <avr/io.h>
  33. #include <avr/interrupt.h>
  34. #include <stdio.h>
  35. /* lib files */
  36. #include <bios.h>
  37. #include <config.h>
  38. #include <timebase.h>
  39.  
  40. #if defined(USE_DS18S20)
  41. #include <ds18x20.h>
  42. #include <onewire.h>
  43. #include <delay.h>
  44. #endif
  45.  
  46. #if defined(USE_TC1047)
  47. #include <tc1047.h>
  48. #endif
  49.  
  50. #if defined(USE_LDR)
  51. #include <ldr.h>
  52. #endif
  53.  
  54. /*-----------------------------------------------------------------------------
  55.  * Main Program
  56.  *---------------------------------------------------------------------------*/
  57. int main(void) {
  58. #if defined(USE_DS18S20)
  59.     uint8_t nSensors, i;
  60.     uint8_t subzero, cel, cel_frac_bits;
  61.     uint32_t timeStamp_DS = 0, timeStamp_DS_del = 0;
  62.     /* This is to identify and give the sensors correct ID (For eqlazers net)
  63.      * TODO read ds18s20 serial id and that way give the "can id" */
  64.     uint16_t sensor_ds_id[] = {SNS_DS18S20_FREEZER, SNS_DS18S20_INSIDE1, SNS_DS18S20_OUTSIDE, SNS_DS18S20_INSIDE2};
  65. #endif
  66. #if defined(USE_TC1047)
  67.     uint32_t tcTemperature = 0, timeStamp_TC = 0;
  68.     adcTemperatureInit();
  69. #endif
  70. #if defined(USE_LDR)
  71.     uint32_t timeStamp_LDR = 0;
  72.     uint8_t ldr;
  73.     LDR_SensorInit();
  74. #endif
  75.     sei();
  76.  
  77.     Timebase_Init();
  78.  
  79. #if defined(USE_DS18S20)
  80.     /* Make sure there is no more then 4 DS-sensors. */
  81.     nSensors = search_sensors();
  82. #endif
  83.  
  84.     Can_Message_t txMsg;
  85.  
  86.     txMsg.ExtendedFlag = 1;
  87.     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.  
  92.     /* main loop */
  93.     while (1) {
  94. #if defined(USE_DS18S20)
  95.         /* check temperature and send on CAN */
  96.         if( Timebase_PassedTimeMillis(timeStamp_DS) >= DS_SEND_PERIOD ){
  97.             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.  
  102.             if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
  103.                 delay_ms(DS18B20_TCONV_12BIT);
  104.                 for ( i=0; i<nSensors; i++ ) {
  105.                     if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero,
  106.                         &cel, &cel_frac_bits) == DS18X20_OK ) {
  107.  
  108.                         if (subzero) {
  109.                             txMsg.Data.bytes[0] = -cel;
  110.                             txMsg.Data.bytes[1] = ~(cel_frac_bits<<4);
  111.                         }else{
  112.                             txMsg.Data.bytes[0] = cel;
  113.                             txMsg.Data.bytes[1] = (cel_frac_bits<<4);
  114.                         }
  115.                     }
  116.                     /* Delay */
  117.                     timeStamp_DS_del = Timebase_CurrentTime();
  118.                     while(Timebase_PassedTimeMillis(timeStamp_DS_del) < DS_SEND_DELAY){}
  119.  
  120.                     /* send txMsg */
  121.                     txMsg.Id &= ~SNS_ID_MASK; /* Clear sensor id */
  122.                     txMsg.Id |= ( sensor_ds_id[i] << SNS_ID_BITS); /* Set sensor id */
  123.                     BIOS_CanSend(&txMsg);
  124.                 }
  125.             }
  126.         }
  127. #endif
  128. #if defined(USE_TC1047)
  129. #error tc1047 id is still not correct // FIXME
  130.             /* check temperature and send on CAN */
  131.         if( bios->timebase_get() - timeStamp_TC >= TC_SEND_PERIOD ){
  132.             timeStamp_TC = bios->timebase_get();
  133.  
  134.             tcTemperature = getTC1047temperature();
  135.             txMsg.Data.bytes[0] = tcTemperature & 0x00FF;
  136.             txMsg.Data.bytes[1] = (tcTemperature & 0xFF00)>>8;
  137.             txMsg.DataLength = 2;
  138.  
  139.             txMsg.Id &= ~TYPE_MASK; // rensa type
  140.             txMsg.Id |= (SNS_TEMP_TC1047 << TYPE_MASK_BITS); // sätt korrekt type
  141.  
  142.             BIOS_CanSend(&txMsg);
  143.         }
  144. #endif
  145. #if defined(USE_LDR)
  146.             /* check light and send on CAN */
  147.         if( Timebase_PassedTimeMillis(timeStamp_LDR) >= LDR_SEND_PERIOD ){
  148.             timeStamp_LDR = Timebase_CurrentTime();
  149.  
  150.             ldr = (uint8_t)LDR_GetData(0);
  151.             txMsg.Data.bytes[0] = ldr;
  152.             txMsg.DataLength = 1;
  153.  
  154.             txMsg.Id &= ~TYPE_MASK; /* Clear TYPE */
  155.             txMsg.Id |= (SNS_LIGHT_LDR << SNS_TYPE_BITS); /* Set SNS_TYPE */
  156.             txMsg.Id &= ~SNS_ID_MASK; /* Clear sensor id */
  157.             txMsg.Id |= ( SNS_LDR_TEST1 << SNS_ID_BITS); /* Set sensor id */
  158.  
  159.             BIOS_CanSend(&txMsg);
  160.         }
  161. #endif
  162.     }
  163.  
  164.     return 0;
  165. }
  166.