Subversion Repositories group.electronics

Rev

Rev 43 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
42 pfowler 1
#include <avr/io.h>
2
#include <avr/pgmspace.h>
3
#include <avr/interrupt.h>
4
 
5
#define F_CPU 12000000
6
#include <util/delay.h>
7
#include <avr/wdt.h>
8
 
9
#include <stdlib.h>
10
#include <string.h>
11
 
12
#include "wire.h"
13
#include "lcd.h"
14
 
15
 
16
volatile uint8_t pcIntCurr[3] = {0,0,0};
17
volatile uint8_t pcIntLast[3] = {0,0,0};
18
volatile uint8_t pcIntMask[3] = {0,0,0};
19
 
20
int main(void) {
21
 
22
  ACSR |= (1<<ACD); // Disable analog comparator
23
 
24
  /*
25
	Setup ADC
26
	ADMUX: 8 bit mode, Avcc ref
27
	ADCSRA: Enable, 128 prescale
28
  */
29
  ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
30
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
31
 
32
  /*
33
  DDR : 1 = Output, 0 = Input
34
  PORT: 1 = Pullup for Input, otherwise set output
35
  PIN : Read input pin
36
  */
37
 
38
  /*
39
	PB0	- Output 		- Keypad 2
40
	PB1	- Output 		- Keypad 7
41
	PB2	- Output 		- Keypad 6
42
	PB3	- Output 		- Keypad 4
43
	PB4	- Input, Pullup		- Function select
44
	PB5	- Input, Pullup		- Function select
45
  */
46
  DDRB		= 0B00001111;
47
  PORTB 	= 0B00111111;
48
 
49
  /*
50
	PD0	- Input, Pullup, PCINT16	- Rotary 1a
51
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
52
 
53
 
54
	PD4	- Output		- Keypad select status led
55
	PD5	- Input, Pullup		- Keypad 3
56
	PD6	- Input, Pullup		- Keypad 1
57
	PD7	- Input, Pullup		- Keypad 5
58
  */
59
  DDRD		= 0B00010000;
60
  PORTD		= 0B11100011;
61
 
62
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
63
  PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
64
 
65
  // Setup timer0 - Enable overflow, 8 times prescaler
66
	TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
67
	TCNT0 = 0x00;				// Set Timer0 to 0
68
	TCCR0B = (1<< CS01) ;			// /8 prescaler
69
 
70
	i2c_master();
71
//	i2c_beginTransmission(0x27);
72
//	i2c_writeByte(0x01);
73
//	i2c_endTransmission(1);
74
	lcd_init();
75
	lcd_char((uint8_t)'A');
76
 
77
 
78
  wdt_enable(WDTO_1S);
79
  sei();
80
 
81
  for(;;) {
82
    wdt_reset();
83
 
84
  }
85
}
86
 
87
uint8_t analogRead(uint8_t pin) {
88
	ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
89
	ADCSRA |= (1<<ADSC);		// Start converting
90
 
91
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
92
	uint8_t result = ADCH;
93
	//ADCSRA |= (0<<ADSC);		// Stop converting
94
 
95
	return result;
96
}
97
 
98
 
99
/*
100
 *
101
 * Process the Pin Change Interrupt.
102
 * pcint provides what bank caused the interrupt
103
 *
104
 */
105
void doInt(uint8_t pcint) {
106
 
107
	// Clear the mask so we know we've delth with it
108
	pcIntMask[pcint] = 0;
109
}
110
 
111
ISR(TIMER0_OVF_vect) {
112
}
113
 
114
ISR(PCINT1_vect)
115
{
116
        // Save the state and work out which pin caused
117
        //  the interrupt to occur
118
        pcIntCurr[1] = PIND;
119
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
120
        pcIntLast[1] = pcIntCurr[1];
121
        doInt(1);
122
}
123
 
124
ISR(PCINT2_vect)
125
{
126
	// Save the state and work out which pin caused
127
	//  the interrupt to occur
128
	pcIntCurr[2] = PIND;
129
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
130
	pcIntLast[2] = pcIntCurr[2];
131
	doInt(2);
132
}
133