Subversion Repositories group.electronics

Rev

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

Rev 130 Rev 131
Line 56... Line 56...
56
  PIN : Read input pin
56
  PIN : Read input pin
57
  */
57
  */
58
 
58
 
59
 
59
 
60
  /*
60
  /*
61
		PA0	-	Output			- HC595 DS
61
	PA0 -	Output		- HC595 DS
62
		PA1	- 	Output			- HC595 ST
62
	PA1 - 	Output		- HC595 ST
63
		PA2 -	Output			- HC595 SH
63
	PA2 -	Output		- HC595 SH
64
		PA3 -	Output			- 4511 blanking
64
	PA3 -	Output		- 4511 blanking
65
		PA4 -	SCK
65
	PA4 -	SCK
66
		PA5 -	Output			- Decimal Point
66
	PA5 -	Output		- Decimal Point
67
		PA6 -	SDA
67
	PA6 -	SDA
68
		PA7 -	Input, Pullup			- Flip button
68
	PA7 -	Input, Pullup	- Flip button
69
  */
69
  */
70
  DDRA          = 0B00101111;
70
  DDRA          = 0B00101111;
71
  PORTA         = 0B10001000;
71
  PORTA         = 0B10001000;
72
  /*
72
  /*
73
        PB0     - Input, PCInt, Pullup	- Rot1a
73
	PB0     - Input, PCInt, Pullup	- Rot1a
74
        PB1     - Input, PCInt, Pullup	- Rot1b
74
	PB1     - Input, PCInt, Pullup	- Rot1b
75
        PB2     - Input, Pullup			- Swap button
75
	PB2     - Input, Pullup		- Swap button
76
        PB3     - N/A					- Reset (Future - I2C address select)
76
	PB3     - N/A			- Reset (Future - I2C address select)
77
  */
77
  */
78
  DDRB          = 0B00000000;
78
  DDRB          = 0B00000000;
79
  PORTB         = 0B00000111;
79
  PORTB         = 0B00000111;
80
 
80
 
81
    systime = 0;
81
    systime = 0;
Line 90... Line 90...
90
 
90
 
91
    twires_begin(I2C_SLAVE_ADDR);
91
    twires_begin(I2C_SLAVE_ADDR);
92
    twires_onReceive(receiveEvent);
92
    twires_onReceive(receiveEvent);
93
 
93
 
94
    wdt_enable(WDTO_1S);	// Watchdog for 1 sec
94
    wdt_enable(WDTO_1S);	// Watchdog for 1 sec
95
    sei();					// Enable interrupts
95
    sei();			// Enable interrupts
96
 
96
 
-
 
97
    
-
 
98
    // Init and clear the displays
97
    hc595_init();
99
    hc595_init();
98
    hc595_write(0x00);
100
    hc595_write(0x00);
99
 
101
 
100
    uint32_t refresh = systime;
102
    uint32_t refresh = systime;
101
    uint8_t disIdx = 0;
103
    uint8_t disIdx = 0;
Line 111... Line 113...
111
    		disVal[0] = input.outer & 0x0f;
113
    		disVal[0] = input.outer & 0x0f;
112
    		disVal[1] = input.outer >> 4;
114
    		disVal[1] = input.outer >> 4;
113
    	}
115
    	}
114
 
116
 
115
		if (pcInt.current != pcInt.last) {
117
		if (pcInt.current != pcInt.last) {
-
 
118
			cei(); 		// Dont process interrupts for a bit
116
			pcInterrupt();
119
			pcInterrupt();
-
 
120
			sei();
117
		}
121
		}
118
 
122
 
119
		if (systime > refresh) {
123
		if (systime > refresh) {
120
			writeSegment(disIdx, disVal[disIdx]);
124
			writeSegment(disIdx, disVal[disIdx]);
121
			refresh = systime + DISPLAY_REFRESH;
125
			refresh = systime + DISPLAY_REFRESH;
Line 131... Line 135...
131
void receiveEvent(uint8_t bytes) {
135
void receiveEvent(uint8_t bytes) {
132
	uint8_t cmd = twires_receive();
136
	uint8_t cmd = twires_receive();
133
	bytes--;
137
	bytes--;
134
	switch (cmd) {
138
	switch (cmd) {
135
		case (0x01): {
139
		case (0x01): {
-
 
140
			// Send 2 bytes with version details
136
			twires_send(HW_VERSION);
141
			twires_send(HW_VERSION);
137
			twires_send(SW_VERSION);
142
			twires_send(SW_VERSION);
138
		}
143
		}
139
			break;
144
			break;
140
		case (0x03): {
145
		case (0x03): {
-
 
146
			// Enable debugging mode
141
			user_debug = twires_receive();
147
			user_debug = twires_receive();
142
		}
148
		}
143
			break;
149
			break;
144
		case (0x05): {
150
		case (0x05): {
-
 
151
			// Receive n numbers of digits to DISPLAY_REFRESH
-
 
152
			// High nibble = segment number
-
 
153
			// Low nibble = digit to display
145
			while (bytes--) {
154
			while (bytes--) {
146
				uint8_t data = twires_receive();
155
				uint8_t data = twires_receive();
147
				disVal[(data >> 4)] = (data & 0x0f);
156
				disVal[(data >> 4)] = (data & 0x0f);
148
			}
157
			}
149
		}
158
		}
150
			break;
159
			break;
151
		case (0x08): {
160
		case (0x08): {
-
 
161
			// Recieve 2 bytes, for the decimal points
-
 
162
			//  5 bits used in eac byte
152
			disPt[0] = twires_receive();
163
			disPt[0] = twires_receive();
153
			disPt[1] = twires_receive();
164
			disPt[1] = twires_receive();
154
		}
165
		}
155
			break;
166
			break;
156
		case (0x09): {
167
		case (0x09): {
-
 
168
			// Reset the rotary
157
			input.outer = 0;
169
			input.outer = 0;
158
			input.inner = 0;
170
			input.inner = 0;
159
		}
171
		}
160
			break;
172
			break;
161
		case (0x0a): {
173
		case (0x0a): {
-
 
174
			// Send the value of the rotary count
162
			twires_send(input.outer);
175
			twires_send(input.outer);
163
		}
176
		}
164
			break;
177
			break;
165
		/* Not used in this version
178
		/* Not used in this version
166
		case (0x0b): {
179
		case (0x0b): {
Line 168... Line 181...
168
		}
181
		}
169
			break;
182
			break;
170
		*/
183
		*/
171
 
184
 
172
		case (0x0c): {
185
		case (0x0c): {
-
 
186
			// Send that status of the buttons
173
			uint8_t out = (!rbi(SB_INPUT, SB_PIN)) << 1;
187
			uint8_t out = (!rbi(SB_INPUT, SB_PIN)) << 1;
174
			out |= (!rbi(FB_INPUT, FB_PIN));
188
			out |= (!rbi(FB_INPUT, FB_PIN));
175
			twires_send(out);
189
			twires_send(out);
176
		}
190
		}
177
		/* Comined into 0x0c
191
		/* Comined into 0x0c
Line 179... Line 193...
179
			twires_send(rbi(FB_INPUT, FB_PIN));
193
			twires_send(rbi(FB_INPUT, FB_PIN));
180
		}
194
		}
181
			break;
195
			break;
182
		*/
196
		*/
183
		default: {
197
		default: {
-
 
198
			// Don't know what it is, empty buffer
184
			while (twires_available())
199
			while (twires_available())
185
				twires_receive();
200
				twires_receive();
186
		}
201
		}
187
			break;
202
			break;
188
	}
203
	}
Line 194... Line 209...
194
	if (digit > 4 && rbi(disPt[1], digit-5)) dp = 1;
209
	if (digit > 4 && rbi(disPt[1], digit-5)) dp = 1;
195
 
210
 
196
	cbi(BL_PORT, BL_PIN);			// Blank the 4511
211
	cbi(BL_PORT, BL_PIN);			// Blank the 4511
197
	cbi(DP_PORT, DP_PIN);			// Drop the DP before changing digit
212
	cbi(DP_PORT, DP_PIN);			// Drop the DP before changing digit
198
	hc595_write((value << 4) | (digit & 0x0f)); //Write the value and digit
213
	hc595_write((value << 4) | (digit & 0x0f)); //Write the value and digit
199
	if (dp) sbi(DP_PORT, DP_PIN);	// Add DP if required
214
	if (dp) sbi(DP_PORT, DP_PIN);		// Add DP if required
200
	sbi(BL_PORT, BL_PIN);			// Enable the 4511
215
	sbi(BL_PORT, BL_PIN);			// Enable the 4511
201
}
216
}
202
 
217
 
203
void pcInterrupt() {
218
void pcInterrupt() {
204
		wdt_reset();
-
 
205
 
-
 
206
		pcInt.mask = pcInt.current ^ pcInt.last;
219
	pcInt.mask = pcInt.current ^ pcInt.last;
207
		pcInt.last = pcInt.current;
220
	pcInt.last = pcInt.current;
208
 
221
 
209
        if (!pcInt.mask)
222
        if (!pcInt.mask)
210
        	return;
223
        	return;
211
 
224
 
212
        // Check which pin caused the interrupt. If they both
225
        // Check which pin caused the interrupt. If they both
Line 240... Line 253...
240
	//20.0Mhz, 1ms = 78ovf
253
	//20.0Mhz, 1ms = 78ovf
241
	//16.5Mhz, 1ms = 64ovf
254
	//16.5Mhz, 1ms = 64ovf
242
	//16.0Mhz, 1ms = 62ovf
255
	//16.0Mhz, 1ms = 62ovf
243
	//12.0Mhz, 1ms = 46ovf
256
	//12.0Mhz, 1ms = 46ovf
244
	// 8.0Mhz, 1ms = 31ovf
257
	// 8.0Mhz, 1ms = 31ovf
245
    // 8.0Mhz, .5ms = 15ovf, 160r
258
	// 8.0Mhz, .5ms = 15ovf, 160r
246
 
259
 
247
	if (tmr0_ovf>=15) {
260
	if (tmr0_ovf>=15) {
248
			systime++;
261
			systime++;
249
			tmr0_ovf = 0;
262
			tmr0_ovf = 0;
250
	}
263
	}