Subversion Repositories group.electronics

Rev

Rev 49 | Rev 52 | 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
 
43 pfowler 88
  sei();
89
 
42 pfowler 90
//	i2c_beginTransmission(0x27);
91
//	i2c_writeByte(0x01);
92
//	i2c_endTransmission(1);
43 pfowler 93
 
94
	i2c_master();
42 pfowler 95
	lcd_init();
48 pfowler 96
	lcd_createChar(0x00, emblock);
42 pfowler 97
 
48 pfowler 98
  wdt_enable(WDTO_8S);
42 pfowler 99
 
48 pfowler 100
	uint16_t oldsystime = systime; 
51 pfowler 101
	uint8_t oldpotVal = -1; 
43 pfowler 102
 
48 pfowler 103
	char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
104
	lcd_setCursor(0, 1);
105
	lcd_print(strTime);
43 pfowler 106
 
42 pfowler 107
  for(;;) {
108
    wdt_reset();
51 pfowler 109
 
48 pfowler 110
	if (oldsystime != systime) {
111
		oldsystime = systime;
112
		char syschar[6];
113
		itoa(systime, syschar, 10);
114
		syschar[5] = 0x00;
49 pfowler 115
		lcd_overprint(syschar, 5, 6, 1);
42 pfowler 116
 
51 pfowler 117
 
118
	uint8_t potVal = map_8(analogRead(0), 0, 255, 0, 100);
119
	if (potVal != oldpotVal) {		
49 pfowler 120
		lcd_pergraph(potVal, 0, 0);
51 pfowler 121
		oldpotVal = potVal;
122
		char pot[3];
123
		itoa(potVal, pot, 10);
124
		lcd_overprint(pot, 3, 12, 0);
125
		lcd_char(0x25);
48 pfowler 126
	}
51 pfowler 127
	}
42 pfowler 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) {
51 pfowler 156
	xbi(PORTD, PD4);
48 pfowler 157
	tmr0_ovf++;
51 pfowler 158
	if (tmr0_ovf>=58) {
159
		xbi(PORTD, PD0);
48 pfowler 160
		systime++;
51 pfowler 161
		tmr0_ovf = 0;
162
	}
163
 
42 pfowler 164
}
165
 
166
ISR(PCINT1_vect)
167
{
168
        // Save the state and work out which pin caused
169
        //  the interrupt to occur
170
        pcIntCurr[1] = PIND;
171
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
172
        pcIntLast[1] = pcIntCurr[1];
173
        doInt(1);
174
}
175
 
176
ISR(PCINT2_vect)
177
{
178
	// Save the state and work out which pin caused
179
	//  the interrupt to occur
180
	pcIntCurr[2] = PIND;
181
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
182
	pcIntLast[2] = pcIntCurr[2];
183
	doInt(2);
184
}
185