Test application to demonstrate MCP4xxxx_SPI library

Dependencies:   DebugLibrary MCP4xxxx_SPI mbed

Committer:
Yann
Date:
Fri Jan 25 16:13:03 2013 +0000
Revision:
0:62020bd58748
Child:
1:a463807bf483
Create library and test application for Microchip Digital Potentiometers MCP42xxx/MCP41xxx

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yann 0:62020bd58748 1 #include <string>
Yann 0:62020bd58748 2 #include <iostream>
Yann 0:62020bd58748 3 #include <iomanip>
Yann 0:62020bd58748 4
Yann 0:62020bd58748 5 #include "MCP4xxxx_SPI.h"
Yann 0:62020bd58748 6
Yann 0:62020bd58748 7 struct UserChoice {
Yann 0:62020bd58748 8 char choice;
Yann 0:62020bd58748 9 unsigned char moduleId;
Yann 0:62020bd58748 10 };
Yann 0:62020bd58748 11
Yann 0:62020bd58748 12 /*
Yann 0:62020bd58748 13 * Declare functions
Yann 0:62020bd58748 14 */
Yann 0:62020bd58748 15 void AvailableIndicator(); // LED1 flashing for program while program is alive
Yann 0:62020bd58748 16 UserChoice DisplayMenuAndGetChoice(); // Display and get the user choice
Yann 0:62020bd58748 17
Yann 0:62020bd58748 18 /*
Yann 0:62020bd58748 19 * Declare statics
Yann 0:62020bd58748 20 */
Yann 0:62020bd58748 21 DigitalOut g_availableLed(LED1); // To verify if program in running
Yann 0:62020bd58748 22 Ticker g_available; // LED1 will flash with a period of 2s
Yann 0:62020bd58748 23 DigitalOut g_chipSelect(p8); // /CS to select MCP4xxxx device
Yann 0:62020bd58748 24 CMCP4xxxx_SPI g_digitalPot(p5, p6, p7, NC, NC); // Create an instance of the class CMCP4xxxx_SPI, p5/p6/p7: SPI#1, /RESET input not connected, , /SHDN input not connected
Yann 0:62020bd58748 25 UserChoice g_userChoice; // Used to store user choice from displayed menu
Yann 0:62020bd58748 26
Yann 0:62020bd58748 27 int main() {
Yann 0:62020bd58748 28
Yann 0:62020bd58748 29 unsigned char potLevel = 0x80; // Initial digital potentiometer value
Yann 0:62020bd58748 30
Yann 0:62020bd58748 31 g_chipSelect = 1; // Device is disabled
Yann 0:62020bd58748 32
Yann 0:62020bd58748 33 // Launch available indicator
Yann 0:62020bd58748 34 g_available.attach(&AvailableIndicator, 2.0);
Yann 0:62020bd58748 35
Yann 0:62020bd58748 36 while (true) {
Yann 0:62020bd58748 37
Yann 0:62020bd58748 38 g_userChoice = DisplayMenuAndGetChoice(); // Retrieve the user selection
Yann 0:62020bd58748 39 switch (g_userChoice.choice) {
Yann 0:62020bd58748 40 case 'a':
Yann 0:62020bd58748 41 potLevel += 1;
Yann 0:62020bd58748 42 g_chipSelect.write(0);
Yann 0:62020bd58748 43 g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel);
Yann 0:62020bd58748 44 g_chipSelect.write(1);
Yann 0:62020bd58748 45 break;
Yann 0:62020bd58748 46 case 'b':
Yann 0:62020bd58748 47 potLevel += 1;
Yann 0:62020bd58748 48 g_chipSelect.write(0);
Yann 0:62020bd58748 49 g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot2, potLevel);
Yann 0:62020bd58748 50 g_chipSelect.write(1);
Yann 0:62020bd58748 51 break;
Yann 0:62020bd58748 52 case 'c':
Yann 0:62020bd58748 53 potLevel -= 1;
Yann 0:62020bd58748 54 g_chipSelect.write(0);
Yann 0:62020bd58748 55 g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel);
Yann 0:62020bd58748 56 g_chipSelect.write(1);
Yann 0:62020bd58748 57 break;
Yann 0:62020bd58748 58 case 'd':
Yann 0:62020bd58748 59 potLevel -= 1;
Yann 0:62020bd58748 60 g_chipSelect.write(0);
Yann 0:62020bd58748 61 g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot2, potLevel);
Yann 0:62020bd58748 62 g_chipSelect.write(1);
Yann 0:62020bd58748 63 break;
Yann 0:62020bd58748 64 default:
Yann 0:62020bd58748 65 std::cout << "Invalid user choice\r" << std::endl;
Yann 0:62020bd58748 66 break;
Yann 0:62020bd58748 67 } // End of 'switch' statement
Yann 0:62020bd58748 68
Yann 0:62020bd58748 69 } // End of 'while' statement
Yann 0:62020bd58748 70 } // End of program - nerver reached
Yann 0:62020bd58748 71
Yann 0:62020bd58748 72 void AvailableIndicator() {
Yann 0:62020bd58748 73 g_availableLed = !g_availableLed;
Yann 0:62020bd58748 74 } // End of AvailableIndicator
Yann 0:62020bd58748 75
Yann 0:62020bd58748 76 UserChoice DisplayMenuAndGetChoice() {
Yann 0:62020bd58748 77 static UserChoice userChoice;
Yann 0:62020bd58748 78
Yann 0:62020bd58748 79 // Display the title
Yann 0:62020bd58748 80 std::cout << "\r" << std::endl << "MCP4xxxx_SPI v0.1\r" << std::endl;
Yann 0:62020bd58748 81
Yann 0:62020bd58748 82 // Display the menu
Yann 0:62020bd58748 83 std::cout << "\tIncrease level on pot #1:\t\t\ta\r" << std::endl;
Yann 0:62020bd58748 84 std::cout << "\tIncrease level on pot #2:\t\t\tb\r" << std::endl;
Yann 0:62020bd58748 85 std::cout << "\tDecrease level on pot #1:\t\t\tc\r" << std::endl;
Yann 0:62020bd58748 86 std::cout << "\tDecrease level on pot #2:\t\t\td\r" << std::endl;
Yann 0:62020bd58748 87 std::cout << "Enter your choice: " << std::flush;
Yann 0:62020bd58748 88 userChoice.choice = getchar();
Yann 0:62020bd58748 89 // Display the menu
Yann 0:62020bd58748 90 std::cout << "\r" << std::endl << std::flush;
Yann 0:62020bd58748 91 return userChoice;
Yann 0:62020bd58748 92 }