Subversion Repositories group.electronics

Rev

Rev 52 | Rev 55 | 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
 
49 pfowler 6
#include "petelib.h"
42 pfowler 7
#include <util/delay.h>
8
 
9
#include <stdlib.h>
10
 
11
#include "wire.h"
12
#include "lcd.h"
13
 
14
 
15
volatile uint8_t pcIntCurr[3] = {0,0,0};
16
volatile uint8_t pcIntLast[3] = {0,0,0};
17
volatile uint8_t pcIntMask[3] = {0,0,0};
18
 
48 pfowler 19
volatile uint8_t tmr0_ovf = 0;
20
volatile uint16_t systime = 0;
21
 
22
uint8_t analogRead(uint8_t);
23
 
24
uint8_t emblock[] = {	0B00011111,
25
			0B00010001,
26
			0B00010001,
27
			0B00010001,
28
			0B00010001,
29
			0B00010001,
30
			0B00010001,
31
			0B00011111 };
32
 
33
 
42 pfowler 34
int main(void) {
35
 
36
  ACSR |= (1<<ACD); // Disable analog comparator
37
 
38
  /*
39
	Setup ADC
40
	ADMUX: 8 bit mode, Avcc ref
41
	ADCSRA: Enable, 128 prescale
42
  */
43
  ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
44
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
45
 
46
  /*
47
  DDR : 1 = Output, 0 = Input
48
  PORT: 1 = Pullup for Input, otherwise set output
49
  PIN : Read input pin
50
  */
51
 
52
  /*
53
	PB0	- Output 		- Keypad 2
54
	PB1	- Output 		- Keypad 7
55
	PB2	- Output 		- Keypad 6
56
	PB3	- Output 		- Keypad 4
57
	PB4	- Input, Pullup		- Function select
58
	PB5	- Input, Pullup		- Function select
59
  */
60
  DDRB		= 0B00001111;
61
  PORTB 	= 0B00111111;
62
 
43 pfowler 63
  DDRC		= 0B00000000;
64
  PORTC		= 0B00110000;
65
 
42 pfowler 66
  /*
51 pfowler 67
	PD0	- Output
42 pfowler 68
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
69
 
70
 
71
	PD4	- Output		- Keypad select status led
72
	PD5	- Input, Pullup		- Keypad 3
73
	PD6	- Input, Pullup		- Keypad 1
74
	PD7	- Input, Pullup		- Keypad 5
75
  */
51 pfowler 76
  DDRD		= 0B00010001;
42 pfowler 77
  PORTD		= 0B11100011;
78
 
79
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
80
  PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
81
 
82
  // Setup timer0 - Enable overflow, 8 times prescaler
83
	TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
84
	TCNT0 = 0x00;				// Set Timer0 to 0
85
	TCCR0B = (1<< CS01) ;			// /8 prescaler
86
 
52 pfowler 87
	sei();
43 pfowler 88
 
89
 
90
	i2c_master();
42 pfowler 91
	lcd_init();
48 pfowler 92
	lcd_createChar(0x00, emblock);
42 pfowler 93
 
52 pfowler 94
	wdt_enable(WDTO_8S);
42 pfowler 95
 
48 pfowler 96
	uint16_t oldsystime = systime; 
51 pfowler 97
	uint8_t oldpotVal = -1; 
43 pfowler 98
 
48 pfowler 99
	char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
100
	lcd_setCursor(0, 1);
101
	lcd_print(strTime);
43 pfowler 102
 
42 pfowler 103
  for(;;) {
104
    wdt_reset();
51 pfowler 105
 
48 pfowler 106
	if (oldsystime != systime) {
107
		oldsystime = systime;
108
		char syschar[6];
109
		itoa(systime, syschar, 10);
110
		syschar[5] = 0x00;
53 pfowler 111
		lcd_setCursor(10, 1);
112
		lcd_print_right(syschar);
42 pfowler 113
 
51 pfowler 114
 
52 pfowler 115
		uint8_t potVal = map_8(analogRead(0), 0, 255, 0, 100);
116
		if (potVal != oldpotVal) {		
117
			lcd_percent_graph(potVal, 0, 0);
118
			oldpotVal = potVal;
119
			char pot[3];
120
			itoa(potVal, pot, 10);
53 pfowler 121
			lcd_setCursor(14, 0);
122
			lcd_print_right(pot);
123
			lcd_setCursor(15, 0);
52 pfowler 124
			lcd_char(0x25);
125
		}
48 pfowler 126
	}
42 pfowler 127
  }
128
}
129
 
130
uint8_t analogRead(uint8_t pin) {
131
	ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
132
	ADCSRA |= (1<<ADSC);		// Start converting
133
 
134
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
135
	uint8_t result = ADCH;
136
	//ADCSRA |= (0<<ADSC);		// Stop converting
137
 
138
	return result;
139
}
140
 
141
 
142
/*
143
 *
144
 * Process the Pin Change Interrupt.
145
 * pcint provides what bank caused the interrupt
146
 *
147
 */
148
void doInt(uint8_t pcint) {
149
 
150
	// Clear the mask so we know we've delth with it
151
	pcIntMask[pcint] = 0;
152
}
153
 
154
ISR(TIMER0_OVF_vect) {
51 pfowler 155
	xbi(PORTD, PD4);
48 pfowler 156
	tmr0_ovf++;
51 pfowler 157
	if (tmr0_ovf>=58) {
158
		xbi(PORTD, PD0);
48 pfowler 159
		systime++;
51 pfowler 160
		tmr0_ovf = 0;
161
	}
162
 
42 pfowler 163
}
164
 
165
ISR(PCINT1_vect)
166
{
167
        // Save the state and work out which pin caused
168
        //  the interrupt to occur
169
        pcIntCurr[1] = PIND;
170
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
171
        pcIntLast[1] = pcIntCurr[1];
172
        doInt(1);
173
}
174
 
175
ISR(PCINT2_vect)
176
{
177
	// Save the state and work out which pin caused
178
	//  the interrupt to occur
179
	pcIntCurr[2] = PIND;
180
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
181
	pcIntLast[2] = pcIntCurr[2];
182
	doInt(2);
183
}
184