Subversion Repositories HomeAutomation

Rev

Rev 952 | Rev 1706 | 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 sns_ds18x20_ReportInterval = (uint8_t)sns_ds18x20_SEND_PERIOD;
  10. uint8_t NumberOfSensors = 0;
  11. uint16_t SensorIds[] = {0, 0, 0, 0};
  12. uint8_t FlagReadTemperature = 0, FlagConvertTemperature = 0, FlagSearchSensors = 0;
  13.  
  14. void ReadTemperature_callback(uint8_t timer)
  15. {
  16.     FlagReadTemperature = 1;
  17. }
  18.  
  19. void ReadTemperature(void)
  20. {
  21.     static uint8_t SearchSensorsTimeout = sns_ds18x20_SEARCH_TIMEOUT;
  22.     static uint8_t CurrentSensor = 0;
  23.     uint8_t subzero, cel, cel_frac_bits = 0;
  24.     StdCan_Msg_t txMsg;
  25.  
  26.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  27.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  28.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DS18X20;
  29.     txMsg.Header.ModuleId = sns_ds18x20_ID;
  30.     txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_TEMPERATURE_CELSIUS;
  31.     txMsg.Length = 3;
  32.  
  33.     txMsg.Data[0] = SensorIds[CurrentSensor];
  34.  
  35.     if (DS18X20_read_meas(&gSensorIDs[CurrentSensor][0], &subzero, &cel, &cel_frac_bits) == DS18X20_OK)
  36.     {
  37.         int16_t temp = cel;
  38.  
  39.         temp = temp << 6;
  40.  
  41.         temp += (cel_frac_bits << 2);
  42.  
  43.         if (subzero)
  44.         {
  45.             temp = -temp;
  46.         }
  47.  
  48.         txMsg.Data[1] = (uint8_t)(temp >> 8);
  49.         txMsg.Data[2] = (uint8_t)(temp & 0xff);
  50.     }
  51.    
  52.     StdCan_Put(&txMsg);
  53.  
  54.     CurrentSensor++;
  55.     if (CurrentSensor < NumberOfSensors)
  56.         Timer_SetTimeout(sns_ds18x20_TIMER, sns_ds18x20_SEND_DELAY, TimerTypeOneShot, &ReadTemperature_callback);
  57.     else
  58.     {
  59.         SearchSensorsTimeout--;
  60.         if (SearchSensorsTimeout == 0)
  61.         {
  62.             SearchSensorsTimeout = sns_ds18x20_SEARCH_TIMEOUT;
  63.             FlagSearchSensors = 1;
  64.         }
  65.        
  66.         CurrentSensor = 0;
  67.  
  68.         Timer_SetTimeout(sns_ds18x20_TIMER, sns_ds18x20_ReportInterval*1000, TimerTypeOneShot, &ConvertTemperature_callback);
  69.     }
  70. }
  71.  
  72. void ConvertTemperature_callback(uint8_t timer)
  73. {
  74.     FlagConvertTemperature = 1;
  75. }
  76.  
  77. void ConvertTemperature(void)
  78. {
  79.     if (DS18X20_start_meas(DS18X20_POWER_PARASITE, NULL) == DS18X20_OK)
  80.     {
  81.         Timer_SetTimeout(sns_ds18x20_TIMER, DS18B20_TCONV_12BIT, TimerTypeOneShot, &ReadTemperature_callback);
  82.     }
  83. }
  84.  
  85. void sns_ds18x20_Init(void)
  86. {
  87.     /* Make sure there is no more then 4 sensors. */
  88.     FlagSearchSensors = 1;
  89.     NumberOfSensors = 0;
  90.    
  91.     Timer_SetTimeout(sns_ds18x20_TIMER, sns_ds18x20_ReportInterval*1000, TimerTypeFreeRunning, &ConvertTemperature_callback);
  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. void sns_ds18x20_HandleMessage(StdCan_Msg_t *rxMsg)
  126. {
  127.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  128.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  129.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_DS18X20 &&
  130.         rxMsg->Header.ModuleId == sns_ds18x20_ID)
  131.     {
  132.         switch (rxMsg->Header.Command)
  133.         {
  134.         case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL:
  135.         if (rxMsg->Length > 0)
  136.             sns_ds18x20_ReportInterval = rxMsg->Data[0];
  137.  
  138.         StdCan_Msg_t txMsg;
  139.  
  140.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  141.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  142.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DS18X20;
  143.         txMsg.Header.ModuleId = sns_ds18x20_ID;
  144.         txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL;
  145.         txMsg.Length = 1;
  146.  
  147.         txMsg.Data[0] = sns_ds18x20_ReportInterval;
  148.  
  149.         StdCan_Put(&txMsg);
  150.         break;
  151.         }
  152.     }
  153. }
  154.  
  155. void sns_ds18x20_List(uint8_t ModuleSequenceNumber)
  156. {
  157.     StdCan_Msg_t txMsg;
  158.    
  159.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  160.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  161.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DS18X20;
  162.     txMsg.Header.ModuleId = sns_ds18x20_ID;
  163.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  164.     txMsg.Length = 6;
  165.  
  166.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  167.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  168.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  169.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  170.    
  171.     txMsg.Data[4] = NUMBER_OF_MODULES;
  172.     txMsg.Data[5] = ModuleSequenceNumber;
  173.    
  174.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  175. }
  176.