Advertisement
makispaiktis

A/D Converter

Jan 6th, 2020 (edited)
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include <vector>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8. // Auxiliary Function
  9. vector <int> binary(int n){
  10.     int numOfDigits = int(log(n) / log(2)) + 1;
  11.     // 3. Check the case of 0 or the overflow case
  12.     if(n > INT_MAX){
  13.         cout << "Cannot make calculations with numbers higher than: " << INT_MAX << endl << endl;
  14.     }
  15.     vector <int> digits = vector <int> ();
  16.     int sum = n;
  17.     for(int i=numOfDigits-1; i>=0; i--){
  18.         if(sum >= pow(2, i)){
  19.             digits.push_back(1);
  20.             sum -= pow(2, i);
  21.         }
  22.         else{
  23.             digits.push_back(0);
  24.         }
  25.     }
  26.     return digits;
  27. }
  28.  
  29.  
  30.  
  31. int main()
  32. {
  33.     // 1. Intro
  34.     cout << "Welcome to the A/D Converter!\n\n";
  35.     cout << "1. Give me the Reference Voltage (V_ref): ";
  36.     double V_ref;
  37.     cin >> V_ref;
  38.     while(V_ref <= 0){
  39.         cout << "Non-positive V_ref is not acceptable.\n";
  40.         cout << "Give me the Reference Voltage (V_ref): ";
  41.         cin >> V_ref;
  42.     }
  43.     cout << "\n2. Now give me how many digits the transformed binary word must have (up to 2).\n";
  44.     cout << "Number of digits: ";
  45.     int numOfDigits;
  46.     cin >> numOfDigits;
  47.     while(numOfDigits < 2){
  48.         cout << "Number of digits: ";
  49.         cin >> numOfDigits;
  50.     }
  51.  
  52.     cout << "\n3. Last step is to give me the voltage you count with your polymeter to convert it into binary word (V_count).\n";
  53.     double V_count;
  54.     cin >> V_count;
  55.     while(V_count < 0 || V_count > V_ref){
  56.         cout << "Give me the voltage you count with your polymeter to convert it into binary word (V_count).\n";
  57.         cin >> V_count;
  58.     }
  59.  
  60.     // 2. Convert
  61.     double step = V_ref / (pow(2, numOfDigits) - 1);
  62.     vector <double> voltages = vector <double> ();
  63.     for(double voltage=0; voltage<=V_ref; voltage+=step){
  64.         voltages.push_back(voltage);
  65.     }
  66.  
  67.     int indexNumber = -1;
  68.     for(unsigned int i=0; i<voltages.size(); i++){
  69.         if(voltages[i]-step/2 <= V_count && V_count < voltages[i]+step/2){
  70.             indexNumber = i;
  71.         }
  72.     }
  73.     cout << V_count << " ---> ";
  74.     vector <int> digits = binary(indexNumber);
  75.     unsigned int digitsSize = digits.size();
  76.     // Complete with zeros when necessary
  77.     for(unsigned int i=0; i<numOfDigits-digitsSize; i++){
  78.         cout << "0";
  79.     }
  80.     // Display the vector "digits"
  81.     for(unsigned int i=0; i<digitsSize; i++){
  82.         cout << digits[i];
  83.     }
  84.  
  85.     return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement