Subversion Repositories group.NITPanels

Rev

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

Rev 2 Rev 24
Line 3... Line 3...
3
#include <avr/interrupt.h>
3
#include <avr/interrupt.h>
4
#include <string.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
 
8
#include "config.h"
9
#include "config.h"
9
#include "avrutil.h"
10
#include "avrutil.h"
10
#include "hc595.h"
11
#include "hc595.h"
11
#include "twires.h"
12
#include "twires.h"
12
 
13
 
Line 41... Line 42...
41
volatile struct {
42
volatile struct {
42
	int8_t outer;
43
	int8_t outer;
43
	int8_t inner;
44
	int8_t inner;
44
} input;
45
} input;
45
 
46
 
46
//volatile uint8_t doInt = 0;
47
uint8_t i2c_address = I2C_SLAVE_ADDR;
47
 
-
 
48
 
-
 
49
//uint8_t disVal[] = {8,8,8,8,8,8,8,8,8,8};
48
uint8_t disIdx = 0;
50
volatile uint8_t disVal[] = {0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a};
49
volatile uint8_t disVal[] = {0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a, 0x0a};
51
volatile uint16_t disPt = 0x00;
50
volatile uint16_t disPt = 0x00;
52
 
51
 
-
 
52
 
-
 
53
 
53
int main(void) {
54
int main(void) {
54
 
55
 
55
 
56
 
-
 
57
#if HW_VERSION==0x01
56
  /*
58
	  /*
57
  DDR : 1 = Output, 0 = Input
59
	  DDR : 1 = Output, 0 = Input
58
  PORT: 1 = Pullup for Input, otherwise set output
60
	  PORT: 1 = Pullup for Input, otherwise set output
59
  PIN : Read input pin
61
	  PIN : Read input pin
60
  */
62
	  */
61
 
-
 
62
 
63
 
63
  /*
64
	  /*
64
	PA0 -	Output		- HC595 DS
65
		PA0 -	Output		- HC595 DS
65
	PA1 - 	Output		- HC595 ST
66
		PA1 - 	Output		- HC595 ST
66
	PA2 -	Output		- HC595 SH
67
		PA2 -	Output		- HC595 SH
67
	PA3 -	Output		- 4511 blanking
68
		PA3 -	Output		- 4511 blanking
68
	PA4 -	SCK
69
		PA4 -	SCK
69
	PA5 -	Output		- Decimal Point
70
		PA5 -	Output		- Decimal Point
70
	PA6 -	SDA
71
		PA6 -	SDA
71
	PA7 -	Input, Pullup	- Flip button
72
		PA7 -	Input, Pullup	- Flip button
72
  */
73
	  */
73
  DDRA          = 0B00101111;
74
	  DDRA          = 0B00101111;
74
  PORTA         = 0B10001000;
75
	  PORTA         = 0B10001000;
75
  /*
76
	  /*
76
	PB0     - Input, PCInt, Pullup	- Rot1a
77
		PB0     - Input, PCInt, Pullup	- Rot1a
77
	PB1     - Input, PCInt, Pullup	- Rot1b
78
		PB1     - Input, PCInt, Pullup	- Rot1b
78
	PB2     - Input, Pullup		- Swap button
79
		PB2     - Input, Pullup		- Swap button
79
	PB3     - N/A			- Reset (Future - I2C address select)
80
		PB3     - N/A			- Reset (Future - I2C address select)
80
  */
81
	  */
81
  DDRB          = 0B00000000;
82
	  DDRB          = 0B00000000;
82
  PORTB         = 0B00000111;
83
	  PORTB         = 0B00000111;
-
 
84
#else
-
 
85
	  /*
-
 
86
	  DDR : 1 = Output, 0 = Input
-
 
87
	  PORT: 1 = Pullup for Input, otherwise set output
-
 
88
	  PIN : Read input pin
-
 
89
	  */
-
 
90
 
-
 
91
	  /*
-
 
92
		PA0 -	Input, Pullup			- i2c addr select 0 (Dip 1)
-
 
93
		PA1 - 	Input, Pullup			- i2c addr select 1 (Dip 2)
-
 
94
		PA2 -	Reset
-
 
95
	  */
-
 
96
	  DDRA          = 0B00000000;
-
 
97
	  PORTA         = 0B00000011;
-
 
98
	  /*
-
 
99
		PB0     - Input, PCInt0, Pullup	- Rot1a
-
 
100
		PB1     - Input, PCInt1, Pullup	- Rot1b
-
 
101
		PB2     - Input, PCInt2, Pullup	- Rot2a
-
 
102
		PB3     - Input, PCInt3, Pullup	- Rot2b
-
 
103
		PB4     - Input, Pullup			- Rot button
-
 
104
		PB5     - SDA
-
 
105
		PB6     - Input, Pullup			- Swap button
-
 
106
		PB7     - SCL
-
 
107
	  */
-
 
108
	  DDRB          = 0B00000000;
-
 
109
	  PORTB         = 0B01011111;
-
 
110
	  /*
-
 
111
		PD0     - Output				- Decimal point
-
 
112
		PD1     - Output				- 4511 Blanking
-
 
113
		PD2     - Output				- '95 DS   Ser
-
 
114
		PD3     - Output				- '95 SHCP Sck
-
 
115
		PD4     - Output				- '95 STCP Rsk
-
 
116
		PD5     - Input, Pullup			- Config (Dip 3)
-
 
117
		PD6     - Input, Pullup			- Config (Dip 4)
-
 
118
	  */
-
 
119
	  DDRD          = 0B00011111;
-
 
120
	  PORTD         = 0B01100010;
-
 
121
#endif
83
 
122
 
84
    systime = 0;
123
    systime = 0;
85
    sysclockInit();
124
    sysclockInit();
86
 
125
 
-
 
126
#if HW_VERSION==0x01
-
 
127
    // hwver1 only has a single rotary shaft
87
    PCMSK1 |= (( 1 << ROT_1A_PIN ) | ( 1 << ROT_1B_PIN ));
128
    PCMSK1 |= (( 1 << ROT_1A_PIN ) | ( 1 << ROT_1B_PIN ));
88
    GIMSK |= (1 << PCIE1);
129
    GIMSK |= (1 << PCIE1);
-
 
130
#else
-
 
131
    // hwver2 only has a selectable single/dual rotary shaft
-
 
132
    PCMSK |= (( 1 << ROT_1A_PIN ) | ( 1 << ROT_1B_PIN ) | ( 1 << ROT_2A_PIN ) | ( 1 << ROT_2B_PIN ));
-
 
133
    GIMSK |= (1 << PCIE);
-
 
134
#endif
89
 
135
 
90
    pcInt.last = ROT_1_PIN;
136
    pcInt.last = ROT_PIN;
91
    pcInt.current = pcInt.last;
137
    pcInt.current = pcInt.last;
92
    pcInt.mask = 0;
138
    pcInt.mask = 0;
93
 
139
 
-
 
140
#if HW_VERSION==0x02
-
 
141
    // hw vers 2 has selectable i2c address. Add the value of
-
 
142
    //  the selections to the default i2c address
-
 
143
    uint8_t i2c_add = ((!rbi(I2CADDR_PIN, I2CADDR_0)) << 1);
-
 
144
    i2c_add |= (!rbi(I2CADDR_PIN, I2CADDR_1));
-
 
145
    i2c_address += i2c_add;
-
 
146
#endif
-
 
147
 
94
    twires_begin(I2C_SLAVE_ADDR);
148
    twires_begin(i2c_address);
95
    twires_onReceive(receiveEvent);
149
    twires_onReceive(receiveEvent);
96
 
150
 
97
    wdt_enable(WDTO_1S);	// Watchdog for 1 sec
151
    wdt_enable(WDTO_1S);	// Watchdog for 1 sec
98
    sei();			// Enable interrupts
152
    sei();			// Enable interrupts
99
 
153
 
100
    uint32_t refresh = systime;
154
    uint32_t refresh = systime;
101
    uint8_t disIdx = 0;
-
 
-
 
155
 
102
 
156
 
103
    // Init and clear the displays
157
    // Init and clear the displays
104
    hc595_init();
158
    hc595_init();
105
    hc595_write(0x00);
159
    hc595_write(0x00);
106
 
160
 
Line 111... Line 165...
111
 
165
 
112
    	if (user_debug) {
166
    	if (user_debug) {
113
    		disVal[9] = rbi(SB_INPUT, SB_PIN);
167
    		disVal[9] = rbi(SB_INPUT, SB_PIN);
114
    		disVal[8] = rbi(FB_INPUT, FB_PIN);
168
    		disVal[8] = rbi(FB_INPUT, FB_PIN);
115
    		disVal[0] = input.outer & 0x0f;
169
    		disVal[0] = input.outer & 0x0f;
116
    		disVal[1] = input.outer >> 4;
170
    		disVal[1] = (input.outer >> 4);
117
    	}
171
    	}
118
 
172
 
119
		if (pcInt.current != pcInt.last) {
173
		if (pcInt.current != pcInt.last) {
120
			cli(); 		// Dont process interrupts for a bit
174
			cli(); 		// Dont process interrupts for a bit
121
			pcInterrupt();
175
			pcInterrupt();
Line 153... Line 207...
153
			// Receive n numbers of digits to DISPLAY_REFRESH
207
			// Receive n numbers of digits to DISPLAY_REFRESH
154
			// High nibble = segment number
208
			// High nibble = segment number
155
			// Low nibble = digit to display
209
			// Low nibble = digit to display
156
			while (bytes--) {
210
			while (bytes--) {
157
				uint8_t data = twires_receive();
211
				uint8_t data = twires_receive();
158
				disVal[(data >> 4)] = (data & 0x0f);
212
				disVal[data >> 4] = (data & 0x0f);
159
			}
213
			}
160
		}
214
		}
161
			break;
215
			break;
-
 
216
		case (0x06): {
-
 
217
			// Recive a digit to retrieve to value of
-
 
218
			uint8_t digit = twires_receive();
-
 
219
			uint8_t out = (digit << 4) | (disVal[digit] & 0x0f);
-
 
220
			twires_send(out);
-
 
221
		}
-
 
222
			break;
162
		case (0x08): {
223
		case (0x08): {
163
			// Recieve 2 bytes, for the decimal points
224
			// Recieve 2 bytes, for the decimal points
164
			//  5 bits used in eac byte
225
			//  5 bits used in eac byte
165
			disPt = (uint16_t)twires_receive() << 8;
226
			disPt = ((uint16_t)twires_receive() << 8);
166
			disPt |= (uint16_t)twires_receive();
227
			disPt |= (uint16_t)twires_receive();
167
		}
228
		}
168
			break;
229
			break;
169
		case (0x09): {
230
		case (0x09): {
170
			// Reset the rotary
231
			// Reset the rotary
171
			input.outer = 0;
232
			input.outer = 0;
172
			input.inner = 0;
233
			input.inner = 0;
173
		}
234
		}
174
			break;
235
			break;
175
		case (0x0a): {
236
		case (0x0a): {
176
			// Send the value of the rotary count
-
 
177
			twires_send(input.outer);
237
			twires_send(input.outer);
178
		}
238
		}
179
			break;
239
			break;
180
		/* Not used in this version
240
#if HW_VERSION==0x02
181
		case (0x0b): {
241
		case (0x0b): {
182
			twires_send(input.inner);
242
			twires_send(input.inner);
183
		}
243
		}
184
			break;
244
			break;
185
		*/
245
#endif
186
 
-
 
187
		case (0x0c): {
246
		case (0x0c): {
188
			// Send that status of the buttons
247
			// Send that status of the buttons
189
			uint8_t out = (!rbi(SB_INPUT, SB_PIN)) << 1;
248
			uint8_t out = (!rbi(SB_INPUT, SB_PIN)) << 1;
190
			out |= (!rbi(FB_INPUT, FB_PIN));
249
			out |= (!rbi(FB_INPUT, FB_PIN));
191
			twires_send(out);
250
			twires_send(out);
192
		}
251
		}
-
 
252
			break;
193
		/* Comined into 0x0c
253
#if HW_VERSION==0x02
194
		case (0x0d): {
254
		case (0x0d): {
-
 
255
			// Send all the config switch data
-
 
256
			uint8_t out = ((!rbi(I2CADDR_PIN, I2CADDR_0)) << 3);
-
 
257
			out |= ((!rbi(I2CADDR_PIN, I2CADDR_1)) << 2);
-
 
258
			out |= ((!rbi(CONFSW_PIN, CONFSW_1)) << 1);
-
 
259
			out |= (!rbi(CONFSW_PIN, CONFSW_0));
195
			twires_send(rbi(FB_INPUT, FB_PIN));
260
			twires_send(out);
196
		}
261
		}
197
			break;
262
		break;
198
		*/
263
#endif
199
		default: {
264
		default: {
200
			// Don't know what it is, empty buffer
265
			// Don't know what it is, empty buffer
201
			while (twires_available())
266
			while (twires_available())
202
				twires_receive();
267
				twires_receive();
203
		}
268
		}
204
			break;
269
			break;
205
	}
270
	}
206
}
271
}
207
 
272
 
208
void writeSegment(uint8_t digit, uint8_t value) {
273
void writeSegment(uint8_t digit, uint8_t value) {
209
	//uint8_t dp = 0;
-
 
210
	//if (digit <= 4 && rbi(disPt[0], digit)) dp = 1;
-
 
211
	//if (digit > 4 && rbi(disPt[1], digit-5)) dp = 1;
-
 
212
 
-
 
213
	cbi(BL_PORT, BL_PIN);			// Blank the 4511
274
	cbi(BL_PORT, BL_PIN);			// Blank the 4511
214
	cbi(DP_PORT, DP_PIN);			// Drop the DP before changing digit
275
	cbi(DP_PORT, DP_PIN);			// Drop the DP before changing digit
215
 
276
 
216
// Changed my mind and reversed order of digit display for release version
-
 
217
//   0-9 is left to right on release, right to left on prototype
-
 
218
//   This below hack for the prototype means i didn't have to change the ls145n
-
 
219
//	 connections, but have been changed on the release version
-
 
220
#ifdef PROTOTYPE
-
 
221
	hc595_write((value << 4) | (9-(digit & 0x0f))); //Write the value and digit
277
	hc595_write((value << 4) | (9-(digit & 0x0f))); //Write the value and digit
222
#else
-
 
223
	hc595_write((value << 4) | (digit & 0x0f)); //Write the value and digit
-
 
224
#endif
-
 
225
	if (rbi(disPt, digit)) sbi(DP_PORT, DP_PIN);		// Add DP if required
278
	if (rbi(disPt, digit)) sbi(DP_PORT, DP_PIN);		// Add DP if required
226
	sbi(BL_PORT, BL_PIN);			// Enable the 4511
279
	sbi(BL_PORT, BL_PIN);			// Enable the 4511
227
}
280
}
228
 
281
 
229
void pcInterrupt() {
282
void pcInterrupt() {
230
	pcInt.mask = pcInt.current ^ pcInt.last;
283
	pcInt.mask = pcInt.current ^ pcInt.last;
231
	pcInt.last = pcInt.current;
284
	pcInt.last = pcInt.current;
232
 
285
 
233
        if (!pcInt.mask)
286
	if (!pcInt.mask)
234
        	return;
287
		return;
235
 
288
 
236
        // Check which pin caused the interrupt. If they both
289
	// Check which pin caused the interrupt. If they both
237
        //  equal 0 || 1, the pin that interrupted is the direction
290
	//  equal 0 || 1, the pin that interrupted is the direction
238
        if (((rbi(pcInt.current, ROT_1A_PIN) == 1 &&
291
	if (((rbi(pcInt.current, ROT_1A_PIN) == 1 &&
239
             rbi(pcInt.current, ROT_1B_PIN) == 1) ||
292
				rbi(pcInt.current, ROT_1B_PIN) == 1) ||
240
             (rbi(pcInt.current, ROT_1A_PIN) == 0 &&
293
		 (rbi(pcInt.current, ROT_1A_PIN) == 0 &&
241
              rbi(pcInt.current, ROT_1B_PIN) == 0))) {
294
				rbi(pcInt.current, ROT_1B_PIN) == 0))) {
242
 
295
 
243
        	if (rbi(pcInt.mask, ROT_1A_PIN) ) {
296
		if (rbi(pcInt.mask, ROT_1A_PIN) ) {
244
        		input.outer -= 1;
297
			input.outer -= 1;
245
              } else if (rbi(pcInt.mask, ROT_1B_PIN) ) {
298
		  } else if (rbi(pcInt.mask, ROT_1B_PIN) ) {
246
            	input.outer += 1;
299
			input.outer += 1;
247
            }
300
		}
248
 
301
	}
249
        }
-
 
250
        // Clear the mask so we know we've dealt with it
-
 
251
        // Shouldn't be required now that 'doInt' has been removed
-
 
252
        //pcInt.mask = 0;
-
 
253
 
302
 
-
 
303
#if HW_VERSION==0x02
-
 
304
	if (((rbi(pcInt.current, ROT_2A_PIN) == 1 &&
-
 
305
				rbi(pcInt.current, ROT_2B_PIN) == 1) ||
-
 
306
		 (rbi(pcInt.current, ROT_2A_PIN) == 0 &&
-
 
307
				rbi(pcInt.current, ROT_2B_PIN) == 0))) {
-
 
308
 
-
 
309
		if (rbi(pcInt.mask, ROT_2A_PIN) ) {
-
 
310
			input.inner -= 1;
-
 
311
		  } else if (rbi(pcInt.mask, ROT_2B_PIN) ) {
-
 
312
			input.inner += 1;
-
 
313
		}
254
}
314
	}
-
 
315
#endif
-
 
316
	// Clear the mask so we know we've dealt with it
-
 
317
	// Shouldn't be required now that 'doInt' has been removed
-
 
318
	//pcInt.mask = 0;
255
 
319
 
-
 
320
}
-
 
321
#if HW_VERSION==0x01
256
ISR(PCINT1_vect) {
322
ISR(PCINT1_vect) {
-
 
323
#else
-
 
324
ISR(PCINT_B_vect) {
-
 
325
#endif
257
	pcInt.current = ROT_1_PIN;
326
	pcInt.current = ROT_PIN;
258
}
327
}
259
 
328
 
-
 
329
 
-
 
330
 
-
 
331
#if HW_VERSION==0x01
260
ISR(TIM0_OVF_vect) {
332
ISR(TIM0_OVF_vect) {
-
 
333
#else
-
 
334
ISR(TIMER0_OVF_vect) {
-
 
335
#endif
261
    tmr0_ovf++;
336
    tmr0_ovf++;
262
 
337
 
263
	// Clk/1 TCCR0B = (1<< CS00);
338
	// Clk/1 TCCR0B = (1<< CS00);
264
	//20.0Mhz, 1ms = 78ovf
339
	//20.0Mhz, 1ms = 78ovf
265
	//16.5Mhz, 1ms = 64ovf
340
	//16.5Mhz, 1ms = 64ovf