Subversion Repositories HomeAutomation

Rev

Rev 817 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1.  
  2. #include "sns_ds18x20.h"
  3.  
  4. void ConvertTemperature(void);
  5. void ConvertTemperature_callback(uint8_t timer);
  6. void ReadTemperature(void);
  7. void ReadTemperature_callback(uint8_t timer);
  8.  
  9. uint8_t NumberOfSensors;
  10. uint16_t SensorIds[] = {0, 0, 0, 0};
  11.  
  12. //flags, used to start different tasks in the main loop, used instead of claculating long tasks in the interupt rutines
  13. uint8_t FlagReadTemperature, FlagConvertTemperature, FlagSearchSensors;
  14.  
  15. void ReadTemperature_callback(uint8_t timer)
  16. {
  17.     FlagReadTemperature = 1;
  18. }
  19.  
  20. void ReadTemperature(void)
  21. {
  22.     static uint8_t SearchSensorsTimeout = sns_ds18x20_SEARCH_TIMEOUT;
  23.     static uint8_t CurrentSensor = 0;
  24.     uint8_t subzero, cel, cel_frac_bits = 0;
  25.     StdCan_Msg_t txMsg;
  26.  
  27.     StdCan_Set_class(txMsg, CAN_MODULE_SNS);
  28.     StdCan_Set_direction(txMsg, OWNER);
  29.     txMsg.Header.ModuleType = MODULE_TYPE_sns_ds18x20;
  30.     txMsg.Header.ModuleId = sns_ds18x20_ID;
  31.     txMsg.Header.Command = SNS_CMD_TEMPERATURE_CELSIUS;
  32.     txMsg.Length = 3;
  33.  
  34.     txMsg.Data[0] = SensorIds[CurrentSensor];
  35.  
  36.     if (DS18X20_read_meas(&gSensorIDs[CurrentSensor][0], &subzero, &cel, &cel_frac_bits) == DS18X20_OK)
  37.     {
  38.         if (subzero)
  39.         {
  40.             txMsg.Data[1] = -cel-1;
  41.             txMsg.Data[2] = ~(cel_frac_bits<<4);
  42.         }
  43.         else
  44.         {
  45.             txMsg.Data[1] = cel;
  46.             txMsg.Data[2] = (cel_frac_bits<<4);
  47.         }
  48.     }
  49.     /* send txMsg */
  50.  
  51.     StdCan_Put(&txMsg);
  52.  
  53.     CurrentSensor++;
  54.     if (CurrentSensor < NumberOfSensors)
  55.         Timer_SetTimeout(sns_ds18x20_TIMER, sns_ds18x20_SEND_DELAY, TimerTypeOneShot, &ReadTemperature_callback);
  56.     else
  57.     {
  58.         SearchSensorsTimeout--;
  59.         if (SearchSensorsTimeout == 0)
  60.         {
  61.             SearchSensorsTimeout = sns_ds18x20_SEARCH_TIMEOUT;
  62.             FlagSearchSensors = 1;
  63.         }
  64.        
  65.         CurrentSensor = 0;
  66.  
  67.         Timer_SetTimeout(sns_ds18x20_TIMER, sns_ds18x20_SEND_PERIOD , TimerTypeOneShot, &ConvertTemperature_callback);
  68.     }
  69. }
  70.  
  71. void ConvertTemperature_callback(uint8_t timer)
  72. {
  73.     FlagConvertTemperature = 1;
  74. }
  75.  
  76. void ConvertTemperature(void)
  77. {
  78.     if (DS18X20_start_meas(DS18X20_POWER_PARASITE, NULL) == DS18X20_OK)
  79.     {
  80.         Timer_SetTimeout(sns_ds18x20_TIMER, DS18B20_TCONV_12BIT, TimerTypeOneShot, &ReadTemperature_callback);
  81.     }
  82. }
  83.  
  84.  
  85. void sns_ds18x20_Init(void)
  86. {
  87.     /* Make sure there is no more then 4 sensors. */
  88.     NumberOfSensors = search_sensors();
  89.    
  90.     Timer_SetTimeout(sns_ds18x20_TIMER, sns_ds18x20_SEND_PERIOD , TimerTypeFreeRunning, &ConvertTemperature_callback);
  91.    
  92. }
  93.  
  94. void sns_ds18x20_Process(void)
  95. {
  96.     if (FlagConvertTemperature)
  97.     {
  98.         ConvertTemperature();
  99.         FlagConvertTemperature = 0;
  100.     }
  101.  
  102.     if (FlagSearchSensors)
  103.     {
  104.         NumberOfSensors = search_sensors();
  105.         uint8_t i, j;
  106.  
  107.         for(i = 0; i < NumberOfSensors; i++)
  108.         {
  109.             SensorIds[i] = 0;
  110.             for (j = 0; j < 7; j++)
  111.             {
  112.                 SensorIds[i] ^= gSensorIDs[i][j];  
  113.             }
  114.         }
  115.         FlagSearchSensors = 0;
  116.     }
  117.  
  118.     if (FlagReadTemperature)
  119.     {
  120.         ReadTemperature();
  121.         FlagReadTemperature = 0;
  122.     }
  123. }
  124.  
  125.  
  126. void sns_ds18x20_HandleMessage(StdCan_Msg_t *rxMsg)
  127. {
  128. }
  129.  
  130. void sns_ds18x20_List(uint8_t ModuleSequenceNumber)
  131. {
  132.     StdCan_Msg_t txMsg;
  133.    
  134.     StdCan_Set_class(txMsg, CAN_MODULE_SNS);
  135.     StdCan_Set_direction(txMsg, OWNER);
  136.     txMsg.Header.ModuleType = MODULE_TYPE_sns_ds18x20;
  137.     txMsg.Header.ModuleId = sns_ds18x20_ID;
  138.     txMsg.Header.Command = CMD_LIST;
  139.     txMsg.Length = 6;
  140.  
  141.     txMsg.Data[3] = NODE_HW_ID&0xff;
  142.     txMsg.Data[2] = (NODE_HW_ID>>8)&0xff;
  143.     txMsg.Data[1] = (NODE_HW_ID>>16)&0xff;
  144.     txMsg.Data[0] = (NODE_HW_ID>>24)&0xff;
  145.    
  146.     txMsg.Data[4] = NUMBEROFMODULES;
  147.     txMsg.Data[5] = ModuleSequenceNumber;
  148.    
  149.     StdCan_Put(&txMsg);
  150.  
  151.  
  152. }
  153.