Subversion Repositories group.electronics

Rev

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

Rev 11 Rev 12
Line 5... Line 5...
5
#include <avr/wdt.h>
5
#include <avr/wdt.h>
6
 
6
 
7
#define F_CPU 12000000
7
#define F_CPU 12000000
8
#include <util/delay.h>
8
#include <util/delay.h>
9
 
9
 
-
 
10
#define DEPRESS_TIME	1
10
 
11
 
11
#define sbi(sfr, bit)   ((sfr) |= _BV(bit))		// Set bit 
12
#define sbi(sfr, bit)   ((sfr) |= _BV(bit))		// Set bit 
12
#define cbi(sfr, bit)   ((sfr) &= ~(_BV(bit)))		// Clear bit
13
#define cbi(sfr, bit)   ((sfr) &= ~(_BV(bit)))		// Clear bit
13
#define xbi(sfr, bit)   ((sfr) ^= _BV(bit))		// Flip bit
14
#define xbi(sfr, bit)   ((sfr) ^= _BV(bit))		// Flip bit
14
//#define rbi(sfr, bit)   ((sfr) ^= _BV(bit))		// Flip bit
-
 
15
#define rbi(sfr, bit)   (((sfr) >> (bit)) & 0x01)
15
#define rbi(sfr, bit)   (((sfr) >> (bit)) & 0x01)
16
 
16
 
17
volatile uint8_t pcIntTrig = 0;
-
 
18
volatile uint8_t pcIntCurr = 0;
17
volatile uint8_t pcIntCurr = 0;
19
volatile uint8_t pcIntLast = 0;
18
volatile uint8_t pcIntLast = 0;
20
volatile uint8_t pcIntMask = 0;
19
volatile uint8_t pcIntMask = 0;
21
volatile uint8_t pcIntChg = 0;
-
 
22
 
-
 
23
volatile uint8_t enc_dir=0;
-
 
24
volatile uint8_t enc_last=0;
-
 
25
volatile uint8_t enc_now=0;
-
 
26
 
20
 
27
void doInt();
21
void doInt();
28
 
22
 
29
int main() {
23
int main() {
30
	cbi(DDRB, PCINT2);
24
	cbi(DDRB, PCINT2);
Line 42... Line 36...
42
	_delay_ms(1000);
36
	_delay_ms(1000);
43
        cbi(PORTD, PD6);
37
        cbi(PORTD, PD6);
44
 
38
 
45
	for (;;) {
39
	for (;;) {
46
 
40
 
47
		if (pcIntTrig == 1) {
41
		if (pcIntMask)
48
			pcIntTrig = 0;
-
 
49
//			doInt();
42
			doInt();
50
		}
-
 
51
	}
43
	}
52
}
44
}
53
 
45
 
54
void doInt() {
46
void doInt() {
55
 
-
 
56
  if ((pcIntMask &= PCMSK) == 0)
-
 
57
    return;
-
 
58
 
-
 
59
  uint8_t biti;
-
 
60
  uint8_t i = 0;
-
 
61
  for (i=0; i<8; i++) {
-
 
62
    biti = 0x01 << i;
-
 
63
    if (biti & PCMSK) {
-
 
64
      pcIntChg |= (1 << i);
-
 
65
    }
-
 
66
  }
-
 
67
 
-
 
68
  xbi(PORTD, PD6);
47
  xbi(PORTD, PD6);
69
 
48
 
70
 
-
 
71
  if (rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntMask, PCINT2) ) {
49
  if (rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntMask, PCINT2) ) {
72
	cbi(PORTD, PD5);
50
	cbi(PORTD, PD5);	// Clear oppposite direction
-
 
51
 
73
	sbi(PORTD, PD4);
52
	sbi(PORTD, PD4);	// Depress current direction for DEPRESS time
74
	_delay_us(200);
53
	_delay_ms(DEPRESS_TIME);
75
	cbi(PORTD, PD4);	
54
	cbi(PORTD, PD4);	
76
  } else if (rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT3) ) {
55
  } else if (rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT3) ) {
77
	cbi(PORTD, PD4);
56
	cbi(PORTD, PD4);
-
 
57
 
78
	sbi(PORTD, PD5);
58
	sbi(PORTD, PD5);
79
	_delay_us(500);
59
	_delay_ms(DEPRESS_TIME);
80
	cbi(PORTD, PD5);
60
	cbi(PORTD, PD5);
81
  }
61
  }
82
 
62
 
83
 
-
 
-
 
63
  pcIntMask = 0;
84
}
64
}
85
 
65
 
86
 
66
 
87
ISR(PCINT_vect)
67
ISR(PCINT_vect)
88
{
68
{
89
  pcIntCurr = PINB;
69
  pcIntCurr = PINB;
90
  pcIntMask = pcIntCurr ^ pcIntLast;
70
  pcIntMask = pcIntCurr ^ pcIntLast;
91
  pcIntLast = pcIntCurr;
71
  pcIntLast = pcIntCurr;
92
  pcIntTrig = 1;
-
 
93
  doInt();
-
 
94
}
72
}