Attachment 'GPIB_USB.c'
Download 1 /***********************************************************************/
2 /* */
3 /* FILE :GPIB_USB.c */
4 /* DATE :Wed, Sep 29, 2010 */
5 /* DESCRIPTION :Main Program */
6 /* CPU TYPE :Other */
7 /* */
8 /* This is the main file for the GPIB-USB converter project V1.0 */
9 /* (C) 2010 by R. Schuster */
10 /* 2011-06-25 RudolfReuter, added comand B, D, E, L, U
11 /***********************************************************************/
12
13 #include "Declarations.h"
14 #include "stdio.h"
15 #include "stdlib.h"
16
17 // interrupt functions
18 #pragma interrupt UART1_receive_int;
19
20 //external functions
21 void Init_Gpib_interface(void);
22 unsigned int send_gpib_command(unsigned int addr,unsigned char *sendstring);
23 unsigned int send_gpib_query(unsigned int addr,unsigned char *sendstring);
24 unsigned int send_gpib_byte(unsigned int mode, unsigned int data);
25 // v. 1.1 2011-06-24 RR
26 unsigned int send_gpib_command_byte(unsigned int addr,unsigned char data);
27 unsigned int send_gpib_listen(unsigned int addr);
28 unsigned int send_gpib_unlisten(void);
29
30 // global Variables
31 char ID_String[] ="GPIB/USB converter V1.1\r\n";
32 char buff[20];
33 char input_buffer[256]; // do NOT change size, because of pointers!
34 char cmd_buffer[20];
35 char cmd;
36 char cmd_pointer,wr_pointer,rd_pointer,Timer_1ms_over;
37 char trmEOI; // terminate on EOI only
38 char res_data; // restore file data byte
39 unsigned int gpib_comand;
40
41 unsigned int deviceaddr,weight,komma,n;
42
43 unsigned long gpib_timeout= 200000;// gpib- timeout default = 200ms
44
45 union {
46 struct {
47 unsigned char data;
48 unsigned char flags;
49 } byte;
50 unsigned int word;
51 } U1input;
52
53
54 // function prototypes
55 void com_init(void); // set UART1 = COM with 38400 Baud 8N1
56 void sendTxd1(char data); // send one byte to UART1
57 void com_print(char *daten); // send string to UART1
58
59
60 //************************************* external oscillator with 20 MHz
61 void OSC_Ext_20Mhz(void)
62 {
63 // till now processor is running with internal 128kHz / 8 clock
64 prc0 = 1; // Protect off
65 cm13 = 1; // Xin Xout pin -> switch to external clock
66 cm15 = 1; // XCIN-XCOUT drive capacity select HIGH -> high driver power
67 cm05 = 0; // Main Clock (Xin-Xout) stop bit ON
68 cm16 = 0; // Main clock = No division mode
69 cm17 = 0; // cm16+17 CPU clock division
70 cm06 = 0; // CM16 and CM17 enable -> enable prescaler
71 asm("nop"); // Waiting for stable of oscillation
72 asm("nop");
73 asm("nop");
74 asm("nop");
75 ocd2 = 0; // Select main clock
76 prc0 = 0; // Protect on
77 } // now running with 20MHz
78
79
80 //************************************* UART1 functions*************************
81
82 //************************************* UART1 = 38400 Baud 8N1
83 void com_init(void)
84 {
85 u1brg = 32; // 38400 baud
86 u1mr = 0x05; // 0000.0101 UART1: 8bit; int. CLK; 1 Stop Bit; No parity; No Sleep
87 // || | |^^^ 8 Bit
88 // || | ^ Internal clock
89 // || ^ 1 Stop Bit
90 // |^ no parity
91 // ^ Sleep Mode not activated
92
93 u1c0 = 0x00; // 0000.0000 UART1: f1 as CLK; CTS, RTS disabled
94 // |||| ^^ f1 is selected as clock
95 // ^ write 0
96
97
98
99
100 u1c1 = 0x05; // 0000.0101 UART1: Rx, TX enabled, 00000101
101 // | ^ Tx enabled
102 // ^ Rx enabled
103
104 txd1sel=1;
105 ucon = 0x20; // P00=RXD11, 00100100, 00100000
106 s1ric = 6; // UART1 receive interrupt level = 6
107 re_u1c1=1; // Receiver enabled
108 }
109
110
111 //************************************* send one byte to UART1
112
113 void sendTxd1(unsigned char data)
114 {
115 while (ti_u1c1 == 0); //Wait for transmission buffer emty
116 u1tb = data; // Set transmission data
117 te_u1c1 = 1; // Transmission enabled
118 }
119
120 //************************************* send string to UART1
121 void com_print(char *daten)
122 {
123 while( *daten) { sendTxd1(*daten++); }
124 }
125
126 //************************************* receive one byte from UART1
127 void UART1_receive_int (void)
128 {
129
130 U1input.word = u1rb;
131 input_buffer[wr_pointer++] = U1input.byte.data;
132 re_u1c1 = 1;
133 }
134
135 //auxiliary function: return position of char c in string buf (0 if not present)
136 unsigned int instr(char *buf, unsigned int c)
137 {
138 unsigned int n=0;
139
140 while (buf[n])
141 {
142 if (buf[n++] == c) return n;
143 }
144 return 0;
145 }
146
147
148 void main(void)
149 {
150 unsigned int error;
151
152 asm("FCLR I"); // Interrupts disabled
153
154 OSC_Ext_20Mhz(); // ext. oscillator with 20 MHz
155
156 com_init(); // Init UART 1
157
158 asm("FSET I"); // enable interrupts
159 Init_Gpib_interface(); // init gpib ports
160
161 //sprintf(buff,"%d %d\r\n", wr_pointer, rd_pointer); // 2011-06-16 RR for debug
162 //com_print(buff);
163
164 while (1)
165 {
166 while (wr_pointer != rd_pointer) //at least one byte received on UART1
167 {
168 cmd = input_buffer[rd_pointer++];
169 //sprintf(buff, "%c", cmd); // 2011-06-16 RR for debug
170 //com_print(buff);
171 if (cmd == LINEFEED) //LF= received String completed
172 {
173 cmd_buffer[cmd_pointer] = '\0';
174 switch (cmd_buffer[0])
175 {
176 case 'B': //Send Command Byte to specified device (no answer) 2011-06-19 RR
177 {
178 komma = instr(&cmd_buffer[0],',');
179 deviceaddr=0;
180
181 if ((komma>2) && (komma <5))
182 {
183 weight=1;
184 for (n=komma-2;n==1;n--) // retrieve deviceaddr
185 {
186 deviceaddr= deviceaddr + ((cmd_buffer[n] - 0x30) * weight);
187 weight = weight * 10;
188 }
189 gpib_comand = atoi(cmd_buffer +komma);
190 error = send_gpib_command_byte(deviceaddr, gpib_comand);
191 if (error)
192 {
193 sprintf(buff,"Error %d\r\n",error);
194 com_print(buff);
195 }
196 }
197 break;
198 }
199 case 'C': //Reset and Init GPIB Bus
200 {
201 Init_Gpib_interface();
202 break;
203 }
204 case 'D': //Send GPIB Data byte
205 {
206 while (wr_pointer == rd_pointer); //wait for one byte received on UART1
207 res_data = input_buffer[rd_pointer++];
208 error = send_gpib_byte(DATA, res_data);
209 // in order to have a handshake to control speed
210 sendTxd1(res_data);
211 if (error)
212 {
213 sprintf(buff,"Error %d\r\n",error);
214 com_print(buff);
215 }
216 }
217 case 'E': //Send string to specified device and read back answer 2011-06-19 RR
218 {
219 komma = instr(&cmd_buffer[0],',');
220 deviceaddr=0;
221 trmEOI = 1; // terminate on EOI only, no LineFeed
222
223 if ((komma>2) && (komma <5))
224 {
225 weight=1;
226 for (n=komma-2;n==1;n--) // retrieve deviceaddr
227 {
228 deviceaddr= deviceaddr + ((cmd_buffer[n] - 0x30) * weight);
229 weight = weight * 10;
230 }
231 error = send_gpib_query(deviceaddr,&cmd_buffer[komma]);
232 if (error)
233 {
234 sprintf(buff,"Error %d\r\n",error);
235 com_print(buff);
236 }
237 }
238 break;
239 }
240 case 'G': //Send GPIB Command
241 {
242 gpib_comand = atoi(cmd_buffer +1);
243 error = send_gpib_byte(COMMAND,gpib_comand);
244 if (error)
245 {
246 sprintf(buff,"Error %d\r\n",error);
247 com_print(buff);
248 }
249 break;
250 }
251 case 'I': //Send ID String
252 {
253 com_print(ID_String);
254 break;
255 }
256 case 'L': //Address Listener (no answer) 2011-06-19 RR
257 {
258 komma = instr(&cmd_buffer[0],',');
259 deviceaddr=0;
260
261 if ((komma>2) && (komma <5))
262 {
263 weight=1;
264 for (n=komma-2;n==1;n--) // retrieve deviceaddr
265 {
266 deviceaddr= deviceaddr + ((cmd_buffer[n] - 0x30) * weight);
267 weight = weight * 10;
268 }
269 error = send_gpib_listen(deviceaddr);
270 if (error)
271 {
272 sprintf(buff,"Error %d\r\n",error);
273 com_print(buff);
274 }
275 }
276 break;
277 }
278 case 'R': //Send string to specified device and read back answer
279 {
280 komma = instr(&cmd_buffer[0],',');
281 deviceaddr=0;
282
283 if ((komma>2) && (komma <5))
284 {
285 weight=1;
286 for (n=komma-2;n==1;n--) // retrieve deviceaddr
287 {
288 deviceaddr= deviceaddr + ((cmd_buffer[n] - 0x30) * weight);
289 weight = weight * 10;
290 }
291 error = send_gpib_query(deviceaddr,&cmd_buffer[komma]);
292 if (error)
293 {
294 sprintf(buff,"Error %d\r\n",error);
295 com_print(buff);
296 }
297 }
298 break;
299 }
300 case 'S': //Check SRQ line (if device needs to be serviced)
301 {
302 if (SRQ) error=0; else error =1;
303 sprintf(buff,"SRQ %d\r\n",error);
304 com_print(buff);
305 break;
306 }
307 case 'T': //Set GPIB Timeout in ms
308 {
309 gpib_timeout = atol(cmd_buffer+1);
310 break;
311 }
312 case 'U': //Output LF+EOI, UNL, UNT
313 {
314 send_gpib_unlisten();
315 break;
316 }
317 case 'W': //Send string to specified device (no answer)
318 {
319 komma = instr(&cmd_buffer[0],',');
320 deviceaddr=0;
321
322 if ((komma>2) && (komma <5))
323 {
324 weight=1;
325 for (n=komma-2;n==1;n--) // retrieve deviceaddr
326 {
327 deviceaddr= deviceaddr + ((cmd_buffer[n] - 0x30) * weight);
328 weight = weight * 10;
329 }
330 error = send_gpib_command(deviceaddr,&cmd_buffer[komma]);
331 if (error)
332 {
333 sprintf(buff,"Error %d\r\n",error);
334 com_print(buff);
335 }
336 }
337 break;
338 }
339 default: break;
340 }
341 cmd_pointer = 0;
342 }
343 else
344 {
345 if (cmd != CARRIAGE_RETURN) cmd_buffer[cmd_pointer++] = cmd;
346 }
347 }
348 }
349 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.