Subversion Repositories group.electronics

Rev

Rev 53 | Rev 56 | 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;
55 pfowler 108
		char syschar[5];
109
		utoa(systime, syschar, 10);
110
		lcd_overprint_right(syschar, 11, 5, 1);
42 pfowler 111
 
52 pfowler 112
		uint8_t potVal = map_8(analogRead(0), 0, 255, 0, 100);
113
		if (potVal != oldpotVal) {		
114
			lcd_percent_graph(potVal, 0, 0);
115
			oldpotVal = potVal;
55 pfowler 116
 
52 pfowler 117
			char pot[3];
55 pfowler 118
			utoa(potVal, pot, 10);
119
			lcd_overprint_right(pot, 3, 11, 0);
120
 
121
			// Set percentage
53 pfowler 122
			lcd_setCursor(15, 0);
52 pfowler 123
			lcd_char(0x25);
124
		}
48 pfowler 125
	}
42 pfowler 126
  }
127
}
128
 
129
uint8_t analogRead(uint8_t pin) {
130
	ADMUX = (1<<ADLAR) | (1<<REFS0) | (0<<REFS1) | (pin & 0x0f);
131
	ADCSRA |= (1<<ADSC);		// Start converting
132
 
133
	while (((ADCSRA >> ADSC) & 1)) {}	//Wait until conversion finished
134
	uint8_t result = ADCH;
135
	//ADCSRA |= (0<<ADSC);		// Stop converting
136
 
137
	return result;
138
}
139
 
140
 
141
/*
142
 *
143
 * Process the Pin Change Interrupt.
144
 * pcint provides what bank caused the interrupt
145
 *
146
 */
147
void doInt(uint8_t pcint) {
148
 
149
	// Clear the mask so we know we've delth with it
150
	pcIntMask[pcint] = 0;
151
}
152
 
153
ISR(TIMER0_OVF_vect) {
51 pfowler 154
	xbi(PORTD, PD4);
48 pfowler 155
	tmr0_ovf++;
55 pfowler 156
	if (tmr0_ovf>=6) {
51 pfowler 157
		xbi(PORTD, PD0);
48 pfowler 158
		systime++;
51 pfowler 159
		tmr0_ovf = 0;
160
	}
42 pfowler 161
}
162
 
163
ISR(PCINT1_vect)
164
{
165
        // Save the state and work out which pin caused
166
        //  the interrupt to occur
167
        pcIntCurr[1] = PIND;
168
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
169
        pcIntLast[1] = pcIntCurr[1];
170
        doInt(1);
171
}
172
 
173
ISR(PCINT2_vect)
174
{
175
	// Save the state and work out which pin caused
176
	//  the interrupt to occur
177
	pcIntCurr[2] = PIND;
178
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
179
	pcIntLast[2] = pcIntCurr[2];
180
	doInt(2);
181
}
182