Subversion Repositories group.electronics

Rev

Rev 42 | Rev 44 | 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
 
43 pfowler 82
	uint8_t chars = 9;
83
	char text[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I' };
42 pfowler 84
 
43 pfowler 85
	//uint8_t i = 0;
86
	//for (i=0; i<=chars; i++)
87
		//lcd_char((uint8_t) text[i]);
88
 
89
	lcd_char((uint8_t) 'a');
90
	lcd_char((uint8_t) 'b');
91
	lcd_char((uint8_t) 'c');
92
	lcd_char((uint8_t) 'd');
93
	lcd_char((uint8_t) 'e');
94
	lcd_char((uint8_t) 'f');
95
	lcd_char((uint8_t) 'g');
96
	lcd_char((uint8_t) 'h');
97
	lcd_char((uint8_t) 'i');
98
	lcd_char((uint8_t) 'j');
99
	lcd_char((uint8_t) 'k');
100
	lcd_char((uint8_t) 'l');
101
	lcd_char((uint8_t) 'm');
102
	lcd_char((uint8_t) 'n');
103
	lcd_char((uint8_t) 'o');
104
	lcd_char((uint8_t) 'p');
105
	lcd_char((uint8_t) 'q');
106
	lcd_char((uint8_t) 'r');
107
	lcd_char((uint8_t) 's');
108
	lcd_char((uint8_t) 't');
109
	lcd_char((uint8_t) 'u');
110
	lcd_char((uint8_t) 'v');
111
	lcd_char((uint8_t) 'w');
112
	lcd_char((uint8_t) 'x');
113
	lcd_char((uint8_t) 'y');
114
	lcd_char((uint8_t) 'z');
115
	lcd_char((uint8_t) '0');
116
	lcd_char((uint8_t) '1');
117
	lcd_char((uint8_t) '2');
118
	lcd_char((uint8_t) '3');
119
	lcd_char((uint8_t) '4');
120
 
121
 
122
 
123
  wdt_enable(WDTO_8S);
42 pfowler 124
 
125
  for(;;) {
126
    wdt_reset();
127
 
128
  }
129
}
130
 
131
uint8_t analogRead(uint8_t pin) {
132
	ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
133
	ADCSRA |= (1<<ADSC);		// Start converting
134
 
135
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
136
	uint8_t result = ADCH;
137
	//ADCSRA |= (0<<ADSC);		// Stop converting
138
 
139
	return result;
140
}
141
 
142
 
143
/*
144
 *
145
 * Process the Pin Change Interrupt.
146
 * pcint provides what bank caused the interrupt
147
 *
148
 */
149
void doInt(uint8_t pcint) {
150
 
151
	// Clear the mask so we know we've delth with it
152
	pcIntMask[pcint] = 0;
153
}
154
 
155
ISR(TIMER0_OVF_vect) {
156
}
157
 
158
ISR(PCINT1_vect)
159
{
160
        // Save the state and work out which pin caused
161
        //  the interrupt to occur
162
        pcIntCurr[1] = PIND;
163
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
164
        pcIntLast[1] = pcIntCurr[1];
165
        doInt(1);
166
}
167
 
168
ISR(PCINT2_vect)
169
{
170
	// Save the state and work out which pin caused
171
	//  the interrupt to occur
172
	pcIntCurr[2] = PIND;
173
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
174
	pcIntLast[2] = pcIntCurr[2];
175
	doInt(2);
176
}
177