Subversion Repositories group.electronics

Rev

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;
57 pfowler 20
volatile uint8_t tmr2_ovf = 0;
56 pfowler 21
volatile uint32_t systime = 0;
57 pfowler 22
volatile uint8_t lcdupdate = 1;
48 pfowler 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
 
56 pfowler 36
	analogInit();
42 pfowler 37
  /*
38
  DDR : 1 = Output, 0 = Input
39
  PORT: 1 = Pullup for Input, otherwise set output
40
  PIN : Read input pin
41
  */
42
 
43
  /*
44
	PB0	- Output 		- Keypad 2
45
	PB1	- Output 		- Keypad 7
46
	PB2	- Output 		- Keypad 6
47
	PB3	- Output 		- Keypad 4
48
	PB4	- Input, Pullup		- Function select
49
	PB5	- Input, Pullup		- Function select
50
  */
51
  DDRB		= 0B00001111;
52
  PORTB 	= 0B00111111;
53
 
43 pfowler 54
  DDRC		= 0B00000000;
55
  PORTC		= 0B00110000;
56
 
42 pfowler 57
  /*
51 pfowler 58
	PD0	- Output
42 pfowler 59
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
60
 
61
 
62
	PD4	- Output		- Keypad select status led
63
	PD5	- Input, Pullup		- Keypad 3
64
	PD6	- Input, Pullup		- Keypad 1
65
	PD7	- Input, Pullup		- Keypad 5
66
  */
51 pfowler 67
  DDRD		= 0B00010001;
42 pfowler 68
  PORTD		= 0B11100011;
69
 
56 pfowler 70
	PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
71
	PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
42 pfowler 72
 
57 pfowler 73
	TIMSK0 = (1<<TOIE0);			// Enable 8bit timer overflow for Timer0
42 pfowler 74
	TCNT0 = 0x00;				// Set Timer0 to 0
57 pfowler 75
	TCCR0B = (1<< CS00) ;			// /1 prescaler
42 pfowler 76
 
57 pfowler 77
	TIMSK2 = (1<<TOIE2);			// Enable 8bit timer overflow for Timer2
78
	TCNT2 = 0x00;				// Set Timer2 to 0
79
	TCCR2B = (1<< CS21 | 1<<CS20 ) ;	// /8 prescaler
56 pfowler 80
 
52 pfowler 81
	sei();
43 pfowler 82
 
83
 
84
	i2c_master();
42 pfowler 85
	lcd_init();
48 pfowler 86
	lcd_createChar(0x00, emblock);
42 pfowler 87
 
52 pfowler 88
	wdt_enable(WDTO_8S);
42 pfowler 89
 
51 pfowler 90
	uint8_t oldpotVal = -1; 
43 pfowler 91
 
48 pfowler 92
	char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
93
	lcd_setCursor(0, 1);
94
	lcd_print(strTime);
43 pfowler 95
 
42 pfowler 96
  for(;;) {
97
    wdt_reset();
51 pfowler 98
 
56 pfowler 99
	if (lcdupdate) {
100
		lcdupdate = 0;
42 pfowler 101
 
56 pfowler 102
		char syschar[10];
103
		ultoa(systime, syschar, 10);
104
		lcd_overprint_right(syschar, 10, 5, 1);
105
 
52 pfowler 106
		uint8_t potVal = map_8(analogRead(0), 0, 255, 0, 100);
107
		if (potVal != oldpotVal) {		
108
			lcd_percent_graph(potVal, 0, 0);
109
			oldpotVal = potVal;
55 pfowler 110
 
52 pfowler 111
			char pot[3];
55 pfowler 112
			utoa(potVal, pot, 10);
113
			lcd_overprint_right(pot, 3, 11, 0);
114
 
115
			// Set percentage
53 pfowler 116
			lcd_setCursor(15, 0);
52 pfowler 117
			lcd_char(0x25);
118
		}
48 pfowler 119
	}
42 pfowler 120
  }
121
}
122
 
123
 
124
/*
125
 *
126
 * Process the Pin Change Interrupt.
127
 * pcint provides what bank caused the interrupt
128
 *
129
 */
130
void doInt(uint8_t pcint) {
131
 
132
	// Clear the mask so we know we've delth with it
133
	pcIntMask[pcint] = 0;
134
}
135
 
136
ISR(TIMER0_OVF_vect) {
48 pfowler 137
	tmr0_ovf++;
57 pfowler 138
	if (tmr0_ovf>=62) {
51 pfowler 139
		xbi(PORTD, PD0);
48 pfowler 140
		systime++;
51 pfowler 141
		tmr0_ovf = 0;
142
	}
42 pfowler 143
}
144
 
57 pfowler 145
ISR(TIMER2_OVF_vect) {
146
	tmr2_ovf++;
147
	if (tmr2_ovf>=78) {
56 pfowler 148
		xbi(PORTD, PD4);
149
		lcdupdate=1;
57 pfowler 150
		tmr2_ovf = 0;
56 pfowler 151
	}
152
}
153
 
154
 
155
 
42 pfowler 156
ISR(PCINT1_vect)
157
{
158
        // Save the state and work out which pin caused
159
        //  the interrupt to occur
160
        pcIntCurr[1] = PIND;
161
        pcIntMask[1] = pcIntCurr[1] ^ pcIntLast[1];
162
        pcIntLast[1] = pcIntCurr[1];
163
        doInt(1);
164
}
165
 
166
ISR(PCINT2_vect)
167
{
168
	// Save the state and work out which pin caused
169
	//  the interrupt to occur
170
	pcIntCurr[2] = PIND;
171
	pcIntMask[2] = pcIntCurr[2] ^ pcIntLast[2];
172
	pcIntLast[2] = pcIntCurr[2];
173
	doInt(2);
174
}
175