Subversion Repositories group.electronics

Rev

Rev 128 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
122 pfowler 1
#include <avr/io.h>
2
#include <avr/wdt.h>
3
#include <avr/interrupt.h>
127 pfowler 4
#include <string.h>
122 pfowler 5
#include <util/delay.h>
6
#include <stdlib.h>
7
 
123 pfowler 8
#include "avrutil.h"
9
#include "wire.h"
10
#include "lcd.h"
122 pfowler 11
#include "config.h"
12
#include "uart.h"
127 pfowler 13
#include "lmd.h"
122 pfowler 14
 
15
#ifndef NULL
16
#define NULL    ((void *)0)
17
#endif
18
 
19
 
20
/* ------------------------------------------------------------------------- */
21
 
127 pfowler 22
#define LCD_SEND_DELAY 80
23
 
24
#define OLED_SEND_DELAY		500
25
#define OLED_LCDWIDTH				128
26
#define OLED_LCDHEIGHT				64
27
 
122 pfowler 28
#define UART_BAUD_RATE	9600
29
 
127 pfowler 30
volatile uint8_t lcdTimer = LCD_SEND_DELAY;
31
volatile uint16_t oledTimer = OLED_SEND_DELAY;
32
 
122 pfowler 33
volatile uint8_t tmr0_ovf = 0;
127 pfowler 34
char display[10];
122 pfowler 35
 
127 pfowler 36
volatile struct {
37
        uint8_t current;
38
        uint8_t last;
39
        uint8_t mask;
40
} pcInt[3];
122 pfowler 41
 
127 pfowler 42
volatile struct {
43
	int8_t outer;
44
	int8_t inner;
45
} input;
122 pfowler 46
 
127 pfowler 47
volatile uint8_t doInt = 0;
48
void pcInterrupt(uint8_t pcint);
123 pfowler 49
 
122 pfowler 50
int main(void) {
51
 
52
 
53
  /*
54
  DDR : 1 = Output, 0 = Input
55
  PORT: 1 = Pullup for Input, otherwise set output
56
  PIN : Read input pin
57
  */
58
 
59
  /*
127 pfowler 60
        PB0     -
61
        PB1     -
62
        PB2     -
63
        PB3     -
64
        PB4     -
65
        PB5     -
66
        PB6     -
67
        PB7     -
122 pfowler 68
  */
127 pfowler 69
  DDRB          = 0B00000111;
122 pfowler 70
  PORTB         = 0B00000000;
71
 
72
  /*
127 pfowler 73
        PC0     -
74
        PC1     -
75
        PC2     -
76
        PC3     -
77
        PC4     -
78
        PC5     -
122 pfowler 79
  */
127 pfowler 80
  DDRC          = 0B00000000;
81
  PORTC         = 0B00001111;
122 pfowler 82
 
83
  /*
127 pfowler 84
        PD0     -
85
        PD1     -
86
        PD2     -
87
        PD3     -
88
        PD4     -
122 pfowler 89
        PD5     - Input PcInt		- * I/O Interrupt PCINT21
90
        PD6     - Output		- * Status LED
91
        PD7     - Input, Pullup		- * Button
92
  */
127 pfowler 93
  DDRD          = 0B01000000;
122 pfowler 94
  PORTD         = 0B10000000;
95
 
128 pfowler 96
  PCMSK1 |= (( 1 << PCINT8 ) | ( 1 << PCINT9 )| ( 1 << PCINT10 )| ( 1 << PCINT11 ));
97
  PCICR |= ( 1 << PCIE1 );
98
 
99
  pcInt[1].last = PINC;
100
  pcInt[1].current = pcInt[1].last;
101
  pcInt[1].mask = 0;
102
 
127 pfowler 103
  	systime = 0;
104
    sysclockInit();
105
 
123 pfowler 106
    uart_init( UART_BAUD_SELECT(UART_BAUD_RATE,F_CPU) );
127 pfowler 107
 
122 pfowler 108
    wdt_enable(WDTO_1S);		// Watchdog for 1 sec
109
    sei();				// Enable interrupts
127 pfowler 110
 
123 pfowler 111
    i2c_master();
112
    lcd_init();
122 pfowler 113
 
127 pfowler 114
    sbi(PORTD, PD6);
123 pfowler 115
    _delay_us(100);
116
    cbi(PORTD, PD6);
122 pfowler 117
 
127 pfowler 118
    lcd_setCursor(0, 1);
119
    lcd_print("Time:\0");
120
 
121
    lmd_init(0x03, PB0, PB1, PB2, 2); // PORTB = 0x03, 5 digits = 0x04
122
    // Let the MAX's settle. There's also a 200us delay inside the init
123
    _delay_ms(200);
124
 
125
 
126
    uint16_t comm1[2];
127
    comm1[0] = 12010;
128
    comm1[1] = 12190;
129
 
130
    input.outer = 0;
131
    input.inner = 0;
132
 
133
    uint8_t c;
134
    for (c=0; c<=1; c++) {
135
    	lmd_scanlimit(c, 0x04);
136
		lmd_decodemode(c, 0xff);
137
		lmd_dp(c, 0x04);
138
		lmd_print_u16(c, comm1[c]);
139
    }
140
 
122 pfowler 141
    for(;;){
127 pfowler 142
    	 wdt_reset();
123 pfowler 143
 
127 pfowler 144
    	 if (doInt) {
128 pfowler 145
    		 pcInterrupt(doInt);
127 pfowler 146
    		 doInt = 0;
147
    	 }
123 pfowler 148
 
128 pfowler 149
#ifdef _DEBUG_
127 pfowler 150
		if (!lcdTimer) {
151
		  ultoa(systime, display, 10);
152
		  lcd_overprint_right(display, 10, 6, 1);
153
 
154
		  lcdTimer = LCD_SEND_DELAY;
155
		}
128 pfowler 156
#endif
127 pfowler 157
 
158
		if (!oledTimer && !rbi(PIND, PD7)) {
159
			cli();
160
			statusLed(ON);
161
 
162
			//swap_u16(&comm1[0], &comm1[1]);
163
			uint16_t t = comm1[1];
164
			comm1[1] = comm1[0];
165
			comm1[0] = t;
166
			_delay_ms(100);
167
 
168
			statusLed(OFF);
169
			sei();
170
			oledTimer = OLED_SEND_DELAY;
171
 
172
			lmd_print_u16(0, comm1[0]);
173
			lmd_print_u16(1, comm1[1]);
174
 
128 pfowler 175
#ifdef _DEBUG_
127 pfowler 176
			uart_puts("Swap: ");
177
			utoa(systime, display, 10);
178
			uart_puts(display);
179
			uart_puts("\r\n");
128 pfowler 180
#endif
127 pfowler 181
		}
182
 
183
		if (input.inner != 0 || input.outer != 0) {
184
 
185
			int16_t delta = 0;
186
 
187
			if (input.inner) {
188
				delta = (input.inner * 5);
189
			}
190
			if (input.outer) {
191
				delta = (input.outer * 100);
192
			}
193
 
194
	    	input.inner = 0;
195
	    	input.outer = 0;
196
 
197
			comm1[1] += delta;
198
			lmd_print_u16(1, comm1[1]);
199
		}
200
 
201
    	_delay_ms(1);
122 pfowler 202
    }
127 pfowler 203
 
204
 
205
 
122 pfowler 206
    return 0;
207
}
208
 
127 pfowler 209
void pcInterrupt(uint8_t pcint) {
210
		wdt_reset();
128 pfowler 211
 
212
		pcInt[pcint].mask = pcInt[pcint].current ^ pcInt[pcint].last;
213
 
214
#ifdef _DEBUG_
215
        utoa(pcInt[pcint].last, display, 2);
216
        uart_puts(display);
217
        uart_puts("\r\n");
218
        utoa(pcInt[pcint].current, display, 2);
219
        uart_puts(display);
220
        uart_puts("\r\n");
221
        utoa(pcInt[pcint].mask, display, 2);
222
        uart_puts(display);
223
        uart_puts("\r\n");
224
#endif
225
 
127 pfowler 226
        pcInt[pcint].last = pcInt[pcint].current;
122 pfowler 227
 
127 pfowler 228
        if (pcInt[pcint].mask == 0)
128 pfowler 229
                		return;
127 pfowler 230
 
231
        // Check which pin caused the interrupt. If they both
232
        //  equal 0 || 1, the pin that interrupted is the direction
233
        if ((
234
        	(rbi(pcInt[pcint].current, PCINT8) == 1 &&
235
             rbi(pcInt[pcint].current, PCINT9) == 1) ||
236
             (rbi(pcInt[pcint].current, PCINT8) == 0 &&
128 pfowler 237
              rbi(pcInt[pcint].current, PCINT9) == 0))) {
127 pfowler 238
 
128 pfowler 239
              if (rbi(pcInt[pcint].mask, PCINT8) ) {
127 pfowler 240
        		input.outer += 1;
128 pfowler 241
        		uart_puts("+Outer\r\n");
242
              } else if (rbi(pcInt[pcint].mask, PCINT9) ) {
243
            	input.outer -= 1;
244
            	uart_puts("-Outer\r\n");
245
              }
127 pfowler 246
 
128 pfowler 247
        }
248
 
249
        if ((
127 pfowler 250
        		(rbi(pcInt[pcint].current, PCINT10) == 1 &&
251
                rbi(pcInt[pcint].current, PCINT11) == 1) ||
252
                (rbi(pcInt[pcint].current, PCINT10) == 0 &&
128 pfowler 253
                 rbi(pcInt[pcint].current, PCINT11) == 0))) {
127 pfowler 254
 
128 pfowler 255
        	if (rbi(pcInt[pcint].mask, PCINT10) ) {
127 pfowler 256
        		input.inner += 1;
128 pfowler 257
        		uart_puts("+Inner\r\n");
258
        	} else if (rbi(pcInt[pcint].mask, PCINT11) ) {
127 pfowler 259
        		input.inner -= 1;
128 pfowler 260
        		uart_puts("-Inner\r\n");
261
        	}
127 pfowler 262
        }
263
 
128 pfowler 264
        // Clear the mask so we know we've dealt with it
127 pfowler 265
        pcInt[pcint].mask = 0;
266
 
267
}
268
 
269
ISR(PCINT1_vect) {
128 pfowler 270
	pcInt[1].current = PINC;
271
	if (!doInt) {
272
		doInt = 1;
273
	}
127 pfowler 274
}
275
 
276
 
122 pfowler 277
ISR(TIMER0_OVF_vect) {
278
        tmr0_ovf++;
279
 
280
	// Clk/1 TCCR0B = (1<< CS00);
281
	//20.0Mhz, 1ms = 78ovf
282
	//16.5Mhz, 1ms = 64ovf
283
	//16.0Mhz, 1ms = 62ovf
284
	//12.0Mhz, 1ms = 46ovf
285
 
286
        if (tmr0_ovf>=64) {
287
                systime++;
288
                tmr0_ovf = 0;
127 pfowler 289
 
290
			if (lcdTimer)
291
				lcdTimer--;
292
 
293
			if (oledTimer)
294
				oledTimer--;
122 pfowler 295
        }
296
}