Subversion Repositories group.electronics

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
82 pfowler 1
/* Name: main.c
2
 * Project: EasyLogger
3
 * Author: Christian Starkjohann
4
 * Creation Date: 2006-04-23
5
 * Tabsize: 4
6
 * Copyright: (c) 2006 by OBJECTIVE DEVELOPMENT Software GmbH
7
 * License: Proprietary, free under certain conditions. See Documentation.
8
 * This Revision: $Id$
9
 */
10
 
11
#include <avr/io.h>
12
#include <avr/wdt.h>
13
#include <avr/eeprom.h>
14
#include <avr/interrupt.h>
15
#include <avr/pgmspace.h>
16
#include <util/delay.h>
17
 
18
#include "config.h"
19
 
20
#ifndef NULL
21
#define NULL    ((void *)0)
22
#endif
23
 
24
/* ------------------------------------------------------------------------- */
25
 
26
uint8_t getKey(void);
27
 
28
struct {
29
	union {
30
		int16_t axis[2];
31
		struct {
32
			int16_t axis0:16;
33
			int16_t axis1:16;
34
		};
35
		uint8_t buttons;
36
		struct {
37
			int8_t b1:1;
38
			int8_t b2:2;
39
			int8_t b3:3;
40
			int8_t b4:4;
41
			int8_t b5:5;
42
			int8_t b6:6;
43
			int8_t b7:7;
44
			int8_t b8:8;
45
		};
46
	};
47
} reportBuffer;
48
 
49
/*
50
volatile struct {
51
        uint8_t current;
52
        uint8_t last;
53
        uint8_t mask;
54
} pcInt[1];
55
*/
56
 
57
 
58
volatile struct {
59
	uint8_t buttons;
60
	uint8_t waitup;
61
	uint8_t timer;
62
} debounce;
63
 
64
 
65
volatile uint8_t tmr0_ovf = 0;
66
volatile uint32_t systime = 0;
67
 
68
/* ------------------------------------------------------------------------- */
69
 
70
int main(void) {
71
 
72
  /*
73
  DDR : 1 = Output, 0 = Input
74
  PORT: 1 = Pullup for Input, otherwise set output
75
  PIN : Read input pin
76
  */
77
  /*
78
 
79
        PA0     - Input, Pullup - ButtonPad 1
80
        PA1     - Input, Pullup - ButtonPad 0
81
        PA2     - Output        - Reset
82
  */
83
  DDRA          = 0B00000000;
84
  PORTA         = 0B00000011;
85
  /*
86
        PB0     - Output                - LED 0
87
        PB1     - Output                - LED 1
88
        PB2     - Output                - LED 2
89
        PB3     - Output                - LED 3
90
        PB4     - Output                - LED 4
91
        PB5     - Output                - LED 5
92
        PB6     - Output                - LED 6
93
        PB7     - Output                - LED 7
94
  */
95
  DDRB          = 0B11111111;
96
  PORTB         = 0B00000000;
97
  /*
98
 
99
        PD0     - Output        - ButtonPad Gnd1
100
        PD1     - Output        - ButtonPad Gnd2
101
        PD2     - Output        - USB D+
102
        PD3     - Output        - USB D-
103
        PD4     - Input, Pullup - ButtonPad 2
104
        PD5     - Input, Pullup - ButtonPad 3
105
        PD6     - Input, Pullup - Select Switch
106
  */
107
  DDRD          = 0B00000011;
108
  PORTD         = 0B01110011;
109
 
110
 
111
    TIMSK = (1<<TOIE0);                    // Enable timer overflow
112
    TCNT0 = 0x00;                           // Set Timer0 initial value to 0
113
    TCCR0B = (1<< CS01) ;                   // /1 prescaler
114
 
115
    sei();
116
 
117
    reportBuffer.axis0 = 0;
118
    reportBuffer.axis1 = 0;
119
    reportBuffer.buttons = 0;
120
 
121
	PORTB = 255;
122
 
123
	_delay_ms(20);
124
 
125
	uint8_t currleds = 0;
126
	PORTB = 128;
127
 
128
    for(;;){    /* main event loop */
129
        wdt_reset();
130
 
131
	uint8_t pressed = getKey();
132
 
133
	// Deboucing
134
	// When i key first goes down, wait 5 ms, check it again to see if its still down
135
	if (pressed && debounce.buttons == 0 && debounce.timer == 0) {
136
		debounce.buttons = pressed;
137
		debounce.timer = 5;
138
	}
139
 
140
	// The key has come up
141
	if (pressed != debounce.buttons) {
142
		debounce.buttons = 0;
143
		debounce.timer = 0;
144
		debounce.waitup = 0;
145
	}
146
 
147
	// Debounce timer is up, process our button	
148
	if (debounce.buttons && debounce.timer == 0 && debounce.waitup != 1) {
149
		uint8_t i = 0;
150
		for (i=0; i<=7; i++) {
151
			// Button pressed and the led is currently on
152
			if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 1 ) {
153
                                if ( i == 6 && rbi(currleds, 7) != 1)  //Dont turn off com1 if no comm2
154
                                        break;
155
 
156
                                if ( i == 7 && rbi(currleds, 6) != 1)  //Dont turn off com2 if no comm1
157
                                        break;
158
 
159
				cbi(currleds, i);
160
			// Button is pressed and led is currently off
161
			 } else if ( rbi(debounce.buttons, i) == 1 && rbi(currleds, i) == 0 ) {
162
				if ( i == 6 && rbi(currleds, 7) == 1)  //Turn on comm2, turn off comm1
163
					cbi(currleds,7);
164
 
165
				if ( i == 7 && rbi(currleds, 6) == 1)  //Turn on comm1, turn off comm2
166
					cbi(currleds,6);
167
 
168
				sbi(currleds, i);
169
			}
170
		}
171
		PORTB = currleds;
172
		reportBuffer.buttons = debounce.buttons;
83 pfowler 173
		// Set debounce to wait to button up
82 pfowler 174
		debounce.waitup = 1;
175
	}
176
 
177
	_delay_ms(1);
178
    }
179
    return 0;
180
}
181
 
182
// Gnd = PD0, PD1
183
// Btn = PA1, PA0, PD4, PD5
184
uint8_t getKey() {
185
        uint8_t key = 0;
186
 
187
        cbi(PORTD, 0);
188
        _delay_us(10);        // Wait for the port change
189
        if (rbi(PIND, 5) == 0) key = 1;
190
        if (rbi(PIND, 4) == 0) key = 2;
191
        if (rbi(PINA, 0) == 0) key = 4;
192
        if (rbi(PINA, 1) == 0) key = 8;
193
        sbi(PORTD, 0);
194
 
195
        cbi(PORTD, 1);
196
        _delay_us(10);
197
        if (rbi(PIND, 5) == 0) key = 16;
198
        if (rbi(PIND, 4) == 0) key = 32;
199
        if (rbi(PINA, 0) == 0) key = 64;
200
        if (rbi(PINA, 1) == 0) key = 128;
201
        sbi(PORTD, 1);
202
 
203
        return key;
204
}
205
 
206
/*
207
void pcInterrupt(uint8_t pcint) {
208
 
209
        switch (pcint) {
210
                case 0: pcInt[pcint].current = PINB; break;
211
        }
212
        pcInt[pcint].mask = pcInt[pcint].current ^ pcInt[pcint].last;
213
        pcInt[pcint].last = pcInt[pcint].current;
214
 
215
        if (pcInt[pcint].mask == 0)
216
		return;
217
 
218
	// PCINT logic here
219
 
220
        // Clear the mask so we know we've delth with it
221
        pcInt[pcint].mask = 0;
222
}
223
 
224
ISR(PCINT0_vect) {
225
        pcInterrupt(0);
226
}
227
*/
228
 
229
ISR(TIMER0_OVF_vect) {
230
        tmr0_ovf++;
231
 
232
	//16.5Mhz, 1ms = 50 ovf
233
 
234
        if (tmr0_ovf>=5) {
235
                systime++;
236
                tmr0_ovf = 0;
237
 
238
		if (debounce.timer != 0)
239
			debounce.timer--;
240
        }
241
}