Subversion Repositories group.electronics

Rev

Rev 29 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 29 Rev 30
Line 13... Line 13...
13
#include "config.h"
13
#include "config.h"
14
#include "hiddesc.h"
14
#include "hiddesc.h"
15
 
15
 
16
 
16
 
17
void doInt(uint8_t pcint);
17
void doInt(uint8_t pcint);
18
int getKey(void);
18
uint8_t getKey(void);
19
uint8_t analogRead(uint8_t pin);
19
uint8_t analogRead(uint8_t pin);
20
 
20
 
21
volatile uint8_t pcIntCurr[3] = {0,0,0};
21
volatile uint8_t pcIntCurr[3] = {0,0,0};
22
volatile uint8_t pcIntLast[3] = {0,0,0};
22
volatile uint8_t pcIntLast[3] = {0,0,0};
23
volatile uint8_t pcIntMask[3] = {0,0,0};
23
volatile uint8_t pcIntMask[3] = {0,0,0};
Line 77... Line 77...
77
 
77
 
78
int main(void) {
78
int main(void) {
79
 
79
 
80
  ACSR |= (1<<ACD); // Disable analog comparator
80
  ACSR |= (1<<ACD); // Disable analog comparator
81
 
81
 
82
  //ADMUX = (1<<ADLAR) | (0<<REFS0);
82
  ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
83
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) | (0<<ADSC);
83
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
84
 
84
 
85
  /*
85
  /*
86
  DDR : 1 = Output, 0 = Input
86
  DDR : 1 = Output, 0 = Input
87
  PORT: 1 = Pullup for Input, otherwise set output
87
  PORT: 1 = Pullup for Input, otherwise set output
88
  PIN : Read input pin
88
  PIN : Read input pin
89
  */
89
  */
90
 
90
 
91
  /*
91
  /*
92
	PB0	- Output 		- Keypad 5
92
	PB0	- Output 		- Keypad 1
93
	PB1	- Output 		- Keypad 6
93
	PB1	- Output 		- Keypad 2
94
	PB2	- Output 		- Keypad 7
94
	PB2	- Output 		- Keypad 3
95
	PB3	- Output 		- Keypad 8
95
	PB3	- Output 		- Keypad 4
96
	PB4	- Input, Pullup		- Function select
96
	PB4	- Input, Pullup		- Function select
97
  */
97
  */
98
  DDRB		= 0B00001111;
98
  DDRB		= 0B00001111;
99
  PORTB 	= 0B00011111;
99
  PORTB 	= 0B00011111;
100
 
100
 
101
  /*
101
  /*
102
	PD0	- Input, Pullup, PCINT16	- Rotary 1a
102
	PD0	- Input, Pullup, PCINT16	- Rotary 1a
103
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
103
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
104
 
104
 
105
 
105
 
106
	PD4	- Input				- Keypad 1
106
	PD4	- Input, Pullup		- Keypad 5
107
	PD5	- Input				- Keypad 2
107
	PD5	- Input, Pullup		- Keypad 6
108
	PD6	- Input				- Keypad 3
108
	PD6	- Input, Pullup		- Keypad 7
109
	PD7	- Input				- Keypad 4
109
	PD7	- Input, Pullup		- Keypad 8
110
  */
110
  */
111
  DDRD		= 0B00000000;
111
  DDRD		= 0B00000000;
112
  PORTD		= 0B11110011;
112
  PORTD		= 0B11110011;
113
 
113
 
114
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
114
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
Line 132... Line 132...
132
  for(;;) {
132
  for(;;) {
133
    wdt_reset();
133
    wdt_reset();
134
    usbPoll();
134
    usbPoll();
135
 
135
 
136
    if(usbInterruptIsReady()){
136
    if(usbInterruptIsReady()){
137
	report.data1[0] = analogRead(0);
137
	report.data1[0] = (-128 + analogRead(0));
138
	report.data1[1] = analogRead(1);
138
	report.data1[1] = (-128 + analogRead(1));
139
	report.data2 = 0x0000;
139
	report.data2 = 0x0000;
140
 
140
 
141
 
141
 
142
	int key = getKey();
142
	uint8_t key = getKey();
143
	if (key > -1)
143
	if (key > 0)
144
		report.data2 |= (1 << key);
144
		report.data2 |= (1 << (key -1));
145
 
145
 
146
	// Now work out what rotary to send, if any
146
	// Now work out what rotary to send, if any
147
	// Also record if we sent a positive response, 
147
	// Also record if we sent a positive response, 
148
	//  so we can send a '0' next time (if selected on PD4)
148
	//  so we can send a '0' next time (if selected on PD4)
149
	
149
	
Line 163... Line 163...
163
	// If our function select is set, dont bother
163
	// If our function select is set, dont bother
164
	//  sending a 'o' between consequtive 1's.
164
	//  sending a 'o' between consequtive 1's.
165
	if (rbi(PINB, PB4))
165
	if (rbi(PINB, PB4))
166
		rot_sent[0] = 0;
166
		rot_sent[0] = 0;
167
 
167
 
-
 
168
	if (rbi(PINB, PB5))
-
 
169
		rot_sent[1] = 0;
-
 
170
 
168
      /* called after every poll of the interrupt endpoint */
171
      /* called after every poll of the interrupt endpoint */
169
      usbSetInterrupt(&report, sizeof(report));
172
      usbSetInterrupt(&report, sizeof(report));
170
    }
173
    }
171
  }
174
  }
172
}
175
}
173
 
176
 
174
uint8_t analogRead(uint8_t pin) {
177
uint8_t analogRead(uint8_t pin) {
175
	//ADMUX |= (1<<pin);
178
	//ADMUX |= (1<<ADLAR) | (0<<REFS0) |(1<<0);
176
	ADMUX = (1<<ADLAR) | (0<<REFS0) | (pin & 0x0f);
179
	ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
177
	ADCSRA |= (1<<ADSC);		// Start converting
180
	ADCSRA |= (1<<ADSC);		// Start converting
178
 
181
 
179
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
182
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
180
	uint8_t result = ADCH;
183
	uint8_t result = ADCH;
181
	ADCSRA |= (0<<ADSC);		// Stop converting
184
	//ADCSRA |= (0<<ADSC);		// Stop converting
182
 
185
 
183
	// result = result * 5/255;		// wtf?
186
	// result = result * 5/255;		// wtf?
184
	return result;
187
	return result;
185
}
188
}
186
 
189
 
187
int getKey() {
190
uint8_t getKey() {
188
	uint8_t row = 0;
191
	uint8_t col, row = 0;
189
	int key = -1;
192
	uint8_t key = 0;
190
	uint8_t n = 0;
193
	uint8_t n = 1;
191
 
194
 
192
	for (row=0; row<=3; row++) {
195
	for (row=0; row<=3; row++) {
193
		cbi(PORTB, row);
196
		cbi(PORTB, row);
194
		if (rbi(PIND, 4) == 0) key = n; n++;
197
		_delay_us(10);				// Wait for the port to change
-
 
198
 
195
		if (rbi(PIND, 5) == 0) key = n; n++;
199
		for (col=5; col<=7; col++) {
196
		if (rbi(PIND, 6) == 0) key = n; n++;
200
			if (rbi(PIND, col) == 0)
197
		if (rbi(PIND, 7) == 0) key = n; n++;
201
				key = n;
-
 
202
			n++;
-
 
203
		}	
-
 
204
 
198
		sbi(PORTB, row);
205
		sbi(PORTB, row);
199
	}
206
	}
200
	return key;
207
	return key;
201
}
208
}
202
 
209