Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: DebugLibrary MCP4xxxx_SPI mbed
main.cpp
00001 #include <string> 00002 #include <iostream> 00003 #include <iomanip> 00004 00005 /** 00006 * Tests were done with schema proposed here: DS11195C-page 11 FIGURE 2-25: Potentiometer Divider Non-Linearity Error Test Circuit (DNL, INL) 00007 */ 00008 #include "MCP4xxxx_SPI.h" 00009 00010 struct UserChoice { 00011 char choice; 00012 unsigned char moduleId; 00013 }; 00014 00015 /* 00016 * Declare functions 00017 */ 00018 void AvailableIndicator(); // LED1 flashing for program while program is alive 00019 UserChoice DisplayMenuAndGetChoice(); // Display and get the user choice 00020 00021 /* 00022 * Declare statics 00023 */ 00024 DigitalOut g_availableLed(LED1); // To verify if program in running 00025 Ticker g_available; // LED1 will flash with a period of 2s 00026 #define __MANAGE_CS__ // /CS managed by application 00027 #undef __MANAGE_CS__ // /CS managed by library 00028 #if defined(__MANAGE_CS__) 00029 DigitalOut g_chipSelect(p8); // /CS to select MCP4xxxx device 00030 CMCP4xxxx_SPI g_digitalPot(p5, p6, p7, NC, NC, NC); // Create an instance of the class CMCP4xxxx_SPI, p5/p6/p7: SPI#1, /CS managed by application, /RESET input not connected, , /SHDN input not connected 00031 #else // __MANAGE_CS__ 00032 CMCP4xxxx_SPI g_digitalPot(p5, p6, p7, p8, NC, NC); // Create an instance of the class CMCP4xxxx_SPI, p5/p6/p7: SPI#1, /CS managed by library, /RESET input not connected, , /SHDN input not connected 00033 #endif // __MANAGE_CS__ 00034 UserChoice g_userChoice; // Used to store user choice from displayed menu 00035 00036 int main() { 00037 00038 unsigned char potLevel = 0x80; // Initial digital potentiometer value 00039 00040 #if defined(__MANAGE_CS__) 00041 g_chipSelect.write(1); // Disable chip 00042 #endif // __MANAGE_CS__ 00043 00044 // Launch available indicator 00045 g_available.attach(&AvailableIndicator, 2.0); 00046 00047 while (true) { 00048 00049 g_userChoice = DisplayMenuAndGetChoice(); // Retrieve the user selection 00050 switch (g_userChoice.choice) { 00051 case 'a': 00052 potLevel += 1; 00053 #if defined(__MANAGE_CS__) 00054 g_chipSelect.write(0); 00055 #endif // __MANAGE_CS__ 00056 g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel); 00057 #if defined(__MANAGE_CS__) 00058 g_chipSelect.write(1); 00059 #endif // __MANAGE_CS__ 00060 break; 00061 case 'b': 00062 potLevel += 1; 00063 #if defined(__MANAGE_CS__) 00064 g_chipSelect.write(0); 00065 #endif // __MANAGE_CS__ 00066 g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot2, potLevel); 00067 #if defined(__MANAGE_CS__) 00068 g_chipSelect.write(1); 00069 #endif // __MANAGE_CS__ 00070 break; 00071 case 'c': 00072 potLevel -= 1; 00073 #if defined(__MANAGE_CS__) 00074 g_chipSelect.write(0); 00075 #endif // __MANAGE_CS__ 00076 g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel); 00077 #if defined(__MANAGE_CS__) 00078 g_chipSelect.write(1); 00079 #endif // __MANAGE_CS__ 00080 break; 00081 case 'd': 00082 potLevel -= 1; 00083 #if defined(__MANAGE_CS__) 00084 g_chipSelect.write(0); 00085 #endif // __MANAGE_CS__ 00086 g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot2, potLevel); 00087 #if defined(__MANAGE_CS__) 00088 g_chipSelect.write(1); 00089 #endif // __MANAGE_CS__ 00090 break; 00091 case 'e': 00092 potLevel -= 1; 00093 #if defined(__MANAGE_CS__) 00094 g_chipSelect.write(0); 00095 #endif // __MANAGE_CS__ 00096 g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot1); 00097 #if defined(__MANAGE_CS__) 00098 g_chipSelect.write(1); 00099 #endif // __MANAGE_CS__ 00100 break; 00101 case 'f': 00102 potLevel -= 1; 00103 #if defined(__MANAGE_CS__) 00104 g_chipSelect.write(0); 00105 #endif // __MANAGE_CS__ 00106 g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot2); 00107 #if defined(__MANAGE_CS__) 00108 g_chipSelect.write(1); 00109 #endif // __MANAGE_CS__ 00110 break; 00111 case 'r': 00112 g_digitalPot.Reset(); 00113 break; 00114 default: 00115 std::cout << "Invalid user choice\r" << std::endl; 00116 break; 00117 } // End of 'switch' statement 00118 00119 } // End of 'while' statement 00120 } // End of program - nerver reached 00121 00122 void AvailableIndicator() { 00123 g_availableLed = !g_availableLed; 00124 } // End of AvailableIndicator 00125 00126 UserChoice DisplayMenuAndGetChoice() { 00127 static UserChoice userChoice; 00128 00129 // Display the title 00130 std::cout << "\r" << std::endl << "MCP4xxxx_SPI v0.1\r" << std::endl; 00131 00132 // Display the menu 00133 std::cout << "\tIncrease level on pot #1:\t\t\ta\r" << std::endl; 00134 std::cout << "\tIncrease level on pot #2:\t\t\tb\r" << std::endl; 00135 std::cout << "\tDecrease level on pot #1:\t\t\tc\r" << std::endl; 00136 std::cout << "\tDecrease level on pot #2:\t\t\td\r" << std::endl; 00137 std::cout << "\tShutdown pot #1 :\t\t\te\r" << std::endl; 00138 std::cout << "\tShutdown pot #2 :\t\t\tf\r" << std::endl; 00139 std::cout << "\tReset MCP4xxxx :\t\t\tr\r" << std::endl; 00140 std::cout << "Enter your choice: " << std::flush; 00141 userChoice.choice = getchar(); 00142 // Display the menu 00143 std::cout << "\r" << std::endl << std::flush; 00144 return userChoice; 00145 }
Generated on Wed Jul 13 2022 23:07:40 by
1.7.2