Subversion Repositories group.electronics

Rev

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