Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. /**
  2.  * CanSensors.
  3.  *
  4.  * A general application for using multiple sensors.
  5.  * ie DS18S20, SHT71...
  6.  *
  7.  * @target  AVR
  8.  * @date    2006-12-09
  9.  * @author  Jimmy Myhrman, Anders Runeson, Erik Larsson
  10.  *  
  11.  * Högst experimentell, decimaldel fel vid negativ temperatur?
  12.  * negativ temperatur helt otestad!
  13.  *
  14.  */
  15.  
  16. //#define DEBUG
  17. #define USE_DS18S20
  18. #define USE_SHT71
  19. #define USE_TC1047
  20.  
  21. /*-----------------------------------------------------------------------------
  22.  * Includes
  23.  *---------------------------------------------------------------------------*/
  24. /* system files */
  25. #include <avr/io.h>
  26. #include <avr/interrupt.h>
  27. #include <avr/wdt.h>
  28. #include <stdio.h>
  29. /* lib files */
  30. #include <can.h>
  31. #include <timebase.h>
  32.  
  33. #ifdef DEBUG
  34. #include <uart.h>
  35. #endif
  36.  
  37. #ifdef USE_DS18S20
  38. #include <onewire.h>
  39. #include <ds18x20.h>
  40. #include <delay.h>
  41. #endif
  42.  
  43. #ifdef USE_TC1047
  44. #include <tc1047.h>
  45. #endif
  46.  
  47. #ifdef USE_SHT71
  48. #include <sht7x.h>
  49. #endif
  50. /*-----------------------------------------------------------------------------
  51.  * Defines
  52.  * --------------------------------------------------------------------------*/
  53.  
  54.  
  55. /*-----------------------------------------------------------------------------
  56.  * Main Program
  57.  *---------------------------------------------------------------------------*/
  58. int main(void) {
  59. #ifdef USE_DS18S20
  60.     uint8_t nSensors, i;
  61.     uint8_t subzero, cel, cel_frac_bits;
  62. #endif
  63.     Mcu_Init();
  64.     Timebase_Init();
  65. #ifdef DEBUG
  66.     Serial_Init();
  67. #endif
  68. #ifdef USE_TC1047
  69.     uint32_t tcTemperature = 0;
  70.     adcTemperatureInit();
  71. #endif
  72. #ifdef USE_SHT71
  73.     HumidityInit();
  74. #endif
  75.     sei();
  76. #ifdef DEBUG
  77. #ifdef USE_DS18S20
  78.     printf( "\nDS18X20 1-Wire-Reader\n" );
  79.     printf( "-----------------------" );
  80.     nSensors = search_sensors();
  81.     printf( "%i DS18X20 Sensor(s) available:\n", (int) nSensors );
  82.    
  83.     for (i=0; i<nSensors; i++) {
  84.         printf("Sensor# %i is a ", (int) i+1);
  85.         if ( gSensorIDs[i][0] == DS18S20_ID)
  86.             printf("DS18S20/DS1820");
  87.         else printf("DS18B20");
  88.         printf(" which is ");
  89.         if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) ==
  90.             DS18X20_POWER_PARASITE )
  91.             printf( "parasite" );
  92.         else printf( "externally" );
  93.         printf( " powered\n" );
  94.     }
  95.    
  96.     printf("CanInit...\n");
  97.     if (Can_Init() != CAN_OK) {
  98.         printf("FAILED!\n");
  99.     }
  100.     else {
  101.         printf("OK!\n");
  102.     }
  103. #endif
  104. #else
  105.     Can_Init();
  106. #endif
  107.  
  108.     uint32_t timeStamp = 0;
  109.    
  110.     Can_Message_t txMsg;
  111.     Can_Message_t rxMsg;
  112.    
  113.     txMsg.DataLength = 2;
  114.     txMsg.Id = 0x450;
  115.     txMsg.RemoteFlag = 0;
  116.     txMsg.ExtendedFlag = 1;
  117.    
  118.     /* main loop */
  119.     while (1) {
  120.         /* service the CAN routines */
  121.         Can_Service();
  122.  
  123.         /* check if any messages have been received */
  124.         while (Can_Receive(&rxMsg) == CAN_OK) {
  125.            
  126.         }
  127.  
  128.         /* check temperature and send on CAN once every other second */
  129.         if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
  130.             timeStamp = Timebase_CurrentTime();
  131.  
  132. #ifdef USE_DS18S20
  133.             if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
  134. #ifdef DEBUG
  135.                 printf("Measuring temperature... ");
  136. #endif
  137.                 delay_ms(DS18B20_TCONV_12BIT);
  138.                 for ( i=0; i<nSensors; i++ ) {
  139.                     if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero,
  140.                             &cel, &cel_frac_bits) == DS18X20_OK ) {
  141.                        
  142.                         //txMsg.Data.bytes[0] = subzero;
  143.                         if (subzero) {
  144.                             txMsg.Data.bytes[i*2] = -cel;
  145.                             txMsg.Data.bytes[i*2+1] = ~(cel_frac_bits<<4);
  146.                         } else {
  147.                             txMsg.Data.bytes[i*2] = cel;
  148.                             txMsg.Data.bytes[i*2+1] = (cel_frac_bits<<4);
  149.                         }
  150.                        
  151.                     }
  152. #ifdef DEBUG
  153.                     else printf("CRC Error (lost connection?)\n");
  154. #endif
  155.                 }
  156.                
  157.                 txMsg.DataLength = nSensors*2;
  158. #ifdef DEBUG
  159.                 printf("sending...\n");
  160. #endif
  161.                 /* send txMsg */
  162.                 Can_Send(&txMsg);
  163.             }
  164. #ifdef DEBUG
  165.             else printf("Start meas. failed (short circuit?)\n");
  166. #endif
  167. #endif
  168. #ifdef USE_TC1047
  169.         tcTemperature = getTC1047temperature();
  170.         txMsg.Data.bytes[0] = tcTemperature & 0x00FF;
  171.         txMsg.Data.bytes[1] = (tcTemperature & 0xFF00)>>8;
  172.         txMsg.DataLength = 2;
  173.         txMsg.Id = 0x460;
  174.  
  175.         Can_Send(&txMsg);
  176. #endif
  177. #ifdef USE_SHT71
  178. //        HumidityMeasure(HUMIDITY_MEASURE_HUMI);
  179. //        HumidityMeasure(HUMIDITY_MEASURE_TEMP);
  180. #endif
  181.         }
  182.     }
  183.    
  184.     return 0;
  185. }
  186.