| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871 |
- /*
- * @Description:
- * @Version: 2.0
- * @Author: Seven
- * @Date: 2022-08-22 09:55:26
- * @LastEditors: Seven
- * @LastEditTime: 2023-04-28 18:19:10
- */
- #include "encoder.h"
- #include "includes.h"
- GPIO_TypeDef_t g_encoder_cfg[] = {
- {GPIOC,GPIO_Pin_6, EXTI_Line6},
- {GPIOD,GPIO_Pin_12,EXTI_Line12},
- {GPIOC,GPIO_Pin_7, EXTI_Line7},
- {GPIOD,GPIO_Pin_13,EXTI_Line13},
- {GPIOB,GPIO_Pin_10,EXTI_Line10},
- {GPIOD,GPIO_Pin_14,EXTI_Line14},
- {GPIOC,GPIO_Pin_9, EXTI_Line9},
- {GPIOD,GPIO_Pin_15,EXTI_Line15},
- };
- uint8_t pulse_index[4] = {1,3,5,7};//脉冲索引
- uint8_t gunsignal_index[4] = {0,2,4,6};//枪信号索引
- exti_info_t exti_info[EXTI_LINE_MAX];//中断线对应信息
- gun_gpio_info_t gun_gpio_info[ENCODER_TAX_NUM][ENCODER_GUN_NUM];//枪引脚定义信息
- encoder_record_info_t encoder_record_info[ENCODER_TAX_NUM][ENCODER_GUN_NUM];//编码器记录信息
- encoder_eeprom_t encoder_eeprom[ENCODER_TAX_NUM][ENCODER_GUN_NUM]; //编码器存储数据
- pulse_up_info_t pulse_up_data[ENCODER_TAX_NUM][ENCODER_GUN_NUM][ENCODER_SAVE_GROUP_NUM]; //脉冲上报信息存储
- encoder_gunnum_ee_t encoder_gunnum_ee[ENCODER_TAX_NUM][ENCODER_GUN_NUM];//抬枪次数存储
- gunnum_time_t gunnum_time[ENCODER_TAX_NUM][ENCODER_GUN_NUM];//空抬枪计时
- //写eeprom计时
- timeout_t eeprom_write_time =
- {
- 0xff,
- 0,
- (1*1000),
- };
- void encoder_data_init(void)//编码器数据初始化
- {
- memset(&encoder_record_info[0][0],0,sizeof(encoder_record_info_t));
- memset(&gun_gpio_info[0][0],0,sizeof(gun_gpio_info_t));
- memset(&encoder_eeprom[0][0],0,sizeof(encoder_eeprom_t));
- }
- //编码器引脚驱动初始化
- void encoder_gpio_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- EXTI_InitTypeDef EXTI_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,ENABLE);//使能PORTA,PORTC时钟
- GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//关闭jtag,使能SWD,可以用SWD模式调试
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz,这里不用传参,直接写死用最大速度50MHZ
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIO
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz,这里不用传参,直接写死用最大速度50MHZ
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9;
- GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化GPIO
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz,这里不用传参,直接写死用最大速度50MHZ
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
- GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化GPIO
-
- //中断配置
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //使能复用功能时钟
- //GPIOE.2 中断线以及中断初始化配置 上升沿触发
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource10);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource6);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource7);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource9);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource12);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource13);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource14);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOD, GPIO_PinSource15);
- EXTI_InitStructure.EXTI_Line = EXTI_Line6|EXTI_Line7|EXTI_Line9|EXTI_Line10|EXTI_Line12|EXTI_Line13|EXTI_Line14|EXTI_Line15;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; //上升沿和下降沿触发
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure); //根据EXTI_InitStruct中指定的参数初始化外设EXTI寄存器
- //设置中断优先级
- NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn; //使能中断通道
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; //抢占优先级2,
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; //子优先级3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
- NVIC_Init(&NVIC_InitStructure);
- EXTI_ClearITPendingBit(EXTI_Line10|EXTI_Line12|EXTI_Line13|EXTI_Line14|EXTI_Line15); //清除LINE11上的中断标志位
- EXTI_ClearITPendingBit(EXTI_Line6|EXTI_Line7|EXTI_Line9);
- }
- //枪引脚对应关系初始化
- void gun_gpio_init(void)
- {
- uint8_t i,j;
- uint8_t pulse,gunsignal;
- for(i = 0; i < ENCODER_TAX_NUM; i++)
- {
- for(j = 0; j < ENCODER_GUN_NUM; j++)
- {
- pulse = pulse_index[i*2+j];
- gunsignal = gunsignal_index[i*2+j];
- //枪信号
- gun_gpio_info[i][j].gunsignal.GPIOx = g_encoder_cfg[gunsignal].GPIOx;
- gun_gpio_info[i][j].gunsignal.GPIO_Pin = g_encoder_cfg[gunsignal].GPIO_Pin;
- gun_gpio_info[i][j].gunsignal.EXTI_Line = g_encoder_cfg[gunsignal].EXTI_Line;
- //脉冲信号
- gun_gpio_info[i][j].pulse.GPIOx = g_encoder_cfg[pulse].GPIOx;
- gun_gpio_info[i][j].pulse.GPIO_Pin = g_encoder_cfg[pulse].GPIO_Pin;
- gun_gpio_info[i][j].pulse.EXTI_Line = g_encoder_cfg[pulse].EXTI_Line;
- }
- }
- }
- //中断线对应关系初始化
- void exti_relation_init(void)
- {
- uint8_t i;
- uint8_t m,n;
- for(i = 0; i < 8; i++)
- {
- for(m = 0; m < ENCODER_TAX_NUM; m++)
- {
- for(n = 0; n < ENCODER_GUN_NUM; n++)
- {
- if(g_encoder_cfg[i].EXTI_Line == gun_gpio_info[m][n].gunsignal.EXTI_Line)
- {
- exti_info[i].tax = m;
- exti_info[i].gun = n;
- exti_info[i].mode = GUN_GPIO;
- exti_info[i].timeout = GUNSIGNAL_IRQ_TIME;
- break;
- }
- else if(g_encoder_cfg[i].EXTI_Line == gun_gpio_info[m][n].pulse.EXTI_Line)
- {
- exti_info[i].tax = m;
- exti_info[i].gun = n;
- exti_info[i].mode = PULSE_GPIO;
- exti_info[i].timeout = PULSE_IRQ_TIME;
- break;
- }
- }
- }
- }
- }
- //获取编码器相应引脚电平
- uint8_t get_encoder_bit(uint8_t tax, uint8_t gun, uint8_t mode)
- {
- uint8_t state = 0;
- if(mode == GUN_GPIO)
- {
- state = GPIO_ReadInputDataBit(gun_gpio_info[tax][gun].gunsignal.GPIOx,gun_gpio_info[tax][gun].gunsignal.GPIO_Pin);
- }
- else if(mode == PULSE_GPIO)
- {
- state = GPIO_ReadInputDataBit(gun_gpio_info[tax][gun].pulse.GPIOx,gun_gpio_info[tax][gun].pulse.GPIO_Pin);
- }
- return state;
- }
- //上电判断抬枪状态
- void gunsignal_init(void)
- {
- int i,j;
- for(i = 0; i < ENCODER_TAX_NUM; i++)
- {
- for(j = 0; j < ENCODER_GUN_NUM; j++)
- {
- encoder_record_info[i][j].gunsignal_irq.last_level = get_encoder_bit(i,j,GUN_GPIO);
- encoder_record_info[i][j].pulse_irq.last_level = get_encoder_bit(i,j,PULSE_GPIO);
- if(!encoder_record_info[i][j].gunsignal_irq.last_level)//低电平抬枪
- {
- encoder_record_info[i][j].encoder_count_info.gun_signal = GUN_UP;
- timeout_setValue(&encoder_record_info[i][j].pulse_stop_time,PULSE_GUN_END_TIME,1); //有抬枪信号定时
- printf_debug(ENCODER_INIT_DEBUG, "tax:%d,gun:%d,抬枪\n",i+1,j+1);
- }
- else
- {
- printf_debug(ENCODER_INIT_DEBUG, "tax:%d,gun:%d,挂枪\n",i+1,j+1);
- }
- // encoder_record_info[i][j].gunup_irq.last_level = encoder_record_info[i][j].encoder_count_info.gun_signal;
- }
- }
- }
- //编码器存储数据初始化
- void encoder_eeprom_init(void)
- {
- int i,j;
- uint8_t num = 0, err1,err2;
- uint8_t err3,err4;
- uint8_t tax1_temp[ENCODER_GUN_NUM*16] = {0};
- uint8_t tax2_temp[ENCODER_GUN_NUM*16] = {0};
- uint8_t tax1_temp_de[ENCODER_GUN_NUM*8] = {0};
- uint8_t tax2_temp_de[ENCODER_GUN_NUM*8] = {0};
- uint8_t tax1_temp_gun[ENCODER_GUN_NUM*8] = {0};
- uint8_t tax2_temp_gun[ENCODER_GUN_NUM*8] = {0};
- uint8_t tax1_temp_gun_de[ENCODER_GUN_NUM*4] = {0};
- uint8_t tax2_temp_gun_de[ENCODER_GUN_NUM*4] = {0};
- move:
- AT24CXX_Read(EEPROM_ENCODER_TAX1_ADDR,(uint8_t *)tax1_temp,ENCODER_GUN_NUM*16);//读编码器报税口1数据
- AT24CXX_Read(EEPROM_ENCODER_TAX2_ADDR,(uint8_t *)tax2_temp,ENCODER_GUN_NUM*16);//读编码器报税口2数据
- data_dump_debug(ENCODER_INIT_DATA_DEBUG, "tax1 read eeprom",tax1_temp,ENCODER_GUN_NUM*16);
- data_dump_debug(ENCODER_INIT_DATA_DEBUG, "tax2 read eeprom",tax2_temp,ENCODER_GUN_NUM*16);
- AT24CXX_Read(EEPROM_ENCODER_TAX1_GUNNUM_ADDR,(uint8_t *)tax1_temp_gun,ENCODER_GUN_NUM*8);//读编码器报税口1空抬枪次数
- AT24CXX_Read(EEPROM_ENCODER_TAX2_GUNNUM_ADDR,(uint8_t *)tax2_temp_gun,ENCODER_GUN_NUM*8);//读编码器报税口2空抬枪次数
-
- //解码
- err1 = HM_decode(tax1_temp,tax1_temp_de,ENCODER_GUN_NUM*16);
- err2 = HM_decode(tax2_temp,tax2_temp_de,ENCODER_GUN_NUM*16);
- err3 = HM_decode(tax1_temp_gun,tax1_temp_gun_de,ENCODER_GUN_NUM*8);
- err4 = HM_decode(tax2_temp_gun,tax2_temp_gun_de,ENCODER_GUN_NUM*8);
- if(err1 || err2 || err3 || err4)
- {
- num++;
- printf("encoder data err\n");
- if(num < 3)
- {
- goto move;
- }
- else//解码错误,清零
- {
- if(err1)//编码器数据
- {
- memset(tax1_temp,0,ENCODER_GUN_NUM*16);
- memset(tax1_temp_de,0,ENCODER_GUN_NUM*8);
- AT24CXX_Write(EEPROM_ENCODER_TAX1_ADDR,(uint8_t *)tax1_temp,ENCODER_GUN_NUM*16);
- }
- if(err2)
- {
- memset(tax2_temp,0,ENCODER_GUN_NUM*16);
- memset(tax2_temp_de,0,ENCODER_GUN_NUM*8);
- AT24CXX_Write(EEPROM_ENCODER_TAX2_ADDR,(uint8_t *)tax2_temp,ENCODER_GUN_NUM*16);
- }
- if(err3)//空抬枪数据
- {
- memset(tax1_temp_gun,0,ENCODER_GUN_NUM*8);
- memset(tax1_temp_gun_de,0,ENCODER_GUN_NUM*4);
- AT24CXX_Write(EEPROM_ENCODER_TAX1_GUNNUM_ADDR,(uint8_t *)tax1_temp_gun,ENCODER_GUN_NUM*8);
- }
- if(err4)
- {
- memset(tax2_temp_gun,0,ENCODER_GUN_NUM*8);
- memset(tax2_temp_gun_de,0,ENCODER_GUN_NUM*4);
- AT24CXX_Write(EEPROM_ENCODER_TAX2_GUNNUM_ADDR,(uint8_t *)tax2_temp_gun,ENCODER_GUN_NUM*8);
- }
- }
- }
- data_dump_debug(ENCODER_INIT_DATA_DEBUG, "tax1 decode",tax1_temp_de,ENCODER_GUN_NUM*8);
- data_dump_debug(ENCODER_INIT_DATA_DEBUG, "tax2 decode",tax2_temp_de,ENCODER_GUN_NUM*8);
- //eeprom存储数据初始化
- for(i = 0; i < ENCODER_TAX_NUM; i++)
- {
- for(j = 0; j < ENCODER_GUN_NUM; j++)
- {
- if(i == 0)
- {
- memcpy((uint8_t *)&encoder_eeprom[i][j].id, tax1_temp_de+(j*8), 8);
- memcpy((uint8_t *)&encoder_gunnum_ee[i][j].gunup_num,tax1_temp_gun_de+(j*4),4);//抬枪次数
- encoder_record_info[i][j].encoder_count_info.gunup_num = encoder_gunnum_ee[i][j].gunup_num;
- }
- else if(i == 1)
- {
- memcpy((uint8_t *)&encoder_eeprom[i][j].id, tax2_temp_de+(j*8), 8);
- memcpy((uint8_t *)&encoder_gunnum_ee[i][j].gunup_num,tax2_temp_gun_de+(j*4),4);//抬枪次数
- encoder_record_info[i][j].encoder_count_info.gunup_num = encoder_gunnum_ee[i][j].gunup_num;
- }
- printf_debug(ENCODER_INIT_DEBUG, "tax:%d,gun:%d,id=%u,total pulse=%u\n",i+1,j+1,encoder_eeprom[i][j].id,encoder_eeprom[i][j].total_pulse);
- printf_debug(ENCODER_INIT_DEBUG, "tax:%d,gun:%d,gun num=%u\n",i+1,j+1,encoder_gunnum_ee[i][j].gunup_num);
- }
- }
- }
- //编码器初始化
- void encoder_init(void)
- {
- encoder_data_init();
- encoder_gpio_init(); //编码器引脚驱动初始化
- gun_gpio_init(); //枪引脚对应关系初始化
- exti_relation_init(); //中断线对应关系初始化
- gunsignal_init(); //上电抬抢信号读取
- encoder_eeprom_init(); //编码器存储信息初始化
- }
- //脉冲数记录函数
- void pulse_save_handle(uint8_t tax, uint8_t gun, uint8_t mode)
- {
- uint8_t write_group = 0;
- uint32_t id = 0;
- uint32_t total_pulse = 0;
- uint32_t single_pulse = 0;
- if(encoder_record_info[tax][gun].encoder_count_info.pulse_count)//脉冲数不为0
- {
- single_pulse = encoder_record_info[tax][gun].encoder_count_info.pulse_count;
- encoder_record_info[tax][gun].encoder_count_info.pulse_count = 0;
- //总脉冲数
- // encoder_eeprom[tax][gun].total_pulse += encoder_record_info[tax][gun].encoder_count_info.pulse_count;
-
- //组号从0开始,id从1开始
- if(encoder_record_info[tax][gun].encoder_count_info.write_id == 0)//上电后第一次写入
- {
- // //读取存储的id
- encoder_record_info[tax][gun].encoder_count_info.write_id = encoder_eeprom[tax][gun].id;
- encoder_record_info[tax][gun].encoder_count_info.read_id = encoder_eeprom[tax][gun].id;
- encoder_record_info[tax][gun].encoder_count_info.read_first_valie = 1;
- write_group = 0;
- }
- else
- {
- //新数据组号+1
- write_group = (encoder_record_info[tax][gun].encoder_count_info.write_group+1)%ENCODER_SAVE_GROUP_NUM; //写入的组号
- }
- // id = encoder_record_info[tax][gun].encoder_count_info.write_id+1;//记录id
- id = encoder_eeprom[tax][gun].id + 1;//记录id
- total_pulse = encoder_eeprom[tax][gun].total_pulse+single_pulse;//总脉冲
- //上报信息存储
- pulse_up_data[tax][gun][write_group].id = id;
- pulse_up_data[tax][gun][write_group].pulse_mode = mode;
- pulse_up_data[tax][gun][write_group].single_pulse_num = single_pulse;
- pulse_up_data[tax][gun][write_group].total_pulse_num = total_pulse;
- pulse_up_data[tax][gun][write_group].time = encoder_gunnum_ee[tax][gun].gunup_num;//encoder_gunnum_ee[tax][gun].gunup_num;//RTC_GetCounter(); //时间
- //记录数据存储
- encoder_record_info[tax][gun].encoder_count_info.write_group = write_group;
- // encoder_record_info[tax][gun].encoder_count_info.write_id = id;
- encoder_eeprom[tax][gun].id = id;
- encoder_eeprom[tax][gun].total_pulse = total_pulse;
- encoder_record_info[tax][gun].encoder_count_info.eeprom_write_flag = 1; //写eeprom标记置1
- printf_debug(ENCODER_NEW_DEBUG, "t= %d,g= %d, group=%u,id=%u,mode=%d,pulse=%u,total=%u,time=%u\n",tax+1,gun+1,write_group,pulse_up_data[tax][gun][write_group].id,pulse_up_data[tax][gun][write_group].pulse_mode,\
- pulse_up_data[tax][gun][write_group].single_pulse_num,pulse_up_data[tax][gun][write_group].total_pulse_num,pulse_up_data[tax][gun][write_group].time);
- }
- else//脉冲数为0,空抬枪
- {
- if(mode == PULSE_END_MODE_GUN)//挂枪产生的交易结算才计算空抬枪次数
- {
- if((gunnum_time[tax][gun].gundown_time - gunnum_time[tax][gun].gunup_time) <= GUN_NUM_TIME)
- {
- printf("gun time short\n");
- }
- else
- {
- encoder_record_info[tax][gun].encoder_count_info.gunup_num++;//抬枪次数++
- encoder_record_info[tax][gun].encoder_count_info.eeprom_write_gunup_flag = 1;//写eeprom标记
- printf("gun time long\n");
- }
- }
- }
- }
- //交易完成处理函数
- void encoder_record_end_handle(void)
- {
- uint8_t i,j;
- for(i = 0; i < ENCODER_TAX_NUM; i++)
- {
- for(j = 0; j < ENCODER_GUN_NUM; j++)
- {
- //脉冲停止计数定时超时
- if(timeout_isOut(&encoder_record_info[i][j].pulse_stop_time))
- {
- pulse_save_handle(i,j,PULSE_END_MODE_LOGIC);
- //抬枪信号设置为无效
- encoder_record_info[i][j].gunsignal_irq.gun_signal_is_work = GUN_SIGNAL_DISABLE;
- printf("tax:%d,gun:%d,pulse timeout\n",i+1,j+1);
- // printf("tax:%d,gun:%d,is work:%d\n",i+1,j+1,encoder_record_info[i][j].gunsignal_irq.gun_signal_is_work);
- }
- //产生挂枪中断
- if(encoder_record_info[i][j].gunsignal_irq.gun_down_irq)
- {
- encoder_record_info[i][j].gunsignal_irq.gun_down_irq = 0;
- pulse_save_handle(i,j,PULSE_END_MODE_GUN);
- }
- }
- }
- }
- //中断内函数
- void encoder_irq_handle(uint8_t exti)
- {
- uint8_t tax,gun;
- tax = exti_info[exti].tax;
- gun = exti_info[exti].gun;
- if(exti_info[exti].mode == GUN_GPIO)
- {
- timeout_setValue(&encoder_record_info[tax][gun].gunsignal_irq.wave_time,GUNSIGNAL_IRQ_TIME,1);
- }
- else if(exti_info[exti].mode == PULSE_GPIO)
- {
- timeout_setValue_us(&encoder_record_info[tax][gun].pulse_irq.wave_time,PULSE_IRQ_TIME,1);
- }
- }
- //抬枪处理函数
- void gun_up_handle(uint8_t tax, uint8_t gun)
- {
- encoder_record_info[tax][gun].gunsignal_irq.gun_signal_is_work = GUN_SIGNAL_ENABLE; //枪信号标记为可用
- encoder_record_info[tax][gun].encoder_count_info.gun_signal = GUN_UP; //枪状态为抬枪
- timeout_setValue(&encoder_record_info[tax][gun].pulse_stop_time,PULSE_GUN_END_TIME,1); //有抬枪信号定时
- //抬枪计时
- gunnum_time[tax][gun].gunup_time = TickCounter;
- // timeout_setValue(&encoder_record_info[tax][gun].gunup_irq.wave_time,GUNUP_NUM_IRQ_TIME,1);//记录抬枪次数
- }
- //挂枪处理函数
- void gun_down_handle(uint8_t tax, uint8_t gun)
- {
- timeout_stop(&encoder_record_info[tax][gun].pulse_stop_time);
- encoder_record_info[tax][gun].gunsignal_irq.gun_down_irq++; //挂枪中断个数加一
- encoder_record_info[tax][gun].gunsignal_irq.gun_signal_is_work = GUN_SIGNAL_ENABLE; //枪信号标记为可用
- encoder_record_info[tax][gun].encoder_count_info.gun_signal = GUN_DOWN; //枪状态为挂枪
- gunnum_time[tax][gun].gundown_time = TickCounter;
- // timeout_setValue(&encoder_record_info[tax][gun].gunup_irq.wave_time,GUNUP_NUM_IRQ_TIME,1);//记录抬枪次数
- }
- //脉冲上升沿处理函数
- void pulse_up_handle(uint8_t tax, uint8_t gun)
- {
- encoder_record_info[tax][gun].encoder_count_info.pulse_count++;
- if(encoder_record_info[tax][gun].encoder_count_info.gun_signal == GUN_DOWN)
- {
- encoder_record_info[tax][gun].gunsignal_irq.gun_signal_is_work = GUN_SIGNAL_DISABLE;
- timeout_setValue(&encoder_record_info[tax][gun].pulse_stop_time,PULSE_NOGUN_END_TIME,1); //无抬枪信号定时
- }
- else
- {
- if(encoder_record_info[tax][gun].gunsignal_irq.gun_signal_is_work == GUN_SIGNAL_DISABLE)//抬枪信号不可用
- {
- timeout_setValue(&encoder_record_info[tax][gun].pulse_stop_time,PULSE_NOGUN_END_TIME,1); //无抬枪信号定时
- }
- else
- {
- timeout_setValue(&encoder_record_info[tax][gun].pulse_stop_time,PULSE_GUN_END_TIME,1); //有抬枪信号定时
- }
- }
- }
- //抬枪信号滤波函数
- void gunsignal_irq_delay_handle(void)
- {
- uint8_t i,j;
- uint8_t current_level;
- for(i = 0; i < ENCODER_TAX_NUM; i++)
- {
- for(j = 0; j < ENCODER_GUN_NUM; j++)
- {
- if(timeout_isOut(&encoder_record_info[i][j].gunsignal_irq.wave_time))
- {
- current_level = get_encoder_bit(i,j,GUN_GPIO);//当次有效电平
- if(current_level != encoder_record_info[i][j].gunsignal_irq.last_level)
- {
- //当次电平和上次电平不同
- if(current_level)//高电平
- {
- //当次为高电平,认为是上升沿,挂枪
- gun_down_handle(i,j);
- printf_debug(ENCODER_GUN_DEBUG, "t=%d, g=%d,gun down\n",i+1,j+1);
- }
- else
- {
- //当次为低电平,认为是下降沿,抬枪
- gun_up_handle(i,j);
- printf_debug(ENCODER_GUN_DEBUG, "t=%d, g=%d,gun up\n",i+1,j+1);
- }
- }
- encoder_record_info[i][j].gunsignal_irq.last_level = current_level;
- }
- }
- }
- }
- //脉冲信号中断处理函数
- void pulse_irq_delay_handle(void)
- {
- uint8_t i,j;
- uint8_t current_level;
- for(i = 0; i < ENCODER_TAX_NUM; i++)
- {
- for(j = 0; j < ENCODER_GUN_NUM; j++)
- {
- if(timeout_isOut_us(&encoder_record_info[i][j].pulse_irq.wave_time))
- {
- current_level = get_encoder_bit(i,j,PULSE_GPIO);//当次有效电平
- if(current_level != encoder_record_info[i][j].pulse_irq.last_level)
- {
- //当次电平和上次电平不同
- if(current_level)//高电平
- {
- //当次为高电平,认为是上升沿
- pulse_up_handle(i,j);
- }
- else
- {
- //当次为低电平,认为是下降沿
- }
- }
- encoder_record_info[i][j].pulse_irq.last_level = current_level;
- }
- }
- }
- }
- /*
- //抬枪次数滤波函数
- void gunnum_irq_delay_handle(void)
- {
- uint8_t i,j;
- uint8_t current_level;
- for(i = 0; i < ENCODER_TAX_NUM; i++)
- {
- for(j = 0; j < ENCODER_GUN_NUM; j++)
- {
- if(timeout_isOut(&encoder_record_info[i][j].gunup_irq.wave_time))
- {
- current_level = encoder_record_info[i][j].encoder_count_info.gun_signal;//当次枪状态
- if(current_level != encoder_record_info[i][j].gunup_irq.last_level)
- {
- //当次电平和上次电平不同
- if(current_level==GUN_UP)//抬枪
- {
- encoder_record_info[i][j].encoder_count_info.gunup_num++;//抬枪次数++
- encoder_record_info[i][j].encoder_count_info.eeprom_write_gunup_flag = 1;//写eeprom标记
- }
- else
- {
- }
- }
- encoder_record_info[i][j].gunup_irq.last_level = current_level;
- }
- }
- }
- }
- */
- uint8_t Tax=0,Gun=0;
- //编码器写eeprom
- void encoder_eeprom_write_handle(void)
- {
- uint8_t temp[8] = {0};
- uint8_t temp_en[16] = {0};
- uint32_t eeprom_addr = 0;
- uint32_t id = 0;
- if(timeout_isOut(&eeprom_write_time))
- {
- if(encoder_record_info[Tax][Gun].encoder_count_info.eeprom_write_flag)
- {
- id = encoder_eeprom[Tax][Gun].id;//记录当前写入的id
- //保存总脉冲数(eeprom)
- memcpy(temp,&encoder_eeprom[Tax][Gun].id,8);
- HM_encode(temp,temp_en,8);
- if(Tax==0)
- {
- eeprom_addr = EEPROM_ENCODER_TAX1_ADDR + Gun*16;
- }
- else if(Tax==1)
- {
- eeprom_addr = EEPROM_ENCODER_TAX2_ADDR + Gun*16;
- }
- AT24CXX_Write(eeprom_addr,(uint8_t *)temp_en,16);
-
- encoder_record_info[Tax][Gun].encoder_count_info.write_id = id;//记录当前写入的id
- data_dump_debug(ENCODER_EEPROM_DATA_DEBUG, "write eeprom", temp_en,16);
-
- printf_debug(ENCODER_EEPROM_DEBUG, "写eeprom:addr=0x%x, tax=%d, gun=%d, id=%u, total pulse=%u\n",eeprom_addr,Tax+1,Gun+1,encoder_eeprom[Tax][Gun].id,encoder_eeprom[Tax][Gun].total_pulse);
- encoder_record_info[Tax][Gun].encoder_count_info.eeprom_write_flag = 0;
- }
-
- //存储空抬枪次数
- if(encoder_record_info[Tax][Gun].encoder_count_info.eeprom_write_gunup_flag)
- {
- encoder_gunnum_ee[Tax][Gun].gunup_num = encoder_record_info[Tax][Gun].encoder_count_info.gunup_num;
- //保存总脉冲数(eeprom)
- memcpy(temp,(uint8_t *)&encoder_gunnum_ee[Tax][Gun].gunup_num,4);
- HM_encode(temp,temp_en,4);
- if(Tax==0)
- {
- eeprom_addr = EEPROM_ENCODER_TAX1_GUNNUM_ADDR + Gun*8;
- }
- else if(Tax==1)
- {
- eeprom_addr = EEPROM_ENCODER_TAX2_GUNNUM_ADDR + Gun*8;
- }
- AT24CXX_Write(eeprom_addr,(uint8_t *)temp_en,8);
- printf_debug(1,"写gun num:addr=0x%x, tax=%d, gun=%d, gun num=%u\n",eeprom_addr,Tax+1,Gun+1,encoder_gunnum_ee[Tax][Gun].gunup_num);
- encoder_record_info[Tax][Gun].encoder_count_info.eeprom_write_gunup_flag = 0;
- }
- Gun = (Gun+1)%ENCODER_GUN_NUM;
- if(Gun==0)
- {
- Tax = (Tax+1)%ENCODER_TAX_NUM;
- }
- }
- }
- //是否正在加油检测(一个报税口)
- //返回:1 正在加油,0 没有加油
- uint8_t tax_oil_judge_single(uint8_t tax)
- {
- uint8_t i,ret = 0;
- for(i = 0; i < ENCODER_GUN_NUM; i++)
- {
- if(encoder_record_info[tax][i].encoder_count_info.pulse_count)
- {
- ret = 1;
- }
- }
- return ret;
- }
- //是否正在加油检测(所有枪)
- uint8_t tax_oil_judge_all(void)
- {
- uint8_t tax1,tax2;
- uint8_t gun_signal = 0;
- tax1 = tax_oil_judge_single(TAX1);
- tax2 = tax_oil_judge_single(TAX2);
- gun_signal = (tax2<<4)|tax1;
- return gun_signal;
- }
- //抬枪信号检测
- uint8_t Gun_Signal_Check(uint8_t tax)
- {
- int i;
- uint8_t gun_signal = 0;
- for(i = 0; i < ENCODER_GUN_NUM; i++)
- {
- if(encoder_record_info[tax][i].gunsignal_irq.gun_signal_is_work == GUN_SIGNAL_DISABLE)//抬枪信号不可用
- {
- //抬枪信号不可用,脉冲计数不为零,认为当前是抬枪
- if(encoder_record_info[tax][i].encoder_count_info.pulse_count)
- {
- gun_signal |= 1<<i;
- }
- }
- else
- {
- //抬枪信号可用,抬枪信号为抬枪状态或脉冲计数不为零,认为当前是抬枪
- if((encoder_record_info[tax][i].encoder_count_info.gun_signal == GUN_UP) || encoder_record_info[tax][i].encoder_count_info.pulse_count)
- {
- gun_signal |= 1<<i;
- }
- }
- if(encoder_record_info[tax][i].gunsignal_irq.wave_time.flag)//抬枪信号滤波过程,也不读报税口
- {
- gun_signal |= 1<<i;
- }
- }
- return gun_signal;
- }
- //全部抬枪信号判断
- uint8_t Gun_Signal_Check_all(void)
- {
- uint8_t tax1,tax2;
- uint8_t gun_signal = 0;
- tax1 = Gun_Signal_Check(TAX1);
- tax2 = Gun_Signal_Check(TAX2);
- gun_signal = (tax2<<4)|tax1;
- return gun_signal;
- }
- //读取时的组号判断
- //返回:0 无新数据;1 有新数据。group为有新数据时读取的组号
- uint8_t read_group_check(uint8_t tax, uint8_t gun, uint8_t *group, uint8_t order)
- {
- uint8_t ret = 0;
- if(order == 0 || order == 2)//默认读取
- {
- if(encoder_record_info[tax][gun].encoder_count_info.write_id == encoder_record_info[tax][gun].encoder_count_info.read_id) //无新数据
- {
- //无新数据,返回最后一笔的交易
- *group = encoder_record_info[tax][gun].encoder_count_info.read_group;
- ret = 0;
- }
- else
- {
- if(encoder_record_info[tax][gun].encoder_count_info.write_id - encoder_record_info[tax][gun].encoder_count_info.read_id > ENCODER_SAVE_GROUP_NUM)
- {
- //已经有一轮数据未读取,从当前写入组的下一组开始读取
- *group = (encoder_record_info[tax][gun].encoder_count_info.write_group+1)%ENCODER_SAVE_GROUP_NUM;
- if(encoder_record_info[tax][gun].encoder_count_info.read_first_valie)
- {
- encoder_record_info[tax][gun].encoder_count_info.read_first_valie = 0;
- }
- }
- else //接着上次读取组号开始读取
- {
- if(encoder_record_info[tax][gun].encoder_count_info.read_first_valie)//上电读取的第一笔有效数据
- {
- encoder_record_info[tax][gun].encoder_count_info.read_first_valie = 0;
- *group = 0;
- }
- else
- *group = (encoder_record_info[tax][gun].encoder_count_info.read_group+1)%ENCODER_SAVE_GROUP_NUM;
- }
- ret = 1;
- }
- encoder_record_info[tax][gun].encoder_count_info.read_group = *group;
- encoder_record_info[tax][gun].encoder_count_info.read_id = pulse_up_data[tax][gun][*group].id;
- }
- else if(order == 1 || order == 3)//读取最新交易
- {
- if(encoder_record_info[tax][gun].encoder_count_info.write_id == encoder_record_info[tax][gun].encoder_count_info.read_id) //无新数据
- {
- ret = 0;
- }
- else
- {
- ret = 1;
- }
- *group = encoder_record_info[tax][gun].encoder_count_info.write_group; //新交易为上次写入的组号
- encoder_record_info[tax][gun].encoder_count_info.read_group = *group;
- encoder_record_info[tax][gun].encoder_count_info.read_id = pulse_up_data[tax][gun][*group].id;
- }
- return ret;
- }
- //编码器返回参数(0和1)
- uint8_t coll_encoder_return_0_1(uint8_t tax, uint8_t gun, uint8_t order, uint8_t *data)
- {
- uint8_t len = 0;
- uint8_t read_group = 0;
- uint8_t new_pulse = 0;
- encoder_up_gun_t *encoder_up = (encoder_up_gun_t *)data;
- encoder_up->order = order;
- encoder_up->tax1_gunstatus = Gun_Signal_Check(TAX1);
- encoder_up->tax2_gunstatus = Gun_Signal_Check(TAX2);
-
- //读取编码器的参数
- read_encoder_para.tax = tax;
- read_encoder_para.gunstatus = Gun_Signal_Check_all();
- read_encoder_para.flag = 1;
- timeout_setValue(&read_encoder_para.tt_encoder,ENCODER_GUNSTATUS_TIMEOUT,1);
- //判断有无新纪录,及查找读取的组号
- new_pulse = read_group_check(tax-1,gun-1,&read_group,order);
- printf_debug(MSG_ENCODER_DOWN_TYPE3, "read group:%d, new pulse:%d\n",read_group,new_pulse);
- encoder_up->new_recode_status = new_pulse;
- encoder_up->id = pulse_up_data[tax-1][gun-1][read_group].id;
- encoder_up->id_mode = (pulse_up_data[tax-1][gun-1][read_group].pulse_mode)|0x80;//记录产生方式最高位置1
- encoder_up->single_pulse_num = pulse_up_data[tax-1][gun-1][read_group].single_pulse_num;
- encoder_up->total_pulse_num = pulse_up_data[tax-1][gun-1][read_group].total_pulse_num;
- encoder_up->time = encoder_gunnum_ee[tax-1][gun-1].gunup_num;//encoder_gunnum_ee[tax-1][gun-1].gunup_num;//pulse_up_data[tax-1][gun-1][read_group].time;//抬枪次数
- len = sizeof(encoder_up_gun_t);
- return len;
- }
- //编码器返回数据(指令2和3)
- uint8_t coll_encoder_return_2_3(uint8_t tax, uint8_t gun, uint8_t order, uint8_t *data)
- {
- uint8_t len = 0;
- uint8_t read_group = 0;
- uint8_t new_recode_gun = 0;
- uint8_t new_recode_tax = 0; //新交易标记
- uint8_t mode = 0; //产生记录方式
- uint8_t temp[20] = {0};
- uint8_t i;
- encoder_up_tax_head_t *encoder_head = (encoder_up_tax_head_t *)data;
- encoder_up_tax_data_t *encoder_data = (encoder_up_tax_data_t *)temp;
- encoder_head->order = order;
- encoder_head->tax1_gunstatus = Gun_Signal_Check(TAX1);
- encoder_head->tax2_gunstatus = Gun_Signal_Check(TAX2);
- //读取编码器的参数
- read_encoder_para.tax = tax;
- read_encoder_para.gunstatus = Gun_Signal_Check_all();
- read_encoder_para.flag = 1;
- timeout_setValue(&read_encoder_para.tt_encoder,ENCODER_GUNSTATUS_TIMEOUT,1);
-
- for(i = 0; i < ENCODER_GUN_NUM; i++)
- {
- new_recode_gun = read_group_check(tax-1, i, &read_group, order);
- if(new_recode_gun)
- {
- new_recode_tax |= 1<<i;
- }
- encoder_data->id = pulse_up_data[tax-1][i][read_group].id;
- encoder_data->single_pulse_num = pulse_up_data[tax-1][i][read_group].single_pulse_num;
- encoder_data->total_pulse_num = pulse_up_data[tax-1][i][read_group].total_pulse_num;
- encoder_data->time = encoder_gunnum_ee[tax-1][i].gunup_num;//pulse_up_data[tax-1][i][read_group].time;//抬枪次数
- if(pulse_up_data[tax-1][i][read_group].pulse_mode)
- {
- mode |= 1<<i;
- }
- memcpy(encoder_head->info+(i*sizeof(encoder_up_tax_data_t)),temp,sizeof(encoder_up_tax_data_t));
- }
- encoder_head->new_recode_status = new_recode_tax;
- encoder_head->id_mode = mode|0x80;//记录产生方式最高位置1
- encoder_head->gun_num = ENCODER_GUN_NUM;
- len = ENCODER_UP_HEAD_NUM + ENCODER_GUN_NUM*sizeof(encoder_up_tax_data_t);
- return len;
- }
- //设备复位判断,编码器没有计数,且已经写eeprom
- //返回:0 可以复位,1 不能复位
- uint8_t Reset_encoder_judge(void)
- {
- uint8_t ret = 0;
- uint8_t tax1 = 0, tax2 = 0;
- uint8_t eeprom = 0;
- uint8_t i,j;
- tax1 = tax_oil_judge_single(TAX1);
- tax2 = tax_oil_judge_single(TAX2);
- for(i = 0; i < ENCODER_TAX_NUM; i++)
- {
- for(j = 0; j < ENCODER_GUN_NUM; j++)
- {
- if(encoder_record_info[i][j].encoder_count_info.eeprom_write_flag == 1)
- {
- eeprom = 1;
- }
- }
- }
-
- if(tax1==0 && tax2==0 && eeprom==0)
- ret = 0;
- else
- ret = 1;
-
- return ret;
- }
|