Subversion Repositories group.electronics

Rev

Rev 57 | 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
 
58 pfowler 6
#include "util.h"
42 pfowler 7
#include <util/delay.h>
8
 
9
#include <stdlib.h>
10
 
11
#include "wire.h"
12
#include "lcd.h"
13
 
48 pfowler 14
volatile uint8_t tmr0_ovf = 0;
57 pfowler 15
volatile uint8_t tmr2_ovf = 0;
56 pfowler 16
volatile uint32_t systime = 0;
57 pfowler 17
volatile uint8_t lcdupdate = 1;
48 pfowler 18
 
19
uint8_t emblock[] = {	0B00011111,
20
			0B00010001,
21
			0B00010001,
22
			0B00010001,
23
			0B00010001,
24
			0B00010001,
25
			0B00010001,
26
			0B00011111 };
27
 
28
 
42 pfowler 29
int main(void) {
30
 
56 pfowler 31
	analogInit();
42 pfowler 32
  /*
33
  DDR : 1 = Output, 0 = Input
34
  PORT: 1 = Pullup for Input, otherwise set output
35
  PIN : Read input pin
36
  */
37
 
38
  /*
39
	PB0	- Output 		- Keypad 2
40
	PB1	- Output 		- Keypad 7
41
	PB2	- Output 		- Keypad 6
42
	PB3	- Output 		- Keypad 4
43
	PB4	- Input, Pullup		- Function select
44
	PB5	- Input, Pullup		- Function select
45
  */
46
  DDRB		= 0B00001111;
47
  PORTB 	= 0B00111111;
48
 
43 pfowler 49
  DDRC		= 0B00000000;
50
  PORTC		= 0B00110000;
51
 
42 pfowler 52
  /*
51 pfowler 53
	PD0	- Output
42 pfowler 54
	PD1	- Input, Pullup, PCINT17	- Rotary 1b
55
 
56
 
57
	PD4	- Output		- Keypad select status led
58
	PD5	- Input, Pullup		- Keypad 3
59
	PD6	- Input, Pullup		- Keypad 1
60
	PD7	- Input, Pullup		- Keypad 5
61
  */
51 pfowler 62
  DDRD		= 0B00010001;
42 pfowler 63
  PORTD		= 0B11100011;
64
 
57 pfowler 65
	TIMSK0 = (1<<TOIE0);			// Enable 8bit timer overflow for Timer0
42 pfowler 66
	TCNT0 = 0x00;				// Set Timer0 to 0
57 pfowler 67
	TCCR0B = (1<< CS00) ;			// /1 prescaler
42 pfowler 68
 
57 pfowler 69
	TIMSK2 = (1<<TOIE2);			// Enable 8bit timer overflow for Timer2
70
	TCNT2 = 0x00;				// Set Timer2 to 0
71
	TCCR2B = (1<< CS21 | 1<<CS20 ) ;	// /8 prescaler
56 pfowler 72
 
52 pfowler 73
	sei();
43 pfowler 74
 
75
 
76
	i2c_master();
42 pfowler 77
	lcd_init();
48 pfowler 78
	lcd_createChar(0x00, emblock);
42 pfowler 79
 
52 pfowler 80
	wdt_enable(WDTO_8S);
42 pfowler 81
 
51 pfowler 82
	uint8_t oldpotVal = -1; 
43 pfowler 83
 
48 pfowler 84
	char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
85
	lcd_setCursor(0, 1);
86
	lcd_print(strTime);
43 pfowler 87
 
42 pfowler 88
  for(;;) {
89
    wdt_reset();
51 pfowler 90
 
56 pfowler 91
	if (lcdupdate) {
92
		lcdupdate = 0;
42 pfowler 93
 
56 pfowler 94
		char syschar[10];
95
		ultoa(systime, syschar, 10);
96
		lcd_overprint_right(syschar, 10, 5, 1);
97
 
52 pfowler 98
		uint8_t potVal = map_8(analogRead(0), 0, 255, 0, 100);
99
		if (potVal != oldpotVal) {		
100
			lcd_percent_graph(potVal, 0, 0);
101
			oldpotVal = potVal;
55 pfowler 102
 
52 pfowler 103
			char pot[3];
55 pfowler 104
			utoa(potVal, pot, 10);
105
			lcd_overprint_right(pot, 3, 11, 0);
106
 
107
			// Set percentage
53 pfowler 108
			lcd_setCursor(15, 0);
52 pfowler 109
			lcd_char(0x25);
110
		}
48 pfowler 111
	}
42 pfowler 112
  }
113
}
114
 
115
 
116
ISR(TIMER0_OVF_vect) {
48 pfowler 117
	tmr0_ovf++;
58 pfowler 118
	if (tmr0_ovf>=46) {
51 pfowler 119
		xbi(PORTD, PD0);
48 pfowler 120
		systime++;
51 pfowler 121
		tmr0_ovf = 0;
122
	}
42 pfowler 123
}
124
 
57 pfowler 125
ISR(TIMER2_OVF_vect) {
126
	tmr2_ovf++;
58 pfowler 127
	if (tmr2_ovf>=58) {
56 pfowler 128
		xbi(PORTD, PD4);
129
		lcdupdate=1;
57 pfowler 130
		tmr2_ovf = 0;
56 pfowler 131
	}
132
}