Subversion Repositories group.electronics

Rev

Rev 43 | Rev 48 | Go to most recent revision | Details | Compare with Previous | 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>
43 pfowler 4
#include <avr/wdt.h>
42 pfowler 5
 
43 pfowler 6
#include "macros.h"
42 pfowler 7
#include <util/delay.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
 
43 pfowler 49
  DDRC		= 0B00000000;
50
  PORTC		= 0B00110000;
51
 
42 pfowler 52
  /*
53
	PD0	- Input, Pullup, PCINT16	- Rotary 1a
54
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
55
 
56
 
57
	PD4	- Output		- Keypad select status led
58
	PD5	- Input, Pullup		- Keypad 3
59
	PD6	- Input, Pullup		- Keypad 1
60
	PD7	- Input, Pullup		- Keypad 5
61
  */
62
  DDRD		= 0B00010000;
63
  PORTD		= 0B11100011;
64
 
65
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
66
  PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
67
 
68
  // Setup timer0 - Enable overflow, 8 times prescaler
69
	TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
70
	TCNT0 = 0x00;				// Set Timer0 to 0
71
	TCCR0B = (1<< CS01) ;			// /8 prescaler
72
 
43 pfowler 73
  sei();
74
 
42 pfowler 75
//	i2c_beginTransmission(0x27);
76
//	i2c_writeByte(0x01);
77
//	i2c_endTransmission(1);
43 pfowler 78
 
79
	i2c_master();
42 pfowler 80
	lcd_init();
81
 
44 pfowler 82
	uint8_t chars = 7;
83
	char text[] = { 'R', 'h', 'i', 'a', 'n', 'n', 'o', 'n' };
42 pfowler 84
 
44 pfowler 85
	uint8_t i = 0;
86
	for (i=0; i<=chars; i++)
87
		lcd_char((uint8_t) text[i]);
43 pfowler 88
 
89
 
90
 
91
 
92
  wdt_enable(WDTO_8S);
42 pfowler 93
 
94
  for(;;) {
95
    wdt_reset();
96
 
97
  }
98
}
99
 
100
uint8_t analogRead(uint8_t pin) {
101
	ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
102
	ADCSRA |= (1<<ADSC);		// Start converting
103
 
104
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
105
	uint8_t result = ADCH;
106
	//ADCSRA |= (0<<ADSC);		// Stop converting
107
 
108
	return result;
109
}
110
 
111
 
112
/*
113
 *
114
 * Process the Pin Change Interrupt.
115
 * pcint provides what bank caused the interrupt
116
 *
117
 */
118
void doInt(uint8_t pcint) {
119
 
120
	// Clear the mask so we know we've delth with it
121
	pcIntMask[pcint] = 0;
122
}
123
 
124
ISR(TIMER0_OVF_vect) {
125
}
126
 
127
ISR(PCINT1_vect)
128
{
129
        // Save the state and work out which pin caused
130
        //  the interrupt to occur
131
        pcIntCurr[1] = PIND;
132
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
133
        pcIntLast[1] = pcIntCurr[1];
134
        doInt(1);
135
}
136
 
137
ISR(PCINT2_vect)
138
{
139
	// Save the state and work out which pin caused
140
	//  the interrupt to occur
141
	pcIntCurr[2] = PIND;
142
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
143
	pcIntLast[2] = pcIntCurr[2];
144
	doInt(2);
145
}
146