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

main.cpp

Committer:
Yann
Date:
2013-04-05
Revision:
0:284a85288653
Child:
2:50b3bd9af686

File content as of revision 0:284a85288653:

#include <string>
#include <iostream>
#include <iomanip>

#include "MCP4xxxx_SPI.h" // Use SPI module #1 and /CS mapped on pin 8
#include "MCP320x_SPI.h" // Use SPI module #1 and /CS mapped on pin 9

struct UserChoice {
    char choice;
    unsigned char potId;
    unsigned char adcId;
};

/*
 * Declare functions
 */
void AvailableIndicator(); // LED1 flashing for program while program is alive
UserChoice DisplayMenuAndGetChoice(); // Display and get the user choice

/*
 * Declare statics
 */
DigitalOut g_availableLed(LED1); // To verify if program in running
Ticker g_available; // LED1 will flash with a period of 2s
CMCP4xxxx_SPI g_digitalPot(p5, p6, p7);
CMCP320x_SPI *g_adc = NULL;
DigitalOut g_cs3201(p9);  // /CS mapped on pin 8 for MCP3201
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

int main() {
    // Deactivate all SPI devices
    g_cs3201 = 1;
    g_cs3208 = 1;
    g_cs42100 = 1;
    g_cs41050 = 1;

    unsigned char potLevel = 0x80; // Initial digital potentiometer value
    
    // Launch available indicator
    g_available.attach(&AvailableIndicator, 2.0);
    
    while (true) {
        // Retrieve user choices 
        g_userChoice = DisplayMenuAndGetChoice();
        // Set the pot. value
        //      1. Enable de right digipot
        switch (g_userChoice.potId) {
            case 'a':
                g_cs42100 = 0;
                break;
            default:
                g_cs41050 = 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);
                break;
            case 'b':
                potLevel -= 1;
                g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel);
                break;
            case 'c':
                potLevel -= 1;
                g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot1);
                break;
            case 'd':
                //g_digitalPot->Reset();
                potLevel = 0x80;
                break;
            default:
                std::cout << "Invalid user choice\r" << std::endl;
                break;
        } // End of 'switch' statement
        //      3. Disable de right digipot
        switch (g_userChoice.potId) {
            case 'a':
                g_cs42100 = 1;
                break;
            default:
                g_cs41050 = 1;
                break;
        } // End of 'switch' statement
        
        // Set adc to use
        switch (g_userChoice.adcId) {
            case 'a': // MCP3201
                g_adc = new CMCP320x_SPI(p5, p6, p7);
                g_csCurrentAdc = &g_cs3201;
                break;
            case 'b': // MCP3208
                g_adc = new CMCP320x_SPI(p5, p6, p7, NC, CMCP320x_SPI::_3208);
                g_csCurrentAdc = &g_cs3208;
                break;
        } // End of 'switch' statement
        g_csCurrentAdc->write(0);
        float sample = g_adc->Read(CMCP320x_SPI::CH3);
        g_csCurrentAdc->write(1);
        std::cout << "Voltage at PW0: " << setprecision(5) << sample << "\r" << std::endl;
        delete g_adc;
        g_csCurrentAdc = NULL;
    } // End of 'while' statement
} // End of program - nerver reached

void AvailableIndicator() {
    g_availableLed = !g_availableLed;
} // End of AvailableIndicator

UserChoice DisplayMenuAndGetChoice() {
    static UserChoice userChoice;

    // Display the title
    std::cout << "\r" << std::endl << "MCP320x_SPI v0.2\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 << "Enter your choice: " << std::flush;
    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();
    std::cout << "\r" << std::endl << std::flush;
    
    // Display the menu
    std::cout << "\tIncrease level of pot:\t\t\ta\r" << std::endl;
    std::cout << "\tDecrease level of pot:\t\t\tb\r" << std::endl;
    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();
    std::cout << "\r" << std::endl << std::flush;
    return userChoice;
}