Test application for simplified access to the Microchip 1/4/8-Channels 12-Bit A/D Converters with SPI Serial Interface library

Dependencies:   DebugLibrary MCP320x_SPI MCP4xxxx_SPI mbed

Here is the schematic used to validate both libraries MCP320x_SPI and MCP4xxx_SPI.

/media/uploads/Yann/mbed.jpg

Revision:
2:50b3bd9af686
Parent:
0:284a85288653
--- a/main.cpp	Fri Apr 05 13:43:23 2013 +0000
+++ b/main.cpp	Thu Jun 06 07:59:33 2013 +0000
@@ -9,13 +9,15 @@
     char choice;
     unsigned char potId;
     unsigned char adcId;
+    CMCP4xxxx_SPI::Mcp4xxxFamilly familly;
+    bool isShutdown;
 };
 
 /*
  * Declare functions
  */
 void AvailableIndicator(); // LED1 flashing for program while program is alive
-UserChoice DisplayMenuAndGetChoice(); // Display and get the user choice
+void DisplayMenuAndGetChoice(); // Display and get the user choice
 
 /*
  * Declare statics
@@ -28,16 +30,29 @@
 DigitalOut g_cs3208(p10); // /CS mapped on pin 10 for MCP3208
 DigitalOut g_cs42100(p8); // /CS mapped on pin 8 for MCP421pp
 DigitalOut g_cs41050(p11); // /CS mapped on pin 11 for MCP41050
-DigitalOut *g_csCurrentAdc = NULL;
 
-UserChoice g_userChoice; // Used to store user choice from displayed menu
+DigitalOut g_cs4152(p13); // /CS mapped on pin 13 for MCP4152
+DigitalOut g_cs4251(p14); // /CS mapped on pin 14 for MCP4251
+DigitalOut g_shdn4251(p15); // /SHDN mapped on pin 15 for MCP4251
+DigitalOut g_wp4251(p16); // /WP mapped on pin 16 for MCP4251
+
+DigitalOut *g_csCurrentAdc = NULL;    
+    
+static UserChoice g_userChoice; // Used to store user choice from displayed menu
 
 int main() {
+    g_userChoice.familly = CMCP4xxxx_SPI::_41xxx; // Default value used by the constructor
+    g_userChoice.isShutdown = false;
+
     // Deactivate all SPI devices
     g_cs3201 = 1;
     g_cs3208 = 1;
     g_cs42100 = 1;
     g_cs41050 = 1;
+    g_cs4152= 1; 
+    g_cs4251 = 1; 
+    g_shdn4251 = 1; // Active at low level 
+    g_wp4251 = 1; 
 
     unsigned char potLevel = 0x80; // Initial digital potentiometer value
     
@@ -46,36 +61,51 @@
     
     while (true) {
         // Retrieve user choices 
-        g_userChoice = DisplayMenuAndGetChoice();
+        DisplayMenuAndGetChoice();
         // Set the pot. value
         //      1. Enable de right digipot
         switch (g_userChoice.potId) {
             case 'a':
+                g_userChoice.familly = CMCP4xxxx_SPI::_42xxx;
+                g_digitalPot.SetFamilly(g_userChoice.familly);
                 g_cs42100 = 0;
                 break;
+            case 'b':
+               g_userChoice.familly = CMCP4xxxx_SPI::_41xxx;
+               g_digitalPot.SetFamilly(g_userChoice.familly);
+               g_cs41050 = 0;
+                break;
+            case 'c':
+                g_userChoice.familly = CMCP4xxxx_SPI::_42xx;
+                g_digitalPot.SetFamilly(g_userChoice.familly);
+                g_digitalPot.ReadRegister(CMCP4xxxx_SPI::Status);
+                g_cs4251 = 0; 
+                break;
             default:
-                g_cs41050 = 0;
+                g_userChoice.familly = CMCP4xxxx_SPI::_41xx;
+                g_digitalPot.SetFamilly(g_userChoice.familly);
+                g_digitalPot.ReadRegister(CMCP4xxxx_SPI::Status);
+                g_cs4152 = 0;
                 break;
         } // End of 'switch' statement
         //      2. Apply user action
         switch (g_userChoice.choice) {
             case 'a':
                 potLevel += 1;
-                g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel);
+                g_digitalPot.Write(CMCP4xxxx_SPI::WriteToDigiPot1, potLevel);
                 break;
             case 'b':
                 potLevel -= 1;
-                g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel);
+                g_digitalPot.Write(CMCP4xxxx_SPI::WriteToDigiPot1, potLevel);
                 break;
             case 'c':
-                potLevel -= 1;
-                g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot1);
+                g_userChoice.isShutdown = !g_userChoice.isShutdown;
+                g_digitalPot.Shutdown(CMCP4xxxx_SPI::ShutdownDigiPot1, g_userChoice.isShutdown);
                 break;
             case 'd':
-                //g_digitalPot->Reset();
                 potLevel = 0x80;
                 break;
-            default:
+           default:
                 std::cout << "Invalid user choice\r" << std::endl;
                 break;
         } // End of 'switch' statement
@@ -84,8 +114,14 @@
             case 'a':
                 g_cs42100 = 1;
                 break;
+            case 'b':
+                g_cs41050 = 1;
+                break;
+            case 'c':
+                g_cs4251 = 1; 
+                break;
             default:
-                g_cs41050 = 1;
+                g_cs4152 = 1;
                 break;
         } // End of 'switch' statement
         
@@ -101,9 +137,25 @@
                 break;
         } // End of 'switch' statement
         g_csCurrentAdc->write(0);
-        float sample = g_adc->Read(CMCP320x_SPI::CH3);
+        float sample = g_adc->Read(CMCP320x_SPI::CH2);
+        g_csCurrentAdc->write(1);
+        std::cout << "Voltage at PW0/41050: " << setprecision(5) << sample << "\r" << std::endl;
+        
+        g_csCurrentAdc->write(0);
+        sample = g_adc->Read(CMCP320x_SPI::CH3);
         g_csCurrentAdc->write(1);
-        std::cout << "Voltage at PW0: " << setprecision(5) << sample << "\r" << std::endl;
+        std::cout << "Voltage at PW0/4152: " << setprecision(5) << sample << "\r" << std::endl;
+        
+        g_csCurrentAdc->write(0);
+        sample = g_adc->Read(CMCP320x_SPI::CH4);
+        g_csCurrentAdc->write(1);
+        std::cout << "Voltage at PW0/4251#1: " << setprecision(5) << sample << "\r" << std::endl;
+        
+        g_csCurrentAdc->write(0);
+        sample = g_adc->Read(CMCP320x_SPI::CH5);
+        g_csCurrentAdc->write(1);
+        std::cout << "Voltage at PW0/4251#2: " << setprecision(5) << sample << "\r" << std::endl;
+        // Remove instance
         delete g_adc;
         g_csCurrentAdc = NULL;
     } // End of 'while' statement
@@ -113,24 +165,23 @@
     g_availableLed = !g_availableLed;
 } // End of AvailableIndicator
 
-UserChoice DisplayMenuAndGetChoice() {
-    static UserChoice userChoice;
-
+void DisplayMenuAndGetChoice() {
     // Display the title
-    std::cout << "\r" << std::endl << "MCP320x_SPI v0.2\r" << std::endl;
+    std::cout << "\r" << std::endl << "MCP320x_SPI v0.3\r" << std::endl;
 
     // Display the pot selection menu
-    std::cout << "\tUse pot #1:\t\t\ta\r" << std::endl;
-    std::cout << "\tUse pot #2:\t\t\tb\r" << std::endl;
+    std::cout << "\tUse pot 42100:\t\t\ta\r" << std::endl;
+    std::cout << "\tUse pot 41050:\t\t\tb\r" << std::endl;
+    std::cout << "\tUse pot  4251:\t\t\tc\r" << std::endl;
     std::cout << "Enter your choice: " << std::flush;
-    userChoice.potId = getchar();
+    g_userChoice.potId = getchar();
     std::cout << "\r" << std::endl << std::flush;
 
     // Display the adc selection menu
     std::cout << "\tUse adc 3201:\t\t\ta\r" << std::endl;
     std::cout << "\tUse adc 3208:\t\t\tb\r" << std::endl;
     std::cout << "Enter your choice: " << std::flush;
-    userChoice.adcId = getchar();
+    g_userChoice.adcId = getchar();
     std::cout << "\r" << std::endl << std::flush;
     
     // Display the menu
@@ -139,7 +190,6 @@
     std::cout << "\tShutdown pot         :\t\t\tc\r" << std::endl;
     std::cout << "\tReset pot            :\t\t\td\r" << std::endl;
     std::cout << "Enter your choice: " << std::flush;
-    userChoice.choice = getchar();
+    g_userChoice.choice = getchar();
     std::cout << "\r" << std::endl << std::flush;
-    return userChoice;
 }