encoder.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * @Description:
  3. * @Version: 2.0
  4. * @Author: Seven
  5. * @Date: 2022-08-22 17:13:45
  6. * @LastEditors: Seven
  7. * @LastEditTime: 2023-04-28 16:58:15
  8. */
  9. #ifndef __ENCODER_H__
  10. #define __ENCODER_H__
  11. #include "stm32f10x.h"
  12. #include "tools.h"
  13. #define EEPROM_ENCODER_TAX1_ADDR 0x80 //编码器总脉冲存储地址(第三页)
  14. #define EEPROM_ENCODER_TAX2_ADDR 0x100 //编码器总脉冲存储地址(第五页)
  15. #define EEPROM_ENCODER_TAX1_GUNNUM_ADDR 0x200 //编码器空抬枪次数存储地址(第九页)
  16. #define EEPROM_ENCODER_TAX2_GUNNUM_ADDR 0x240 //编码器空抬枪次数存储地址(第十页)
  17. #define ENCODER_TAX_NUM 2 //报税口个数
  18. #define ENCODER_GUN_NUM 2 //一个报税口下枪的个数
  19. #define TAX1 0
  20. #define TAX2 1
  21. #define GUN1 0
  22. #define GUN2 1
  23. #define GUN3 2
  24. #define GUN4 3
  25. #define ENCODER_SAVE_GROUP_NUM 16 //脉冲数据存储的最大组数
  26. #define GUN_GPIO 0 //枪
  27. #define PULSE_GPIO 1 //脉冲
  28. #define GUNSIGNAL_IRQ_TIME (100)//(10) //抬枪信号中断消抖延时,单位ms
  29. #define PULSE_IRQ_TIME (1000)//(500) //脉冲信号中断消抖延时,单位us
  30. #define GUNUP_NUM_IRQ_TIME (200) //抬枪次数中断消抖,单位ms
  31. #define GUN_UP 1 //抬枪
  32. #define GUN_DOWN 0 //挂枪
  33. #define GUN_SIGNAL_ENABLE 0 //抬枪信号有效
  34. #define GUN_SIGNAL_DISABLE 1 //抬枪信号无效
  35. #define PULSE_NOGUN_END_TIME (5*1000) //无抬枪信号是判断脉冲结束的时间间隔
  36. #define PULSE_GUN_END_TIME (5*60*1000) //有抬枪信号是判断脉冲结束的时间间隔
  37. #define PULSE_END_MODE_GUN 1 //抬枪信号产生的记录
  38. #define PULSE_END_MODE_LOGIC 0 //逻辑判断产生的记录
  39. #define GUN_NUM_TIME (3*1000) //抬枪信号定时时间
  40. /*接口配置数据结构*/
  41. typedef struct _GPIO_TypeDef_t
  42. {
  43. GPIO_TypeDef* GPIOx;
  44. uint16_t GPIO_Pin;
  45. uint32_t EXTI_Line;
  46. }GPIO_TypeDef_t;
  47. //枪引脚信息
  48. typedef struct _gun_gpio_info_t
  49. {
  50. GPIO_TypeDef_t pulse;
  51. GPIO_TypeDef_t gunsignal;
  52. }gun_gpio_info_t;
  53. //中断线对应信息
  54. typedef struct _exti_info_t
  55. {
  56. uint8_t tax;
  57. uint8_t gun;
  58. uint8_t mode;
  59. uint32_t timeout;
  60. }exti_info_t;
  61. //中断线
  62. enum {
  63. EXTI_LINE6 = 0,
  64. EXTI_LINE12,
  65. EXTI_LINE7,
  66. EXTI_LINE13,
  67. EXTI_LINE10,
  68. EXTI_LINE14,
  69. EXTI_LINE9,
  70. EXTI_LINE15,
  71. EXTI_LINE_MAX,
  72. };
  73. extern exti_info_t exti_info[EXTI_LINE_MAX];
  74. //脉冲上报信息存储
  75. typedef struct _pulse_up_info_t
  76. {
  77. uint32_t id; //记录id
  78. uint8_t pulse_mode; //产生记录方式
  79. uint32_t single_pulse_num; //单次脉冲个数
  80. uint32_t total_pulse_num; //总脉冲数
  81. uint32_t time; //时间戳
  82. }__attribute__((packed)) pulse_up_info_t;
  83. //编码器中断信息
  84. typedef struct _encoder_irq_info_t
  85. {
  86. uint8_t last_level; //上次有效电平
  87. uint8_t current_level; //当次有效电平
  88. uint8_t gun_signal_is_work; //抬枪信号是否有效(0:有效;1:无效)
  89. uint8_t gun_down_irq; //挂枪中断标记
  90. timeout_t wave_time; //中断消抖定时
  91. }encoder_irq_info_t;
  92. //脉冲计数信息
  93. typedef struct _encoder_count_info_t
  94. {
  95. uint8_t eeprom_write_flag; //写eeprom标记
  96. uint8_t gun_signal; //枪信号状态
  97. uint32_t pulse_count; //脉冲计数
  98. uint8_t write_group; //写入的组号
  99. uint32_t write_id; //写入的记录id
  100. uint8_t read_group; //读取的组号
  101. uint32_t read_id; //读取的记录id
  102. uint32_t record_id; //记录id
  103. uint32_t record_time; //记录每次交易产生时间
  104. uint8_t read_first_valie; //第一次有效读取
  105. uint32_t gunup_num; //空抬枪次数
  106. uint8_t eeprom_write_gunup_flag;//空抬枪次数写eeprom标记
  107. }encoder_count_info_t;
  108. //空抬枪计时
  109. typedef struct _gunnum_time
  110. {
  111. uint8_t flag;
  112. uint32_t gunup_time;
  113. uint32_t gundown_time;
  114. uint32_t interval_time;
  115. }gunnum_time_t;
  116. //枪信息
  117. typedef struct _encoder_record_info_t
  118. {
  119. encoder_count_info_t encoder_count_info; //脉冲计数信息
  120. encoder_irq_info_t gunsignal_irq; //抬抢信号中断信息
  121. encoder_irq_info_t pulse_irq; //脉冲信号中断信息
  122. // encoder_irq_info_t gunup_irq; //抬枪次数信息
  123. timeout_t pulse_stop_time; //脉冲计数停止定时器
  124. }encoder_record_info_t;
  125. //编码器存储信息
  126. typedef struct _encoder_eeprom_t
  127. {
  128. uint32_t id; //记录id
  129. uint32_t total_pulse; //总脉冲数
  130. }encoder_eeprom_t;
  131. //存储空抬枪次数
  132. typedef struct _encoder_gunnum_ee_t
  133. {
  134. uint32_t gunup_num; //抬枪次数
  135. }encoder_gunnum_ee_t;
  136. ////////////////////通信协议格式//////////
  137. //编码器下发数据格式
  138. typedef struct _encoder_down_data_t
  139. {
  140. uint8_t order; //指令
  141. }__attribute__((packed)) encoder_down_data_t;
  142. //脉冲上传(指令0和1),以枪为单位
  143. typedef struct _encoder_up_gun_t
  144. {
  145. uint8_t order; //指令
  146. uint8_t tax1_gunstatus; //报税口1抬枪状态(按位记录,抬枪时置1)
  147. uint8_t tax2_gunstatus; //报税口2抬枪状态(按位记录,抬枪时置1)
  148. uint8_t new_recode_status; //是否新交易状态(0:无新交易;1:有新交易)
  149. uint32_t id; //记录id
  150. uint8_t id_mode; //产生记录方式(0:逻辑判断产生;1:抬枪信号产生)
  151. uint32_t single_pulse_num; //单组脉冲数
  152. uint32_t total_pulse_num; //总脉冲数
  153. uint32_t time; //时间戳
  154. }__attribute__((packed)) encoder_up_gun_t;
  155. //指令2和3,以报税口为单位
  156. typedef struct _encoder_up_tax_head_t
  157. {
  158. uint8_t order; //指令
  159. uint8_t tax1_gunstatus; //报税口1抬枪状态(按位记录,抬枪时置1)
  160. uint8_t tax2_gunstatus; //报税口2抬枪状态(按位记录,抬枪时置1)
  161. uint8_t new_recode_status; //是否新交易状态(0:无新交易;1:有新交易)
  162. uint8_t id_mode; //产生记录方式(0:逻辑判断产生;1:抬枪信号产生)
  163. uint8_t gun_num; //最大枪数
  164. char info[100];
  165. }__attribute__((packed)) encoder_up_tax_head_t;
  166. #define ENCODER_UP_HEAD_NUM 6
  167. //编码器数据
  168. typedef struct _encoder_up_tax_data_t
  169. {
  170. uint32_t id; //记录id
  171. uint32_t single_pulse_num; //单组脉冲数
  172. uint32_t total_pulse_num; //总脉冲数
  173. uint32_t time; //时间戳
  174. }__attribute__((packed)) encoder_up_tax_data_t;
  175. void encoder_init(void);
  176. uint8_t tax_oil_judge_single(uint8_t tax);
  177. uint8_t tax_oil_judge_all(void);
  178. uint8_t Gun_Signal_Check(uint8_t tax);
  179. uint8_t Gun_Signal_Check_all(void);
  180. void encoder_record_end_handle(void);
  181. void encoder_irq_handle(uint8_t exti);
  182. void gunsignal_irq_delay_handle(void);
  183. void pulse_irq_delay_handle(void);
  184. //void gunnum_irq_delay_handle(void);
  185. void encoder_eeprom_write_handle(void);
  186. uint8_t coll_encoder_return_0_1(uint8_t tax, uint8_t gun, uint8_t order, uint8_t *data);
  187. uint8_t coll_encoder_return_2_3(uint8_t tax, uint8_t gun, uint8_t order, uint8_t *data);
  188. uint8_t Reset_encoder_judge(void);
  189. #endif