AVR1631 - Energy Meter Reference Design with ATxmega32A4  Rev 1.0
 All Data Structures Files Functions Variables Typedefs Macros
meter_rtc.c
Go to the documentation of this file.
1 
2 /* This file has been prepared for Doxygen automatic documentation generation.*/
50 #include "meter.h"
51 
53 uint8_t power_status_flag = 0;
55 const uint8_t days_lookup[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
57 RTC_BCD_t rtcTime = {0,0,0,1,1,2012};
58 
59 
63 void rtc_init()
64 {
65  CLK.RTCCTRL = CLK_RTCSRC_TOSC_gc | CLK_RTCEN_bm;
66  do {
67  } while ( RTC_Busy() );
68  RTC_Initialize( RTC_CYCLES_1S, 0, 0, RTC_PRESCALER_DIV1_gc );
69  RTC.INTFLAGS = RTC_OVFIF_bm | RTC_COMPIF_bm;
70  RTC_SetIntLevels( RTC_OVFINTLVL_HI_gc, RTC_COMPINTLVL_OFF_gc );
71  PMIC.CTRL |= PMIC_HILVLEN_bm;
72 }
73 
77 ISR(RTC_OVF_vect)
78 {
79  SLEEP.CTRL &= ~SLEEP_SEN_bm;
80  uint8_t pin_read = 0;
82  update_time();
84  pin_read = PORTD.IN & PIN2_bm;
85  //pin_read = PIN2_bm;
86  if(pin_read == PIN2_bm)
87  {
89  //TCC1.CTRLA = ( TCC1.CTRLA & ~TC1_CLKSEL_gm ) | TC_CLKSEL_OFF_gc;
90  TCC1.INTCTRLA = (TCC1.INTCTRLA & ~(TC1_OVFINTLVL_gm | TC1_ERRINTLVL_gm));
91  rtc_flag = 1;
92  }
93  else
94  {
96  rtc_flag = 0;
97  if(power_status_flag == POWERED_UP)
98  power_status_flag = POWER_OFF_DETECTED;
99  }
100 }
101 
102 
107 {
108  if ( ++rtcTime.sec > 59 )
109  {
110  rtcTime.sec = 0;
111  rtcTime.min++;
112  }
113  if ( rtcTime.min > 59 )
114  {
115  rtcTime.min = 0;
116  rtcTime.hr++;
117  }
118  if (rtcTime.hr > 23)
119  {
120  rtcTime.hr = 0;
121  rtcTime.day++;
122  }
123  if (rtcTime.day > days_lookup[rtcTime.month-1] )
124  {
125  if (rtcTime.month == 2)
126  {
127  if (rtcTime.year % 4 == 0 && rtcTime.day == 29)
128  {
129  asm("nop");
130  }
131  else
132  {
133  rtcTime.day = 1;
134  rtcTime.month++;
135  }
136  }
137  else
138  {
139  rtcTime.day = 1;
140  rtcTime.month++;
141  }
142 
143  }
144  if (rtcTime.month > 12)
145  {
146  rtcTime.year++;
147  rtcTime.month = 1;
148  }
149 }
150 
151