Test application to demonstrate MCP4xxxx_SPI library

Dependencies:   DebugLibrary MCP4xxxx_SPI mbed

Committer:
Yann
Date:
Sun Jan 27 17:04:34 2013 +0000
Revision:
1:a463807bf483
Parent:
0:62020bd58748
Child:
3:7dbac45b8fd3
Validate test application with hardware (MCP4100)

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 1:a463807bf483 64 case 'e':
Yann 1:a463807bf483 65 potLevel -= 1;
Yann 1:a463807bf483 66 g_chipSelect.write(0);
Yann 1:a463807bf483 67 g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot1);
Yann 1:a463807bf483 68 g_chipSelect.write(1);
Yann 1:a463807bf483 69 break;
Yann 1:a463807bf483 70 case 'f':
Yann 1:a463807bf483 71 potLevel -= 1;
Yann 1:a463807bf483 72 g_chipSelect.write(0);
Yann 1:a463807bf483 73 g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot2);
Yann 1:a463807bf483 74 g_chipSelect.write(1);
Yann 1:a463807bf483 75 break;
Yann 1:a463807bf483 76 case 'r':
Yann 1:a463807bf483 77 g_chipSelect.write(0);
Yann 1:a463807bf483 78 g_digitalPot.Reset();
Yann 1:a463807bf483 79 g_chipSelect.write(1);
Yann 1:a463807bf483 80 break;
Yann 0:62020bd58748 81 default:
Yann 0:62020bd58748 82 std::cout << "Invalid user choice\r" << std::endl;
Yann 0:62020bd58748 83 break;
Yann 0:62020bd58748 84 } // End of 'switch' statement
Yann 0:62020bd58748 85
Yann 0:62020bd58748 86 } // End of 'while' statement
Yann 0:62020bd58748 87 } // End of program - nerver reached
Yann 0:62020bd58748 88
Yann 0:62020bd58748 89 void AvailableIndicator() {
Yann 0:62020bd58748 90 g_availableLed = !g_availableLed;
Yann 0:62020bd58748 91 } // End of AvailableIndicator
Yann 0:62020bd58748 92
Yann 0:62020bd58748 93 UserChoice DisplayMenuAndGetChoice() {
Yann 0:62020bd58748 94 static UserChoice userChoice;
Yann 0:62020bd58748 95
Yann 0:62020bd58748 96 // Display the title
Yann 0:62020bd58748 97 std::cout << "\r" << std::endl << "MCP4xxxx_SPI v0.1\r" << std::endl;
Yann 0:62020bd58748 98
Yann 0:62020bd58748 99 // Display the menu
Yann 0:62020bd58748 100 std::cout << "\tIncrease level on pot #1:\t\t\ta\r" << std::endl;
Yann 0:62020bd58748 101 std::cout << "\tIncrease level on pot #2:\t\t\tb\r" << std::endl;
Yann 0:62020bd58748 102 std::cout << "\tDecrease level on pot #1:\t\t\tc\r" << std::endl;
Yann 0:62020bd58748 103 std::cout << "\tDecrease level on pot #2:\t\t\td\r" << std::endl;
Yann 1:a463807bf483 104 std::cout << "\tShutdown pot #1 :\t\t\te\r" << std::endl;
Yann 1:a463807bf483 105 std::cout << "\tShutdown pot #2 :\t\t\tf\r" << std::endl;
Yann 1:a463807bf483 106 std::cout << "\tReset MCP4xxxx :\t\t\tr\r" << std::endl;
Yann 0:62020bd58748 107 std::cout << "Enter your choice: " << std::flush;
Yann 0:62020bd58748 108 userChoice.choice = getchar();
Yann 0:62020bd58748 109 // Display the menu
Yann 0:62020bd58748 110 std::cout << "\r" << std::endl << std::flush;
Yann 0:62020bd58748 111 return userChoice;
Yann 0:62020bd58748 112 }