Subversion Repositories group.electronics

Rev

Rev 123 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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