Subversion Repositories group.electronics

Rev

Rev 55 | Rev 57 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 55 Rev 56
Line 15... Line 15...
15
volatile uint8_t pcIntCurr[3] = {0,0,0};
15
volatile uint8_t pcIntCurr[3] = {0,0,0};
16
volatile uint8_t pcIntLast[3] = {0,0,0};
16
volatile uint8_t pcIntLast[3] = {0,0,0};
17
volatile uint8_t pcIntMask[3] = {0,0,0};
17
volatile uint8_t pcIntMask[3] = {0,0,0};
18
 
18
 
19
volatile uint8_t tmr0_ovf = 0;
19
volatile uint8_t tmr0_ovf = 0;
-
 
20
volatile uint8_t tmr1_ovf = 0;
20
volatile uint16_t systime = 0;
21
volatile uint32_t systime = 0;
21
 
-
 
22
uint8_t analogRead(uint8_t);
22
volatile uint8_t lcdupdate = 0;
23
 
23
 
24
uint8_t emblock[] = {	0B00011111,
24
uint8_t emblock[] = {	0B00011111,
25
			0B00010001,
25
			0B00010001,
26
			0B00010001,
26
			0B00010001,
27
			0B00010001,
27
			0B00010001,
Line 31... Line 31...
31
			0B00011111 };
31
			0B00011111 };
32
			
32
			
33
 
33
 
34
int main(void) {
34
int main(void) {
35
 
35
 
36
  ACSR |= (1<<ACD); // Disable analog comparator
-
 
37
 
-
 
38
  /*
-
 
39
	Setup ADC
36
	analogInit();
40
	ADMUX: 8 bit mode, Avcc ref
-
 
41
	ADCSRA: Enable, 128 prescale
-
 
42
  */
-
 
43
  ADMUX = (1<<ADLAR) | (0<<REFS0) | (1<<REFS1);
-
 
44
  ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0) ;
-
 
45
 
-
 
46
  /*
37
  /*
47
  DDR : 1 = Output, 0 = Input
38
  DDR : 1 = Output, 0 = Input
48
  PORT: 1 = Pullup for Input, otherwise set output
39
  PORT: 1 = Pullup for Input, otherwise set output
49
  PIN : Read input pin
40
  PIN : Read input pin
50
  */
41
  */
Line 74... Line 65...
74
	PD7	- Input, Pullup		- Keypad 5
65
	PD7	- Input, Pullup		- Keypad 5
75
  */
66
  */
76
  DDRD		= 0B00010001;
67
  DDRD		= 0B00010001;
77
  PORTD		= 0B11100011;
68
  PORTD		= 0B11100011;
78
 
69
 
79
  PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
70
	PCMSK2 |= (( 1 << PCINT16 ) | ( 1 << PCINT17 )); //enable encoder pins interrupt sources
80
  PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
71
	PCICR |= ( 1 << PCIE2 ); //enable pin change interupts
81
 
72
 
82
  // Setup timer0 - Enable overflow, 8 times prescaler
-
 
83
	TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
73
	TIMSK0 = (1<<TOIE0);			// Eable timer overflow for Timer0
84
	TCNT0 = 0x00;				// Set Timer0 to 0
74
	TCNT0 = 0x00;				// Set Timer0 to 0
85
	TCCR0B = (1<< CS01) ;			// /8 prescaler
75
	TCCR0B = (1<< CS01) ;			// /8 prescaler
86
 
76
 
-
 
77
	TIMSK1 = (1<<TOIE1);			// Eable timer overflow for Timer1
-
 
78
	TCNT1 = 0x00;				// Set Timer1 to 0
-
 
79
	TCCR1B = (1<< CS11 | 1 << CS10) ;			// /8 prescaler
-
 
80
 
87
	sei();
81
	sei();
88
 
82
 
89
 
83
 
90
	i2c_master();
84
	i2c_master();
91
	lcd_init();
85
	lcd_init();
92
	lcd_createChar(0x00, emblock);
86
	lcd_createChar(0x00, emblock);
93
 
87
 
94
	wdt_enable(WDTO_8S);
88
	wdt_enable(WDTO_8S);
95
 
89
 
96
	uint16_t oldsystime = systime; 
-
 
97
	uint8_t oldpotVal = -1; 
90
	uint8_t oldpotVal = -1; 
98
 
91
 
99
	char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
92
	char strTime[] = {'T', 'i', 'm', 'e', ':', 0x00};
100
	lcd_setCursor(0, 1);
93
	lcd_setCursor(0, 1);
101
	lcd_print(strTime);
94
	lcd_print(strTime);
102
 
95
 
103
  for(;;) {
96
  for(;;) {
104
    wdt_reset();
97
    wdt_reset();
105
 
98
 
106
	if (oldsystime != systime) {
99
	if (lcdupdate) {
107
		oldsystime = systime;
100
		lcdupdate = 0;
-
 
101
 
108
		char syschar[5];
102
		char syschar[10];
109
		utoa(systime, syschar, 10);
103
		ultoa(systime, syschar, 10);
110
		lcd_overprint_right(syschar, 11, 5, 1);
104
		lcd_overprint_right(syschar, 10, 5, 1);
111
 
105
 
112
		uint8_t potVal = map_8(analogRead(0), 0, 255, 0, 100);
106
		uint8_t potVal = map_8(analogRead(0), 0, 255, 0, 100);
113
		if (potVal != oldpotVal) {		
107
		if (potVal != oldpotVal) {		
114
			lcd_percent_graph(potVal, 0, 0);
108
			lcd_percent_graph(potVal, 0, 0);
115
			oldpotVal = potVal;
109
			oldpotVal = potVal;
Line 124... Line 118...
124
		}
118
		}
125
	}
119
	}
126
  }
120
  }
127
}
121
}
128
 
122
 
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
 
123
 
141
/*
124
/*
142
 *
125
 *
143
 * Process the Pin Change Interrupt.
126
 * Process the Pin Change Interrupt.
144
 * pcint provides what bank caused the interrupt
127
 * pcint provides what bank caused the interrupt
Line 149... Line 132...
149
	// Clear the mask so we know we've delth with it
132
	// Clear the mask so we know we've delth with it
150
	pcIntMask[pcint] = 0;
133
	pcIntMask[pcint] = 0;
151
}
134
}
152
 
135
 
153
ISR(TIMER0_OVF_vect) {
136
ISR(TIMER0_OVF_vect) {
154
	xbi(PORTD, PD4);
-
 
155
	tmr0_ovf++;
137
	tmr0_ovf++;
156
	if (tmr0_ovf>=6) {
138
	if (tmr0_ovf>=6) {
157
		xbi(PORTD, PD0);
139
		xbi(PORTD, PD0);
158
		systime++;
140
		systime++;
159
		tmr0_ovf = 0;
141
		tmr0_ovf = 0;
160
	}
142
	}
161
}
143
}
162
 
144
 
-
 
145
ISR(TIMER1_OVF_vect) {
-
 
146
	tmr1_ovf++;
-
 
147
	if (tmr1_ovf>=73) {
-
 
148
		xbi(PORTD, PD4);
-
 
149
		lcdupdate=1;
-
 
150
		tmr1_ovf = 0;
-
 
151
	}
-
 
152
}
-
 
153
		
-
 
154
	
-
 
155
 
163
ISR(PCINT1_vect)
156
ISR(PCINT1_vect)
164
{
157
{
165
        // Save the state and work out which pin caused
158
        // Save the state and work out which pin caused
166
        //  the interrupt to occur
159
        //  the interrupt to occur
167
        pcIntCurr[1] = PIND;
160
        pcIntCurr[1] = PIND;