Subversion Repositories group.electronics

Rev

Rev 12 | Go to most recent revision | Details | 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
 
10
 
11
#define sbi(sfr, bit)   ((sfr) |= _BV(bit))		// Set bit 
12
#define cbi(sfr, bit)   ((sfr) &= ~(_BV(bit)))		// Clear bit
13
#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)
16
 
17
volatile uint8_t pcIntTrig = 0;
18
volatile uint8_t pcIntCurr = 0;
19
volatile uint8_t pcIntLast = 0;
20
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
 
27
void doInt();
28
 
29
int main() {
30
	cbi(DDRB, PCINT2);
31
	cbi(DDRB, PCINT3);
32
	PORTB |= (( 1 << PCINT2 ) | ( 1 << PCINT3 )); //turn on pullups
33
	PCMSK |= (( 1 << PCINT2 ) | ( 1 << PCINT3 )); //enable encoder pins interrupt sources
34
	sei();
35
	GIMSK |= ( 1 << PCIE ); //enable pin change interupts
36
 
37
	DDRD |= ( 1 << PD4 );
38
	DDRD |= ( 1 << PD5 );
39
	DDRD |= ( 1 << PD6 );
40
 
41
	sbi(PORTD, PD6);
42
	_delay_ms(1000);
43
        cbi(PORTD, PD6);
44
 
45
	for (;;) {
46
 
47
		if (pcIntTrig == 1) {
48
			pcIntTrig = 0;
49
//			doInt();
50
		}
51
	}
52
}
53
 
54
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);
69
 
70
 
71
  if (rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntMask, PCINT2) ) {
72
	cbi(PORTD, PD5);
73
	sbi(PORTD, PD4);
74
	_delay_us(200);
75
	cbi(PORTD, PD4);	
76
  } else if (rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT3) ) {
77
	cbi(PORTD, PD4);
78
	sbi(PORTD, PD5);
79
	_delay_us(500);
80
	cbi(PORTD, PD5);
81
  }
82
 
83
 
84
}
85
 
86
 
87
ISR(PCINT_vect)
88
{
89
  pcIntCurr = PINB;
90
  pcIntMask = pcIntCurr ^ pcIntLast;
91
  pcIntLast = pcIntCurr;
92
  pcIntTrig = 1;
93
  doInt();
94
}