hlw8110_handle.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. #ifndef __HL8810_HANELD_H
  2. #define __HL8810_HANELD_H
  3. #include "termios.h"
  4. #include "ctype.h"
  5. //8112/8110 reg define
  6. #define REG_SYSCON_ADDR 0x00
  7. #define REG_EMUCON_ADDR 0x01
  8. #define REG_HFCONST_ADDR 0x02
  9. #define REG_EMUCON2_ADDR 0x13
  10. #define REG_ANGLE_ADDR 0x22
  11. #define REG_UFREQ_ADDR 0x23
  12. #define REG_RMSIA_ADDR 0x24
  13. #define REG_RMSIB_ADDR 0x25
  14. #define REG_RMSU_ADDR 0x26
  15. #define REG_PF_ADDR 0x27
  16. #define REG_ENERGY_PA_ADDR 0x28
  17. #define REG_ENERGY_PB_ADDR 0x29
  18. #define REG_POWER_PA_ADDR 0x2C
  19. #define REG_POWER_PB_ADDR 0x2D
  20. #define REG_OVLVL_ADDR 0x19
  21. #define REG_INT_ADDR 0x1D
  22. #define REG_IE_ADDR 0x40
  23. #define REG_IF_ADDR 0x41
  24. #define REG_RIF_ADDR 0x42
  25. #define REG_RDATA_ADDR 0x44
  26. #define REG_CHECKSUM_ADDR 0x6f
  27. #define REG_RMS_IAC_ADDR 0x70
  28. #define REG_RMS_IBC_ADDR 0x71
  29. #define REG_RMS_UC_ADDR 0x72
  30. #define REG_POWER_PAC_ADDR 0x73
  31. #define REG_POWER_PBC_ADDR 0x74
  32. #define REG_POWER_SC_ADDR 0x75
  33. #define REG_ENERGY_AC_ADDR 0x76
  34. #define REG_ENERGY_BC_ADDR 0x77
  35. #define CHANEL_A 0
  36. #define CHANEL_B 1
  37. #define D_TIME_50MS 50000
  38. #define D_TIME_10MS 10000
  39. #define D_TIME_20MS 20000
  40. #define D_TIME_100MS 100000
  41. #define D_CAL_U 1000/1000
  42. #define D_CAL_A_I 1000/1000
  43. #define D_CAL_A_P 1000/1000
  44. #define D_CAL_A_E 1000/1000
  45. #define D_CAL_B_P 1000/1000
  46. #define D_CAL_B_I 1000/1000
  47. #define D_CAL_B_E 1000/1000
  48. //GPIO
  49. #define GPIO_DIR_PATH "/sys/class/gpio/"
  50. #define GPIO_EXPORT_PATH GPIO_DIR_PATH "export"
  51. #define GPIO_UNEXPORT_PATH GPIO_DIR_PATH "unexport"
  52. #define GPIO_VALUE_PATH GPIO_DIR_PATH "gpio%d/value"
  53. #define GPIO_DIRECTION_PATH GPIO_DIR_PATH "gpio%d/direction"
  54. #define GPIO_PE3 131
  55. #define GPIO_PD11 107
  56. //#define DEBUG
  57. #ifdef DEBUG
  58. #define _DEBUG(dircetion,buff,size) \
  59. do{\
  60. printf("%-40s:%4d -- <%4s>",__func__,__LINE__,(dircetion ? "GET":"SET"));\
  61. for(int i = 0; i < size; i++)\
  62. {\
  63. if((unsigned int)(buff[i]) > 15)\
  64. printf("%X ",(unsigned int)(buff[i]));\
  65. else\
  66. printf("0%X ",(unsigned int)(buff[i]));\
  67. }\
  68. printf("\n");\
  69. }while(0);
  70. #else
  71. #define _DEBUG(dircetion,buff,size)
  72. #endif
  73. #define SET 0
  74. #define GET 1
  75. typedef unsigned char uint8_t;
  76. typedef unsigned short uint16_t;
  77. typedef unsigned int uint32_t;
  78. typedef struct LowConf_8110 LowConf;
  79. typedef struct LowConf_8110{
  80. //日志等级
  81. int log_level;
  82. //串口文件描述符
  83. int h_hlw8110_fd;
  84. uint8_t opreation;
  85. //串口基本配置
  86. struct termios bak_opt;
  87. struct termios new_opt;
  88. // hlw8110 参数
  89. uint16_t U16_RMSIAC_RegData; //A通道电流转换系数
  90. uint16_t U16_RMSIBC_RegData; //B通道电流转换系数
  91. uint16_t U16_RMSUC_RegData; //电压通道转换系数
  92. uint16_t U16_PowerPAC_RegData; //A通道功率转化系数
  93. uint16_t U16_PowerPBC_RegData; //B通道功率转化系数
  94. uint16_t U16_PowerSC_RegData; //视在功率转化系数,if A通道,则是A通道视在转换系数,AB二选其一
  95. uint16_t U16_EnergyAC_RegData; //A通道有功电能转换系数
  96. uint16_t U16_EnergyBC_RegData; //B通道有功电能转换系数
  97. uint16_t U16_CheckSUM_RegData; //转换系数checksum
  98. uint16_t U16_CheckSUM_Data; //转换系数计算出的Checksum
  99. uint16_t U16_HFConst_RegData;
  100. uint16_t U16_Check_SysconReg_Data;
  101. uint16_t U16_Check_EmuconReg_Data;
  102. uint16_t U16_Check_Emucon2Reg_Data;
  103. uint32_t U32_RMSU_RegData; //电压有效值寄存器
  104. uint32_t U32_RMSIA_RegData; //A通道电流有效值寄存器
  105. uint32_t U32_POWERPA_RegData; //A通道功率有效值寄存器
  106. uint32_t U32_ENERGY_PA_RegData; //A通道有功电能有效值寄存器
  107. uint32_t U32_RMSIB_RegData; //B通道电流有效值寄存器
  108. uint32_t U32_POWERPB_RegData; //B通道功率有效值寄存器
  109. uint32_t U32_ENERGY_PB_RegData; //B通道有功电能有效值寄存器
  110. float F_AC_BACKUP_E; //A通道电量备份
  111. float F_BC_BACKUP_E; //B通道电量备份
  112. uint8_t current_channel;
  113. uint8_t serial_tx_buf[10]; //串口数据发送
  114. uint8_t serial_rx_buf[10];
  115. int tx_lenth;
  116. int rx_lenth;
  117. //pthread_mutex_t mutex_lwx8110;
  118. float F_AC_V;
  119. float F_AC_I;
  120. float F_AC_P;
  121. float F_AC_E;
  122. float F_AC_PF;
  123. float F_Angle;
  124. float F_AC_I_B;
  125. float F_AC_P_B;
  126. float F_AC_E_B;
  127. float F_AC_BACKUP_E_B;
  128. float F_AC_LINE_Freq;
  129. int F_AC_status;
  130. }LowConf;
  131. float hlw8110_read_Angle(LowConf *conf);
  132. float hwl8110_read_PF(LowConf *conf);
  133. float hwl8110_read_LineFreq(LowConf *conf);
  134. float hwl8110_read_EA(LowConf *conf);
  135. float hwl8110_read_PA(LowConf *conf);
  136. float hlw8110_read_IA(LowConf *conf);
  137. float hlw8110_read_UV(LowConf *conf);
  138. int hlw8110_read_Status(LowConf* conf,int* nStatus);
  139. LowConf * hlw8110_init(void);
  140. void hlw8110_deinit(LowConf *conf);
  141. LowConf* hlw8110_Reset(LowConf *conf);
  142. #endif