Subversion Repositories group.electronics

Rev

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