Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1053 - 1
/*****************************************************************************
2
 *   Copyright (C) 2009 Peter "woggle" Mack, mac@denich.net                  *
3
 *   based on the key handling by Peter Dannegger                            *
4
 *     see www.mikrocontroller.net                                           *
5
 *   Copyright (C) 2011 Christian Brandtner brandtner@brandtner.net          *
6
 *                                                                           *
7
 *   This program is free software; you can redistribute it and/or modify    *
8
 *   it under the terms of the GNU General Public License as published by    *
9
 *   the Free Software Foundation; either version 2 of the License.          *
10
 *                                                                           *
11
 *   This program is distributed in the hope that it will be useful,         *
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of          *
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
14
 *   GNU General Public License for more details.                            *
15
 *                                                                           *
16
 *   You should have received a copy of the GNU General Public License       *
17
 *   along with this program; if not, write to the                           *
18
 *   Free Software Foundation, Inc.,                                         *
19
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.               *
20
 *                                                                                                                                               *
21
 *   22.03.2011 Zeitgesteuerte Displaybeleuchtung C.B.                                           *
22
 *                                                                           *
23
 *****************************************************************************/
24
 
25
#include <avr/io.h>
26
#include <avr/interrupt.h>
27
#include <avr/pgmspace.h>
28
#include <string.h>
29
 
30
 
31
#include "main.h"
32
#include "timer.h"
33
#include "eeprom.h"
34
#include "lcd.h"
35
 
36
volatile uint16_t timer;
37
volatile uint16_t abo_timer;
38
volatile uint8_t pwm1 = 255;
39
 
40
#define FRAME_LENGTH 20
41
uint16_t icrval = (F_CPU / 64) * FRAME_LENGTH / 1000 ;
42
uint16_t minocr_a = (F_CPU / 64) * 1            / 1000;// 312
43
uint16_t maxocr_a = (F_CPU / 64) * 2            / 1000;// 624
44
uint16_t minocr_b = (F_CPU / 64) * 1           / 1000;// 312
45
uint16_t maxocr_b = (F_CPU / 64) * 2            / 1000;// 624
46
 
47
 
48
uint8_t key_state = 0;          // debounced and inverted key state:
49
                                                                                                // bit = 1: key pressed
50
uint8_t key_press = 0;          // key press detect
51
uint8_t key_rpt;                        // key long press and repeat
52
uint16_t DisplayTime = 0;       // Leuchtdauer
53
volatile uint8_t Display_on;// Flag Display on/off
54
 
55
 
56
 
57
//*****************************************************************************
58
// 
59
 
60
 
61
#if defined (__AVR_ATmega32__)
62
ISR(TIMER0_COMP_vect)                                           // Timer-Interrupt (100 Hz)
63
#else
64
ISR(TIMER0_COMPA_vect)                                  // Timer-Interrupt (100 Hz)
65
#endif
66
{
67
  static uint8_t ct0 = 0;
68
        static uint8_t ct1 = 0;
69
        static uint8_t rpt = 0;
70
        uint8_t i;
71
 
72
        // Key handling by Peter Dannegger
73
        // see www.mikrocontroller.net
74
        i = key_state ^ ~KEY_PIN;               // key changed ?
75
        ct0 = ~(ct0 & i);                               // reset or count ct0
76
        ct1 = ct0 ^ (ct1 & i);                  // reset or count ct1
77
        i &= (ct0 & ct1);                               // count until roll over ?
78
        key_state ^= i;                                 // then toggle debounced state
79
        key_press |= (key_state & i);   // 0->1: key press detect
80
 
81
        if (i!=0)
82
                {                                                       // Displaylicht einschalten, und bzw. Timeoutzählerreset wenn Taste gedrückt wurde
83
                if (Display_on ==0)
84
                 {                                                      // einschalten
85
//              PORTD &= ~(1<<PORTD7);
86
        #ifdef HWVERSION1_2
87
                        PORTC &= ~(1<<PORTC0);
88
                        PORTC &= ~(1<<PORTC1);
89
                        PORTD &= ~(1<<PORTD7);
90
        #endif
91
        #ifdef HWVERSION1_3
92
                        PORTD &= ~(1<<PORTD6);
93
                        PORTC &= ~(1<<PORTC2);
94
                        PORTD &= ~(1<<PORTD7);
95
        #endif
96
        #ifdef HWVERSION3_1
97
                        set_D_LIGHT();
98
        #endif
99
 
100
                 }                                                      // if Display_on ==0
101
                Display_on =1;                          // Flag Display on
102
                DisplayTime = 0;                        // Timer Reset
103
 
104
                }
105
 
106
 
107
        if ((key_state & REPEAT_MASK) == 0)     // check repeat function
108
        {
109
                rpt = REPEAT_START;                             // start delay
110
        }
111
        if (--rpt == 0)
112
        {
113
                rpt = REPEAT_NEXT;                              // repeat delay
114
                key_rpt |= (key_state & REPEAT_MASK);
115
        }
116
 
117
 
118
        if (timer > 0)
119
        {
120
                timer --;
121
        }
122
 
123
        if (abo_timer > 0)
124
        {
125
                abo_timer --;
126
        }
127
 
128
        if (DisplayTimeout > 0)
129
 
130
        {
131
         if (Display_on==1)
132
                {
133
                DisplayTime++;
134
                if ((DisplayTime/100) == DisplayTimeout)  //ISR läuft mit 100Hz
135
 
136
                {                                                               //Displaylicht ausschalten
137
//              PORTD |= (1<<PORTD7);
138
        #ifdef HWVERSION1_2
139
                        PORTC |= (1<<PORTC0);
140
                        PORTC |= (1<<PORTC1);
141
                        PORTD |= (1<<PORTD7);
142
        #endif
143
        #ifdef HWVERSION1_3
144
                        PORTD |= (1<<PORTD6);
145
                        PORTD |= (1<<PORTD7);
146
                        PORTC |= (1<<PORTC2);
147
        #endif
148
        #ifdef HWVERSION3_1
149
                        clr_D_LIGHT();
150
        #endif
151
                        Display_on = 0;                         // Flag Display off
152
                }
153
         }
154
 
155
        }
156
 
157
 
158
}
159
 
160
 
161
void TIMER1_Init (void)
162
{
163
 
164
    DDRD |= (1<<PORTD4)|(1<<PORTD5);
165
    TCCR1A |= (1<<COM1B1) |(1<<COM1A1) | (1<<WGM11);
166
    TCCR1B |= (1<<CS11)|(1<<CS10)| (1<<WGM12)| (1<<WGM13);
167
 
168
    ICR1 = icrval;
169
        OCR1A = minocr_a + ((maxocr_a-minocr_a)/2);
170
        OCR1B = minocr_b + ((maxocr_b-minocr_b)/2);
171
 
172
//      OCR1A = minocr_a;
173
//      OCR1B = maxocr_b;
174
 
175
}
176
 
177
void set_pwm_a(uint16_t value)
178
{
179
        uint16_t setv = ((value * 9) / 10 ) + minocr_a;
180
 
181
        if((setv > 311)&&(setv < 637))
182
        {
183
                OCR1A = setv;
184
        }
185
}
186
void set_pwm_b(uint8_t value)
187
{
188
        uint16_t setv = ((value * 4 * 5 ) / 6 ) + minocr_b;
189
        if((setv > 200)&&(setv < 750))
190
        {
191
                OCR1B = setv;
192
        }
193
}
194
 
195
 
196
 
197
//*****************************************************************************
198
// 
199
void TIMER0_Init (void)
200
{
201
        timer = 0;
202
 
203
#if defined (__AVR_ATmega32__)
204
        TCCR0 = (1 << CS02) | (1 << CS00) | (1 << WGM01);               // Prescaler 1024
205
        OCR0 = (F_CPU / (100L * 1024L)) ;
206
 
207
        TIMSK |= (1 << OCIE0);  // enable interrupt for OCR
208
#else
209
        TCCR0A = (1 << WGM01);
210
        TCCR0B = (1 << CS02) | (1 << CS00);
211
        OCR0A = (F_CPU / (100L * 1024L)) ;
212
 
213
        TIMSK0 |= (1 << OCIE0A);        // enable interrupt for OCR
214
#endif
215
}
216
 
217
 
218
//*****************************************************************************
219
// 
220
uint8_t get_key_press (uint8_t key_mask)
221
{
222
        uint8_t sreg = SREG;
223
 
224
        // disable all interrupts
225
        cli();
226
 
227
  key_mask &= key_press;        // read key(s)
228
  key_press ^= key_mask;        // clear key(s)
229
 
230
        SREG = sreg;    // restore status register
231
 
232
  return key_mask;
233
}
234
 
235
 
236
//*****************************************************************************
237
// 
238
uint8_t get_key_rpt (uint8_t key_mask)
239
{
240
        uint8_t sreg = SREG;
241
 
242
        // disable all interrupts
243
        cli();
244
 
245
  key_mask &= key_rpt;  // read key(s)
246
  key_rpt ^= key_mask;  // clear key(s)
247
 
248
        SREG = sreg;    // restore status register
249
 
250
  return key_mask;
251
}
252
 
253
 
254
//*****************************************************************************
255
// 
256
uint8_t get_key_short (uint8_t key_mask)
257
{
258
        uint8_t ret;
259
        uint8_t sreg = SREG;
260
 
261
        // disable all interrupts
262
        cli();
263
 
264
  ret = get_key_press (~key_state & key_mask);
265
 
266
        SREG = sreg;    // restore status register
267
 
268
  return ret;
269
}
270
 
271
 
272
//*****************************************************************************
273
// 
274
uint8_t get_key_long (uint8_t key_mask)
275
{
276
  return get_key_press (get_key_rpt (key_mask));
277
}
278
 
279
 
280
//*****************************************************************************
281
// 
282
uint8_t get_key_long2 (uint8_t key_mask)
283
{
284
  return get_key_press (get_key_rpt (key_press^key_mask));
285
}
286
 
287
 
288
//*****************************************************************************
289
// 
290
uint8_t get_key_long_rpt (uint8_t key_mask)
291
{
292
  return get_key_rpt (~key_press^key_mask);
293
}