commit c3750fc19e79957aa042552f0ab387d3e9fa3565 Author: sdagit.io Date: Sun Oct 20 10:53:08 2024 +0900 Upload files to "/" diff --git a/main.c b/main.c new file mode 100644 index 0000000..3093a93 --- /dev/null +++ b/main.c @@ -0,0 +1,3144 @@ +#include +#include +#include +#include +#include + +#define DATA_REGISTER_EMPTY (1< 0U) + { + batt_volt_1 = link_data_buffer[13]; + } + else + { + batt_volt_1 = 0; + } + batt_current_1 = link_data_buffer[12]; + batt_health_1 = link_data_buffer[11]; + + //batt2 temperature + temp = link_data_buffer[10]; + batt_temperature_2 = temp; + + + if(link_data_buffer[9] > 0U) + { + batt_volt_2 = link_data_buffer[9]; + } + else + { + batt_volt_2 = 0U; + } +} + +// USART0 Receiver interrupt service routine +interrupt [USART0_RXC] void usart0_rx_isr(void) //(422)to +{ + uint8_t status; + uint8_t data; + status=UCSR0A; + data=UDR0; + if (((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))) == 0) + { + rx_buffer0[rx_wr_index0++]=data; + #if RX_BUFFER_SIZE0 == 256U + // special case for receiver buffer size=256 + if (++rx_counter0 == 0U) {rx_buffer_overflow0=1;} + #else + if (rx_wr_index0 == (uint8_t)RX_BUFFER_SIZE0){rx_wr_index0 = 0;} + if (++rx_counter0 == (uint8_t)RX_BUFFER_SIZE0) + { + rx_counter0=0U; + rx_buffer_overflow0=1; + } + #endif + } + + + //¾²±â + if((link_data_buffer[3] == 0xaaU)&&(link_data_buffer[2] == 0x55U)) + { + i2c_add = (uint16_t) (link_data_buffer[1] * 256U) + link_data_buffer[0]; + i2c_w_data = data; + i2c_write_act = 1; + link_data_buffer[3] = 0x00; + link_data_buffer[2] = 0x00; + link_data_buffer[1] = 0x00; + link_data_buffer[0] = 0x00; + } + + //Àбâ + if((link_data_buffer[2] == 0xaaU)&&(link_data_buffer[1] == 0x00U)) + { + i2c_add = (link_data_buffer[0] * 256U) + data; + i2c_read_act = 1; + link_data_buffer[2] = 0x00; + link_data_buffer[1] = 0x00; + link_data_buffer[0] = 0x00; + } + + + //Àбâ list + if((link_data_buffer[2] == 0xaaU)&&(link_data_buffer[1] == 0xaaU)) + { + i2c_add = (link_data_buffer[0] * 256U) + data; + i2c_read_all_act = 1; + link_data_buffer[2] = 0x00; + link_data_buffer[1] = 0x00; + link_data_buffer[0] = 0x00; + } + + + + if(data == 0x0aU) + { + //ä³Î¼³Á¤ »óÅ + if((link_data_buffer[5] == 0x7fU) && (link_data_buffer[4] == 0xfeU) && (link_data_buffer[3] == 0x55U) && (link_data_buffer[0] == 0x0dU)) + { + ch_status_1 = link_data_buffer[2]; + ch_status_2 = link_data_buffer[1]; + + link_data_buffer[5] = 0; + link_data_buffer[4] = 0; + link_data_buffer[3] = 0; + link_data_buffer[2] = 0; + link_data_buffer[1] = 0; + link_data_buffer[0] = 0; + + for(buff_loop = 0U;buff_loop < 6U;buff_loop++) + { + link_data_buffer[buff_loop] = 0U; + } + } + + //ºÐ¹è±â»óÅ ¹× ¿¡·¯Á¤º¸ + if((link_data_buffer[94] == 0x7fU) && (link_data_buffer[93] == 0xfeU) && (link_data_buffer[92] == 0x00U) && (link_data_buffer[0] == 0x0dU)) + { + //err info + usart0_err_check(); + + + switch(temp & 0x07U) + { + case 1: + mode = BATT; + break; + + case 2: + mode = GEN; + break; + + case 4: + mode = AC; + break; + + default: + break; + } + + // ä³Îº° Àü¾Ð ¹× Àü·ù + voltage_ch1 = ((uint16_t)link_data_buffer[80] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[79]); //v ch1 CS + currunt_ch1 = ((uint16_t)link_data_buffer[78] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[77]); + voltage_ch2 = ((uint16_t)link_data_buffer[76] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[75]); //v ch2 SW + currunt_ch2 = ((uint16_t)link_data_buffer[74] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[73]); + voltage_ch3 = ((uint16_t)link_data_buffer[72] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[71]); //v ch3 EX + currunt_ch3 = ((uint16_t)link_data_buffer[70] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[69]); + voltage_ch4 = ((uint16_t)link_data_buffer[68] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[67]); //v ch4 MAST + currunt_ch4 = ((uint16_t)link_data_buffer[66] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[65]); + voltage_ch5 = ((uint16_t)link_data_buffer[64] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[63]); //v ch5 OC1 + currunt_ch5 = ((uint16_t)link_data_buffer[62] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[61]); + voltage_ch6 = ((uint16_t)link_data_buffer[60] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[59]); //v ch6 ST1 + currunt_ch6 = ((uint16_t)link_data_buffer[58] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[57]); + voltage_ch7 = ((uint16_t)link_data_buffer[56] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[55]); //v ch7 MC + currunt_ch7 = ((uint16_t)link_data_buffer[54] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[53]); + voltage_ch8 = ((uint16_t)link_data_buffer[52] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[51]); //v ch5 NAVI + currunt_ch8 = ((uint16_t)link_data_buffer[50] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[49]); + voltage_ch9 = ((uint16_t)link_data_buffer[48] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[47]); //v ch6 SB + currunt_ch9 = ((uint16_t)link_data_buffer[46] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[45]); + voltage_ch10 = ((uint16_t)link_data_buffer[44] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[43]); //v ch7 OC2 + currunt_ch10 = ((uint16_t)link_data_buffer[42] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[41]); + voltage_ch11 = ((uint16_t)link_data_buffer[40] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[39]); //v ch5 ST2 + currunt_ch11 = ((uint16_t)link_data_buffer[38] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[37]); + voltage_ch12 = ((uint16_t)link_data_buffer[36] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[35]); //v ch6 GETC + currunt_ch12 = ((uint16_t)link_data_buffer[34] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[33]); + voltage_ch13 = ((uint16_t)link_data_buffer[32] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[31]); //v ch5 GC + currunt_ch13 = ((uint16_t)link_data_buffer[30] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[29]); + voltage_ch14 = ((uint16_t)link_data_buffer[28] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[27]); //v ch6 CNIC + currunt_ch14 = ((uint16_t)link_data_buffer[26] * (uint16_t)0x100U) + (uint16_t)(link_data_buffer[25]); + + //ac + ac_volt = (link_data_buffer[24] * 256U) + link_data_buffer[23]; + + + //generator + gen_rpm = link_data_buffer[22]; + gen_volt = ((link_data_buffer[21]*256U)+ link_data_buffer[20]); + gen_current = (uint16_t)link_data_buffer[19]; + gen_fuel = link_data_buffer[18]; + gen_status = link_data_buffer[17]; + gen_temp = link_data_buffer[16]; + + usart0_gen_info(); + + + batt_current_2 = link_data_buffer[8]; + batt_health_2 = link_data_buffer[7]; + + batt_fuel_1 = link_data_buffer[6]; + batt_fuel_2 = link_data_buffer[5]; + + + //batt1 info + if((link_data_buffer[4] & 0xC0U) == 0x00U) + { + batt1_status = DISCONNECT; + } + else + { + if((link_data_buffer[4] & 0xC0U)!= 0xc0U) + { + if((link_data_buffer[4] & 0x80U) == 0x80U){batt1_status = CHARGE;} + if((link_data_buffer[4] & 0x40U) == 0x40U){batt1_status = DISCHARGE;} + if((link_data_buffer[4] & 0x20U) == 0x20U){batt1_heater = ON;}else{batt1_heater = OFF;} + } + + } + //batt2 info + if((link_data_buffer[4] & 0x0cU) == 0x00U) + { + batt2_status = DISCONNECT; + } + else + { + if((link_data_buffer[4] & 0x0cU)!= 0x0cU) + { + if((link_data_buffer[4] & 0x08U) == 0x08U){batt2_status = CHARGE;} + if((link_data_buffer[4] & 0x04U) == 0x04U){batt2_status = DISCHARGE;} + if((link_data_buffer[4] & 0x02U) == 0x02U){batt2_heater = ON;}else{batt2_heater = OFF;} + } + } + + //source 4,3 + voltage_source = (link_data_buffer[3] * 256U) + link_data_buffer[2]; + + //check sum + check_sum = link_data_buffer[1]; + + for(buff_loop = 0U;buff_loop < 95U;buff_loop++) + { + link_data_buffer[buff_loop] = 0; + } + + } + } + + for(link_buffer_count = 93U;link_buffer_count > 0U;link_buffer_count--) + { + link_data_buffer[link_buffer_count + 1U] = link_data_buffer[link_buffer_count]; + } + link_data_buffer[1] = link_data_buffer[0]; + link_data_buffer[0] = data; + + loss_count = 0U; //¸µÅ© ´ÜÀý È®Àοë +} + +// Get a character from the USART0 Receiver buffer +#define _ALTERNATE_GETCHAR_ +#pragma used+ + +uint8_t getchar(void) +{ + uint8_t data; + while (rx_counter0==0){}; + data=rx_buffer0[rx_rd_index0++]; + #if RX_BUFFER_SIZE0 != 256 + if (rx_rd_index0 == RX_BUFFER_SIZE0){ rx_rd_index0=0;} + #endif + #asm("cli") + --rx_counter0; + #asm("sei") + return data; +} +#pragma used- + +// USART1 Receiver buffer +#define RX_BUFFER_SIZE1 8 +uint8_t rx_buffer1[RX_BUFFER_SIZE1]; + +#if RX_BUFFER_SIZE1 <= 256 +uint8_t rx_wr_index1=0,rx_rd_index1=0; +#else +unsigned int rx_wr_index1=0,rx_rd_index1=0; +#endif + +#if RX_BUFFER_SIZE1 < 256 +uint8_t rx_counter1=0; +#else +unsigned int rx_counter1=0; +#endif + +// This flag is set on USART1 Receiver buffer overflow +bit rx_buffer_overflow1; + +// USART1 Receiver interrupt service routine +interrupt [USART1_RXC] void usart1_rx_isr(void) //(422) to lan +{ + uint8_t status; + uint8_t data; +status=UCSR1A; +data=UDR1; + if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0U) + { + rx_buffer1[rx_wr_index1++]=data; + #if RX_BUFFER_SIZE1 == 256U + // special case for receiver buffer size=256 + if (++rx_counter1 == 0U) rx_buffer_overflow1=1U; + #else + if (rx_wr_index1 == RX_BUFFER_SIZE1) {rx_wr_index1=0;} + if (++rx_counter1 == RX_BUFFER_SIZE1) + { + rx_counter1=0U; + rx_buffer_overflow1=1; + } + #endif + } + + for(lan_buffer_count = 93U;lan_buffer_count > 0U;lan_buffer_count--) + { + lan_data_buffer[lan_buffer_count + 1U] = lan_data_buffer[lan_buffer_count]; + } + lan_data_buffer[1] = lan_data_buffer[0]; + lan_data_buffer[0] = data; + + //ä³Î»óÅ ¹× ¿¡·¯Á¤º¸ ¿äû + if((lan_data_buffer[12] == 0x9bU)&&(lan_data_buffer[11] == 0x22U)&&(lan_data_buffer[10] == (uint8_t )mc_id)&&(lan_data_buffer[9] == (uint8_t )power_control_id)&&(lan_data_buffer[7] == 0x04U)&&(lan_data_buffer[6] == 0x03U)&&(lan_data_buffer[5] == 0x00U)&&(lan_data_buffer[4] == 0x00U)&&(lan_data_buffer[3] == 0x00U)) + { + ch_sel_2 = lan_data_buffer[2]; //¸®Æ²Àεð¾ð Àû¿ë 240408 + ch_sel_1 = lan_data_buffer[1]; + + + if((lan_data_buffer[0] & 0x80U) == 0x80U) //ºñ»ó¼Ò°Å + { + em_off_time = lan_data_buffer[0]; + ch_sel_2 = 0x00; + ch_sel_1 = 0x00; + } + else + { + em_off_time = 0; + } + + + for(buff_loop = 0U;buff_loop < 13U;buff_loop++) + { + lan_data_buffer[buff_loop] = 0U; + } + power_manage_control_response_act = 1U; + ch_set_act = 1U; + } + + //start_bit + if((lan_data_buffer[10] == 0x94U)&&(lan_data_buffer[9] == 0x22U)&&(lan_data_buffer[8] == (uint8_t )mc_id)&&(lan_data_buffer[7] == (uint8_t )power_control_id)&&(lan_data_buffer[5] == 0x04U)&&(lan_data_buffer[4] == 0x01U)&&(lan_data_buffer[3] == 0x00U)&&(lan_data_buffer[2] == 0x00U)&&(lan_data_buffer[1] == 0x00U)&&(lan_data_buffer[0] == 0x01U)) + { + power_manage_start_response_act = 1U; + for(buff_loop = 0U;buff_loop < 11U;buff_loop++) + { + lan_data_buffer[buff_loop] = 0U; + } + } + + + //pbit + if((lan_data_buffer[9] == (uint8_t )PBIT)&&(lan_data_buffer[8] == 0x24U)&&(lan_data_buffer[7] == (uint8_t )mc_id)&&(lan_data_buffer[6] == (uint8_t )power_control_id)&&(lan_data_buffer[4] == 0x04U)&&(lan_data_buffer[3] == 0x00U)&&(lan_data_buffer[2] == 0x00U)&&(lan_data_buffer[1] == 0x00U)&&(lan_data_buffer[0] == 0x00U)) + { + power_management_pbit_response_act = 1U; + for(buff_loop = 0U;buff_loop < 10U;buff_loop++) + { + lan_data_buffer[buff_loop] = 0U; + } + } + + + //detail pit + if((lan_data_buffer[10] == 0x9aU)&&(lan_data_buffer[9] == 0x24U)&&(lan_data_buffer[8] == mc_id)&&(lan_data_buffer[7] == power_control_id)&&(lan_data_buffer[5] == 0x04U)&&(lan_data_buffer[4] == 0x01U)&&(lan_data_buffer[3] == 0x00U)&&(lan_data_buffer[2] == 0x00U)&&(lan_data_buffer[1] == 0x00U)&&(lan_data_buffer[0] == 0x01U)) + { + power_manage_detail_response_act = 1U; + for(buff_loop = 0U;buff_loop < 11U;buff_loop++) + { + lan_data_buffer[buff_loop] = 0U; + } + } + + //detail cbit + if((lan_data_buffer[10] == 0x9aU)&&(lan_data_buffer[9] == 0x24U)&&(lan_data_buffer[8] == mc_id)&&(lan_data_buffer[7] == power_control_id)&&(lan_data_buffer[5] == 0x04U)&&(lan_data_buffer[4] == 0x01U)&&(lan_data_buffer[3] == 0x00U)&&(lan_data_buffer[2] == 0x00U)&&(lan_data_buffer[1] == 0x00U)&&(lan_data_buffer[0] == 0x00U)) + { + power_manage_detail_response_act = 1U; + for(buff_loop = 0U;buff_loop < 11U;buff_loop++) + { + lan_data_buffer[buff_loop] = 0U; + } + } + + //heart_bit + if((lan_data_buffer[10] == 0x92U)&&(lan_data_buffer[9] == 0x10U)&&(lan_data_buffer[8] == mc_id)&&(lan_data_buffer[7] == power_control_id)&&(lan_data_buffer[5] == 0x04U)&&(lan_data_buffer[4] == 0x01U)&&(lan_data_buffer[3] == 0x00U)&&(lan_data_buffer[2] == 0x00U)&&(lan_data_buffer[1] == 0x00U)&&(lan_data_buffer[0] == 0x00U)) + { + lan_reset_count = 0; + for(buff_loop = 0U;buff_loop < 11U;buff_loop++) + { + lan_data_buffer[buff_loop] = 0U; + } + } + +} + +// Get a character from the USART1 Receiver buffer +#pragma used+ +uint8_t getchar1(void) +{ + uint8_t data; + while (rx_counter1==0U){}; + data=rx_buffer1[rx_rd_index1++]; + #if RX_BUFFER_SIZE1 != 256U + if (rx_rd_index1 == RX_BUFFER_SIZE1) {rx_rd_index1=0U;} + #endif + #asm("cli") + --rx_counter1; + #asm("sei") + return data; +} + +#pragma used- +// Write a character to the USART1 Transmitter +#pragma used+ + +static void putchar1(uint8_t c) +{ + while ((UCSR1A & DATA_REGISTER_EMPTY)==0U){}; + UDR1 = c; +} +#pragma used- + + + + +// Timer 0 overflow interrupt service routine +interrupt [TIM0_OVF] void timer0_ovf_isr(void) +{ + if(count_a >= 6274U) //100ms + { + count_a = 0U; + sec_count++; + } + else + { + count_a++; + } +} + +static void header_stx(void) +{ + + + putchar(0x7fU); + + + + putchar(0xfeU); + +} + +static void header_eot(void) +{ + putchar((uint8_t)0x0dU); + putchar((uint8_t)0x0aU); +} + +static void power_divider_chset(void) +{ + header_stx(); + putchar((uint8_t)0x55U);//data set + ch_status_1 = 0U; + + if((ch_sel_1 & 0x01U) == 0x01U){ch_status_1 |= 0x80U;} //cs + if((ch_sel_2 & 0x20U) == 0x20U){ch_status_1 |= 0x40U;} //sw + if((ch_sel_1 & 0x02U) == 0x02U){ch_status_1 |= 0x20U;} //rec + if((ch_sel_2 & 0x80U) == 0x80U){ch_status_1 |= 0x10U;} //mast + if((ch_sel_2 & 0x02U) == 0x02U){ch_status_1 |= 0x08U;} //oc1 + if((ch_sel_2 & 0x04U) == 0x04U){ch_status_1 |= 0x04U;} //streeng1 + if((ch_sel_2 & 0x01U) == 0x01U){ch_status_1 |= 0x02U;} //mc + if((ch_sel_2 & 0x40U) == 0x40U){ch_status_1 |= 0x01U;} //navi + + + putchar(ch_status_1); + + ch_status_2 = 0; + + ch_status_2 |= 0x80U; //Â÷·®º¸°üÇÔ + if((ch_sel_2 & 0x08U) == 0x08U){ch_status_2 |= 0x40U;} + if((ch_sel_2 & 0x10U) == 0x10U){ch_status_2 |= 0x20U;} + ch_status_2 |= 0x10U; //ge + ch_status_2 |= 0x08U; //getc + if((ch_sel_1 & 0x04U) == 0x04U){ch_status_2 |= 0x04U;} + + putchar((uint8_t)ch_status_2);//data channel 2 + + putchar((uint8_t)em_off_time);//reserve1 + + header_eot(); + ch_set_act = 0U; + +} + +static void power_divider_request(void) +{ + header_stx(); + putchar(0x00U);//request + putchar(0x00U);//reserve1 + putchar(0x00U);//reserve2 + putchar(0x00U);//reserve3 + header_eot(); + ch_req_act = 0U; +} + +static void lan_sn(void) +{ + putchar1(sn_count); + if(sn_count >= 255U){sn_count = 0;}else{sn_count++;} +} + +/******************** + lan coounication +********************/ + + +static void power_management_status_report1(void) +{ + putchar1(0x90U);//1001 0000 message type + putchar1(0x03U);//00000011 message id + putchar1((uint8_t)power_control_id);//source unit + putchar1((uint8_t)mc_id);//00110001 destination unit + lan_sn();//SN DATA Àü¼Û 0 ~ 255 ¼øȯ + putchar1(0x04U);//00001000 TM + putchar1(0x2AU);//message length1 + putchar1(0x00);//message length2 + putchar1(0x00);//message length3 + putchar1(0x00);//message length4 + + switch(mode) + { + case BATT: + temp_lan = 0x02U; + break; + + case AC: + temp_lan = 0x01U; + break; + + case GEN: + temp_lan = 0x00U; + break; + + default: + temp_lan = 0x01U; + break; + } + putchar1((uint8_t)temp_lan);//source + + temp_lan = 0; + if((ch_status_1 & 0x10U) == 0x10U){temp_lan |= 0x80U;} //¸¶½ºÆ® + if((ch_status_1 & 0x01U) == 0x01U){temp_lan |= 0x40U;} //NAVI + if((ch_status_1 & 0x40U) == 0x40U){temp_lan |= 0x20U;} //SW + if((ch_status_2 & 0x20U) == 0x20U){temp_lan |= 0x10U;} //¿î¿ëÅëÁ¦Á¶Á¤±â2 + if((ch_status_2 & 0x40U) == 0x40U){temp_lan |= 0x08U;} //¿î¿ëÅëÁ¦±â2 + if((ch_status_1 & 0x04U) == 0x04U){temp_lan |= 0x04U;} //¿î¿ëÅëÁ¦Á¶Á¤±â1 + if((ch_status_1 & 0x08U) == 0x08U){temp_lan |= 0x02U;} //¿î¿ëÅëÁ¦±â1 + if((ch_status_1 & 0x02U) == 0x02U){temp_lan |= 0x01U;} //ÀÓ¹«ÅëÁ¦±â + putchar1((uint8_t)temp_lan); + + temp_lan = 0; + if((ch_status_2 & 0x04U)== 0x04U){temp_lan |= 0x04U;} //cnic + if((ch_status_1 & 0x20U)== 0x20U){temp_lan |= 0x02U;} //±â·Ï±â + if((ch_status_1 & 0x80U)== 0x80U){temp_lan |= 0x01U;} //ÅëÁ¦±¹ + putchar1((uint8_t)temp_lan); + + temp_lan = gen_status; + putchar1((uint8_t)temp_lan); + + if(batt_volt_1 == 0U){temp_lan = 0U;}else{temp_lan = batt_volt_1;} + if(power_divider_link_err == ERROR){temp_lan = 0U;} + + putchar1((uint8_t)temp_lan);//battery1 volt +} + +static void power_management_sub_status_report1(void) +{ + temp_lan = batt_current_1; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//battery1 current + + temp_lan = batt_health_1; + if((batt1_charge == 1U)&&(batt1_discharge == 0U)){temp_lan |= 0x80U;} + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//battery1 current + + temp_lan = batt_fuel_1; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//battery1 level + + temp_lan = batt_temperature_1; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//battery1 temperature + + if(batt_volt_2 == 0U){temp_lan = 0U;}else{temp_lan = batt_volt_2;} + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//battery2 volt + + temp_lan = batt_current_2; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//battery2 current + + temp_lan = batt_health_2; + if((batt2_charge == 1U)&&(batt2_discharge == 0U)){temp_lan |= 0x80U;} + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//battery2 current + + temp_lan = batt_fuel_2; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//battery2 level + + temp_lan = batt_temperature_2; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//battery1 temperature + + temp_lan = 0x00U; + if(batt1_heater == ON){temp_lan |= 0x08U;} + if(batt2_heater == ON){temp_lan |= 0x02U;} + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//battery heater status +} + + +static void power_management_status_report2(void) +{ + //1 mcu + if((voltage_ch7 < 150U)||(power_divider_link_err == ERROR)){temp_lan = 0U;}else{temp_lan = (uint8_t)(voltage_ch7 - 150U);} + putchar1((uint8_t)temp_lan);//mcu_power volt + temp_lan = (uint8_t)currunt_ch7; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//mcu_power current + + //2 + if((voltage_ch5 <150U)||(power_divider_link_err == ERROR)){temp_lan = 0U;}else{temp_lan = (uint8_t)(voltage_ch5 - 150U);} + putchar1((uint8_t)temp_lan);//opu1_power volt + temp_lan = (uint8_t)currunt_ch5; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//opu1_power current + //3 + if((voltage_ch6 <150U)||(power_divider_link_err == ERROR)){temp_lan = 0U;}else{temp_lan = (uint8_t)(voltage_ch6 - 150U);} + putchar1((uint8_t)temp_lan);//ocu1_power volt + temp_lan = (uint8_t)currunt_ch6; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + + putchar1((uint8_t)temp_lan);//ocu1_power current + + //4 + if((voltage_ch10 <150U)||(power_divider_link_err == ERROR)){temp_lan = 0U;}else{temp_lan = (uint8_t)(voltage_ch10 - 150U);} + putchar1(temp_lan);//opu2_power volt + temp_lan = (uint8_t)currunt_ch10; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//opu2_power current + + //5 + if((voltage_ch11 <150U)||(power_divider_link_err == ERROR)){temp_lan = 0U;}else{temp_lan = (uint8_t)(voltage_ch11 - 150U);} + putchar1(temp_lan);//ocu2_power volt + if(power_divider_link_err == ERROR){temp_lan = 0U;} + temp_lan = (uint8_t)currunt_ch11; + putchar1((uint8_t)temp_lan);//ocu2_power current + + //6 + if((voltage_ch1 <150U)||(power_divider_link_err == ERROR)){temp_lan = 0U;}else{temp_lan = (uint8_t)(voltage_ch1 - 150U);} + putchar1((uint8_t)temp_lan);//b_station_power volt + temp_lan = (uint8_t)currunt_ch1; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t )(temp_lan % 256U));//b_station_power current //¸®Æ²Àεð¾ð Àû¿ë + putchar1((uint8_t )(temp_lan / 256U));//b_station_power current + + + //7 + if((voltage_ch4 <150U)||(power_divider_link_err == ERROR)){temp_lan = 0U;}else{temp_lan = (uint8_t)(voltage_ch4 - 150U);} + putchar1((uint8_t)temp_lan);//ant_mast_power volt + temp_lan = (uint8_t)currunt_ch4; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//ant_mast_power current + + //8 NAVI + if((voltage_ch8 <150U)||(power_divider_link_err == ERROR)){temp_lan = 0U;}else{temp_lan = (uint8_t)(voltage_ch8 - 150U);} + putchar1((uint8_t)temp_lan);//navi_power volt + temp_lan = (uint8_t)currunt_ch8; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//navi_power current + + // 9 record + if((voltage_ch3 <150U)||(power_divider_link_err == ERROR)){temp_lan = 0U;}else{temp_lan = (uint8_t)(voltage_ch3 - 150U);} + putchar1((uint8_t)temp_lan);//ins_power volt + temp_lan = (uint8_t)currunt_ch3; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//ins_power current + + + //10 + if((voltage_ch2 <150U)||(power_divider_link_err == ERROR)){temp_lan = 0U;}else{temp_lan = (uint8_t)(voltage_ch2 - 150U);} + putchar1((uint8_t)temp_lan);//switch_power volt + temp_lan = (uint8_t)currunt_ch2; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//switch_power current + + //11 + if((voltage_ch14 <150U)||(power_divider_link_err == ERROR)){temp_lan = 0U;}else{temp_lan = (uint8_t)(voltage_ch14 - 150U);} + putchar1((uint8_t)temp_lan);//cnic_power volt + temp_lan = (uint8_t)currunt_ch14; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//cnic_power current + + //12 generator + + + if((gen_volt <150U)||(power_divider_link_err == ERROR)){temp_lan = 0U;}else{temp_lan = (uint8_t)(gen_volt - 150U);} + putchar1((uint8_t)temp_lan);//gen voltage + + temp_lan = (uint8_t)gen_current; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//gen_current + + if(gen_temp > 250U) + { + temp_lan = 255U; + } + else + { + temp_lan = (uint8_t)gen_temp + 5U; + } + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//gen_temp + + temp_lan = (uint8_t)gen_rpm; + if(power_divider_link_err == ERROR){temp_lan = 0U;} + putchar1((uint8_t)temp_lan);//gen_rpm + + power_management_status_report_act = 0U; +} + + + +static void pbit_cbit(void) +{ + if((err_ch_ovp_1 != 0x00U)||(err_ch_ovp_2 != 0x00U)||(err_ch_lvp_1 != 0x00U)||(err_ch_lvp_2 != 0x00U)||(err_ch_current_1 != 0x00U)||(err_ch_current_2 != 0x00U)||(ac_err == ERROR)) + {divider_total_err = ERROR;}else{divider_total_err = NOR;} + + if((gen_link_err == ERROR)||(batt_link_1_err == ERROR)||(batt_link_2_err == ERROR)||(power_divider_link_err == ERROR)||(divider_total_err == ERROR)) + {comm_total_err = ERROR;}else{comm_total_err = NOR;} + + + + if((gen_volt_sensor_err == ERROR)||(gen_high_temp == ERROR)||(gen_low_presure_err == ERROR)||(gen_ovp == ERROR)|| + (gen_ocp == ERROR)||(gen_em_stop == ERROR)||(gen_start_fail == ERROR)) + {gen_main_err = ERROR;}else {gen_main_err = NOR;} + + if((gen_low_fuel == ERROR)||(gen_current_sensor_err == ERROR)||(gen_temp_sensor_err == ERROR)||(gen_fuel_level_sensor_err == ERROR)||(gen_oil_presure_sensor_err == ERROR)) + {gen_sub_err = ERROR;}else {gen_sub_err = NOR;} + + + if((batt1_temp_err == ERROR)||(batt1_ovp == ERROR)||(batt1_ocp == ERROR)){batt1_total_err = ERROR;}else{batt1_total_err = NOR;} + if((batt2_temp_err == ERROR)||(batt2_ovp == ERROR)||(batt2_ocp == ERROR)){batt2_total_err = ERROR;}else{batt2_total_err = NOR;} + + //cs-control status + + temp_lan = 0; + //function + //batt and dividier err + if((batt1_total_err == ERROR)||(batt1_total_err == ERROR)||(divider_total_err == ERROR)){temp_lan |= 0x20U;}//NOT WORK + + //gen err + if(gen_main_err == ERROR) //||(cs_comm_err == ERR) cs comm err + { + temp_lan |= 0x08U; + } + else + { + if(gen_sub_err == ERROR) + { + temp_lan |= 0x04U; + } + } + + if((divider_total_err == ERROR)||(comm_total_err == ERROR)||(ac_fault_err == ERROR)||(ac_err == ERROR)){temp_lan |= 0x02U;}//notice + if(temp_lan != 0x00U) + { + if(((temp_lan & 0x30U) == 0x20U)||((temp_lan & 0x0cU) == 0x08U)||((temp_lan & 0x03U) == 0x02U)) + { + temp_lan |= 0x80U; + } + else + { + temp_lan |= 0x40U; + } + } + + putchar1((uint8_t)temp_lan); + + + + //device + //cs-control status + temp_lan = 0; + + //gen err + if((gen_main_err == ERROR)||(gen_sub_err == ERROR)){temp_lan |= 0x04U;} + + //divider err + if((divider_total_err == ERROR)||(ac_temp_err == ERROR)){temp_lan |= 0x02U;}//notice + + //control err + if((batt1_total_err == ERROR)||(batt2_total_err == ERROR)||(divider_total_err == ERROR)){temp_lan |= 0x01U;} //||cs_comm_err == ERR + if(temp_lan != 0x00U){temp_lan |= 0x10U;} + + putchar1((uint8_t)temp_lan);//4: ¿ÜºÎÀü¿ø°ø±Þ±â 3:¿ÜºÎ ¹èÅ͸®2, 2:¿ÜºÎ ¹èÅ͸®1 1:¹ßÀü±â 0:Àü¿øºÐ¹è±â +} + + +static void heart_bit_sent(void) +{ + putchar1(0x92U);//1001 0010 message type + putchar1(0x10U);//0001 0000 message id + putchar1((uint8_t)power_control_id);//sou1 e unit + putchar1((uint8_t)mc_id);//00110001 destination unit + lan_sn();//SN DATA Àü¼Û 0 ~ 255 ¼øȯ + putchar1(0x04U);//0010 0100 TM 240925 0x24 ->0x04·Î º¯°æ ÇÑÈ­ ¿äû ¼­°ÇŸ¿ö + putchar1(0x01U);//message length1 Àεð¾ð½Ã È°¼ºÈ­ + putchar1(0x00U);//message length2 + putchar1(0x00U);//message length3 + putchar1(0x00U);//message length4 + putchar1(0x00U);//message length1 HEART BIT RESERVE +} + +static void power_management_pbit_response(void) +{ + putchar1(0x98U);//1001 1000 message type + putchar1(0x34U);//0011 0100 message id + putchar1((uint8_t)power_control_id);//source unit + putchar1((uint8_t)mc_id);//00110001 destination unit + lan_sn();//SN DATA Àü¼Û 0 ~ 255 ¼øȯ + putchar1(0x04U);//00000100 TM + putchar1(0x02U);//message length1 Àεð¾ð½Ã È°¼º È­ + putchar1(0x00U);//message length2 + putchar1(0x00U);//message length3 + putchar1(0x00U);//message length4 + //putchar1(0x02U);//message length1 Àεð¾ð½Ã ºñ È°¼º È­ + + pbit_cbit(); + response_power_management_pbit_act_p = 0; +} + +static void power_management_start_response(void) +{ + putchar1(0x94U);//1001 1000 message type + putchar1(0x32U);//0011 0100 message id + putchar1((uint8_t)power_control_id);//source unit + putchar1((uint8_t)mc_id);//00110001 destination unit + lan_sn();//SN DATA Àü¼Û 0 ~ 255 ¼øȯ + putchar1(0x24U);//00000100 TM + putchar1(0x02U);//message length1 Àεð¾ð½Ã È°¼º È­ + putchar1(0x00U);//message length2 + putchar1(0x00U);//message length3 + putchar1(0x00U);//message length4 + putchar1(0x01U);//device + putchar1(VERSION);//VERSION + + response_power_manage_start_act_p = 0; +} + +static void power_management_cbit_report(void) +{ + putchar1(0x99);//10011001 message type + putchar1(0x04U);//00000200 message id + putchar1((uint8_t)power_control_id);//source unit + putchar1((uint8_t)mc_id);//00110001 destination unit + lan_sn();//SN DATA Àü¼Û 0 ~ 255 ¼øȯ + putchar1(0x04U);//00001000 TM + putchar1(0x02U);//message length1 + putchar1(0x00U);//message length2 + putchar1(0x00U);//message length3 + putchar1(0x00U);//message length4 + + + pbit_cbit(); + + power_managementbit_cbit_report_act = 0; +} + +static void power_managementbit_detail_response(void) +{ + putchar1(0x9aU);//10011010 message type + putchar1(0x34U);//00110100 message id + putchar1((uint8_t)power_control_id);//source unit + putchar1((uint8_t)mc_id);//00110001 destination unit + lan_sn();//SN DATA Àü¼Û 0 ~ 255 ¼øȯ + putchar1(0x04U);//00000100 TM + putchar1(0x04U);//message length1 + putchar1(0x00U);//message length2 + putchar1(0x00U);//message length3 + putchar1(0x00U);//message length4 + + temp_lan = 0;// response pbit 1 cbit 0 + + putchar1((uint8_t)temp_lan); + //link + temp_lan = 0; + if(gen_link_err == ERROR){temp_lan |= 0x40U;} + if(batt_link_1_err == ERROR){temp_lan |= 0x20U;} + if(batt_link_2_err == ERROR){temp_lan |= 0x10U;} + if(power_divider_link_err == ERROR){temp_lan |= 0x08U;} + if(divider_total_err == ERROR){temp_lan |= 0x02U;} + + putchar1((uint8_t)temp_lan); + // ¿ÜºÎÀü¿ø°ø±Þ±â 3:¿ÜºÎ ¹èÅ͸®2, 2:¿ÜºÎ ¹èÅ͸®1 1:¹ßÀü±â 0:Àü¿øºÐ¹è±â + + //¹ßÀü±â + temp_lan = 0; + + if(gen_volt_sensor_err == ERROR){temp_lan |= 0x80U;} + if(gen_high_temp == ERROR){temp_lan |= 0x40U;} + if(gen_low_presure_err == ERROR){temp_lan |= 0x20U;} + if(gen_ovp == ERROR){temp_lan |= 0x10U;} + if(gen_ocp == ERROR){temp_lan |= 0x08U;} + if(gen_em_stop == ERROR){temp_lan |= 0x04U;} + if(gen_start_fail == ERROR){temp_lan |= 0x02U;} + + putchar1((uint8_t)temp_lan); + + /* + temp_lan = 0; ¹èÅ͸®1 ºÒ·® + if(batt1_temp_err == ERR){temp_lan |= 0x04;} + if(batt1_ocp == ERR){temp_lan |= 0x02;} + if(batt1_ovp == ERR){temp_lan |= 0x01;} + putchar1((uint8_t)temp_lan); ¹èÅ͸®1 ¿Âµµ:2 Àü·ù:1 Àü¾Ð:0 + + temp_lan = 0; ¹èÅ͸®2 ºÒ·® + if(batt2_temp_err == ERR){temp_lan |= 0x04;} + if(batt2_ocp == ERR){temp_lan |= 0x02;} + if(batt2_ovp == ERR){temp_lan |= 0x01;} + putchar1((uint8_t)temp_lan); ¹èÅ͸®2 ¿Âµµ:2 Àü·ù:1 Àü¾Ð:0 + */ + + temp_lan = 0; //Àü¿ø°ø±Þ±â + if(gen_low_fuel == ERROR){temp_lan |= 0x80U;} + if(gen_current_sensor_err == ERROR){temp_lan |= 0x40U;} + if(gen_temp_sensor_err == ERROR){temp_lan |= 0x20U;} + if(gen_fuel_level_sensor_err == ERROR){temp_lan |= 0x10U;} + if(gen_oil_presure_sensor_err == ERROR){temp_lan |= 0x08U;} + + putchar1((uint8_t)temp_lan);// Àü¿ø°ø±Þ±â 0:Ãâ·ÂÀü¾Ð + response_power_manage_detail_act_p = 0; +} + +static void power_management_control_response(void) +{ + putchar1(0x9bU);//10011011 message type + putchar1(0x32U);//00110010 message id + putchar1((uint8_t)power_control_id);//source unit + putchar1((uint8_t)mc_id);//00110001 destination unit + lan_sn();//SN DATA Àü¼Û 0 ~ 255 ¼øȯ + putchar1(0x04U);//00000100 TM + putchar1(0x03U);//message length1 + putchar1(0x00U);//message length2 + putchar1(0x00U);//message length3 + putchar1(0x00U);//message length4 + // ch_sel_2 = lan_data_buffer[2]; //¸®Æ²Àεð¾ð Àû¿ë 240408 + + temp_lan = ch_sel_2; + putchar1((uint8_t)temp_lan); + + temp_lan = ch_sel_1; + putchar1((uint8_t)temp_lan); + + temp_lan = 0; + if((em_off_time & 0x80U)== 0x80U) + { + temp_lan = em_off_time & 0x7fU; + } + putchar1((uint8_t)temp_lan); + response_power_manage_control_act_p = 0; +} + + +static void pulse1(void) +{ + delay_us(2); + LED_CLK1 = 1; + delay_us(2); + LED_CLK1 = 0; +} + +static void pulse2(void) +{ + delay_us(2); + LED_CLK2 = 1; + delay_us(2); + LED_CLK2 = 0; +} + +static void pulse3(void) +{ + delay_us(2); + LED_CLK3 = 1; + delay_us(2); + LED_CLK3 = 0; +} + +static void fnd_display1(void) +{ + //FND 1 CONTROL + LED_LOAD1 = 0; + delay_us(2); + LED_DIN1 = 1; + for(count_temp = 0U; count_temp <4U; count_temp++) + { + pulse1(); + } + //command digit 3 + if((command & 0x08U) == 0x08U){LED_DIN1 = 1;} else{LED_DIN1 = 0;} + pulse1(); + //command digit 2 + if((command & 0x04U) == 0x04U){LED_DIN1 = 1;} else{LED_DIN1 = 0;} + pulse1(); + //command digit 1 + if((command & 0x02U) == 0x02U){LED_DIN1 = 1;} else{LED_DIN1 = 0;} + pulse1(); + //command digit 0 + if((command & 0x01U) == 0x01U){LED_DIN1 = 1;} else{LED_DIN1 = 0;} + pulse1(); + //data digit 7 + if((fnd_data & 0x80U) == 0x80U){LED_DIN1 = 1;} else{LED_DIN1 = 0;} + pulse1(); + //data digit 6 + if((fnd_data & 0x40U) == 0x40U){LED_DIN1 = 1;} else{LED_DIN1 = 0;} + pulse1(); + //data digit 5 + if((fnd_data & 0x20U) == 0x20U){LED_DIN1 = 1;} else{LED_DIN1 = 0;} + pulse1(); + //data digit 4 + if((fnd_data & 0x10U) == 0x10U){LED_DIN1 = 1;} else{LED_DIN1 = 0;} + pulse1(); + //data digit 3 + if((fnd_data & 0x08U) == 0x08U){LED_DIN1 = 1;} else{LED_DIN1 = 0;} + pulse1(); + //data digit 2 + if((fnd_data & 0x04U) == 0x04U){LED_DIN1 = 1;} else{LED_DIN1 = 0;} + pulse1(); + //data digit 1 + if((fnd_data & 0x02U) == 0x02U){LED_DIN1 = 1;} else{LED_DIN1 = 0;} + pulse1(); + //data digit 0 + if((fnd_data & 0x01U) == 0x01U){LED_DIN1 = 1;} else{LED_DIN1 = 0;} + pulse1(); + LED_DIN1 = 1; + delay_us(1); + LED_LOAD1 = 1; + delay_us(1); +} + +static void fnd_display2(void) +{ + //FND 2 CONTROL + LED_LOAD2 = 0; + delay_us(1); + LED_DIN2 = 1; + for(count_temp = 0U; count_temp <4U; count_temp++) + { + pulse2(); + } + //command digit 3 + if((command & 0x08U) == 0x08U){LED_DIN2 = 1;} else{LED_DIN2 = 0;} + pulse2(); + //command digit 2 + if((command & 0x04U) == 0x04U){LED_DIN2 = 1;} else{LED_DIN2 = 0;} + pulse2(); + //command digit 1 + if((command & 0x02U) == 0x02U){LED_DIN2 = 1;} else{LED_DIN2 = 0;} + pulse2(); + //command digit 0 + if((command & 0x01U) == 0x01U){LED_DIN2 = 1;} else{LED_DIN2 = 0;} + pulse2(); + //data digit 7 + if((fnd_data & 0x80U) == 0x80U){LED_DIN2 = 1;} else{LED_DIN2 = 0;} + pulse2(); + //data digit 6 + if((fnd_data & 0x40U) == 0x40U){LED_DIN2 = 1;} else{LED_DIN2 = 0;} + pulse2(); + //data digit 5 + if((fnd_data & 0x20U) == 0x20U){LED_DIN2 = 1;} else{LED_DIN2 = 0;} + pulse2(); + //data digit 4 + if((fnd_data & 0x10U) == 0x10U){LED_DIN2 = 1;} else{LED_DIN2 = 0;} + pulse2(); + //data digit 3 + if((fnd_data & 0x08U) == 0x08U){LED_DIN2 = 1;} else{LED_DIN2 = 0;} + pulse2(); + //data digit 2 + if((fnd_data & 0x04U) == 0x04U){LED_DIN2 = 1;} else{LED_DIN2 = 0;} + pulse2(); + //data digit 1 + if((fnd_data & 0x02U) == 0x02U){LED_DIN2 = 1;} else{LED_DIN2 = 0;} + pulse2(); + //data digit 0 + if((fnd_data & 0x01U) == 0x01U){LED_DIN2 = 1;} else{LED_DIN2 = 0;} + pulse2(); + LED_DIN2 = 1; + delay_us(1); + LED_LOAD2 = 1; + delay_us(1); +} + +static void fnd_display3(void) +{ + //FND 3 CONTROL + LED_LOAD3 = 0; + delay_us(1); + LED_DIN3 = 1 ; + for(count_temp = 0U; count_temp <4U; count_temp++) + { + pulse1(); + } + //command digit 3 + if((command & 0x08U) == 0x08U){LED_DIN3 = 1;} else{LED_DIN3 = 0;} + pulse3(); + //command digit 2 + if((command & 0x04U) == 0x04U){LED_DIN3 = 1;} else{LED_DIN3 = 0;} + pulse3(); + //command digit 1 + if((command & 0x02U) == 0x02U){LED_DIN3 = 1;} else{LED_DIN3 = 0;} + pulse3(); + //command digit 0 + if((command & 0x01U) == 0x01U){LED_DIN3 = 1;} else{LED_DIN3 = 0;} + pulse3(); + //data digit 7 + if((fnd_data & 0x80U) == 0x80U){LED_DIN3 = 1;} else{LED_DIN3 = 0;} + pulse3(); + //data digit 6 + if((fnd_data & 0x40U) == 0x40U){LED_DIN3 = 1;} else{LED_DIN3 = 0;} + pulse3(); + //data digit 5 + if((fnd_data & 0x20U) == 0x20U){LED_DIN3 = 1;} else{LED_DIN3 = 0;} + pulse3(); + //data digit 4 + if((fnd_data & 0x10U) == 0x10U){LED_DIN3 = 1;} else{LED_DIN3 = 0;} + pulse3(); + //data digit 3 + if((fnd_data & 0x08U) == 0x08U){LED_DIN3 = 1;} else{LED_DIN3 = 0;} + pulse3(); + //data digit 2 + if((fnd_data & 0x04U) == 0x04U){LED_DIN3 = 1;} else{LED_DIN3 = 0;} + pulse3(); + //data digit 1 + if((fnd_data & 0x02U) == 0x02U){LED_DIN3 = 1;} else{LED_DIN3 = 0;} + pulse3(); + //data digit 0 + if((fnd_data & 0x01U) == 0x01U){LED_DIN3 = 1;} else{LED_DIN3 = 0;} + pulse3(); + LED_DIN3 = 1; + delay_us(1); + LED_LOAD3 = 1; + delay_us(1); +} + +static void num_convert(uint8_t num) +{ + switch(num) + { + case 0: + fnd_data = 0x7eU; + break; + + case 1: + fnd_data = 0x30U; + break; + + case 2: + fnd_data = 0x6dU; + break; + + case 3: + fnd_data = 0x79U; + break; + + case 4: + fnd_data = 0x33U; + break; + + case 5: + fnd_data = 0x5bU; + break; + + case 6: + fnd_data = 0x5fU; + break; + + case 7: + fnd_data = 0x70U; + break; + + case 8: + fnd_data = 0x7fU; + break; + + case 9: + fnd_data = 0x7bU; + break; + + case 15: + fnd_data = 0xffU; + break; + + default: + fnd_data = 0x00U; + break; + } +} + +static void fnd_init(void) +{ + LED_LOAD1 = HIGH; + LED_LOAD2 = HIGH; + LED_LOAD3 = HIGH; + + // scan limit + command = 0x0bU; + fnd_data = 0x05U; + fnd_display1(); + fnd_data = 0x03U; + fnd_display2(); + fnd_data = 0x05U; + fnd_display3(); + + // intensity + command = 0x0aU; + fnd_data = 0x07U; + fnd_display1(); + fnd_display2(); + fnd_display3(); + + // decode mode + command = 0x09U; + fnd_data = 0x00U; + fnd_display1(); + fnd_display2(); + fnd_display3(); + + // shut down + command = 0x0cU; + fnd_data = 0x01U; + fnd_display1(); + fnd_display2(); + fnd_display3(); + + // fnd test normar + command = 0x0fU; + fnd_data = 0x00U; + fnd_display1(); + fnd_display2(); + fnd_display3(); + + LED_LOAD1 = LOW; + LED_LOAD2 = LOW; + LED_LOAD3 = LOW; +} + + +static void charge_work_display(void) +{ + switch(charge_display_count) + { + case 0: + fnd_data = 0x48; //a 0x40 + break; + + case 1: + fnd_data = 0x24; //b 0x20 + break; + + case 2: + fnd_data = 0x12; //c 0x10 + break; + + default: + break; + } +} + +static void digit_low_sub(uint8_t digit_num, uint8_t temp1) +{ + switch(digit_num) + { + case 6://current 1 + command = 0x01U; + num_convert(temp1); + fnd_display1(); + break; + + case 7: //fuel 100 + command = 0x03; + if(gen_link_err == ERROR) + { + num_convert(0); + } + else + { + num_convert(temp1); + } + + if(p_bit == 0U) + { + if(gen_err == ERROR) + { + fnd_data = 0x4FU;//"E" + } + else + { + if((gen_low_fuel == 1U)&&(sec_500ms == 1U)){fnd_data = 0x00U;} //1¾ÈÀû¿ë blank + + } + } + fnd_display2(); + break; + + case 8: //fuel 10 + command = 0x02; + if(gen_link_err == ERROR) + { + num_convert(0); + } + else + { + num_convert(temp1); + } + + if(p_bit == 0U) + { + if(gen_err == ERROR) + { + fnd_data = 0x05U;//"r" + } + else + { + if((gen_low_fuel == 1U)&&(sec_500ms == 1U)){fnd_data = 0x00U;} //1¾ÈÀû¿ë blank + + } + } + fnd_display2(); + break; + + case 9: //fuel 1 + command = 0x01U; + if(gen_link_err == ERROR) + { + num_convert(0); + } + else + { + num_convert(temp1); + } + + if(p_bit == 0U) + { + if(gen_err == ERROR) + { + fnd_data = 0x05U;//"r" + } + else + { + if((gen_low_fuel == 1U)&&(sec_500ms == 1U)){fnd_data = 0x00U;} //1¾ÈÀû¿ë blank + } + } + fnd_display2(); + break; + + case 10: //sco1 100 + command = 0x06U; + if((batt_link_1_err == ERROR)||(power_divider_link_err == ERROR)) + { + + num_convert(0); + } + else + { + if(p_bit == 0U) + { + if(batt1_status == FAIL) + { + fnd_data = 0x4F;//"E" + } + else + { + if(batt1_heater == ON){fnd_data = 0x00U;}//" " + else + { + if((batt1_charge == 1U)&&(batt1_discharge == 0U)&&(fnd_soc1 < 100U)&&(mode != BATT)) + { + charge_work_display(); + } + else + { + if((batt1_soc_low == ERROR)&&(sec_500ms == 1U)){fnd_data = 0x00U;}else{num_convert(temp1);} + } + } + } + } + } + fnd_display3(); + break; + + + default: + command = 0x00U; + break; + } +} + +static void digit_low(uint8_t digit_num, uint8_t temp1) +{ + switch(digit_num) + { + case 1: //voltage 100 + command = 0x06U; + num_convert(temp1); + fnd_display1(); + break; + + case 2: //voltage 10 + command = 0x05U; + num_convert(temp1); + if(p_bit == 0U){fnd_data |= 0x80U;} ///´ÜÀ§ + fnd_display1(); + break; + + case 3: //voltage 1 + command = 0x04U; + num_convert(temp1); + fnd_display1(); + break; + + case 4: //current 100 + command = 0x03U; + num_convert(temp1); + fnd_display1(); + break; + + case 5: //current 10 + command = 0x02U; + num_convert(temp1); + if(p_bit == 0U){fnd_data |= 0x80U;} ///´ÜÀ§ + fnd_display1(); + break; + + default: + command = 0x00U; + break; + + } +} + +static void digit_high_sub(uint8_t digit_num, uint8_t temp1) +{ + switch(digit_num) + { + case 13: //sco2 100 + command = 0x03; + if((batt_link_2_err == ERROR)||(power_divider_link_err == ERROR)) + { + + num_convert(0); + } + else + { + if(p_bit == 0U) + { + if(batt2_status == FAIL) + { + fnd_data = 0x4FU;//"E" + } + else + { + if(batt2_heater == ON){fnd_data = 0x00U;}//" " + else + { + if((batt2_charge == 1U)&&(batt2_discharge == 0U)&&(fnd_soc2 < 100U)&&(mode != BATT)) + { + charge_work_display(); + } + else + { + if((batt2_soc_low == ERROR)&&(sec_500ms == 1U)){fnd_data = 0x00U;}else{num_convert(temp1);} + } + } + } + } + } + fnd_display3(); + break; + + + case 14: //sco2 10 + command = 0x02U; + if((batt_link_2_err == ERROR)||(power_divider_link_err == ERROR)) + { + + num_convert(0); + } + else + { + if(p_bit == 0U) + { + + if(batt2_status == FAIL) + { + fnd_data = 0x05U;//"r" + } + else + { + if(batt2_heater == ON){fnd_data = 0x37U;}//"H" + else + { + if((batt2_soc_low == ERROR)&&(sec_500ms == 1U)){fnd_data = 0x00U;}else{num_convert(temp1);} + } + } + + } + } + fnd_display3(); + break; + + case 15: //sco2 1 + command = 0x01U; + if((batt_link_2_err == ERROR)||(power_divider_link_err == ERROR)) + { + num_convert(0); + } + else + { + if(p_bit == 0U) + { + + if(batt2_status == FAIL) + { + fnd_data = 0x05U; //"r" + } + else + { + if(batt2_heater == ON){fnd_data = 0x0fU;}//"t" + else + { + if((batt2_soc_low == ERROR)&&(sec_500ms == 1U)){fnd_data = 0x00U;}else{num_convert(temp1);} + } + } + + } + } + fnd_display3(); + break; + + default: + command = 0x00U; + break; + + } +} + + +static void digit_high(uint8_t digit_num, uint8_t temp1) +{ + switch(digit_num) + { + case 11: //sco1 10 + command = 0x05; + + if((batt_link_1_err == ERROR)||(power_divider_link_err == ERROR)) + { + + num_convert(0); + } + else + { + if(p_bit == 0U) + { + if(batt1_status == FAIL) + { + fnd_data = 0x05U; //"r" + } + else + { + if(batt1_heater == ON){fnd_data = 0x37U;}//"H" + else + { + if((batt1_soc_low == ERROR)&&(sec_500ms == 1U)){fnd_data = 0x00U;}else{num_convert(temp1);} + } + } + } + } + fnd_display3(); + break; + + case 12: //sco1 + command = 0x04U; + if((batt_link_1_err == ERROR)||(power_divider_link_err == ERROR)) + { + + num_convert(0); + } + else + { + if(p_bit == 0U) + { + if(batt1_status == FAIL) + { + fnd_data = 0x05U;//"r" + } + else + { + if(batt1_heater == ON){fnd_data = 0x0fU;}//"t" + else + { + if((batt1_soc_low == ERROR)&&(sec_500ms == 1U)){fnd_data = 0x00;}else{num_convert(temp1);} + } + } + } + } + fnd_display3(); + break; + + + + default: + command = 0x00U; + break; + } +} + +static void digit(uint8_t digit_num, uint8_t temp1)//,char num) +{ + if(digit_num < 11U) + { + if(digit_num < 6U){digit_low(digit_num, temp1);}else{digit_low_sub(digit_num, temp1);} + } + else + { + if(digit_num < 13U){digit_high(digit_num, temp1);}else{digit_high_sub(digit_num, temp1);} + } +} + +static void display_culculate( void) +{ + //volt + if(fnd_voltage < 1000U) + { + digit(1,(uint8_t)(fnd_voltage/100U)); + digit(2,(uint8_t)((uint8_t)(fnd_voltage % 100U) / 10U)); + digit(3,(uint8_t)(fnd_voltage % 10U)); + } + else + { + digit(1,10U); + digit(2,10U); + digit(3,10U); + } + + //current + if(fnd_current < 1000U) + { + digit(4,(uint8_t)(fnd_current / 100U)); + digit(5,(uint8_t)((uint8_t)(fnd_current % 100U) / 10U)); + digit(6,(uint8_t)(fnd_current % 10U)); + } + else + { + digit(4,10U); + digit(5,10U); + digit(6,10U); + } + ; + //fuel + if(fnd_fuel <= 100U) + { + digit(7,(uint8_t)(fnd_fuel / 100U)); + digit(8,(uint8_t)((uint8_t)(fnd_fuel % 100U)/ 10U)); + digit(9,(uint8_t)(fnd_fuel % 10U)); + } + else + { + digit(7,10U); + digit(8,10U); + digit(9,10U); + } + + //soc1 + + if(fnd_soc1 <= 100U) + { + digit(10,(uint8_t) (fnd_soc1 / 100U)); + digit(11,(uint8_t)((uint8_t) (fnd_soc1 % 100U)/10U)); + digit(12,(uint8_t) (fnd_soc1 % 10U)); + } + else + { + digit(10,10U); + digit(11,10U); + digit(12,10U); + } + + //soc2 + if(fnd_soc2 <= 100U) + { + digit(13,(uint8_t) (fnd_soc2/100U)); + digit(14,(uint8_t)((uint8_t) (fnd_soc2%100U)/10U)); + digit(15,(uint8_t) (fnd_soc2%10U)); + } + else + { + digit(13,10U); + digit(14,10U); + digit(15,10U); + } +} + +static void display(void) +{ + // LED Ãâ·ÂÁ¦¾î + if(power_divider_link_err == ERROR) + { + fnd_voltage = 0U; + fnd_current = 0U; + display_culculate(); + fnd_soc1 = 0U; + fnd_soc2 = 0U; + fnd_fuel = 0U; + LED_AC = OFF; + LED_GEN = OFF; + LED_BATT = OFF; + } + else + { + switch(mode) + { + case AC: + if(ac_err == ERROR) + { + if(sec_100ms == 1U){LED_AC = ON;}else{LED_AC = OFF;} + } + else + { + LED_AC = ON; + } + LED_GEN = OFF; + LED_BATT = OFF; + fnd_fuel = gen_fuel; + mode_sw_err = 0U; + break; + + case GEN: + LED_AC = OFF; + + if(gen_link_err == ERROR) + { + if(sec_500ms == 1U){LED_GEN = ON;}else{LED_GEN = OFF;} + } + else + { + if(gen_err == ERROR) + { + if(sec_100ms == 1U){LED_GEN = ON;}else{LED_GEN = OFF;} + } + else + { + LED_GEN = ON; + } + } + + + LED_BATT = OFF; + fnd_fuel = gen_fuel; + mode_sw_err = 0U; + break; + + case BATT: + LED_AC = OFF; + LED_GEN = OFF; + + if((batt_link_1_err == ERROR)||(batt_link_2_err == ERROR)) + { + if(sec_500ms == 1U){LED_BATT = ON;}else{LED_BATT = OFF;} + } + else + { + if((batt_err == ERROR)||(batt1_status == FAIL)||(batt2_status == FAIL)) + { + if(sec_100ms == 1U){LED_BATT = ON;}else{LED_BATT = OFF;} + } + else + { + LED_BATT = ON; + } + } + + fnd_fuel = gen_fuel; + mode_sw_err = 0U; + break; + + default: + LED_AC = OFF; + LED_GEN = OFF; + LED_BATT = OFF; + fnd_fuel = gen_fuel; + mode_sw_err = 1U; + break; + } + + fnd_voltage = voltage_source; + current_sum = currunt_ch1 + currunt_ch2 + currunt_ch3 + currunt_ch4 + currunt_ch5 + currunt_ch6 + currunt_ch7 + currunt_ch8 + + currunt_ch9 + currunt_ch10 + currunt_ch11 + currunt_ch12 + currunt_ch13 + currunt_ch14; + fnd_current = current_sum; + display_culculate(); + + //soc battery charge level + fnd_soc1 = batt_fuel_1; + fnd_soc2 = batt_fuel_2; + if(init_delay_count == 0U) + { + if(fnd_soc1 < 15U){batt1_soc_low = ERROR;} + if(fnd_soc2 < 15U){batt2_soc_low = ERROR;} + if(fnd_soc1 >= 20U){batt1_soc_low = NOR;} + if(fnd_soc2 >= 20U){batt2_soc_low = NOR;} + } + else + { + batt1_soc_low = NOR; + batt2_soc_low = NOR; + } + } +} + +static void buzzer_check(void) +{ + //¾î´À Çϳª¶óµµ °æº¸½Ã ¾Ë¶÷ °æº¸ + if((power_divider_link_err == ERROR)||(gen_link_err == ERROR)||(batt_link_1_err == ERROR)||(batt_link_2_err == ERROR)||((err_info_1 & 0XFCU)!= 0U)|| + ((err_info_2 & 0XFCU)!= 0U)||(err_ch_ovp_1 != 0U)||(err_ch_ovp_2 != 0U)||(err_ch_lvp_1 != 0U)||(err_ch_lvp_2 != 0U)||(err_ch_current_1 != 0U)|| + (err_ch_current_2 != 0U)||(gen_err == ERROR)||(gen_sensor_err == ERROR)||(batt1_soc_low == ERROR)||(batt2_soc_low == ERROR)){alram_count_flag = 1;}else{alram_count_flag = 0;} + + if(alram_count >= ALRAM_COUNT_MAX){alram = 1U;}else{alram = 0U;} + + if(alram == 1U) + { + if(buzzer_act == 0U) + { + buzzer_act = 1U; + } + } + else + { + buzzer_stop_act = 0U; + buzzer_act = 0U; + } + + if(buzzer_act == 1U) + { + if(BUZZER_STOP_SW == 0U) + { + if((buzzer_act == 1U)&&(buzzer_stop_act == 0U)) + { + buzzer_stop_act = 1U; + } + } + } + + if((buzzer_act == 1U)&&(buzzer_stop_act == 0U)) + { + buzzer_work = 1U; + } + else + { + buzzer_work = 0U; + } + + if(buzzer_work == 1U) + { + BUZZER = ON; + } + else + { + BUZZER = OFF; + } + + + if(BUZZER_STOP_SW == 0U) + { + buzzer_press = 1U; + } + else + { + buzzer_press = 0U; + } +} + +static void buzzer_pulse_one(void) +{ + BUZZER = ON; + delay_ms(10); + BUZZER = OFF; +} + +static void display_p_bit(void) +{ + p_bit = 1U; + for(loop = 0U; loop < 2U; loop++) + { + LED_AC = ON; + LED_GEN = ON; + LED_BATT = ON; + + for(fnd_digit_loop = 1U;fnd_digit_loop <16U;fnd_digit_loop++) + { + digit(fnd_digit_loop,0x0fU); //È­¸é Àüü full + + } + delay_ms(500); + + LED_AC = OFF; + LED_GEN = OFF; + LED_BATT = OFF; + for(fnd_digit_loop = 1U;fnd_digit_loop <16U;fnd_digit_loop++) + { + digit(fnd_digit_loop,0x0aU); //È­¸é Àüü full + } + delay_ms(500); + } + fnd_voltage = 0U; + fnd_current = 0U; + fnd_fuel = 0U; + fnd_soc1 = 0U; + fnd_soc2 = 0U; + display(); + if(p_bit == 1U){p_bit = 0U;} +} + + + + +static void i2c_pulse(void) +{ + SW_LAN_SCL = 0U; + delay_us(30); + SW_LAN_SCL = 1U; + delay_us(30);//30 + SW_LAN_SCL = 0U; + delay_us(30); +} + +static void i2c_start_sw(void) +{ + DDRE.7 = 1U; + SW_LAN_SDI = 1U; + SW_LAN_SCL = 1U; + delay_us(40); + DDRE.7 = 1U; + SW_LAN_SDI = 0U; + delay_us(40); + DDRE.7 = 1U; + SW_LAN_SCL = 0U; + delay_us(40); +} + +static void i2c_stop_sw(void) +{ + SW_LAN_SDI = 0U; + SW_LAN_SCL = 0U; + delay_us(40); + SW_LAN_SCL = 1U; + delay_us(40); + SW_LAN_SDI = 1U; + delay_us(40); +} + +static void sw_head(void) +{ + //TOUCH ADDRESS + //BC, B8, B4 + SW_LAN_SDI = 1U; + i2c_pulse(); + + SW_LAN_SDI = 0U; + i2c_pulse(); + + SW_LAN_SDI = 1U; + i2c_pulse(); + + SW_LAN_SDI = 1U; + i2c_pulse(); + + //CHIP ID 0x + SW_LAN_SDI = 1U; + i2c_pulse(); + + SW_LAN_SDI = 1U; + i2c_pulse(); + + SW_LAN_SDI = 1U; + i2c_pulse(); + + //WRITE 0 + SW_LAN_SDI = 0U; + i2c_pulse(); + + delay_us(10); + DDRE.7 = 0U; + delay_us(50); + i2c_pulse(); + DDRE.7 = 1U; + delay_us(50); +} + +static void sw_head_r(void) +{ + + //BC, B8, B4 + SW_LAN_SDI = 1U; + i2c_pulse(); + + SW_LAN_SDI = 0U; + i2c_pulse(); + + SW_LAN_SDI = 1U; + i2c_pulse(); + + SW_LAN_SDI = 1U; + i2c_pulse(); + + //CHIP ID 0xb4 + SW_LAN_SDI = 1U; + i2c_pulse(); + + SW_LAN_SDI = 1U; + i2c_pulse(); + + SW_LAN_SDI = 1U; + i2c_pulse(); + + //read 1 + SW_LAN_SDI = 1U; + i2c_pulse(); + + SW_LAN_SDI = 0U; + + delay_us(10); + DDRE.7 = 0U; + delay_us(50); + i2c_pulse(); + DDRE.7 = 1U; + delay_us(50); +} + + +static void sw_lan_cont_r(uint16_t temp_sw_add) +{ + i2c_start_sw(); + delay_us(10); + sw_head(); + + // //command digit 3 + if((temp_sw_add & 0x8000U) == 0x8000U){ SW_LAN_SDI = 1U;} else { SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 2 + if((temp_sw_add & 0x4000U) == 0x4000U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 1 + if((temp_sw_add & 0x2000U) == 0x2000U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 0 + if((temp_sw_add & 0x1000U) == 0x1000U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 3 + if((temp_sw_add & 0x0800U) == 0x0800U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 2 + if((temp_sw_add & 0x0400U) == 0x0400U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 1 + if((temp_sw_add & 0x0200U) == 0x0200U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 0 + if((temp_sw_add & 0x0100U) == 0x0100U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + delay_us(10); + DDRE.7 = 0; + //ACK + delay_us(10); + i2c_pulse(); + + DDRE.7 = 1; + delay_us(10); + + + // //command digit 3 + if((temp_sw_add & 0x0080U) == 0x0080U){ SW_LAN_SDI = 1U;} else { SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 2 + if((temp_sw_add & 0x0040U) == 0x0040U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 1 + if((temp_sw_add & 0x0020U) == 0x0020U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 0 + if((temp_sw_add & 0x0010U) == 0x0010U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 3 + if((temp_sw_add & 0x0008U) == 0x0008U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 2 + if((temp_sw_add & 0x0004U) == 0x0004U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 1 + if((temp_sw_add & 0x0002U) == 0x0002U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 0 + if((temp_sw_add & 0x0001U) == 0x0001U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + delay_us(10); + DDRE.7 = 0U; + //ACK + delay_us(10); + i2c_pulse(); + + DDRE.7 = 1U; + delay_us(10); + //data read + i2c_stop_sw(); + delay_us(10); + + delay_us(100); + i2c_start_sw(); + sw_head_r(); + + delay_us(10); + + SW_LAN_SDI = 0U; + + DDRE.7 = 0U; + delay_us(100); + temp_sw = 0x00U; + //data digit 7 + if((SW_LAN_SDI_IN) == 1U) + { + temp_sw = 0x01U; + } + temp_sw = temp_sw << 1; + i2c_pulse(); + + //data digit 6 + if((SW_LAN_SDI_IN) == 1U) + { + temp_sw = temp_sw + 0x01U; + } + temp_sw = temp_sw << 1U; + i2c_pulse(); + + //data digit 5 + if((SW_LAN_SDI_IN) == 1U) + { + temp_sw = temp_sw + 0x01U; + } + temp_sw = temp_sw << 1; + i2c_pulse(); + //data digit 4 + if((SW_LAN_SDI_IN) == 1U) + { + temp_sw = temp_sw + 0x01U; + } + temp_sw = temp_sw << 1; + i2c_pulse(); + + //data digit 3 + if((SW_LAN_SDI_IN) == 1U) + { + temp_sw = temp_sw + 0x01U; + } + temp_sw = temp_sw << 1; + i2c_pulse(); + + //data digit 2 + if((SW_LAN_SDI_IN) == 1U) + { + temp_sw = temp_sw + 0x01U; + } + temp_sw = temp_sw << 1; + i2c_pulse(); + + //data digit 1 + if((SW_LAN_SDI_IN) == 1U) + { + temp_sw = temp_sw + 0x01U; + } + temp_sw = temp_sw << 1; + i2c_pulse(); + + + //data digit 0 + if((SW_LAN_SDI_IN) == 1U) + { + temp_sw = temp_sw + 0x01U; + } + i2c_pulse(); + + delay_us(10); + i2c_pulse(); + + delay_us(10); + DDRE.7 = 1; + delay_us(10); + SW_LAN_SDI = 0; + delay_us(10); + i2c_stop_sw(); + delay_us(10); + sw_result = temp_sw; + + putchar(sw_result); +} + +//sw control +static void sw_lan_cont(uint16_t temp_sw_add, uint8_t temp_sw_data) +{ + i2c_start_sw(); + delay_us(1); + sw_head(); + // //command digit 3 + if((temp_sw_add & 0x8000U) == 0x8000U){ SW_LAN_SDI = 1U;} else { SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 2 + if((temp_sw_add & 0x4000U) == 0x4000U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 1 + if((temp_sw_add & 0x2000U) == 0x2000U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 0 + if((temp_sw_add & 0x1000U) == 0x1000U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 3 + if((temp_sw_add & 0x0800U) == 0x0800U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 2 + if((temp_sw_add & 0x0400U) == 0x0400U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 1 + if((temp_sw_add & 0x0200U) == 0x0200U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 0 + if((temp_sw_add & 0x0100U) == 0x0100U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + SW_LAN_SDI = 0; + + delay_us(10); + DDRE.7 = 0; + //ACK + delay_us(50); + i2c_pulse(); + DDRE.7 = 1; + delay_us(50); + + // //command digit 3 + if((temp_sw_add & 0x0080U) == 0x0080U){ SW_LAN_SDI = 1U;} else { SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 2 + if((temp_sw_add & 0x0040U) == 0x0040U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 1 + if((temp_sw_add & 0x0020U) == 0x0020U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 0 + if((temp_sw_add & 0x0010U) == 0x0010U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 3 + if((temp_sw_add & 0x0008U) == 0x0008U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 2 + if((temp_sw_add & 0x0004U) == 0x0004U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 1 + if((temp_sw_add & 0x0002U) == 0x0002U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //command digit 0 + if((temp_sw_add & 0x0001U) == 0x0001U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + SW_LAN_SDI = 0; + + delay_us(10); + DDRE.7 = 0; + //ACK + delay_us(50); + i2c_pulse(); + DDRE.7 = 1; + delay_us(50); + + //data digit 7 + if((temp_sw_data & 0x80U) == 0x80U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //data digit 6 + if((temp_sw_data & 0x40U) == 0x40U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //data digit 5 + if((temp_sw_data & 0x20U) == 0x20U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //data digit 4 + if((temp_sw_data & 0x10U) == 0x10U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //data digit 3 + if((temp_sw_data & 0x08U) == 0x08U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //data digit 2 + if((temp_sw_data & 0x04U) == 0x04U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + //data digit 1 + if((temp_sw_data & 0x02U) == 0x02U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + + //data digit 0 + if((temp_sw_data & 0x01U) == 0x01U){ SW_LAN_SDI = 1U;} else{ SW_LAN_SDI = 0U;} + i2c_pulse(); + + SW_LAN_SDI = 0; + delay_us(50); + DDRE.7 = 0; + //ACK + delay_us(50); + i2c_pulse(); + delay_us(50); + DDRE.7 = 1; + + delay_us(50); + i2c_stop_sw(); + delay_us(10); + + putchar((uint8_t)(temp_sw_add / 256U)); + putchar((uint8_t)(temp_sw_add % 256U)); + putchar((uint8_t)temp_sw_data); +} + + + + + +static void sw_list(void) +{ + static uint16_t tt_count = 0; + + for( tt_count = i2c_add ;tt_count < (i2c_add + 100U);tt_count++) + { + sw_lan_cont_r(tt_count); + } +} + +static void lan_reset_work(void) +{ + DDRE=(1<= 1U) + { + if(sec_100ms == 0U){sec_100ms = 1U;}else{sec_100ms = 0U;} + sec_count = 0; + count_b++; + + + ch_req_act = 1U; + + fnd_init(); + + display(); + if(sw_up_link_count >= 20U){ sw_up_link_count = 20U;}else{sw_up_link_count++;} + + if(alram_count_flag == ERROR) + { + if(alram_count >= ALRAM_COUNT_MAX){alram_count = ALRAM_COUNT_MAX;}else{alram_count++;} + } + else + { + alram_count = 0; + } + if(init_delay_count > 0U){init_delay_count--;} + + if(lan_reset_count < 100U) + { + lan_reset_count++; + } + + if(lan_reset_count >= 100U) + { + lan_reset_count = 0; + lan_reset_work(); + } + + if(count_sent_time == 0U){if(power_management_status_report_act == 0U){power_management_status_report_act = 1U;}} + if(count_sent_time == 1U) + { + if(power_manage_start_response_act == 1U) + { + power_manage_start_response_act = 0; + if(response_power_manage_start_act_p == 0U){response_power_manage_start_act_p = 1U;} + } + } + + if(count_sent_time == 2U) + { + if(power_management_pbit_response_act == 1U) + { + power_management_pbit_response_act = 0U; + if(response_power_management_pbit_act_p == 0U){response_power_management_pbit_act_p = 1;} + } + } + + if(count_sent_time == 3U) + { + if(power_manage_detail_response_act == 1U) + { + power_manage_detail_response_act = 0; + if(response_power_manage_detail_act_p == 0U){response_power_manage_detail_act_p = 1U;} + } + } + + if(count_sent_time == 4U){if(power_management_status_report_act == 0U){power_managementbit_cbit_report_act = 1U;}} + + if(count_sent_time == 6U){if(heart_bit_sent_act == 0U){heart_bit_sent_act = 1U;}} + if(count_sent_time == 8U) + { + if(power_manage_control_response_act == 1U) + { + power_manage_control_response_act = 0; + if(response_power_manage_control_act_p == 0U){response_power_manage_control_act_p = 1;} + } + } + if(count_sent_time >= 9U){count_sent_time = 0;}else{count_sent_time++;} + } + +} + +static void main_time(void) +{ + if(count_b >= 4U) + { + count_b = 0; + count_c++; + if(sec_500ms == 1U) + { + sec_500ms = 0U; + } + else + { + sec_500ms = 1U; + } + if(charge_display_count < 2U){charge_display_count++;}else{charge_display_count = 0U;} //5 ÃæÀü½Ã ȸÀü Ç¥½Ã¿ë + } + + main_time_sub(); + + + if(count_c >= 2U) + { + count_c = 0U; + if(power_divider_link_err == LINK ) + { + if(loss_count < 4U) + { + loss_count++; + } + else + { + loss_count = 4U; + } + } + if(loss_count >= 2U){ power_divider_link_err = ERROR;}else{power_divider_link_err = NOR;} + + if(sec_1000ms == 1U){sec_1000ms = 0U;}else{sec_1000ms = 1U;} + //Àüü ä³Î Â÷´Ü ½Ã 5ÃÊÀÌ»ó ´©¸£¸é Àüü ü³Î ÃʱâÈ­ ó¸® + if(buzzer_press == 1U) + { + if(buzzer_press_count >= 5U) + { + buzzer_press_count = 5U; + if(buzzer_press_act == 0U) + { + buzzer_press_act = 1U; + ch_sel_1 = 0xff; + ch_sel_2 = 0xff; + buzzer_pulse_one(); + ch_set_act = 1U; + } + } + else + { + buzzer_press_count++; + } + } + else + { + buzzer_press_count = 0U; + buzzer_press_act = 0U; + } + + } + + if(sec_100ms == 1U) + { + LED_HEALTH = ON; + + } + else + { + LED_HEALTH = OFF; + + } + + + +} + + +static void init(void) +{ + delay_ms(100); + DDRE=(1<