52 uint8_t rx_buffer[10] = {0},rx_complete_flag = 0,tx_count=0,rx_count = 0;
53 uint8_t tx_read[31], calibration_flag = 0, calibration_count;
54 __flash
unsigned char tx_array[7][31] = {
56 {
"\r\nEnter the Time \r\n"},
57 {
"\r\nEnter the Date \r\n"},
58 {
"\r\nEnter the voltage? \r\n"},
59 {
"\r\nPut the meter in No Load? \r\n"},
60 {
"\r\nEnter the Watt value? \r\n"},
61 {
"\r\nEnter the Current in Amps \r\n"}, };
69 SLEEP.CTRL &= ~SLEEP_SEN_bm;
84 SLEEP.CTRL &= ~SLEEP_SEN_bm;
88 if ((PORTC.IN & PIN3_bm) == 0)
90 rx_complete_flag = 0x00;
95 else if (uart_rx ==
'v')
99 else if (uart_rx ==
'n')
101 calibration_flag = 3;
103 else if (uart_rx ==
'c')
105 calibration_flag = 4;
107 else if (uart_rx ==
'w')
109 calibration_flag = 5;
111 else if (uart_rx ==
'i')
113 calibration_flag = 6;
115 else if(uart_rx == 13)
117 rx_complete_flag = 1;
121 rx_buffer[rx_count] = uart_rx-48;
125 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
126 USART.DATA = uart_rx;
135 USART.DATA = tx_read[tx_count];
140 USART.CTRLA &= ~USART_TXCINTLVL_gm;
155 for(uint8_t k = 0; k < 9; k++)
158 USART.CTRLA |= USART_TXCINTLVL0_bm;
160 while(rx_complete_flag == 0){__watchdog_reset();}
161 rx_complete_flag = 0;
162 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
164 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
174 for(uint8_t k = 0; k < 30; k++)
176 tx_read[k] = tx_array[cnt][k];
189 calibration_flag = 0;
197 rtcTime.
hr = rx_buffer[0]*10+rx_buffer[1];
210 rtcTime.
year = rx_buffer[6]*1000+rx_buffer[7]*100+rx_buffer[8]*10+rx_buffer[9];
222 static float calibration_temp = 0;
223 static int64_t calibration_watt = 0;
224 if (calibration_count < 1)
227 calibration_temp = 0;
228 if(rx_buffer[0] != 0x49)
230 calibration_flag = 0;
231 calibration_count = 0xFF;
233 meter.shunt_offset[6] = 0;
234 meter.watt_offset[6] = 0;
240 calibration_temp = calibration_temp + (float)shunt_temp;
241 calibration_watt = calibration_watt + (int64_t)watts_sum_calib;
244 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
246 if (calibration_count == 30)
248 calibration_temp /= calibration_count;
249 meter.shunt_offset[6] = (uint16_t)calibration_temp;
250 eeprom.shunt_offset[6] = meter.shunt_offset[6];
251 calibration_watt /= (calibration_count);
252 meter.watt_offset[6] = -1*(int32_t)calibration_watt;
253 eeprom.watt_offset[6] = meter.watt_offset[6];
254 calibration_watt = 0;
255 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
257 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
259 calibration_temp = 0;
260 calibration_flag = 0;
261 calibration_count = 0;
273 static float calibration_temp = 0, volt_input = 0;
275 if (calibration_count < 1)
279 calibration_temp = 0;
280 volt_input = (float) (rx_buffer[0]*100 + rx_buffer[1]*10 + rx_buffer[2]);
281 volt_input += (float) rx_buffer[4]/10 + (
float)rx_buffer[5]/100;
284 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
286 calibration_temp = calibration_temp + (float)volt_temp;
287 if (calibration_count > 30)
289 calibration_temp /= calibration_count;
290 meter.volt_const = (uint16_t)(65536* (volt_input/sqrt(calibration_temp)));
291 eeprom.volt_const = meter.volt_const;
292 calibration_temp = 0;
293 calibration_flag = 0;
294 calibration_count = 0;
295 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
297 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
312 static float calibration_temp = 0, current_input = 0;
313 static uint8_t cal_gain_stage = 0, cal_offset_cnt = 0;
314 if (calibration_count < 1)
317 calibration_temp = 0;
318 calib_temp = (float)(rx_buffer[0]*10 + rx_buffer[1]);
319 calib_temp += (float)rx_buffer[3]/10 + (
float)rx_buffer[4]/100 + (float)rx_buffer[5]/1000;
320 current_input = calib_temp;
322 if(current_input < 0.500)
327 else if(current_input > 0.500 && current_input < 1.500)
332 else if(current_input > 1.500 && current_input < 2.500)
337 else if(current_input > 2.500 && current_input < 4.000)
342 else if(current_input > 4.000 && current_input < 7.000)
347 else if(current_input > 7.000 && current_input < 14.500)
352 else if(current_input > 14.500)
359 calibration_flag = 0;
360 calibration_count = 0xFF;
365 calibration_temp = calibration_temp + (float)shunt_temp;
367 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
371 offset_cnt = cal_offset_cnt;
372 if(cal_offset_cnt == 6 && offset_cnt!= 6)
375 ADC_CH_INPUTMODE_DIFFWGAIN_gc,
378 else if(cal_offset_cnt ==5 && offset_cnt!= 5)
381 ADC_CH_INPUTMODE_DIFFWGAIN_gc,
384 if (calibration_count > 30)
386 calibration_temp /= (calibration_count-1);
387 meter.shunt_const[cal_gain_stage] = (uint16_t)(65536*(current_input/sqrt(calibration_temp)));
388 eeprom.shunt_const[cal_gain_stage] = meter.shunt_const[cal_gain_stage];
389 calibration_temp = 0;
390 calibration_flag = 0;
391 calibration_count = 0;
392 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
394 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
407 static int64_t calibration_watt = 0;
408 static uint8_t cal_gain_stage = 0, cal_offset_cnt = 0;
409 static uint16_t watt_input = 0;
410 static float A1_temp, B1_temp;
411 if (calibration_count < 1)
414 calibration_watt = 0;
415 watt_input = rx_buffer[0]*1000 + rx_buffer[1]*100 + rx_buffer[2]*10 + rx_buffer[3];
422 else if(Irms[0] > 500 && Irms[0] < 1500)
427 else if(Irms[0] > 1500 && Irms[0] < 2500)
432 else if(Irms[0] > 2500 && Irms[0] < 4000)
437 else if(Irms[0] > 4000 && Irms[0] < 7000)
442 else if(Irms[0] > 7000 && Irms[0] < 14500)
447 else if(Irms[0] > 14500)
454 calibration_flag = 0;
455 calibration_count = 0xFF;
457 A1_temp = meter.A1[cal_gain_stage];
458 B1_temp = meter.B1[cal_gain_stage];
459 meter.A1[cal_gain_stage] = 1;
460 meter.B1[cal_gain_stage] = 0;
464 calibration_watt = calibration_watt + (int64_t)active_energy_signed;
466 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
469 if(cal_offset_cnt == 6 && offset_cnt!= 6)
472 ADC_CH_INPUTMODE_DIFFWGAIN_gc,
475 else if(cal_offset_cnt ==5 && offset_cnt!= 5)
478 ADC_CH_INPUTMODE_DIFFWGAIN_gc,
481 if (calibration_count > 30)
483 calibration_watt /= (calibration_count-1);
484 meter.watt_const[cal_gain_stage] = (float)watt_input/abs(calibration_watt);
485 eeprom.watt_const[cal_gain_stage] = meter.watt_const[cal_gain_stage];
486 meter.A1[cal_gain_stage] = A1_temp;
487 meter.B1[cal_gain_stage] = B1_temp;
488 calibration_watt = 0;
489 calibration_flag = 0;
490 calibration_count = 0;
491 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
493 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
508 static float calibration_temp = 0, current_input = 0;
510 static uint8_t cal_gain_stage = 0, cal_offset_cnt = 0;
511 if (calibration_count < 1)
515 calibration_temp = 0;
516 calib_temp = (float)(rx_buffer[0]*10 + rx_buffer[1]);
517 calib_temp += (float)rx_buffer[3]/10 + (
float)rx_buffer[4]/100 + (float)rx_buffer[5]/1000;
518 current_input = calib_temp;
520 if(current_input > 0.001 && current_input < 0.500)
525 else if(current_input > 0.500 && current_input < 1.500)
530 else if(current_input > 1.500 && current_input < 2.500)
535 else if(current_input > 2.500 && current_input < 4.000)
540 else if(current_input > 4.000 && current_input < 7.000)
545 else if(current_input > 7.000 && current_input < 14.500)
550 else if(current_input > 14.500)
557 calibration_flag = 0;
558 calibration_count = 0xFF;
560 meter.A1[cal_gain_stage] = 1;
561 meter.B1[cal_gain_stage] = 0;
565 calibration_temp = calibration_temp + ((float)active_power[0]);
567 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
571 offset_cnt = cal_offset_cnt;
573 if (calibration_count > 60)
575 calibration_temp /= (calibration_count-1);
576 pf_temp = calibration_temp / (float)(240 * current_input);
578 calibration_temp = 0;
579 calibration_flag = 0;
580 calibration_count = 0;
581 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
583 while(
USART.STATUS & USART_TXCIF_bm == 0 ){}
594 float phase_angle, g_delay, t1, t2, t3, beta, gain;
595 phase_angle = acos(pf_avg) - acos(0.5);
596 g_delay = phase_angle/
OMEGA;
598 t1 = (1-(2*g_delay)) *cos(
OMEGA);
599 t2= sqrt( pow((1-(2*g_delay)),2) * (cos(
OMEGA)*cos(
OMEGA)) + 4*g_delay*(1-g_delay));
602 eeprom.B1[g_stage] = beta;
603 meter.B1[g_stage] = beta;
604 gain = pow(pow( (cos(
OMEGA) + beta),2) + pow(sin(
OMEGA),2),-0.5);
605 eeprom.A1[g_stage] = gain;
606 meter.A1[g_stage] = gain;