Steven Beard / Mbed 2 deprecated MCP4822_demo

Dependencies:   mbed MCP4822A

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * MCP4822A DAC array library demonstration program.
00003  *
00004  * Copyright (c) 2011 Steven Beard, UK Astronomy Technology Centre.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a copy
00007  * of this software and associated documentation files (the "Software"), to deal
00008  * in the Software without restriction, including without limitation the rights
00009  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00010  * copies of the Software, and to permit persons to whom the Software is
00011  * furnished to do so, subject to the following conditions:
00012  *
00013  * The above copyright notice and this permission notice shall be included in
00014  * all copies or substantial portions of the Software.
00015  *
00016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00017  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00018  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00019  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00020  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00021  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00022  * THE SOFTWARE.
00023  */
00024 
00025 #include "mbed.h"
00026 #include "MCP4822A.h"
00027 
00028 /*
00029  * In this example there are 4 DAC chips daisy-chained on the SPI bus,
00030  * which together provide 8 channels.
00031  */
00032 const int ndacs = 4;
00033 const int nchans = ndacs * 2;
00034 
00035 /*
00036  * The following mbed pins are used:
00037  *
00038  * p11 - SPI mosi
00039  * p13 - SPI sclk
00040  * p14, p15, p16, p17 - CS pins, each connected to a separate DAC.
00041  * p21 - LDAC pin, connected to all DAC chips.
00042  */
00043 PinName cspins[] = {(p14),(p15),(p16),(p17)};
00044 MCP4822A MCP(ndacs, p11, p13, cspins, p21);    // MOSI, SCLK, nCS-list, nLdac
00045 
00046 int main() {
00047     int i, buffer;
00048 
00049     printf("\r\n\r\nSetting SPI clock frequency to 10 MHz.\r\n");
00050     MCP.frequency(10000000);
00051     
00052     // Begin by setting all the DACs to 1000 millivolts at gain 2.
00053     printf("Loading all channels with 1000mV at gain 2...\r\n");
00054     MCP.latch_disable();
00055     for (i=0; i<ndacs; i++) {
00056         MCP.writeA2(i, 1000);
00057         MCP.writeB2(i, 1000);
00058     }
00059     wait(4);
00060     printf("Latching now.\n");
00061     MCP.latch_enable();
00062     wait(4);
00063 
00064     // Initialise an array of test voltages (in mV).
00065     int test_voltages[nchans];
00066     for (i=0; i<nchans; i++) {
00067         test_voltages[i] = i * 500;
00068     }
00069 
00070     while (1) {
00071 
00072         // Write the array of test voltages to the DACs
00073         // (at gain 2 with automatic latching).
00074         printf("Writing: ");
00075         for (i=0; i<nchans; i++) {
00076             printf("%d ", test_voltages[i]);
00077         }
00078         printf("\r\n");
00079         MCP.write(nchans, test_voltages, 2, 1);
00080         wait(4);
00081 
00082         //Move the values in the array along one place.
00083         buffer = test_voltages[0];
00084         for (i=0; i<(nchans-1); i++) {
00085             test_voltages[i] = test_voltages[i+1];
00086         }
00087         test_voltages[nchans-1] = buffer;
00088     }
00089 }