Subversion Repositories group.electronics

Rev

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

Rev 16 Rev 18
Line 3... Line 3...
3
#include <avr/interrupt.h>
3
#include <avr/interrupt.h>
4
 
4
 
5
#define F_CPU 12000000
5
#define F_CPU 12000000
6
#include <util/delay.h>
6
#include <util/delay.h>
7
#include <avr/wdt.h>
7
#include <avr/wdt.h>
8
#include <avr/eeprom.h>
-
 
9
#include <usbdrv.h>
8
#include <usbdrv.h>
10
 
9
 
11
#include <stdlib.h>
10
#include <stdlib.h>
12
#include <string.h>
11
#include <string.h>
13
 
12
 
Line 21... Line 20...
21
volatile uint8_t pcIntCurr = 0;
20
volatile uint8_t pcIntCurr = 0;
22
volatile uint8_t pcIntLast = 0;
21
volatile uint8_t pcIntLast = 0;
23
volatile uint8_t pcIntMask = 0;
22
volatile uint8_t pcIntMask = 0;
24
 
23
 
25
volatile uint8_t timer0_ovf = 0;
24
volatile uint8_t timer0_ovf = 0;
26
 
-
 
27
uint8_t time_rot = 0;
25
volatile uint8_t time_rot = 0;
28
 
26
 
29
struct{
27
struct{
30
  union {
28
  union {
31
    uint8_t data;
29
    uint8_t data;
32
    struct {
30
    struct {
Line 62... Line 60...
62
  ACSR |= (1<<ACD); // Disable analog comparator
60
  ACSR |= (1<<ACD); // Disable analog comparator
63
 
61
 
64
  // DDR : 1 = Output, 0 = Input
62
  // DDR : 1 = Output, 0 = Input
65
  // PORT: 1 = Pullup for Input, otherwise set output
63
  // PORT: 1 = Pullup for Input, otherwise set output
66
  // PIN : Read input pin
64
  // PIN : Read input pin
67
  DDRB		= 0B00011111;
65
  DDRB		= 0B00010011;
68
  PORTB 	= 0B00011111;
66
  PORTB 	= 0B00011111;
69
 
67
 
70
  DDRD		= 0B00110000;
68
  DDRD		= 0B01110000;
71
  PORTD		= 0B00110000;
69
  PORTD		= 0B00110000;
72
 
70
 
-
 
71
  PORTB |= (( 1 << PCINT2 ) | ( 1 << PCINT3 )); //turn on pullups
-
 
72
  PCMSK |= (( 1 << PCINT2 ) | ( 1 << PCINT3 )); //enable encoder pins interrupt sources
-
 
73
  GIMSK |= ( 1 << PCIE ); //enable pin change interupts
-
 
74
 
73
  // Setup timer0
75
  // Setup timer0
74
  TIMSK = (1<<TOIE0);			// Eable timer overflow for Timer0
76
  TIMSK = (1<<TOIE0);			// Eable timer overflow for Timer0
75
  TCNT0 = 0x00;				// Set Timer0 to 0
77
  TCNT0 = 0x00;				// Set Timer0 to 0
76
  TCCR0B = (1<< CS02) | (1<<CS00);	// /1024 prescaler
78
  TCCR0B = (1<< CS01) | (1<<CS00);	// /1024 prescaler
77
 
79
 
78
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
80
  usbDeviceDisconnect();  /* enforce re-enumeration, do this while interrupts are disabled! */
79
  _delay_ms(500);
81
  _delay_ms(500);
80
  usbDeviceConnect();
82
  usbDeviceConnect();
81
 
83
 
Line 86... Line 88...
86
 
88
 
87
  for(;;) {
89
  for(;;) {
88
    wdt_reset();
90
    wdt_reset();
89
    usbPoll();
91
    usbPoll();
90
 
92
 
91
	if (pcIntMask);
93
//	if (pcIntMask);
92
		doInt();
94
//		doInt();
93
 
95
 
94
	if (!time_rot) {
96
	if (!time_rot) {
95
		report.rot1 = 0;
97
		report.rot1 = 0;
96
		report.rot2 = 0;
98
		report.rot2 = 0;
-
 
99
		cbi(PORTD, PD6);
97
	}	
100
	}	
98
 
101
 
99
    if(usbInterruptIsReady()){
102
    if(usbInterruptIsReady()){
100
      report.data = 0x05; // Center pad, little endian
103
      //report.data = 0x05; // Center pad, little endian
101
 
104
 
-
 
105
/*
102
	if (bit_is_clear(PINB, PB0)) 
106
	if (bit_is_clear(PINB, PB0)) 
103
		report.X++;
107
		report.X++;
104
	if (bit_is_clear(PINB, PB1))
108
	if (bit_is_clear(PINB, PB1))
105
		report.Y--;
109
		report.Y--;
106
	if (bit_is_clear(PINB, PB2))
110
	if (bit_is_clear(PINB, PB2))
107
		report.X--;
111
		report.X--;
108
	if (bit_is_clear(PINB, PB3))
112
	if (bit_is_clear(PINB, PB3))
109
		report.Y++;
113
		report.Y++;
110
 
114
 
111
	if (bit_is_clear(PIND, PD4))
115
	if (bit_is_clear(PIND, PD4))
112
		report.A = 1;
116
		report.A = 1; */
113
	if (bit_is_clear(PIND, PD5))
117
	if (bit_is_clear(PIND, PD5))
114
		report.B = 1;
118
		report.B = 1;
115
 
119
 
116
      /* called after every poll of the interrupt endpoint */
120
      /* called after every poll of the interrupt endpoint */
117
      usbSetInterrupt(&report, sizeof(report));
121
      usbSetInterrupt(&report, sizeof(report));
Line 119... Line 123...
119
  }
123
  }
120
}
124
}
121
 
125
 
122
void doInt() {
126
void doInt() {
123
  if (rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntMask, PCINT2) ) {
127
  if (rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntMask, PCINT2) ) {
124
	time_rot = 5;
128
	time_rot = 60;
125
	report.rot1 = 1;
129
	report.rot1 = 1;
126
	report.rot2 = 0;
130
	report.rot2 = 0;
-
 
131
	sbi(PORTD, PD6);
127
  } else if (rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT3) ) {
132
  } else if (rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT3) ) {
128
	time_rot = 5;
133
	time_rot = 60;
129
	report.rot1 = 0;
134
	report.rot1 = 0;
130
	report.rot2 = 1;
135
	report.rot2 = 1;
-
 
136
	sbi(PORTD, PD6);
131
  }
137
  }
132
 
138
 
133
  pcIntMask = 0;
139
  pcIntMask = 0;
134
}
140
}
135
 
141
 
Line 144... Line 150...
144
ISR(PCINT_vect)
150
ISR(PCINT_vect)
145
{
151
{
146
  pcIntCurr = PINB;
152
  pcIntCurr = PINB;
147
  pcIntMask = pcIntCurr ^ pcIntLast;
153
  pcIntMask = pcIntCurr ^ pcIntLast;
148
  pcIntLast = pcIntCurr;
154
  pcIntLast = pcIntCurr;
-
 
155
  doInt();
149
}
156
}
150
 
157