Subversion Repositories group.electronics

Rev

Rev 44 | Rev 49 | 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
 
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
  /*
68
	PD0	- Input, Pullup, PCINT16	- Rotary 1a
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
  */
77
  DDRD		= 0B00010000;
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; 
43 pfowler 101
 
48 pfowler 102
	char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
103
	lcd_setCursor(0, 1);
104
	lcd_print(strTime);
43 pfowler 105
 
42 pfowler 106
  for(;;) {
107
    wdt_reset();
48 pfowler 108
	if (oldsystime != systime) {
109
		oldsystime = systime;
110
		char syschar[6];
111
		itoa(systime, syschar, 10);
112
		syschar[5] = 0x00;
113
		lcd_setCursor(6,1);
114
		lcd_print(syschar);
42 pfowler 115
 
48 pfowler 116
		char pot[4];
117
		itoa(analogRead(0), pot, 10);
118
		//pot[3] = 0x00;
119
		//lcd_setCursor(0,0);
120
		//lcd_print(pot);
121
		lcd_overprint(pot, 4, 0, 0);
122
 
123
		lcd_setCursor(9, 0);
124
		lcd_char(0x00);
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) {
48 pfowler 154
	tmr0_ovf++;
155
	if (tmr0_ovf==255)
156
		systime++;
42 pfowler 157
}
158
 
159
ISR(PCINT1_vect)
160
{
161
        // Save the state and work out which pin caused
162
        //  the interrupt to occur
163
        pcIntCurr[1] = PIND;
164
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
165
        pcIntLast[1] = pcIntCurr[1];
166
        doInt(1);
167
}
168
 
169
ISR(PCINT2_vect)
170
{
171
	// Save the state and work out which pin caused
172
	//  the interrupt to occur
173
	pcIntCurr[2] = PIND;
174
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
175
	pcIntLast[2] = pcIntCurr[2];
176
	doInt(2);
177
}
178