Subversion Repositories HomeAutomation

Rev

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