Subversion Repositories HomeAutomation

Rev

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.  
  31.  
  32.  
  33. #define MAXSENSORS 4
  34.  
  35. uint8_t gSensorIDs[MAXSENSORS][OW_ROMCODE_SIZE];
  36.  
  37. uint8_t search_sensors(void)
  38. {
  39.     uint8_t i;
  40.     uint8_t id[OW_ROMCODE_SIZE];
  41.     uint8_t diff, nSensors;
  42.    
  43.     printf( "\nScanning Bus for DS18X20\n" );
  44.    
  45.     nSensors = 0;
  46.    
  47.     for( diff = OW_SEARCH_FIRST;
  48.         diff != OW_LAST_DEVICE && nSensors < MAXSENSORS ; )
  49.     {
  50.         DS18X20_find_sensor( &diff, &id[0] );
  51.        
  52.         if( diff == OW_PRESENCE_ERR ) {
  53.             printf( "No Sensor found\n" );
  54.             break;
  55.         }
  56.        
  57.         if( diff == OW_DATA_ERR ) {
  58.             printf( "Bus Error\n" );
  59.             break;
  60.         }
  61.        
  62.         for (i=0;i<OW_ROMCODE_SIZE;i++)
  63.             gSensorIDs[nSensors][i]=id[i];
  64.        
  65.         nSensors++;
  66.     }
  67.    
  68.     return nSensors;
  69. }
  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 = 3;
  117.     txMsg.RemoteFlag = 0;
  118.     txMsg.ExtendedFlag = 1;
  119.    
  120.     /* main loop */
  121.     while (1) {
  122.         /* service the CAN routines */
  123.         Can_Service();
  124.  
  125.         /* check if any messages have been received */
  126.         while (Can_Receive(&rxMsg) == CAN_OK) {
  127.             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));
  128.             printf("data={ ");
  129.             for (uint8_t i=0; i<rxMsg.DataLength; i++) {
  130.                 printf("%x ", rxMsg.Data.bytes[i]);
  131.             }
  132.             printf("}\n");
  133.         }
  134.  
  135.         /* check temperature and send on CAN once every other second */
  136.         if (Timebase_PassedTimeMillis(timeStamp) >= 2000) {
  137.             timeStamp = Timebase_CurrentTime();
  138.            
  139.             if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ) == DS18X20_OK) {
  140.                 delay_ms(DS18B20_TCONV_12BIT);
  141.                 for ( i=0; i<nSensors; i++ ) {
  142.                     if ( DS18X20_read_meas( &gSensorIDs[i][0], &subzero,
  143.                             &cel, &cel_frac_bits) == DS18X20_OK ) {
  144.                        
  145.                         //txMsg.Data.bytes[0] = subzero;
  146.                         if (subzero) {
  147.                             txMsg.Data.bytes[i*2] = -cel;
  148.                         } else {
  149.                             txMsg.Data.bytes[i*2] = cel;
  150.                         }
  151.                         //hur göra med decimaldelen???
  152.                         txMsg.Data.bytes[i*2+1] = (cel_frac_bits<<4);
  153.                        
  154.                     }
  155.                     else printf("CRC Error (lost connection?)");
  156.                     printf("\n");
  157.                 }
  158.             }
  159.             else printf("Start meas. failed (short circuit?)");
  160.  
  161.             txMsg.DataLength = nSensors*2;
  162.             /* send txMsg */
  163.             Can_Send(&txMsg);
  164.         }
  165.     }
  166.    
  167.     return 0;
  168. }
  169.