Subversion Repositories group.electronics

Rev

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

Rev 21 Rev 22
Line 11... Line 11...
11
#include <string.h>
11
#include <string.h>
12
 
12
 
13
#include "config.h"
13
#include "config.h"
14
#include "hiddesc.h"
14
#include "hiddesc.h"
15
 
15
 
16
#define	DEPRESS_TIME	1
-
 
17
 
16
 
18
void doInt(void);
17
void doInt(void);
19
 
18
 
20
volatile uint8_t pcIntCurr = 0;
19
volatile uint8_t pcIntCurr = 0;
21
volatile uint8_t pcIntLast = 0;
20
volatile uint8_t pcIntLast = 0;
22
volatile uint8_t pcIntMask = 0;
21
volatile uint8_t pcIntMask = 0;
23
 
22
 
24
volatile uint8_t timer0_ovf = 0;
23
volatile uint8_t timer0_ovf = 0;
-
 
24
volatile uint8_t rot_time = 0;
-
 
25
volatile uint8_t rot_stat = 0;
25
volatile uint8_t time_rot = 0;
26
volatile uint8_t rot_pos = 0;
26
 
27
 
27
struct{
28
struct{
28
  union {
29
  union {
29
    uint8_t data;
30
    uint8_t data;
30
    struct {
31
    struct {
Line 63... Line 64...
63
  // PORT: 1 = Pullup for Input, otherwise set output
64
  // PORT: 1 = Pullup for Input, otherwise set output
64
  // PIN : Read input pin
65
  // PIN : Read input pin
65
  DDRB		= 0B00000001;
66
  DDRB		= 0B00000001;
66
  PORTB 	= 0B00000110;
67
  PORTB 	= 0B00000110;
67
 
68
 
68
  DDRD		= 0B00000000;
69
  DDRD		= 0B10000000;
69
  PORTD		= 0B01100000;
70
  PORTD		= 0B01100000;
70
 
71
 
71
  //PORTB |= (( 1 << PCINT1 ) | ( 1 << PCINT2 )); //turn on pullups
72
  //PORTB |= (( 1 << PCINT1 ) | ( 1 << PCINT2 )); //turn on pullups
72
  PCMSK0 |= (( 1 << PCINT1 ) | ( 1 << PCINT2 )); //enable encoder pins interrupt sources
73
  PCMSK0 |= (( 1 << PCINT1 ) | ( 1 << PCINT2 )); //enable encoder pins interrupt sources
73
  PCICR |= ( 1 << PCIE0 ); //enable pin change interupts
74
  PCICR |= ( 1 << PCIE0 ); //enable pin change interupts
74
 
75
 
75
  // Setup timer0
76
  // Setup timer0
76
  TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
77
  TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
77
  TCNT0 = 0x00;				// Set Timer0 to 0
78
  TCNT0 = 0x00;				// Set Timer0 to 0
78
  TCCR0B = (1<< CS01) | (1<<CS00);	// /1024 prescaler
79
  TCCR0B = (1<< CS01) ;	// /8 prescaler
79
 
80
 
80
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
81
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
81
  _delay_ms(500);
82
  _delay_ms(500);
82
  usbDeviceConnect();
83
  usbDeviceConnect();
83
 
84
 
Line 88... Line 89...
88
 
89
 
89
  for(;;) {
90
  for(;;) {
90
    wdt_reset();
91
    wdt_reset();
91
    usbPoll();
92
    usbPoll();
92
 
93
 
93
//	if (pcIntMask);
94
	//if (pcIntMask);
94
//		doInt();
95
	//	doInt();
95
 
-
 
96
	if (!time_rot) {
-
 
97
		report.rot1 = 0;
-
 
98
		report.rot2 = 0;
-
 
99
		cbi(PORTB, PB0);
-
 
100
	}	
-
 
101
 
96
 
102
    if(usbInterruptIsReady()){
97
    if(usbInterruptIsReady()){
103
      //report.data = 0x05; // Center pad, little endian
98
        report.data = 0x05; // Center pad, little endian
104
 
99
 
105
/*
-
 
106
	if (bit_is_clear(PINB, PB0)) 
-
 
107
		report.X++;
-
 
108
	if (bit_is_clear(PINB, PB1))
-
 
109
		report.Y--;
-
 
110
	if (bit_is_clear(PINB, PB2))
-
 
111
		report.X--;
-
 
112
	if (bit_is_clear(PINB, PB3))
-
 
113
		report.Y++;
-
 
114
*/
-
 
115
	if (bit_is_clear(PIND, PD5))
100
	if (bit_is_clear(PIND, PD5))
116
		report.A = 1;
101
		report.A = 1;
117
 
-
 
118
	if (bit_is_clear(PIND, PD6))
102
	if (bit_is_clear(PIND, PD6))
119
		report.B = 1;
103
		report.B = 1;
120
 
104
 
-
 
105
	if (rot_stat == 0x01 && rot_pos == 0) {
-
 
106
		report.rot1 = 1;
-
 
107
		rot_pos = 0;
-
 
108
	} else if (rot_stat == 0x02 && rot_pos == 0) {
-
 
109
		report.rot2 = 1;
-
 
110
		rot_pos = 0;
-
 
111
	} else {
-
 
112
		rot_pos = 0;
-
 
113
	}
-
 
114
 
-
 
115
	rot_stat = 0;
-
 
116
	cbi(PORTB, PB0);
-
 
117
 
121
      /* called after every poll of the interrupt endpoint */
118
      /* called after every poll of the interrupt endpoint */
122
      usbSetInterrupt(&report, sizeof(report));
119
      usbSetInterrupt(&report, sizeof(report));
123
    }
120
    }
124
  }
121
  }
125
}
122
}
126
 
123
 
127
void doInt() {
124
void doInt() {
-
 
125
	if (rot_stat != 0) {
-
 
126
		pcIntMask = 0;
-
 
127
		return;
-
 
128
	}
-
 
129
		
128
  if (rbi(pcIntCurr, PCINT1) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT1) ) {
130
  if (rbi(pcIntCurr, PCINT1) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT1) ) {
129
	time_rot = 60;
-
 
130
	report.rot1 = 1;
131
	rot_stat = 1;
131
	report.rot2 = 0;
-
 
132
	sbi(PORTB, PB0);
132
	sbi(PORTB, PB0);
133
  } else if (rbi(pcIntCurr, PCINT1) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT2) ) {
133
  } else if (rbi(pcIntCurr, PCINT1) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT2) ) {
134
	time_rot = 60;
-
 
135
	report.rot1 = 0;
-
 
136
	report.rot2 = 1;
134
	rot_stat = 2;
137
	sbi(PORTB, PB0);
135
	sbi(PORTB, PB0);
138
  }
136
  }
139
 
-
 
140
  pcIntMask = 0;
137
  pcIntMask = 0;
141
}
138
}
142
 
139
 
143
ISR(TIMER0_OVF_vect) {
140
ISR(TIMER0_OVF_vect) {
144
	timer0_ovf++;
141
	timer0_ovf++;
145
 
142
 
146
	if (time_rot) {
143
	//xbi(PORTD, PD7);
147
		time_rot--;
-
 
148
	}
144
 
149
}
145
}
150
 
146
 
151
ISR(PCINT0_vect)
147
ISR(PCINT0_vect)
152
{
148
{
153
  pcIntCurr = PINB;
149
  pcIntCurr = PINB;
154
  pcIntMask = pcIntCurr ^ pcIntLast;
150
  pcIntMask = pcIntCurr ^ pcIntLast;
155
  pcIntLast = pcIntCurr;
151
  pcIntLast = pcIntCurr;
-
 
152
	xbi(PORTD, PD7);
156
  doInt();
153
  doInt();
157
}
154
}
158
 
155