Subversion Repositories group.electronics

Rev

Rev 11 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 pfowler 1
#include <stdint.h>
2
#include <avr/pgmspace.h>
3
#include <avr/io.h>
4
#include <avr/interrupt.h>
5
#include <avr/wdt.h>
6
 
7
#define F_CPU 12000000
8
#include <util/delay.h>
9
 
12 pfowler 10
#define DEPRESS_TIME	1
11 pfowler 11
 
12
#define sbi(sfr, bit)   ((sfr) |= _BV(bit))		// Set bit 
13
#define cbi(sfr, bit)   ((sfr) &= ~(_BV(bit)))		// Clear bit
14
#define xbi(sfr, bit)   ((sfr) ^= _BV(bit))		// Flip bit
15
#define rbi(sfr, bit)   (((sfr) >> (bit)) & 0x01)
16
 
17
volatile uint8_t pcIntCurr = 0;
18
volatile uint8_t pcIntLast = 0;
19
volatile uint8_t pcIntMask = 0;
20
 
21
void doInt();
22
 
23
int main() {
24
	cbi(DDRB, PCINT2);
25
	cbi(DDRB, PCINT3);
26
	PORTB |= (( 1 << PCINT2 ) | ( 1 << PCINT3 )); //turn on pullups
27
	PCMSK |= (( 1 << PCINT2 ) | ( 1 << PCINT3 )); //enable encoder pins interrupt sources
28
	sei();
29
	GIMSK |= ( 1 << PCIE ); //enable pin change interupts
30
 
31
	DDRD |= ( 1 << PD4 );
32
	DDRD |= ( 1 << PD5 );
33
	DDRD |= ( 1 << PD6 );
34
 
35
	sbi(PORTD, PD6);
36
	_delay_ms(1000);
37
        cbi(PORTD, PD6);
38
 
39
	for (;;) {
40
 
12 pfowler 41
		if (pcIntMask)
42
			doInt();
11 pfowler 43
	}
44
}
45
 
46
void doInt() {
47
  xbi(PORTD, PD6);
48
 
12 pfowler 49
  if (rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntMask, PCINT2) ) {
50
	cbi(PORTD, PD5);	// Clear oppposite direction
11 pfowler 51
 
12 pfowler 52
	sbi(PORTD, PD4);	// Depress current direction for DEPRESS time
53
	_delay_ms(DEPRESS_TIME);
11 pfowler 54
	cbi(PORTD, PD4);	
55
  } else if (rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT3) ) {
56
	cbi(PORTD, PD4);
12 pfowler 57
 
11 pfowler 58
	sbi(PORTD, PD5);
12 pfowler 59
	_delay_ms(DEPRESS_TIME);
11 pfowler 60
	cbi(PORTD, PD5);
61
  }
62
 
12 pfowler 63
  pcIntMask = 0;
11 pfowler 64
}
65
 
66
 
67
ISR(PCINT_vect)
68
{
69
  pcIntCurr = PINB;
70
  pcIntMask = pcIntCurr ^ pcIntLast;
71
  pcIntLast = pcIntCurr;
72
}