Subversion Repositories HomeAutomation

Rev

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