MDC04_ATY.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /**
  2. * @file MDC04_ATY.h
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2026 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief functions of MDC04 sensor for all embedded device
  20. *
  21. * @version
  22. * - 1_00_250101 > ATY
  23. * -# Initial version, support OneWire and I2C modes
  24. ********************************************************************************
  25. */
  26. #ifndef __MDC04_ATY_H
  27. #define __MDC04_ATY_H
  28. #include "INCLUDE_ATY.h"
  29. /******************************* For user *************************************/
  30. // MDC04 Communication Modes
  31. #define MDC04_MODE_ONEWIRE 0x01
  32. #define MDC04_MODE_I2C 0x02
  33. // MDC04 Commands
  34. #define MDC04_CMD_READ_TEMP 0x44 // Start temperature conversion
  35. #define MDC04_CMD_READ_ROM 0x33 // Read ROM (OneWire mode)
  36. #define MDC04_CMD_SKIP_ROM 0xCC // Skip ROM (OneWire mode)
  37. #define MDC04_CMD_SEARCH_ROM 0xF0 // Search ROM (OneWire mode)
  38. // MDC04 I2C Default Address
  39. #define MDC04_I2C_ADDR_DEFAULT 0x48
  40. // MDC04 Status Codes
  41. #define MDC04_STATUS_OK 0x00
  42. #define MDC04_STATUS_ERROR 0x01
  43. #define MDC04_STATUS_TIMEOUT 0x02
  44. #define MDC04_STATUS_CRC_ERROR 0x03
  45. #define MDC04_STATUS_NO_DEVICE 0x04
  46. // MDC04 Timing Constants (microseconds)
  47. #define MDC04_RESET_PULSE_TIME 480
  48. #define MDC04_PRESENCE_WAIT_TIME 70
  49. #define MDC04_PRESENCE_PULSE_TIME 240
  50. #define MDC04_WRITE_0_LOW_TIME 60
  51. #define MDC04_WRITE_1_LOW_TIME 6
  52. #define MDC04_READ_LOW_TIME 6
  53. #define MDC04_READ_SAMPLE_TIME 9
  54. #define MDC04_SLOT_TIME 60
  55. /******************************************************************************/
  56. struct MDC04_ATY_Dev
  57. {
  58. // Communication mode
  59. uint8_t mode; // MDC04_MODE_ONEWIRE or MDC04_MODE_I2C
  60. // OneWire mode functions
  61. void (*oneWireSetHigh)(void); // Set data line high
  62. void (*oneWireSetLow)(void); // Set data line low
  63. uint8_t (*oneWireRead)(void); // Read data line state
  64. void (*delayUs)(uint32_t us); // Microsecond delay function
  65. // I2C mode functions
  66. uint8_t (*i2cStart)(void); // I2C start condition
  67. uint8_t (*i2cStop)(void); // I2C stop condition
  68. uint8_t (*i2cWriteByte)(uint8_t data); // I2C write byte
  69. uint8_t (*i2cReadByte)(uint8_t ack); // I2C read byte
  70. uint8_t i2cAddress; // I2C device address
  71. // Device data
  72. uint8_t romCode[8]; // ROM code for OneWire mode
  73. float temperature; // Last read temperature
  74. uint16_t rawTemperature; // Raw temperature data
  75. // Status and control
  76. uint8_t devicePresent; // Device presence flag
  77. uint8_t lastStatus; // Last operation status
  78. uint8_t initialized; // Initialization flag
  79. uint32_t conversionTime; // Temperature conversion time (ms)
  80. // Debug and logging
  81. uint8_t lock;
  82. uint8_t debugEnable;
  83. void (*LOG)(const char*, ...);
  84. };
  85. // Function prototypes
  86. uint8_t MDC04_Init(struct MDC04_ATY_Dev *dev);
  87. uint8_t MDC04_Reset(struct MDC04_ATY_Dev *dev);
  88. uint8_t MDC04_ReadTemperature(struct MDC04_ATY_Dev *dev);
  89. uint8_t MDC04_StartConversion(struct MDC04_ATY_Dev *dev);
  90. uint8_t MDC04_ReadROM(struct MDC04_ATY_Dev *dev);
  91. uint8_t MDC04_SearchDevices(struct MDC04_ATY_Dev *dev, uint8_t *deviceCount);
  92. // OneWire specific functions
  93. uint8_t MDC04_OneWire_WriteByte(struct MDC04_ATY_Dev *dev, uint8_t data);
  94. uint8_t MDC04_OneWire_ReadByte(struct MDC04_ATY_Dev *dev);
  95. uint8_t MDC04_OneWire_WriteBit(struct MDC04_ATY_Dev *dev, uint8_t bit);
  96. uint8_t MDC04_OneWire_ReadBit(struct MDC04_ATY_Dev *dev);
  97. uint8_t MDC04_OneWire_CRC8(uint8_t *data, uint8_t len);
  98. // I2C specific functions
  99. uint8_t MDC04_I2C_WriteRegister(struct MDC04_ATY_Dev *dev, uint8_t reg, uint8_t data);
  100. uint8_t MDC04_I2C_ReadRegister(struct MDC04_ATY_Dev *dev, uint8_t reg, uint8_t *data);
  101. uint8_t MDC04_I2C_ReadMultiple(struct MDC04_ATY_Dev *dev, uint8_t reg, uint8_t *data, uint8_t len);
  102. // Utility functions
  103. float MDC04_ConvertRawToTemperature(uint16_t rawTemp);
  104. uint8_t MDC04_GetDeviceStatus(struct MDC04_ATY_Dev *dev);
  105. void MDC04_SetI2CAddress(struct MDC04_ATY_Dev *dev, uint8_t address);
  106. #endif /* __MDC04_ATY_H */
  107. /******************************** End Of File *********************************/