Subversion Repositories HomeAutomation

Rev

Rev 350 | Blame | 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  Jimmy Myhrman, Anders Runeson, Erik Larsson
  10.  *
  11.  * TODO: SHTxx
  12.  */
  13.  
  14. /*-----------------------------------------------------------------------------
  15.  * Defines
  16.  * --------------------------------------------------------------------------*/
  17. #define USE_DS18S20
  18. //#define USE_TC1047
  19.  
  20. #define DS_SEND_PERIOD  10000
  21. #define DS_SEND_DELAY   2000
  22. #define TC_SEND_PERIOD  10000
  23.  
  24. #include <funcdefs.h>
  25.  
  26. /*-----------------------------------------------------------------------------
  27.  * Includes
  28.  *---------------------------------------------------------------------------*/
  29. /* system files */
  30. #include <avr/io.h>
  31. #include <avr/interrupt.h>
  32. #include <avr/wdt.h>
  33. #include <stdio.h>
  34. /* lib files */
  35. #include <bios.h>
  36.  
  37. #if defined(USE_DS18S20)
  38. #include <ds18x20.h>
  39. #include <onewire.h>
  40. #include <delay.h>
  41. #endif
  42.  
  43. #if defined(USE_TC1047)
  44. #include <tc1047.h>
  45. #endif
  46.  
  47. /*-----------------------------------------------------------------------------
  48.  * Main Program
  49.  *---------------------------------------------------------------------------*/
  50. int main(void) {
  51. #if defined(USE_DS18S20)
  52.     uint8_t nSensors, i;
  53.     uint8_t subzero, cel, cel_frac_bits;
  54.     uint32_t timeStamp_DS = 0, timeStamp_DS_del = 0;
  55.     /* This is to identify and give the sensors correct ID (For eqlazers net)
  56.      * TODO read ds18s20 serial id and that way give the "can id" */
  57.     uint16_t sensor_ds_id[] = {SNS_DS18S20_FREEZER, SNS_DS18S20_INSIDE1, SNS_DS18S20_OUTSIDE, SNS_DS18S20_INSIDE2};
  58. #endif
  59. #if defined(USE_TC1047)
  60.     uint32_t tcTemperature = 0, timeStamp_TC = 0;
  61.     adcTemperatureInit();
  62. #endif
  63.     sei();
  64.  
  65. #if defined(USE_DS18S20)
  66.     /* Make sure there is no more then 4 DS-sensors. */
  67.     nSensors = search_sensors();
  68. #endif
  69.  
  70.     Can_Message_t txMsg;
  71.  
  72.     txMsg.ExtendedFlag = 1;
  73.     txMsg.DataLength = 2;
  74.  
  75.     /* Set class and sid (sid=NODE_ID from bios.h) */
  76.     txMsg.Id = (CLASS_SNS << CLASS_MASK_BITS)|(NODE_ID);
  77.  
  78.     /* main loop */
  79.     while (1) {
  80. #if defined(USE_DS18S20)
  81.         /* check temperature and send on CAN */
  82.         if( bios->timebase_get() - timeStamp_DS >= DS_SEND_PERIOD ){
  83.             timeStamp_DS = bios->timebase_get();
  84.  
  85.             txMsg.Id &= ~TYPE_MASK; /* Clear TYPE */
  86.             txMsg.Id |= (SNS_TEMP_DS18S20 << SNS_TYPE_BITS); /* Set SNS_TYPE */
  87.  
  88.             if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
  89.                 delay_ms(DS18B20_TCONV_12BIT);
  90.                 for ( i=0; i<nSensors; i++ ) {
  91.                     if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero,
  92.                         &cel, &cel_frac_bits) == DS18X20_OK ) {
  93.  
  94.                         if (subzero) {
  95.                             txMsg.Data.bytes[0] = -cel;
  96.                             txMsg.Data.bytes[1] = ~(cel_frac_bits<<4);
  97.                         }else{
  98.                             txMsg.Data.bytes[0] = cel;
  99.                             txMsg.Data.bytes[1] = (cel_frac_bits<<4);
  100.                         }
  101.                     }
  102.                     /* Delay */
  103.                     timeStamp_DS_del = bios->timebase_get();
  104.                     while(bios->timebase_get() - timeStamp_DS_del < DS_SEND_DELAY){}
  105.  
  106.                     /* send txMsg */
  107.                     txMsg.Id &= ~SNS_ID_MASK; /* Clear sensor id */
  108.                     txMsg.Id |= ( sensor_ds_id[i] << SNS_ID_BITS); /* Set sesor id */
  109.                     bios->can_send(&txMsg);
  110.                 }
  111.             }
  112.         }
  113. #endif
  114. #if defined(USE_TC1047)
  115. #error msg id is still not correct // FIXME
  116.             /* check temperature and send on CAN */
  117.         if( bios->timebase_get() - timeStamp_TC >= TC_SEND_PERIOD ){
  118.             timeStamp_TC = bios->timebase_get();
  119.  
  120.             tcTemperature = getTC1047temperature();
  121.             txMsg.Data.bytes[0] = tcTemperature & 0x00FF;
  122.             txMsg.Data.bytes[1] = (tcTemperature & 0xFF00)>>8;
  123.             txMsg.DataLength = 2;
  124.  
  125.             txMsg.Id &= ~TYPE_MASK; // rensa type
  126.             txMsg.Id |= (SNS_TEMP_TC1047 << TYPE_MASK_BITS); // sätt korrekt type
  127.  
  128.             //Can_Send(&txMsg);
  129.             bios->can_send(&txMsg);
  130.         }
  131. #endif
  132.     }
  133.  
  134.     return 0;
  135. }
  136.