Carmel (MAXREFDES18#) Code Documentation  V01.00
High Accuracy Analog Current/Voltage Output
 All Files Functions Variables Macros Pages
MAXREFDES18.c
Go to the documentation of this file.
1 
29 /*
30  * Copyright (C) 2012 Maxim Integrated Products, Inc., All Rights Reserved.
31  *
32  * Permission is hereby granted, free of charge, to any person obtaining a
33  * copy of this software and associated documentation files (the "Software"),
34  * to deal in the Software without restriction, including without limitation
35  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
36  * and/or sell copies of the Software, and to permit persons to whom the
37  * Software is furnished to do so, subject to the following conditions:
38  *
39  * The above copyright notice and this permission notice shall be included
40  * in all copies or substantial portions of the Software.
41  *
42  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
43  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45  * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
46  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
47  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
48  * OTHER DEALINGS IN THE SOFTWARE.
49  *
50  * Except as contained in this notice, the name of Maxim Integrated Products
51  * shall not be used except as stated in the Maxim Integrated Products
52  * Branding Policy.
53  *
54  * The mere transfer of this software does not imply any licenses
55  * of trade secrets, proprietary technology, copyrights, patents,
56  * trademarks, maskwork rights, or any other form of intellectual
57  * property whatsoever. Maxim Integrated Products retains all ownership rights.
58  *
59  ***************************************************************************/
76 #include <stdio.h>
77 #include "platform.h"
78 #include "menu.h"
79 #include "utilities.h"
80 #include "MAXREFDES18.h"
81 
82 #define MAJOR_REVISION 1
83 #define MINOR_REVISION 0
84 
85 // Globals
86 char g_sInputString[ INPUT_STRING_MAX_SIZE ]; //This string will hold user input from terminal.
89 
91 XGpio g_xGpioLed;
93 
94 u32 g_unActivePeripheralAddressSPI =XPAR_SPI_0_BASEADDR;
95 
96 int main()
107 {
108  // Variables for the main() function
109  u8 uchInput=0;
110  int nMenuState=0;
111  XUartLite Uart;
112  u8 uchMOSIBuffer[3];
113  u8 uchMISOBuffer[3];
114  int DAC_CODE = 0;
115 
116  // Xilinx provided utility to initialize the hardware/firmware drivers and enable/disable cache
117  init_platform();
118 
119  // Clear the Hyperterminal screen by sending printf("\033[2J");
120  menu_cls();
121 
122  // Initialize the USB UART on the LX9 board (used to communicate with Hyperterminal)
123  XUartLite_Initialize(&Uart,DEFAULT_HYPERTERMINAL_UART_ID);
124  XUartLite_ResetFifos(&Uart);
125  delay(100);
126 
127  // Toggle the LEDs so that the user knows the board is awake
128  XGpio_Initialize(&g_xGpioLed, XPAR_LEDS_4BITS_DEVICE_ID);
129  XGpio_SetDataDirection(&g_xGpioLed, 1, 0x00000000); // Set the LED peripheral to outputs
131 
132  // Initialize the GPIO connected to PMOD Ports A, B.
133  XGpio_Initialize(&g_xGpioPmodPortA, XPAR_AXI_GPIO_0_DEVICE_ID);
134  XGpio_Initialize(&g_xGpioPmodPortB, XPAR_AXI_GPIO_1_DEVICE_ID);
135  XGpio_SetDataDirection(&g_xGpioPmodPortA, 1, 0x00000000); // Set the GPIO to all be outputs
136  XGpio_SetDataDirection(&g_xGpioPmodPortB, 1, 0x00000000); // Set the GPIO to all be outputs
137 
138  // Configure the PMOD Port Multiplexers for the default PMOD interfaces (ports a valid only).
140 
141  // Clear the Screen, and then display the big Maxim banner for about 2 seconds
142  menu_cls();
144  printf("Maxim Carmel (MAXREFDES18#) for the LX9 Board\r\n");
145  printf("Revision v%02d.%02d\r\n",MAJOR_REVISION,MINOR_REVISION);
146 
147  // write the MAX5316 configuration register to set it in normal operation mode.
148  // R3:R0=0100, D15:D0=0b0010 1000 0000 0000, +0b0000.
149  uchMOSIBuffer[0]=0x42;
150  uchMOSIBuffer[1]=0x80;
151  uchMOSIBuffer[2]=0x00;
152  SpiRW(g_unActivePeripheralAddressSPI, 1, 0, uchMOSIBuffer, uchMISOBuffer, 3, 0, 2);
153 
154  delay(ABOUT_ONE_SECOND * 3);
155 
156  /* The main menu loop runs infinitely to display an output mode or range select menu.
157  */
158  while(1)
159  {
160  switch(nMenuState)
161  {
162  case MODE_MENU:
164  nMenuState = WAIT_KEYPRESS;
165  do{
167  printf("%c\r\n",uchInput+48);
168  if(uchInput<=1)
169  {
170  if(uchInput==0)
171  nMenuState = CURRENT_RANGE_MENU;
172  else
173  nMenuState = VOLTAGE_RANGE_MENU;
174  }
175  }while(uchInput>1);
176  break;
177 
178  // current modes.
179  case CURRENT_RANGE_MENU:
181  nMenuState = WAIT_KEYPRESS;
182  do{
184  printf("%c\r\n",uchInput+48);
185  if(uchInput<=3)
186  {
187  if(uchInput==0)
188  nMenuState = CURRENT_RANGE_20_20_MENU;
189  else if(uchInput==1)
190  nMenuState = CURRENT_RANGE_0_20_MENU;
191  else if(uchInput==2)
192  nMenuState = CURRENT_RANGE_4_20_MENU;
193  else
194  nMenuState = MODE_MENU;
195  }
196  }while(uchInput>3);
197  break;
198 
200  nMenuState = WAIT_KEYPRESS;
201 
202  printf("Mode 1: Bipolar current +/-20mA.\r\n");
203  printf("IdealIout = 0.5 * (VAIN - 0.5 * VREFIN)/Rsense.\r\n");
204  printf("Where Rsense = 42.2OHM for 20%% OVERRANGE, Rsense = 48.7OHM for 5%% OVERRANGE.\r\n");
205  printf("VAIN = 4.096V * DAC_CODE/65536.\r\n");
206  printf("VREFIN = 4.096V.\r\n");
207 
209 
210  // 1. configure the MAX15500. DIN[10], DIN[2:0], reserved, default to 0.
211  // DIN[13:11]=001, mode DIN[9:7]=001, DIN[6:4]=VBOTH[2:0], default to 101 = +/-20V. DIN[3]= thermal protection on/off.
212  uchMOSIBuffer[0]=0x23;
213  uchMOSIBuffer[1]=0x60;
214  SpiRW(g_unActivePeripheralAddressSPI, 1, 0, uchMOSIBuffer, uchMISOBuffer, 2, 0, 1);
215 
216  // 2. write MAX5316 DIN register. 0x0000
217  // R3:R0=0001, D15:D0=16-bit straight binary data, +0b0000.
218  uchMOSIBuffer[0]=0x10 + ((DAC_CODE >> 12) & 0xF);
219  uchMOSIBuffer[1]=(DAC_CODE >> 4) & 0xFF;
220  uchMOSIBuffer[2]=(DAC_CODE << 4) & 0xF0;
221 
222  SpiRW(g_unActivePeripheralAddressSPI, 1, 0, uchMOSIBuffer, uchMISOBuffer, 3, 0, 2);
223 
224  nMenuState = CURRENT_RANGE_MENU;
225  break;
227  nMenuState = WAIT_KEYPRESS;
228 
229  printf("Mode 2: Unipolar current 0 to 20mA.\r\n");
230  printf("IdealIout = 0.2625 * (VAIN - 0.05 * VREFIN)/Rsense.\r\n");
231  printf("Where Rsense = 42.2OHM for 20%% OVERRANGE, Rsense = 48.7OHM for 5%% OVERRANGE.\r\n");
232  printf("VAIN = 4.096V * DAC_CODE/65536.\r\n");
233  printf("VREFIN = 4.096V.\r\n");
234 
236 
237  // 1. configure the MAX15500. DIN[10], DIN[2:0], reserved, default to 0.
238  // DIN[13:11]=001, mode DIN[9:7]=010, DIN[6:4]=VBOTH[2:0], default to 101 = +/-20V. DIN[3]= thermal protection on/off.
239  uchMOSIBuffer[0]=0x25;
240  uchMOSIBuffer[1]=0x60;
241  SpiRW(g_unActivePeripheralAddressSPI, 1, 0, uchMOSIBuffer, uchMISOBuffer, 2, 0, 1);
242 
243  // 2. write MAX5316 DIN register. 0x0000
244  // R3:R0=0001, D15:D0=16-bit straight binary data, +0b0000.
245  uchMOSIBuffer[0]=0x10 + ((DAC_CODE >> 12) & 0xF);
246  uchMOSIBuffer[1]=(DAC_CODE >> 4) & 0xFF;
247  uchMOSIBuffer[2]=(DAC_CODE << 4) & 0xF0;
248 
249  SpiRW(g_unActivePeripheralAddressSPI, 1, 0, uchMOSIBuffer, uchMISOBuffer, 3, 0, 2);
250 
251  nMenuState = CURRENT_RANGE_MENU;
252  break;
254  nMenuState = WAIT_KEYPRESS;
255 
256  printf("Mode 3: Unipolar current 4 to 20mA.\r\n");
257  printf("IdealIout = 0.2625 * (VAIN - 0.05 * VREFIN)/Rsense.\r\n");
258  printf("Where Rsense = 42.2OHM for 20%% OVERRANGE, Rsense = 48.7OHM for 5%% OVERRANGE.\r\n");
259  printf("VAIN = 4.096V * DAC_CODE/65536.\r\n");
260  printf("VREFIN = 4.096V.\r\n");
261 
263 
264  // 1. configure the MAX15500. DIN[10], DIN[2:0], reserved, default to 0.
265  // DIN[13:11]=001, mode DIN[9:7]=011, DIN[6:4]=VBOTH[2:0], default to 101 = +/-20V. DIN[3]= thermal protection on/off.
266  uchMOSIBuffer[0]=0x27;
267  uchMOSIBuffer[1]=0x60;
268  SpiRW(g_unActivePeripheralAddressSPI, 1, 0, uchMOSIBuffer, uchMISOBuffer, 2, 0, 1);
269 
270  // 2. write MAX5316 DIN register. 0x0000
271  // R3:R0=0001, D15:D0=16-bit straight binary data, +0b0000.
272  uchMOSIBuffer[0]=0x10 + ((DAC_CODE >> 12) & 0xF);
273  uchMOSIBuffer[1]=(DAC_CODE >> 4) & 0xFF;
274  uchMOSIBuffer[2]=(DAC_CODE << 4) & 0xF0;
275 
276  SpiRW(g_unActivePeripheralAddressSPI, 1, 0, uchMOSIBuffer, uchMISOBuffer, 3, 0, 2);
277 
278  nMenuState = CURRENT_RANGE_MENU;
279  break;
280 
281  // voltage modes.
282  case VOLTAGE_RANGE_MENU:
284  nMenuState = WAIT_KEYPRESS;
285  do{
287  printf("%c\r\n",uchInput+48);
288  if(uchInput<=3)
289  {
290  if(uchInput==0)
291  nMenuState = VOLTAGE_RANGE_10_10_MENU;
292  else if(uchInput==1)
293  nMenuState = VOLTAGE_RANGE_0_10_MENU;
294  else if(uchInput==2)
295  nMenuState = VOLTAGE_RANGE_0_5_MENU;
296  else
297  nMenuState = MODE_MENU;
298  }
299  }while(uchInput>3);
300  break;
301 
303  nMenuState = WAIT_KEYPRESS;
304 
305  printf("Mode 5: Bipolar voltage +/-10V.\r\n");
306  printf("IdealVout = IdealGain * (VAIN - 0.5 * VREFIN).\r\n");
307  printf("Where IdealGain = 6 for 20%% OVERRANGE, IdealGain = 5.25 for 5%% OVERRANGE.\r\n");
308  printf("VAIN = 4.096V * DAC_CODE/65536.\r\n");
309  printf("VREFIN = 4.096V.\r\n");
310 
312 
313  // 1. configure the MAX15500. DIN[10], DIN[2:0], reserved, default to 0.
314  // DIN[13:11]=001, mode DIN[9:7]=101, DIN[6:4]=VBOTH[2:0], default to 101 = +/-20V. DIN[3]= thermal protection on/off.
315  uchMOSIBuffer[0]=0x2B;
316  uchMOSIBuffer[1]=0x60;
317  SpiRW(g_unActivePeripheralAddressSPI, 1, 0, uchMOSIBuffer, uchMISOBuffer, 2, 0, 1);
318 
319  // 2. write MAX5316 DIN register. 0x0000
320  // R3:R0=0001, D15:D0=16-bit straight binary data, +0b0000.
321  uchMOSIBuffer[0]=0x10 + ((DAC_CODE >> 12) & 0xF);
322  uchMOSIBuffer[1]=(DAC_CODE >> 4) & 0xFF;
323  uchMOSIBuffer[2]=(DAC_CODE << 4) & 0xF0;
324 
325  SpiRW(g_unActivePeripheralAddressSPI, 1, 0, uchMOSIBuffer, uchMISOBuffer, 3, 0, 2);
326 
327  nMenuState = VOLTAGE_RANGE_MENU;
328  break;
329 
331  nMenuState = WAIT_KEYPRESS;
332 
333  printf("Mode 6: Unipolar voltage 0 to 10V.\r\n");
334  printf("IdealVout = IdealGain * (VAIN - 0.05 * VREFIN).\r\n");
335  printf("Where IdealGain = 3.125 for 20%% OVERRANGE, IdealGain = 2.75 for 5%% OVERRANGE.\r\n");
336  printf("VAIN = 4.096V * DAC_CODE/65536.\r\n");
337  printf("VREFIN = 4.096V.\r\n");
338 
340 
341  // 1. configure the MAX15500. DIN[10], DIN[2:0], reserved, default to 0.
342  // DIN[13:11]=001, mode DIN[9:7]=110, DIN[6:4]=VBOTH[2:0], default to 101 = +/-20V. DIN[3]= thermal protection on/off.
343  uchMOSIBuffer[0]=0x2D;
344  uchMOSIBuffer[1]=0x60;
345  SpiRW(g_unActivePeripheralAddressSPI, 1, 0, uchMOSIBuffer, uchMISOBuffer, 2, 0, 1);
346 
347  // 2. write MAX5316 DIN register. 0x0000
348  // R3:R0=0001, D15:D0=16-bit straight binary data, +0b0000.
349  uchMOSIBuffer[0]=0x10 + ((DAC_CODE >> 12) & 0xF);
350  uchMOSIBuffer[1]=(DAC_CODE >> 4) & 0xFF;
351  uchMOSIBuffer[2]=(DAC_CODE << 4) & 0xF0;
352 
353  SpiRW(g_unActivePeripheralAddressSPI, 1, 0, uchMOSIBuffer, uchMISOBuffer, 3, 0, 2);
354 
355  nMenuState = VOLTAGE_RANGE_MENU;
356  break;
357 
359  nMenuState = WAIT_KEYPRESS;
360 
361  printf("Mode 7: Unipolar voltage 0 to 5V.\r\n");
362  printf("IdealVout = IdealGain * (VAIN - 0.05 * VREFIN).\r\n");
363  printf("Where IdealGain = 1.5625 for 20%% OVERRANGE, IdealGain = 1.375 for 5%% OVERRANGE.\r\n");
364  printf("VAIN = 4.096V * DAC_CODE/65536.\r\n");
365  printf("VREFIN = 4.096V.\r\n");
366 
368 
369  // 1. configure the MAX15500. DIN[10], DIN[2:0], reserved, default to 0.
370  // DIN[13:11]=001, mode DIN[9:7]=111, DIN[6:4]=VBOTH[2:0], default to 101 = +/-20V. DIN[3]= thermal protection on/off.
371  uchMOSIBuffer[0]=0x2F;
372  uchMOSIBuffer[1]=0x60;
373  SpiRW(g_unActivePeripheralAddressSPI, 1, 0, uchMOSIBuffer, uchMISOBuffer, 2, 0, 1);
374 
375  // 2. write MAX5316 DIN register. 0x0000
376  // R3:R0=0001, D15:D0=16-bit straight binary data, +0b0000.
377  uchMOSIBuffer[0]=0x10 + ((DAC_CODE >> 12) & 0xF);
378  uchMOSIBuffer[1]=(DAC_CODE >> 4) & 0xFF;
379  uchMOSIBuffer[2]=(DAC_CODE << 4) & 0xF0;
380 
381  SpiRW(g_unActivePeripheralAddressSPI, 1, 0, uchMOSIBuffer, uchMISOBuffer, 3, 0, 2);
382 
383  nMenuState = VOLTAGE_RANGE_MENU;
384  break;
385  }
386  }
387 
389  return 0;
390 }