Subversion Repositories HomeAutomation

Rev

Rev 245 | 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 SENSOR_SEND 0x1500
  21. #define SENSOR_GET 0x1501
  22. #define SENSOR_SEND_DS 0x1502
  23. #define SENSOR_SEND_TC 0x1503
  24.  
  25. #define DS_SEND_PERIOD 5000
  26. #define TC_SEND_PERIOD 2000
  27.  
  28. /*-----------------------------------------------------------------------------
  29.  * Includes
  30.  *---------------------------------------------------------------------------*/
  31. /* system files */
  32. #include <avr/io.h>
  33. #include <avr/interrupt.h>
  34. #include <avr/wdt.h>
  35. #include <stdio.h>
  36. /* lib files */
  37. #include <can.h>
  38. #include <timebase.h>
  39.  
  40. #if defined(USE_DS18S20)
  41. #include <onewire.h>
  42. #include <ds18x20.h>
  43. #include <delay.h>
  44. #endif
  45.  
  46. #if defined(USE_TC1047)
  47. #include <tc1047.h>
  48. #endif
  49.  
  50. /*-----------------------------------------------------------------------------
  51.  * Defines
  52.  * --------------------------------------------------------------------------*/
  53.  
  54.  
  55. /*-----------------------------------------------------------------------------
  56.  * Main Program
  57.  *---------------------------------------------------------------------------*/
  58. int main(void) {
  59. #if defined(USE_DS18S20)
  60.     uint8_t nSensors, i;
  61.     uint8_t subzero, cel, cel_frac_bits;
  62.     uint32_t timeStamp_DS = 0;
  63. #endif
  64. #if defined(USE_TC1047)
  65.     uint32_t tcTemperature = 0, timeStamp_TC = 0;
  66.     adcTemperatureInit();
  67. #endif
  68.     Timebase_Init();
  69.     sei();
  70.  
  71. #if defined(USE_DS18S20)
  72.     /* Make sure there is no more then 4 DS-sensors. */
  73.     nSensors = search_sensors();
  74. #endif
  75.     Can_Init();
  76.  
  77.     Can_Message_t txMsg;
  78.     Can_Message_t rxMsg;
  79.  
  80.     txMsg.ExtendedFlag = 1;
  81.  
  82.     /* main loop */
  83.     while (1) {
  84.         /* service the CAN routines */
  85.        Can_Service();
  86.  
  87.         /* check if any messages have been received */
  88.         while (Can_Receive(&rxMsg) == CAN_OK) {
  89.  
  90.         }
  91.  
  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,
  101.                         &cel, &cel_frac_bits) == DS18X20_OK ) {
  102.  
  103.                         //txMsg.Data.bytes[0] = subzero;
  104.                         if (subzero) {
  105.                             txMsg.Data.bytes[i*2] = -cel;
  106.                             txMsg.Data.bytes[i*2+1] = ~(cel_frac_bits<<4);
  107.                         }else{
  108.                             txMsg.Data.bytes[i*2] = cel;
  109.                             txMsg.Data.bytes[i*2+1] = (cel_frac_bits<<4);
  110.                         }
  111.                     }
  112.                 }
  113.                 txMsg.DataLength = nSensors*2;
  114.                 /* send txMsg */
  115.                 txMsg.Id = SENSOR_SEND_DS;
  116.                 Can_Send(&txMsg);
  117.             }
  118.         }
  119. #endif
  120. #if defined(USE_TC1047)
  121.             /* check temperature and send on CAN */
  122.         if (Timebase_PassedTimeMillis(timeStamp_TC) >= TC_SEND_PERIOD) {
  123.             timeStamp_TC = Timebase_CurrentTime();
  124.  
  125.             tcTemperature = getTC1047temperature();
  126.             txMsg.Data.bytes[0] = tcTemperature & 0x00FF;
  127.             txMsg.Data.bytes[1] = (tcTemperature & 0xFF00)>>8;
  128.             txMsg.DataLength = 2;
  129.             txMsg.Id = SENSOR_SEND_TC;
  130.  
  131.             Can_Send(&txMsg);
  132.         }
  133. #endif
  134.     }
  135.  
  136.     return 0;
  137. }
  138.