AVR1631 - Energy Meter Reference Design with ATxmega32A4  Rev 1.0
 All Data Structures Files Functions Variables Typedefs Macros
meter_main.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 int main(void)
52 {
56  clock_init();
58  init_lcd();
60  lcd_show_all();
62  init_ioport();
64  init_UART();
68  get_offset();
70  init_ADC();
72  init_eeprom();
74  rtc_init();
76  init_timer();
77 
78  while(1)
79  {
81  if (rtc_flag == 1)
82  {
84  calculate();
85  //for debugging
86  if(cover_open_flag ==1)
87  PORTD.OUTTGL = PIN3_bm;
88  //TCC1.CTRLA = ( TCC1.CTRLA & ~TC1_CLKSEL_gm ) | TC_CLKSEL_DIV64_gc;
89  TCC1.INTCTRLA = (TCC1.INTCTRLA & ~(TC1_OVFINTLVL_gm | TC1_ERRINTLVL_gm))|TC1_OVFINTLVL0_bm;
91  if(power_status_flag == POWER_ON_DETECTED)
92  {
94  CLKSYS_Prescalers_Config( CLK_PSADIV_1_gc, CLK_PSBCDIV_1_1_gc );
96  PORTD.PIN4CTRL = PORT_OPC_WIREDANDPULL_gc;
97  //PORTD.OUTSET = PIN4_bm;
99  init_ioport();
101  SLEEP.CTRL = (SLEEP.CTRL & ~SLEEP_SMODE_gm)| SLEEP_SMODE_IDLE_gc;
103  ADC_Enable(&ADCA);
104  TCC1.CTRLA = ( TCC1.CTRLA & ~TC1_CLKSEL_gm ) | TC_CLKSEL_DIV64_gc;
106  init_lcd();
108  power_status_flag = POWERED_UP;
109  }
111  lcd_disp_key();
112  tamper_check();
113  if( active_power[0] > max_demand )
114  {
115  max_demand = active_power[0];
116  }
118  if(calibration_flag != 0)
119  {
121  if (calibration_flag == 1)
122  { calibrate_time_date(); }
124  else if (calibration_flag == 2)
125  { calibrate_voltage(); }
127  else if (calibration_flag == 3)
128  { calibrate_no_load(); }
130  else if (calibration_flag == 4)
131  { calibrate_phase(); }
133  else if (calibration_flag == 5)
134  { calibrate_watt(); }
135  else if (calibration_flag == 6)
137  { calibrate_current(); }
138  }
139  rtc_flag = 0;
140  __watchdog_reset();
141  }
143  else if(power_status_flag == POWER_OFF_DETECTED)
144  {
146  PORTD.PIN4CTRL = PORT_OPC_WIREDAND_gc;
147  //PORTD.OUTCLR = PIN4_bm;
149  SLEEP.CTRL = (SLEEP.CTRL & ~SLEEP_SMODE_gm)| SLEEP_SMODE_PSAVE_gc;
151  lcd_command(LCD_COMMAND,0x02);
155  ADC_Disable(&ADCA);
157  TCC1.CTRLA = ( TCC1.CTRLA & ~TC1_CLKSEL_gm ) | TC_CLKSEL_OFF_gc;
159  meter_flush();
161  CLKSYS_Prescalers_Config( CLK_PSADIV_64_gc, CLK_PSBCDIV_1_1_gc );
163  power_status_flag = POWER_OFF;
164  __watchdog_reset();
165  }
167  SLEEP.CTRL |= SLEEP_SEN_bm;
168  asm("sleep");
169  }
170 }
171 
172 
173