Subversion Repositories HomeAutomation

Rev

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

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