Subversion Repositories HomeAutomation

Rev

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

  1. #include "sns_ds18x20.h"
  2.  
  3. #ifdef CHN_LOADED
  4. #include "../chn_ChnMaster/chn_ChnMaster.h"
  5. #endif
  6.  
  7. void ConvertTemperature(void);
  8. void ConvertTemperature_callback(uint8_t timer);
  9. void ReadTemperature(void);
  10. void ReadTemperature_callback(uint8_t timer);
  11.  
  12. uint8_t sns_ds18x20_ReportInterval = (uint8_t)sns_ds18x20_SEND_PERIOD;
  13. uint8_t NumberOfSensors = 0;
  14. uint16_t SensorIds[] = {0, 0, 0, 0};
  15. uint8_t FlagReadTemperature = 0, FlagConvertTemperature = 0, FlagSearchSensors = 0;
  16.  
  17. void ReadTemperature_callback(uint8_t timer)
  18. {
  19.     FlagReadTemperature = 1;
  20. }
  21.  
  22. void ReadTemperature(void)
  23. {
  24.     static uint8_t SearchSensorsTimeout = sns_ds18x20_SEARCH_TIMEOUT;
  25.     static uint8_t CurrentSensor = 0;
  26.     uint8_t subzero, cel, cel_frac_bits = 0;
  27.     StdCan_Msg_t txMsg;
  28.  
  29.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  30.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  31.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DS18X20;
  32.     txMsg.Header.ModuleId = sns_ds18x20_ID;
  33.     txMsg.Header.Command = CAN_MODULE_CMD_PHYSICAL_TEMPERATURE_CELSIUS;
  34.     txMsg.Length = 3;
  35.  
  36.     txMsg.Data[0] = SensorIds[CurrentSensor];
  37.  
  38.     if (DS18X20_read_meas(&gSensorIDs[CurrentSensor][0], &subzero, &cel, &cel_frac_bits) == DS18X20_OK)
  39.     {
  40.         int16_t temp = cel;
  41.  
  42.         temp = temp << 6;
  43.  
  44.         temp += (cel_frac_bits << 2);
  45.  
  46.         if (subzero)
  47.         {
  48.             temp = -temp;
  49.         }
  50.  
  51.         txMsg.Data[1] = (uint8_t)(temp >> 8);
  52.         txMsg.Data[2] = (uint8_t)(temp & 0xff);
  53.  
  54. #ifdef CHN_LOADED
  55.         chn_ChnMaster_UpdateChannel( sns_ds18x20_CHN_CHANNEL, (temp<<2)+25600 );
  56. #endif
  57.     }
  58.    
  59.     StdCan_Put(&txMsg);
  60.  
  61.     CurrentSensor++;
  62.     if (CurrentSensor < NumberOfSensors)
  63.         Timer_SetTimeout(sns_ds18x20_TIMER, sns_ds18x20_SEND_DELAY, TimerTypeOneShot, &ReadTemperature_callback);
  64.     else
  65.     {
  66.         SearchSensorsTimeout--;
  67.         if (SearchSensorsTimeout == 0)
  68.         {
  69.             SearchSensorsTimeout = sns_ds18x20_SEARCH_TIMEOUT;
  70.             FlagSearchSensors = 1;
  71.         }
  72.        
  73.         CurrentSensor = 0;
  74.  
  75.         Timer_SetTimeout(sns_ds18x20_TIMER, sns_ds18x20_ReportInterval*1000, TimerTypeOneShot, &ConvertTemperature_callback);
  76.     }
  77. }
  78.  
  79. void ConvertTemperature_callback(uint8_t timer)
  80. {
  81.     FlagConvertTemperature = 1;
  82. }
  83.  
  84. void ConvertTemperature(void)
  85. {
  86.     if (DS18X20_start_meas(DS18X20_POWER_PARASITE, NULL) == DS18X20_OK)
  87.     {
  88.         Timer_SetTimeout(sns_ds18x20_TIMER, DS18B20_TCONV_12BIT, TimerTypeOneShot, &ReadTemperature_callback);
  89.     }
  90. }
  91.  
  92. void sns_ds18x20_Init(void)
  93. {
  94.     /* Make sure there is no more then 4 sensors. */
  95.     FlagSearchSensors = 1;
  96.     NumberOfSensors = 0;
  97.    
  98.     Timer_SetTimeout(sns_ds18x20_TIMER, sns_ds18x20_ReportInterval*1000, TimerTypeFreeRunning, &ConvertTemperature_callback);
  99. }
  100.  
  101. void sns_ds18x20_Process(void)
  102. {
  103.     if (FlagConvertTemperature)
  104.     {
  105.         ConvertTemperature();
  106.         FlagConvertTemperature = 0;
  107.     }
  108.  
  109.     if (FlagSearchSensors)
  110.     {
  111.         NumberOfSensors = search_sensors();
  112.         uint8_t i, j;
  113.  
  114.         for(i = 0; i < NumberOfSensors; i++)
  115.         {
  116.             SensorIds[i] = 0;
  117.             for (j = 0; j < 7; j++)
  118.             {
  119.                 SensorIds[i] ^= gSensorIDs[i][j];  
  120.             }
  121.         }
  122.         FlagSearchSensors = 0;
  123.     }
  124.  
  125.     if (FlagReadTemperature)
  126.     {
  127.         ReadTemperature();
  128.         FlagReadTemperature = 0;
  129.     }
  130. }
  131.  
  132. void sns_ds18x20_HandleMessage(StdCan_Msg_t *rxMsg)
  133. {
  134.     if (    StdCan_Ret_class(rxMsg->Header) == CAN_MODULE_CLASS_SNS &&
  135.         StdCan_Ret_direction(rxMsg->Header) == DIRECTIONFLAG_TO_OWNER &&
  136.         rxMsg->Header.ModuleType == CAN_MODULE_TYPE_SNS_DS18X20 &&
  137.         rxMsg->Header.ModuleId == sns_ds18x20_ID)
  138.     {
  139.         switch (rxMsg->Header.Command)
  140.         {
  141.         case CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL:
  142.         if (rxMsg->Length > 0)
  143.             sns_ds18x20_ReportInterval = rxMsg->Data[0];
  144.  
  145.         StdCan_Msg_t txMsg;
  146.  
  147.         StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  148.         StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  149.         txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DS18X20;
  150.         txMsg.Header.ModuleId = sns_ds18x20_ID;
  151.         txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_REPORT_INTERVAL;
  152.         txMsg.Length = 1;
  153.  
  154.         txMsg.Data[0] = sns_ds18x20_ReportInterval;
  155.  
  156.         StdCan_Put(&txMsg);
  157.         break;
  158.         }
  159.     }
  160. }
  161.  
  162. void sns_ds18x20_List(uint8_t ModuleSequenceNumber)
  163. {
  164.     StdCan_Msg_t txMsg;
  165.    
  166.     StdCan_Set_class(txMsg.Header, CAN_MODULE_CLASS_SNS);
  167.     StdCan_Set_direction(txMsg.Header, DIRECTIONFLAG_FROM_OWNER);
  168.     txMsg.Header.ModuleType = CAN_MODULE_TYPE_SNS_DS18X20;
  169.     txMsg.Header.ModuleId = sns_ds18x20_ID;
  170.     txMsg.Header.Command = CAN_MODULE_CMD_GLOBAL_LIST;
  171.     txMsg.Length = 6;
  172.  
  173.     txMsg.Data[0] = NODE_HW_ID_BYTE0;
  174.     txMsg.Data[1] = NODE_HW_ID_BYTE1;
  175.     txMsg.Data[2] = NODE_HW_ID_BYTE2;
  176.     txMsg.Data[3] = NODE_HW_ID_BYTE3;
  177.    
  178.     txMsg.Data[4] = NUMBER_OF_MODULES;
  179.     txMsg.Data[5] = ModuleSequenceNumber;
  180.    
  181.     while (StdCan_Put(&txMsg) != StdCan_Ret_OK);
  182. }
  183.