Subversion Repositories group.electronics

Rev

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

Rev 22 Rev 23
Line 18... Line 18...
18
 
18
 
19
volatile uint8_t pcIntCurr = 0;
19
volatile uint8_t pcIntCurr = 0;
20
volatile uint8_t pcIntLast = 0;
20
volatile uint8_t pcIntLast = 0;
21
volatile uint8_t pcIntMask = 0;
21
volatile uint8_t pcIntMask = 0;
22
 
22
 
23
volatile uint8_t timer0_ovf = 0;
-
 
24
volatile uint8_t rot_time = 0;
-
 
25
volatile uint8_t rot_stat = 0;
23
volatile uint8_t rot_stat = 0;
26
volatile uint8_t rot_pos = 0;
24
volatile uint8_t rot_sent = 0;
27
 
25
 
28
struct{
26
struct{
29
  union {
27
  union {
30
    uint8_t data;
28
    uint8_t data;
31
    struct {
29
    struct {
Line 58... Line 56...
58
 
56
 
59
int main(void) {
57
int main(void) {
60
 
58
 
61
  ACSR |= (1<<ACD); // Disable analog comparator
59
  ACSR |= (1<<ACD); // Disable analog comparator
62
 
60
 
-
 
61
  /*
63
  // DDR : 1 = Output, 0 = Input
62
  DDR : 1 = Output, 0 = Input
64
  // PORT: 1 = Pullup for Input, otherwise set output
63
  PORT: 1 = Pullup for Input, otherwise set output
65
  // PIN : Read input pin
64
  PIN : Read input pin
-
 
65
  */
-
 
66
 
-
 
67
  /*
-
 
68
	PB0	- Output		- Status LED
-
 
69
	PB1	- Input, Pullup, PCINT1	- Rotary 1
-
 
70
	PB2	- Input, Pullup, PCINT2	- Rotary 2
-
 
71
  */
66
  DDRB		= 0B00000001;
72
  DDRB		= 0B00000001;
67
  PORTB 	= 0B00000110;
73
  PORTB 	= 0B00000110;
68
 
74
 
-
 
75
  /*
-
 
76
	PD4	- Input, Pullup 	- Rotary function select
-
 
77
	PD5	- Input, Pullup 	- Button
-
 
78
	PD6	- Input, Pullup 	- Button
-
 
79
	PD7	- Output		- Status LED
-
 
80
  */
69
  DDRD		= 0B10000000;
81
  DDRD		= 0B10000000;
70
  PORTD		= 0B01100000;
82
  PORTD		= 0B01110000;
71
 
83
 
72
  //PORTB |= (( 1 << PCINT1 ) | ( 1 << PCINT2 )); //turn on pullups
-
 
73
  PCMSK0 |= (( 1 << PCINT1 ) | ( 1 << PCINT2 )); //enable encoder pins interrupt sources
84
  PCMSK0 |= (( 1 << PCINT1 ) | ( 1 << PCINT2 )); //enable encoder pins interrupt sources
74
  PCICR |= ( 1 << PCIE0 ); //enable pin change interupts
85
  PCICR |= ( 1 << PCIE0 ); //enable pin change interupts
75
 
86
 
76
  // Setup timer0
87
  // Timers not used for the moment
-
 
88
  // Setup timer0 - Enable overflow, 8 times prescaler
77
  TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
89
  //TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
78
  TCNT0 = 0x00;				// Set Timer0 to 0
90
  //TCNT0 = 0x00;				// Set Timer0 to 0
79
  TCCR0B = (1<< CS01) ;	// /8 prescaler
91
  //TCCR0B = (1<< CS01) ;			// /8 prescaler
80
 
92
 
81
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
93
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
82
  _delay_ms(500);
94
  _delay_ms(500);
83
  usbDeviceConnect();
95
  usbDeviceConnect();
84
 
96
 
Line 89... Line 101...
89
 
101
 
90
  for(;;) {
102
  for(;;) {
91
    wdt_reset();
103
    wdt_reset();
92
    usbPoll();
104
    usbPoll();
93
 
105
 
94
	//if (pcIntMask);
-
 
95
	//	doInt();
-
 
96
 
-
 
97
    if(usbInterruptIsReady()){
106
    if(usbInterruptIsReady()){
98
        report.data = 0x05; // Center pad, little endian
107
        report.data = 0x05; // Center pad, little endian
99
 
108
 
-
 
109
	// Send our buttons 
100
	if (bit_is_clear(PIND, PD5))
110
	if (bit_is_clear(PIND, PD5))
101
		report.A = 1;
111
		report.A = 1;
102
	if (bit_is_clear(PIND, PD6))
112
	if (bit_is_clear(PIND, PD6))
103
		report.B = 1;
113
		report.B = 1;
104
 
114
 
-
 
115
	// Now work out what rotary to send, if any
-
 
116
	// Also record if we sent a positive response, 
-
 
117
	//  so we can send a '0' next time (if selected on PD4)
105
	if (rot_stat == 0x01 && rot_pos == 0) {
118
	if (rot_stat == 0x01 && rot_sent == 0) {
106
		report.rot1 = 1;
119
		report.rot1 = 1;
107
		rot_pos = 0;
120
		rot_sent = 1;
108
	} else if (rot_stat == 0x02 && rot_pos == 0) {
121
	} else if (rot_stat == 0x02 && rot_sent == 0) {
109
		report.rot2 = 1;
122
		report.rot2 = 1;
110
		rot_pos = 0;
123
		rot_sent = 1;
111
	} else {
124
	} else {
112
		rot_pos = 0;
125
		rot_sent = 0;
113
	}
126
	}
114
 
127
 
-
 
128
	// Reset our stat so ready for next turn
115
	rot_stat = 0;
129
	rot_stat = 0;
-
 
130
 
-
 
131
	// If our function select is set, dont bother
-
 
132
	//  sending a 'o' between consequtive 1's.
116
	cbi(PORTB, PB0);
133
	if (rbi(PIND, PD4))
-
 
134
		rot_sent = 0;
117
 
135
 
118
      /* called after every poll of the interrupt endpoint */
136
      /* called after every poll of the interrupt endpoint */
119
      usbSetInterrupt(&report, sizeof(report));
137
      usbSetInterrupt(&report, sizeof(report));
120
    }
138
    }
121
  }
139
  }
122
}
140
}
123
 
141
 
124
void doInt() {
142
void doInt() {
-
 
143
	// If rot_stat is not 0, we havn't sent
-
 
144
	//  our last results yet. Skip this click.
125
	if (rot_stat != 0) {
145
	if (rot_stat != 0) {
126
		pcIntMask = 0;
146
		pcIntMask = 0;
127
		return;
147
		return;
128
	}
148
	}
129
		
149
 
-
 
150
	// Check which pin caused the interrupt. If they both
-
 
151
	//  equal 0, the pin that interrupted is the direction
130
  if (rbi(pcIntCurr, PCINT1) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT1) ) {
152
  	if (rbi(pcIntCurr, PCINT1) == 0 
-
 
153
		&& rbi(pcIntCurr, PCINT2) == 0 
-
 
154
		&& rbi(pcIntMask, PCINT1) ) {
131
	rot_stat = 1;
155
			rot_stat = 1;
-
 
156
  	} else if (rbi(pcIntCurr, PCINT1) == 0 
132
	sbi(PORTB, PB0);
157
		&& rbi(pcIntCurr, PCINT2) == 0 
133
  } else if (rbi(pcIntCurr, PCINT1) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT2) ) {
158
		&& rbi(pcIntMask, PCINT2) ) {
134
	rot_stat = 2;
159
			rot_stat = 2;
135
	sbi(PORTB, PB0);
-
 
136
  }
160
  	}
-
 
161
 
-
 
162
	// Clear the mask so we know we've delth with it
137
  pcIntMask = 0;
163
	pcIntMask = 0;
138
}
164
}
139
 
165
 
-
 
166
/* Not used for the moment
140
ISR(TIMER0_OVF_vect) {
167
ISR(TIMER0_OVF_vect) {
141
	timer0_ovf++;
168
	timer0_ovf++;
142
 
-
 
143
	//xbi(PORTD, PD7);
-
 
144
 
-
 
145
}
169
}
-
 
170
*/
146
 
171
 
147
ISR(PCINT0_vect)
172
ISR(PCINT0_vect)
148
{
173
{
-
 
174
	// Save the state and work out which pin caused
-
 
175
	//  the interrupt to occur
149
  pcIntCurr = PINB;
176
	pcIntCurr = PINB;
150
  pcIntMask = pcIntCurr ^ pcIntLast;
177
	pcIntMask = pcIntCurr ^ pcIntLast;
151
  pcIntLast = pcIntCurr;
178
	pcIntLast = pcIntCurr;
152
	xbi(PORTD, PD7);
-
 
153
  doInt();
179
	doInt();
154
}
180
}
155
 
181