Subversion Repositories group.electronics

Rev

Rev 12 | 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
 
15 pfowler 21
volatile uint8_t timer0_ovf = 0;
22
volatile uint8_t time_rot = 0;
23
 
11 pfowler 24
void doInt();
25
 
26
int main() {
27
	cbi(DDRB, PCINT2);
28
	cbi(DDRB, PCINT3);
15 pfowler 29
 
30
	TIMSK = (1<<TOIE0);                   // Eable timer overflow for Timer0
31
	TCNT0 = 0x00;                         // Set Timer0 to 0
32
	TCCR0B = (1<< CS02) | (1<<CS00);      // /1024 prescaler
33
 
11 pfowler 34
	PORTB |= (( 1 << PCINT2 ) | ( 1 << PCINT3 )); //turn on pullups
35
	PCMSK |= (( 1 << PCINT2 ) | ( 1 << PCINT3 )); //enable encoder pins interrupt sources
36
	sei();
37
	GIMSK |= ( 1 << PCIE ); //enable pin change interupts
38
 
39
	DDRD |= ( 1 << PD4 );
40
	DDRD |= ( 1 << PD5 );
41
	DDRD |= ( 1 << PD6 );
42
 
43
	sbi(PORTD, PD6);
44
	_delay_ms(1000);
45
        cbi(PORTD, PD6);
46
 
47
	for (;;) {
48
 
15 pfowler 49
		if (!time_rot) {
50
			cbi(PORTD, PD4);
51
			cbi(PORTD, PD5);
52
		}
53
 
12 pfowler 54
		if (pcIntMask)
55
			doInt();
11 pfowler 56
	}
57
}
58
 
59
void doInt() {
60
  xbi(PORTD, PD6);
61
 
12 pfowler 62
  if (rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntMask, PCINT2) ) {
15 pfowler 63
	cbi(PORTD, PD5);
64
	sbi(PORTD, PD4);
65
	time_rot = 5;
11 pfowler 66
  } else if (rbi(pcIntCurr, PCINT3) == 0 && rbi(pcIntCurr, PCINT2) == 0 && rbi(pcIntMask, PCINT3) ) {
67
	cbi(PORTD, PD4);
68
	sbi(PORTD, PD5);
15 pfowler 69
	time_rot = 5;
11 pfowler 70
  }
71
 
12 pfowler 72
  pcIntMask = 0;
11 pfowler 73
}
74
 
15 pfowler 75
ISR(TIMER0_OVF_vect) {
76
        timer0_ovf++;
11 pfowler 77
 
15 pfowler 78
        if (time_rot) {
79
                time_rot--;
80
        }
81
}
82
 
83
 
11 pfowler 84
ISR(PCINT_vect)
85
{
86
  pcIntCurr = PINB;
87
  pcIntMask = pcIntCurr ^ pcIntLast;
88
  pcIntLast = pcIntCurr;
89
}