heat control  r71
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
timer.c
Go to the documentation of this file.
1 #include "timer.h"
2 #include "setup.h"
3 
7 UINT volatile g_debouncer = 0;
11 UCHAR volatile g_s100 = 0;
15 UCHAR volatile g_sec = 0;
19 UINT volatile g_min = 0;
23 UCHAR volatile g_mov = 0;
27 CTRL_MODE volatile g_ctm = CM_MEAS;
31 ENG_TIMER volatile g_et;
32 
33 
38 void TimerInit(void) {
45  TCC_R0 = (1<<CS02) | (0<<CS01) | (0<<CS00);
46 
50  TCCR0A = (0<<TCW0) | (0<<ICEN0) | (0<<ICNC0) | (0<<ICES0) | (0<<ACIC0) | (1<<CTC0);
51 
55  OCR0A = T0_INC20;
56 
60  TIMSK = (0 << OCIE1D) | (0 << OCIE1A) | (0 << OCIE1B) | (1 << OCIE0A) | (0 << OCIE0B) | (0 << TOIE1) | (0 << TOIE0) | (0 << TICIE0);
61 
65  PLLCSR=(0<<PCKE) | (0<<PLLE) | (0<<PLOCK);
66 }
67 
72 ISR(TIMER0_COMPA_vect) {
76  static UCHAR s_s100 = 0;
80  static UINT s_1s = 0;
81 // static UCHAR s_ecp = 0; //counter for error compensation
82 
83  ++g_debouncer;
84  if(++s_s100 > (IV_1S/10)) {
85  s_s100 = 0;
86  if(++g_s100 > 9)
87  g_s100 = 0;
88  //count and handle mixer engine timeout
89  ++g_et.cnt;
90  if(g_et.allow && g_et.cnt >= gEcd()) {
91  g_et.allow = FALSE;
92  g_et.em = EM_STOP;
93  //explicitly switch off engine (take advantage of doing this in interrupt)
96  }
97  }
98  if(++s_1s > IV_1S) {
99  s_1s = 0;
100  if(++g_sec > 59) {
101  //one period gone?
102  if((++g_min) > (NUM_MS * gIvMeas()) - 1) {
103  g_min = 0;
104  ++g_mov;
105  }
106  g_ctm = CM_MEAS; //allow control in new minute
107  g_sec = 0;
108  }
109  }
110  //adjust compare match value (changed by comm)
111  OCR0A = T0_INC20;
112 }
113 
120 {
121  //bblp: both buttons long pressed
122  static UCHAR btnUp=FALSE, btnDn=FALSE, firstUp=FALSE, firstDn=FALSE, bblp=FALSE;
123  static UINT debUp=DEBOUNCED_S, debDn=DEBOUNCED_S;
124  BTN_CODE bc = BC_NONE;
125 
126  if(BTN_UP && !btnUp) {
127  btnUp = TRUE;
128  g_debouncer = 0;
129  }
130  else if(btnUp && BTN_UP && (g_debouncer > debUp)) {
131  //enhance debouncer threshold after first button press, before doing action repetitive
132  if(firstUp) {
133  debUp = DEBOUNCED_L;
134  firstUp=FALSE;
135  }
136  else
137  debUp = DEBOUNCED_S;
138  btnUp = FALSE;
139 // *btnOff = OFF_NEAR;
140  bc |= BC_UP;
141  }
142  if(!(BTN_UP) && (g_debouncer > DEBOUNCED_S)) {
143  btnUp = FALSE;
144  //prepare first delay ability
145  firstUp=TRUE;
146  debUp = DEBOUNCED_S;
147  bblp = FALSE;
148  }
149 
150  if(BTN_DN && !btnDn) {
151  btnDn = TRUE;
152  g_debouncer = 0;
153  }
154  else if(btnDn && BTN_DN && (g_debouncer > debDn)) {
155  //enhance debouncer threshold after first button press, before doing action repetitive
156  if(firstDn) {
157  debDn = DEBOUNCED_L;
158  firstDn=FALSE;
159  }
160  else
161  debDn = DEBOUNCED_S;
162  btnDn = FALSE;
163 // *btnOff = OFF_NEAR;
164  bc |= BC_DN;
165  }
166  if(!(BTN_DN) && (g_debouncer > DEBOUNCED_S)) {
167  btnDn = FALSE;
168  //prepare first delay ability
169  firstDn=TRUE;
170  debDn = DEBOUNCED_S;
171  bblp = FALSE;
172  }
173  //are both buttons pressed for a longer time?
174  if(bc == BC_DNUP && (!firstUp) && (!firstDn)) {
175  if(bblp) {
176  bblp = FALSE;
177  bc = BC_DNUP_L;
178  }
179  else {
180  bblp = TRUE;
181  }
182  debUp = DEBOUNCED_L;
183  debDn = DEBOUNCED_L;
184  }
185  return bc;
186 }
187 
193  return g_s100;
194 }
195 
201  return g_mov;
202 }
203 
208 void sMov(UCHAR mov) {
209  g_mov = mov;
210 }
211 
212 
213 
214 
215 
216 
#define TCC_R0
Definition: types.h:47
Definition: types.h:311
BTN_CODE
Definition: types.h:299
ENG_MODE em
Definition: types.h:336
#define DEBOUNCED_L
Definition: timer.h:36
void TimerInit(void)
initialize timer resource period: 32┬Ás
Definition: timer.c:38
#define CTC0
Definition: types.h:53
UINT volatile g_min
Definition: timer.c:19
UINT volatile g_debouncer
Definition: timer.c:7
Definition: types.h:303
UCHAR GetTenthSec(void)
interface to get the global tenth seconds
Definition: timer.c:192
void sMov(UCHAR mov)
set minute overflow counter
Definition: timer.c:208
UCHAR volatile g_mov
Definition: timer.c:23
#define T0_INC20
Definition: timer.h:16
#define ENG_STOP_CW
Definition: timer.h:48
#define FALSE
Definition: types.h:93
UCHAR allow
Definition: types.h:332
#define UCHAR
Definition: types.h:73
#define IV_1S
Definition: timer.h:20
UCHAR cnt
Definition: types.h:328
Definition: types.h:307
ENG_TIMER volatile g_et
Definition: timer.c:31
UCHAR volatile g_s100
Definition: timer.c:11
Definition: types.h:261
Definition: types.h:315
#define BTN_DN
Definition: timer.h:24
#define BTN_UP
Definition: timer.h:28
Definition: types.h:193
#define TRUE
Definition: types.h:97
#define ENG_STOP_CCW
Definition: timer.h:56
CTRL_MODE
Definition: types.h:253
CTRL_MODE volatile g_ctm
Definition: timer.c:27
BTN_CODE DecodeButton(void)
identify whether a (and which) button is pressed or not
Definition: timer.c:119
UCHAR volatile g_sec
Definition: timer.c:15
#define DEBOUNCED_S
Definition: timer.h:32
#define NUM_MS
Definition: types.h:223
#define UINT
Definition: types.h:81
UCHAR gMov()
return minute overflow counter
Definition: timer.c:200
ISR(TIMER0_COMPA_vect)
compare match interrupt service for OCR0A
Definition: timer.c:72