Subversion Repositories group.electronics

Rev

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

Rev 143 Rev 150
Line 22... Line 22...
22
 
22
 
23
//static void requestData(uint8_t addr, uint8_t code, uint8_t* data, uint8_t len);
23
//static void requestData(uint8_t addr, uint8_t code, uint8_t* data, uint8_t len);
24
 
24
 
25
void print16(uint8_t dis, uint8_t dig, uint16_t val);
25
void print16(uint8_t dis, uint8_t dig, uint16_t val);
26
 
26
 
27
#define DISPLAY_ATTACHED 1
27
#define DISPLAYS_ATTACHED 2
28
#define INPUT_REFRESH 5
28
#define INPUT_REFRESH 5
29
 
29
 
30
struct display_type {
30
struct display_type {
31
	uint8_t address;
31
	uint8_t address;
32
	uint8_t value[10];
32
	uint8_t value[10];
33
	uint16_t decpts;
33
	uint16_t decpts;
34
 
34
 
35
	uint8_t rotary;		// State of the rotary encoder
35
	int8_t rotary;		// State of the rotary encoder
36
	uint8_t buttons;	// State of the buttons
36
	uint8_t buttons;	// State of the buttons
37
} display[2];
37
} display[DISPLAYS_ATTACHED];
38
 
38
 
39
 
39
 
40
 
40
 
41
static uint8_t usbReplyBuf[8];
41
static uint8_t usbReplyBuf[8];
42
static uint8_t update = 255;
42
static uint8_t update = 255;
Line 81... Line 81...
81
    usbInit();
81
    usbInit();
82
    sei();
82
    sei();
83
 
83
 
84
 
84
 
85
    // Set the displays to blank
85
    // Set the displays to blank
86
    display[0].address = 0x26;
-
 
87
    display[0].decpts = 0x00;
-
 
-
 
86
 
-
 
87
 
88
    uint8_t i;
88
    uint8_t i;
89
    for (i=0; i<DISPLAY_ATTACHED; i++) {
89
    for (i=0; i<DISPLAYS_ATTACHED; i++) {
-
 
90
    	display[i].address = 0x26 + i;
-
 
91
    	display[i].decpts = 0x00;
-
 
92
 
90
    	uint8_t j;
93
    	uint8_t j;
91
    	for (j=0; j<10; j++)
94
    	for (j=0; j<10; j++)
92
    		display[i].value[j] = 0x0a;
95
    		display[i].value[j] = 0x0a;
93
    	updateDisplay(i);
96
    	updateDisplay(i);
94
    }
97
    }
Line 103... Line 106...
103
    		updateDisplay(update);
106
    		updateDisplay(update);
104
    		update = 255;
107
    		update = 255;
105
    	}
108
    	}
106
 
109
 
107
		if (systime > refresh) {
110
		if (systime > refresh) {
108
			refresh = systime + 5;
111
			refresh = systime + INPUT_REFRESH;
109
			updateInput();
112
			updateInput();
110
		}
113
		}
111
    }
114
    }
112
    return 0;
115
    return 0;
113
}
116
}
114
 
117
 
115
static void updateInput() {
118
static void updateInput() {
116
	/*
-
 
117
	uint8_t data[1];
-
 
118
	requestData(0x4c, 0x0a, data, 1);
-
 
119
	display[0].rotary = data[0];
-
 
120
	requestData(0x4c, 0x0c, data, 1);
-
 
121
	display[0].buttons = data[0];
-
 
122
	*/
-
 
123
 
-
 
124
 
-
 
125
	i2cbb_Init();
-
 
126
	i2cbb_Start();
-
 
127
	i2cbb_Write( 0x4c );
-
 
128
	i2cbb_Write( 0x0a );
-
 
129
	i2cbb_Stop();
-
 
130
 
-
 
131
	i2cbb_Start();
-
 
132
	i2cbb_Write( 0x4d );
-
 
133
	display[0].rotary = i2cbb_Read(1);
-
 
134
	i2cbb_Stop();
-
 
135
 
-
 
136
	i2cbb_Init();
-
 
137
	i2cbb_Start();
-
 
138
	i2cbb_Write( 0x4c );
-
 
139
	i2cbb_Write( 0x0c );
-
 
140
	i2cbb_Stop();
-
 
141
 
-
 
142
	i2cbb_Start();
-
 
143
	i2cbb_Write( 0x4d );
-
 
144
	display[0].buttons = i2cbb_Read(1);
-
 
145
	i2cbb_Stop();
-
 
146
 
-
 
147
}
-
 
148
 
-
 
149
/*
-
 
150
static void requestData(uint8_t addr, uint8_t code, uint8_t* data, uint8_t len) {
-
 
151
	i2cbb_Init();
-
 
152
	i2cbb_Start();
-
 
153
	i2cbb_Write( addr );
-
 
154
	i2cbb_Write( code );
-
 
155
	i2cbb_Stop();
-
 
156
 
-
 
157
	uint8_t i;
119
	uint8_t i;
-
 
120
	for (i = 0; i < DISPLAYS_ATTACHED; i++) {
-
 
121
		i2cbb_Init();
-
 
122
		i2cbb_Start();
-
 
123
		i2cbb_Write( display[i].address << 1 );
158
	i2cbb_Write( addr + 1 );
124
		i2cbb_Write( 0x0a );
-
 
125
		i2cbb_Stop();
-
 
126
 
-
 
127
		i2cbb_Start();
-
 
128
		i2cbb_Write( (display[i].address << 1) + 1 );
-
 
129
		display[i].rotary += (int8_t)i2cbb_Read(1);
-
 
130
		i2cbb_Stop();
-
 
131
 
-
 
132
		i2cbb_Init();
-
 
133
		i2cbb_Start();
-
 
134
		i2cbb_Write( display[i].address << 1 );
-
 
135
		i2cbb_Write( 0x09 );
-
 
136
		i2cbb_Stop();
-
 
137
 
-
 
138
 
-
 
139
		i2cbb_Init();
-
 
140
		i2cbb_Start();
-
 
141
		i2cbb_Write( display[i].address << 1 );
159
	for (i=0; i<len; i++)
142
		i2cbb_Write( 0x0c );
-
 
143
		i2cbb_Stop();
-
 
144
 
-
 
145
		i2cbb_Start();
-
 
146
		i2cbb_Write( (display[i].address << 1) + 1 );
160
		data[i] = i2cbb_Read(1);
147
		display[i].buttons = i2cbb_Read(1);
161
	i2cbb_Stop();
148
		i2cbb_Stop();
-
 
149
	}
-
 
150
 
162
}
151
}
163
*/
-
 
164
 
152
 
165
static void updateDisplay(uint8_t dis) {
153
static void updateDisplay(uint8_t dis) {
166
    cbi(PORTB, PB0);
154
    cbi(PORTB, PB0);
167
 
155
 
168
    // Send the display buffer to display board
156
    // Send the display buffer to display board
169
    i2cbb_Init();
157
    i2cbb_Init();
170
    i2cbb_Start();
158
    i2cbb_Start();
171
    i2cbb_Write(0x4c);
159
    i2cbb_Write( display[dis].address << 1);
172
    i2cbb_Write(0x05);
160
    i2cbb_Write( 0x05 );
173
    uint8_t n;
161
    uint8_t n;
174
    for (n=0; n<10; n++) {
162
    for (n=0; n<10; n++) {
175
    	uint8_t send = (n << 4) | display[0].value[n];
163
    	uint8_t send = (n << 4) | display[dis].value[n];
176
    	i2cbb_Write( send );
164
    	i2cbb_Write( send );
177
    }
165
    }
178
    i2cbb_Stop();
166
    i2cbb_Stop();
179
 
167
 
180
    /*
168
    /*
Line 203... Line 191...
203
{
191
{
204
    usbRequest_t    *rq = (void *)data;
192
    usbRequest_t    *rq = (void *)data;
205
 
193
 
206
	switch (rq->bRequest ) {
194
	switch (rq->bRequest ) {
207
		case USB_SET_LATCH: {
195
		case USB_SET_LATCH: {
208
			update = 0;
196
			update = rq->wValue.bytes[0];;
209
			break;
197
			break;
210
		}
198
		}
211
		case USB_SET_DISPLAY1: {
199
		case USB_SET_DISPLAY1: {
212
			uint8_t dis = rq->wValue.bytes[1];
200
			uint8_t dis = rq->wValue.bytes[1];
213
			uint8_t dig = rq->wValue.bytes[0];
201
			uint8_t dig = rq->wValue.bytes[0];
Line 220... Line 208...
220
			else
208
			else
221
				cbi(display[dis].decpts, 1 << dig);
209
				cbi(display[dis].decpts, 1 << dig);
222
			//display[dis].decpts |= dp << dig;*/
210
			//display[dis].decpts |= dp << dig;*/
223
			break;
211
			break;
224
		}
212
		}
225
		case USB_SET_INT16: {
-
 
226
			uint8_t dis = rq->wValue.bytes[1];
-
 
227
			uint8_t dig = rq->wValue.bytes[0];
-
 
228
			uint16_t val = ((uint16_t)rq->wIndex.bytes[1]) << 8;
-
 
229
			val |= (rq->wIndex.bytes[0] & 0x0f);
-
 
230
			print16(dis, dig, val);
-
 
231
			break;
-
 
232
		}
-
 
233
		case USB_GET_INPUT: {
213
		case USB_GET_INPUT: {
-
 
214
			uint8_t i;
-
 
215
			for (i=0; i<DISPLAYS_ATTACHED; i++) {
234
			usbReplyBuf[0] = display[0].buttons;
216
				usbReplyBuf[(i*2)] = display[i].buttons;
235
			usbReplyBuf[1] = display[0].rotary;
217
				usbReplyBuf[(i*2+1)] = display[i].rotary;
-
 
218
				display[i].rotary = 0;
-
 
219
			}
-
 
220
 
236
			usbMsgPtr = usbReplyBuf;
221
			usbMsgPtr = usbReplyBuf;
237
			return sizeof(usbReplyBuf);
222
			return sizeof(usbReplyBuf);
238
		}
223
		}
239
 
224
 
240
	}
225
	}
241
	return 0;
226
	return 0;
242
}
227
}
243
 
228
 
244
void print16(uint8_t dis, uint8_t dig, uint16_t val) {
-
 
245
	uint8_t buf[] = {0x0a, 0x0a, 0x0a, 0x0a, 0x0a};
-
 
246
	uint8_t len = itobcd16(val, buf);
-
 
247
 
-
 
248
	uint8_t i;
-
 
249
	for (i=0; i<5; i++) {
-
 
250
		if (i < len)
-
 
251
			display[dis].value[dig+i] = buf[i];
-
 
252
	}
-
 
253
}
-
 
254
 
-
 
255
static void calibrateOscillator(void) {
229
static void calibrateOscillator(void) {
256
    uchar step = 128;
230
    uchar step = 128;
257
    uchar trialValue = 0, optimumValue;
231
    uchar trialValue = 0, optimumValue;
258
    int x, optimumDev;
232
    int x, optimumDev;
259
    int targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);
233
    int targetValue = (unsigned)(1499 * (double)F_CPU / 10.5e6 + 0.5);