Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1011 - 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
        #endif
90
        #ifdef HWVERSION1_3
91
                        PORTD &= ~(1<<PORTD6);
92
                        PORTC &= ~(1<<PORTC2);
93
        #endif
94
        #ifdef HWVERSION3_1
95
                        set_D_LIGHT();
96
        #endif
97
 
98
                 }                                                      // if Display_on ==0
99
                Display_on =1;                          // Flag Display on
100
                DisplayTime = 0;                        // Timer Reset
101
 
102
                }
103
 
104
 
105
        if ((key_state & REPEAT_MASK) == 0)     // check repeat function
106
        {
107
                rpt = REPEAT_START;                             // start delay
108
        }
109
        if (--rpt == 0)
110
        {
111
                rpt = REPEAT_NEXT;                              // repeat delay
112
                key_rpt |= (key_state & REPEAT_MASK);
113
        }
114
 
115
 
116
        if (timer > 0)
117
        {
118
                timer --;
119
        }
120
 
121
        if (abo_timer > 0)
122
        {
123
                abo_timer --;
124
        }
125
 
126
        if (DisplayTimeout > 0)
127
 
128
        {
129
         if (Display_on==1)
130
                {
131
                DisplayTime++;
132
                if ((DisplayTime/100) == DisplayTimeout)  //ISR läuft mit 100Hz
133
 
134
                {                                                               //Displaylicht ausschalten
135
//              PORTD |= (1<<PORTD7);
136
        #ifdef HWVERSION1_2
137
                        PORTC |= (1<<PORTC0);
138
                        PORTC |= (1<<PORTC1);
139
        #endif
140
        #ifdef HWVERSION1_3
141
                        PORTD |= (1<<PORTD6);
142
                        PORTC |= (1<<PORTC2);
143
        #endif
144
        #ifdef HWVERSION3_1
145
                        clr_D_LIGHT();
146
        #endif
147
                        Display_on = 0;                         // Flag Display off
148
                }
149
         }
150
 
151
        }
152
 
153
 
154
}
155
 
156
 
157
void TIMER1_Init (void)
158
{
159
 
160
    DDRD |= (1<<PORTD4)|(1<<PORTD5);
161
    TCCR1A |= (1<<COM1B1) |(1<<COM1A1) | (1<<WGM11);
162
    TCCR1B |= (1<<CS11)|(1<<CS10)| (1<<WGM12)| (1<<WGM13);
163
 
164
    ICR1 = icrval;
165
        OCR1A = minocr_a + ((maxocr_a-minocr_a)/2);
166
        OCR1B = minocr_b + ((maxocr_b-minocr_b)/2);
167
 
168
//      OCR1A = minocr_a;
169
//      OCR1B = maxocr_b;
170
 
171
}
172
 
173
void set_pwm_a(uint16_t value)
174
{
175
        uint16_t setv = ((value * 9) / 10 ) + minocr_a;
176
 
177
        if((setv > 311)&&(setv < 637))
178
        {
179
                OCR1A = setv;
180
        }
181
}
182
void set_pwm_b(uint8_t value)
183
{
184
        uint16_t setv = ((value * 4 * 5 ) / 6 ) + minocr_b;
185
        if((setv > 200)&&(setv < 750))
186
        {
187
                OCR1B = setv;
188
        }
189
}
190
 
191
 
192
 
193
//*****************************************************************************
194
// 
195
void TIMER0_Init (void)
196
{
197
        timer = 0;
198
 
199
#if defined (__AVR_ATmega32__)
200
        TCCR0 = (1 << CS02) | (1 << CS00) | (1 << WGM01);               // Prescaler 1024
201
        OCR0 = (F_CPU / (100L * 1024L)) ;
202
 
203
        TIMSK |= (1 << OCIE0);  // enable interrupt for OCR
204
#else
205
        TCCR0A = (1 << WGM01);
206
        TCCR0B = (1 << CS02) | (1 << CS00);
207
        OCR0A = (F_CPU / (100L * 1024L)) ;
208
 
209
        TIMSK0 |= (1 << OCIE0A);        // enable interrupt for OCR
210
#endif
211
}
212
 
213
 
214
//*****************************************************************************
215
// 
216
uint8_t get_key_press (uint8_t key_mask)
217
{
218
        uint8_t sreg = SREG;
219
 
220
        // disable all interrupts
221
        cli();
222
 
223
  key_mask &= key_press;        // read key(s)
224
  key_press ^= key_mask;        // clear key(s)
225
 
226
        SREG = sreg;    // restore status register
227
 
228
  return key_mask;
229
}
230
 
231
 
232
//*****************************************************************************
233
// 
234
uint8_t get_key_rpt (uint8_t key_mask)
235
{
236
        uint8_t sreg = SREG;
237
 
238
        // disable all interrupts
239
        cli();
240
 
241
  key_mask &= key_rpt;  // read key(s)
242
  key_rpt ^= key_mask;  // clear key(s)
243
 
244
        SREG = sreg;    // restore status register
245
 
246
  return key_mask;
247
}
248
 
249
 
250
//*****************************************************************************
251
// 
252
uint8_t get_key_short (uint8_t key_mask)
253
{
254
        uint8_t ret;
255
        uint8_t sreg = SREG;
256
 
257
        // disable all interrupts
258
        cli();
259
 
260
  ret = get_key_press (~key_state & key_mask);
261
 
262
        SREG = sreg;    // restore status register
263
 
264
  return ret;
265
}
266
 
267
 
268
//*****************************************************************************
269
// 
270
uint8_t get_key_long (uint8_t key_mask)
271
{
272
  return get_key_press (get_key_rpt (key_mask));
273
}
274
 
275
 
276
//*****************************************************************************
277
// 
278
uint8_t get_key_long2 (uint8_t key_mask)
279
{
280
  return get_key_press (get_key_rpt (key_press^key_mask));
281
}
282
 
283
 
284
//*****************************************************************************
285
// 
286
uint8_t get_key_long_rpt (uint8_t key_mask)
287
{
288
  return get_key_rpt (~key_press^key_mask);
289
}