1

Dependencies:   DebugLibrary MCP4xxxx_SPI mbed

Fork of MCP41xxxApp by Yann Garcia

Files at this revision

API Documentation at this revision

Comitter:
viniciushl
Date:
Wed Nov 30 18:18:05 2016 +0000
Parent:
3:7dbac45b8fd3
Commit message:
Oi

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Tue Jan 29 15:02:41 2013 +0000
+++ b/main.cpp	Wed Nov 30 18:18:05 2016 +0000
@@ -1,7 +1,8 @@
 #include <string>
 #include <iostream>
 #include <iomanip>
-
+#include <mbed.h>
+#include <math.h>
 /**
  * Tests were done with schema proposed here: DS11195C-page 11 FIGURE 2-25: Potentiometer Divider Non-Linearity Error Test Circuit (DNL, INL)
  */
@@ -21,125 +22,78 @@
 /*
  * Declare statics
  */
-DigitalOut g_availableLed(LED1); // To verify if program in running
-Ticker g_available; // LED1 will flash with a period of 2s
-#define __MANAGE_CS__ // /CS managed by application
-#undef  __MANAGE_CS__ // /CS managed by library
-#if defined(__MANAGE_CS__)
-DigitalOut g_chipSelect(p8); // /CS to select MCP4xxxx device
-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
-#else // __MANAGE_CS__
-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
-#endif // __MANAGE_CS__
-UserChoice g_userChoice; // Used to store user choice from displayed menu
+
+DigitalOut chipSelect[] = {p8,p9,p10};    // c -  MAIS SIGNIFICATIVO
+
+
+
+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 library, /RESET input not connected, , /SHDN input not connected
 
 int main() {
 
-    unsigned char potLevel = 0x80; // Initial digital potentiometer value
+    //unsigned int potLevel1 = 0x0; // Initial digital potentiometer value
+    //unsigned int potLevel2 = 0x0; // Initial digital potentiometer value
     
-#if defined(__MANAGE_CS__)
-    g_chipSelect.write(1); // Disable chip
-#endif // __MANAGE_CS__
+    int value = 0;
+    int num_pot = 0;
 
-    // Launch available indicator
-    g_available.attach(&AvailableIndicator, 2.0);
-    
-    while (true) {
+    while (true) 
+    {
+        do {
+        
+        printf("\r\nDigite o numero do potenciometro (1 a 8):");
+        scanf("%d", &num_pot);
+        }while(num_pot < 1 || num_pot > 12);
     
-        g_userChoice = DisplayMenuAndGetChoice(); // Retrieve the user selection
-         switch (g_userChoice.choice) {
-            case 'a':
-                potLevel += 1;
-#if defined(__MANAGE_CS__)
-                g_chipSelect.write(0);
-#endif // __MANAGE_CS__
-                g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel);
-#if defined(__MANAGE_CS__)
-                g_chipSelect.write(1);
-#endif // __MANAGE_CS__
-                break;
-            case 'b':
-                potLevel += 1;
-#if defined(__MANAGE_CS__)
-                g_chipSelect.write(0);
-#endif // __MANAGE_CS__
-                g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot2, potLevel);
-#if defined(__MANAGE_CS__)
-                g_chipSelect.write(1);
-#endif // __MANAGE_CS__
-               break;
-            case 'c':
-                potLevel -= 1;
-#if defined(__MANAGE_CS__)
-                g_chipSelect.write(0);
-#endif // __MANAGE_CS__
-                g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel);
-#if defined(__MANAGE_CS__)
-                g_chipSelect.write(1);
-#endif // __MANAGE_CS__
-                break;
-            case 'd':
-                potLevel -= 1;
-#if defined(__MANAGE_CS__)
-                g_chipSelect.write(0);
-#endif // __MANAGE_CS__
-                g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot2, potLevel);
-#if defined(__MANAGE_CS__)
-                g_chipSelect.write(1);
-#endif // __MANAGE_CS__
-                break;
-            case 'e':
-                potLevel -= 1;
-#if defined(__MANAGE_CS__)
-                g_chipSelect.write(0);
-#endif // __MANAGE_CS__
-                g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot1);
-#if defined(__MANAGE_CS__)
-                g_chipSelect.write(1);
-#endif // __MANAGE_CS__
-                break;
-            case 'f':
-                potLevel -= 1;
-#if defined(__MANAGE_CS__)
-                g_chipSelect.write(0);
-#endif // __MANAGE_CS__
-                g_digitalPot.Write(CMCP4xxxx_SPI::ShutdownPot2);
- #if defined(__MANAGE_CS__)
-                g_chipSelect.write(1);
-#endif // __MANAGE_CS__
-                break;
-            case 'r':
-                g_digitalPot.Reset();
-                break;
-            default:
-                std::cout << "Invalid user choice\r" << std::endl;
-                break;
-         } // End of 'switch' statement
-       
-    } // End of 'while' statement
-} // End of program - nerver reached
+        do {
+        
+        printf("\r\nDigite o valor do volume (0 a 8):");
+        scanf("%d", &value);
+        }while(value < 0 || value >255);
+        
+        
+           
+        chipSelect[0].write(((num_pot-1) >> 2) & 0x1);
+        chipSelect[1].write(((num_pot-1) >> 1) & 0x1);
+        chipSelect[2].write(((num_pot-1) >> 0) & 0x1);
+        
+        int value2 = 256 - (pow((double)2,(double)value));
+        printf("Value = %d", value2);
+        
+        g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, value2);
+        
+        
+        
+        chipSelect[0].write(1);
+        chipSelect[1].write(1);
+        chipSelect[2].write(1);
+/*
 
-void AvailableIndicator() {
-    g_availableLed = !g_availableLed;
-} // End of AvailableIndicator
+        g_chipSelect1.write(0);
+        
+        g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel1);
 
-UserChoice DisplayMenuAndGetChoice() {
-    static UserChoice userChoice;
-
-    // Display the title
-    std::cout << "\r" << std::endl << "MCP4xxxx_SPI v0.1\r" << std::endl;
+        
+        g_chipSelect1.write(1);
+        
+        
+        g_chipSelect2.write(0);
+        
+        g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel2);
 
-    // Display the menu
-    std::cout << "\tIncrease level on pot #1:\t\t\ta\r" << std::endl;
-    std::cout << "\tIncrease level on pot #2:\t\t\tb\r" << std::endl;
-    std::cout << "\tDecrease level on pot #1:\t\t\tc\r" << std::endl;
-    std::cout << "\tDecrease level on pot #2:\t\t\td\r" << std::endl;
-    std::cout << "\tShutdown pot #1         :\t\t\te\r" << std::endl;
-    std::cout << "\tShutdown pot #2         :\t\t\tf\r" << std::endl;
-    std::cout << "\tReset MCP4xxxx          :\t\t\tr\r" << std::endl;
-    std::cout << "Enter your choice: " << std::flush;
-    userChoice.choice = getchar();
-    // Display the menu
-    std::cout << "\r" << std::endl << std::flush;
-    return userChoice;
+        
+        g_chipSelect2.write(1);
+        
+        potLevel1+=1;
+        potLevel2-=1;
+        
+        g_chipSelect2.write(0);        
+        g_digitalPot.Write(CMCP4xxxx_SPI::WriteToPot1, potLevel2);
+        g_chipSelect2.write(1);*/
+    
+    }
+
+            
 }
+
+