hlw8110.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #ifndef HLW8110_H
  2. #define HLW8110_H
  3. #include <stdint.h>
  4. #define CONST_2_23 8388608
  5. #define CONST_2_22 4194304
  6. #define CONST_2_31 2147483648
  7. #define CONST_F 3579545
  8. #define HLW_REG_SYSCON 0x00
  9. #define HLW_REG_EMUCON 0x01
  10. #define HLW_REG_EMUCON2 0x13
  11. #define HLW_REG_HFConst 0x02
  12. #define HLW_REG_PstartPA 0x03
  13. #define HLW_REG_PstartPB 0x60
  14. #define HLW_REG_PAGain 0x05
  15. #define HLW_REG_PBGain 0x06
  16. #define HLW_REG_PSGain 0x11
  17. #define HLW_REG_PhaseA 0x07
  18. #define HLW_REG_PhaseB 0x08
  19. #define HLW_REG_PAOS 0x0a
  20. #define HLW_REG_PBOS 0x0b
  21. #define HLW_REG_PSOS 0x12
  22. #define HLW_REG_RmsIAOS 0x0e
  23. #define HLW_REG_RmsIBOS 0x0f
  24. #define HLW_REG_IBGain 0x10
  25. #define HLW_REG_DCIA 0x14
  26. #define HLW_REG_DCIB 0x15
  27. #define HLW_REG_DCU 0x16
  28. #define HLW_REG_SAGCYC 0x17
  29. #define HLW_REG_SYSLVL 0x18
  30. #define HLW_REG_OVLVL 0x19
  31. #define HLW_REG_OIALVL 0x1a
  32. #define HLW_REG_OIBLVL 0x1b
  33. #define HLW_REG_OPLVL 0x1c
  34. #define HLW_REG_PFCnt_PA 0x20
  35. #define HLW_REG_PFCnt_PB 0x21
  36. #define HLW_REG_Angle 0x22
  37. #define HLW_REG_Ufreq 0x23
  38. #define HLW_REG_RmsIA 0x24
  39. #define HLW_REG_RmsIB 0x25
  40. #define HLW_REG_RmsU 0x26
  41. #define HLW_REG_PF 0x27
  42. #define HLW_REG_E_PA 0x28
  43. #define HLW_REG_E_PB 0x29
  44. #define HLW_REG_PowerA 0x2c
  45. #define HLW_REG_PowerB 0x2d
  46. #define HLW_REG_PowerS 0x2e
  47. #define HLW_REG_EMUStatus 0x2f
  48. #define HLW_REG_PeakIA 0x30
  49. #define HLW_REG_PeakIB 0x31
  50. #define HLW_REG_PeakU 0x32
  51. #define HLW_REG_InstanIA 0x33
  52. #define HLW_REG_InstanIB 0x34
  53. #define HLW_REG_InstanU 0x35
  54. #define HLW_REG_InstanP 0x3c
  55. #define HLW_REG_InstanS 0x3d
  56. #define HLW_REG_WaveIA 0x36
  57. #define HLW_REG_WaveIB 0x37
  58. #define HLW_REG_WaveU 0x38
  59. #define HLW_REG_IE 0x40
  60. #define HLW_REG_IF 0x41
  61. #define HLW_REG_RIF 0x42
  62. #define HLW_REG_SysStatus 0x43
  63. #define HLW_REG_RmsIAC 0x70
  64. #define HLW_REG_RmsIBC 0x71
  65. #define HLW_REG_RmsUC 0x72
  66. #define HLW_REG_PowerPAC 0x73
  67. #define HLW_REG_PowerPBC 0x74
  68. #define HLW_REG_PowerSC 0x75
  69. #define HLW_REG_EnergyAC 0x76
  70. #define HLW_REG_EnergyBC 0x77
  71. typedef struct _HLW
  72. {
  73. uint16_t u;
  74. uint16_t i;
  75. uint16_t pf;
  76. uint16_t f;
  77. uint16_t c;
  78. int32_t RMSU;
  79. uint16_t RMSUC;
  80. int32_t RMSIA;
  81. uint16_t RMSIAC;
  82. int32_t RMSIB;
  83. uint16_t RMSIBC;
  84. uint16_t UF;
  85. uint32_t t_v1;
  86. uint32_t t_v2;
  87. uint32_t t_i1;
  88. uint32_t t_i2;
  89. uint32_t t_f;
  90. uint32_t t_p;
  91. uint32_t t_pf;
  92. uint32_t t_c;
  93. int32_t RMSU_v1;
  94. int32_t RMSIA_i1;
  95. float KU;
  96. uint16_t DU;
  97. float KIA;
  98. uint16_t DIA;
  99. float KF;
  100. } HLW;
  101. typedef void (*HLW8110_TRANS_FUN)(uint8_t *, uint32_t);
  102. void hlw8110_set_trans_fun(HLW8110_TRANS_FUN fun);
  103. void hlw8110_read(uint8_t reg);
  104. void hlw8110_write(uint8_t reg, uint8_t *data, uint8_t len);
  105. void hlw8110_enable_write();
  106. void hlw8110_disable_write();
  107. void hlw8110_set_channel_A();
  108. void hlw8110_set_channel_B();
  109. void hlw8110_reset();
  110. void hlw8110_ac_config();
  111. uint8_t hlw8110_unpack_UF(HLW *hlw, uint8_t *data, uint32_t len);
  112. uint8_t hlw8110_unpack_RMSU(HLW *hlw, uint8_t *data, uint32_t len);
  113. uint8_t hlw8110_unpack_RMSUC(HLW *hlw, uint8_t *data, uint32_t len);
  114. uint8_t hlw8110_unpack_RMSIA(HLW *hlw, uint8_t *data, uint32_t len);
  115. uint8_t hlw8110_unpack_RMSIAC(HLW *hlw, uint8_t *data, uint32_t len);
  116. uint8_t hlw8110_unpack_RMSIB(HLW *hlw, uint8_t *data, uint32_t len);
  117. uint8_t hlw8110_unpack_RMSIBC(HLW *hlw, uint8_t *data, uint32_t len);
  118. void hlw8110_read_UF();
  119. void hlw8110_read_RMSU();
  120. void hlw8110_read_RMSUC();
  121. void hlw8110_read_RMSIA();
  122. void hlw8110_read_RMSIAC();
  123. void hlw8110_read_RMSIB();
  124. void hlw8110_read_RMSIBC();
  125. void hlw8110_cal(HLW *hlw);
  126. void hlw8110_cal_v1(HLW *hlw, uint32_t t);
  127. void hlw8110_cal_v2(HLW *hlw, uint32_t t);
  128. void hlw8110_cal_i1(HLW *hlw, uint32_t t);
  129. void hlw8110_cal_i2(HLW *hlw, uint32_t t);
  130. void hlw8110_cal_f(HLW *hlw, uint32_t t);
  131. #endif