AVR1631 - Energy Meter Reference Design with ATxmega32A4  Rev 1.0
 All Data Structures Files Functions Variables Typedefs Macros
meter_task.c
Go to the documentation of this file.
1 /* This file has been prepared for Doxygen automatic documentation generation.*/
49 #include "meter.h"
50 
51 
52 uint8_t rtc_flag = 0, key_flag=0;
53 uint8_t magnetic_tamper_flag = 0;
54 uint8_t cover_open_flag = 0;
58 void tamper_check(void)
59 {
60  uint16_t Irms_tamper_low, Irms_tamper_high;
61  static uint8_t rev_current = 0, earth_fault = 0;
64  Irms_tamper_low = Irms[0] - ((Irms[0]*64)>>8);
65  Irms_tamper_high = Irms[0] + ((Irms[0]*64)>>8);
66  if ((Nrms < Irms_tamper_low) | (Nrms > Irms_tamper_high))
67  {
68  earth_fault++;
69  }
70  else
71  {
72  earth_fault = 0;
73  }
75  if(earth_fault > 15) //checking for 15 seconds
76  {
77  PORTB.OUTSET = TAMPER_EARTH_FAULT;
78  earth_fault = 15;
79  }
80  else
81  {
82  PORTB.OUTCLR = TAMPER_EARTH_FAULT;
83  }
84 
86  if (active_energy_signed < -5 || neutral_power < -5) //comparing the sign of neutral and pahse power
87  { rev_current = 1; }
88  else
89  { rev_current = 0; }
91  if (rev_current == 1)
92  { PORTB.OUTTGL = TAMPER_CURRENT_REVERSAL; } // toggle the LED on current reversal detection
93  else
94  { PORTB.OUTCLR = TAMPER_CURRENT_REVERSAL; }
96  if ((Vrms[0] == 0) && (Irms[0] > 500))
97  { PORTB.OUTSET = TAMPER_NEUTRAL_CUT; }
98  else
99  { PORTB.OUTCLR = TAMPER_NEUTRAL_CUT; }
101  if(magnetic_tamper_flag == 1)
102  {
103  lcd_read_write(LCD_DATA_READ_WRITE,24,1);
104  }
105 }
106 
107 
108 
114 ISR(PORTB_INT0_vect)
115 {
116  SLEEP.CTRL &= ~SLEEP_SEN_bm;
117  PORTB.INT0MASK = 0x00;
118  key_flag++;
119  if (key_flag > 10)
120  {
121  key_flag = 0;
122  }
123  delay_ms(50); // to avoid switch bouncing
124  PORTB.INTFLAGS = 0x00;
125  PORTB.INT0MASK = PIN2_bm;
126  init_lcd();
127 }
128 
134 ISR(PORTC_INT0_vect)
135 {
136  SLEEP.CTRL &= ~SLEEP_SEN_bm;
137  uint8_t pin_val = 0;
138  PORTC.INT0MASK = 0x00;
139  delay_ms(20);
140  pin_val = PORTC.IN;
142  if((pin_val & PIN2_bm) == 0) // cover open interrupt, currently used to store energy value to eeprom for debugging
143  {
144  cover_open_flag ^= 0x1;
145  }
147  else if((pin_val & PIN4_bm) == 0x10)
148  {
149  magnetic_tamper_flag = 1;
150  }
152  else if((pin_val & PIN5_bm) == 0) //currently used to change the display for testing
153  {
154  key_flag--;
155  if (key_flag == 255)
156  {
157  key_flag = 10;
158  }
159  }
160  PORTC.INTFLAGS = 0x00;
161  PORTC.INT0MASK = PIN2_bm |PIN4_bm |PIN5_bm;
162 }
163 
164 
171 ISR(PORTD_INT0_vect)
172 {
173  SLEEP.CTRL &= ~SLEEP_SEN_bm;
174  PORTD.PIN4CTRL = PORT_OPC_WIREDAND_gc;
175  //PORTD.OUTCLR = PIN4_bm;
176  __watchdog_reset();
177 }