1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
# include <iostream>;
# include <cmath>;
# include <string>;
# include <cstring>;
# include <iomanip>;
#include <windows.h>;
#include <stdio.h>;
using namespace std;
//temp location for ereperation
HANDLE hCon;
//list of colors 1-15 to interface with the console
enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
//color function
void SetColor(Color c) //Color c is for the ereperation ^^^^
{
if(hCon == NULL) //if there isn't a memory location
hCon = GetStdHandle(STD_OUTPUT_HANDLE); //creates space in console buffer
SetConsoleTextAttribute(hCon, c); //sets color in console to selected color for that memory location
}
//main function
int main()
{
char answer;
do {
SetColor(DARKRED); cout << "*********************************************************************************";
SetColor(TEAL);cout << " Welcome To the Analog to Digital Converter Program ";SetColor(DARKRED); cout << "* ";
double n ;
int d = 0;
double x, y;
int z, twotothen, j;
double an, bit, ref;
SetColor(WHITE); cout << " Enter Vref: "; //All functions have defensive programming:
SetColor(YELLOW);
std::cin >> std::setw(3) >> ref;
for (; ref < 0 ;)
{
SetColor(WHITE); cout << " Vref Cannot be less than 0, Enter a new Vref: "; //Here
SetColor(YELLOW); cin >>ref;
}
SetColor(WHITE); cout << " Enter number of bits (1-32): ";
SetColor(YELLOW); cin >> bit;
for (;bit <= 0 || bit >= 33;)
{
SetColor(WHITE); cout << " Please enter a bit value between 1 and 32: "; //Here
SetColor(YELLOW);cin >>bit;
}
SetColor(WHITE); cout << " Please enter the analog voltage: ";
SetColor(YELLOW); cin >> an;
for(; an < 0 || an > ref;)
{
SetColor(WHITE); cout << " The analog voltage must be greater than zero and less than Vref, \nPlease enter new value: "; //And here
SetColor(YELLOW); cin >> an;
}
y = ref/pow(2,bit); //Calculatng the step size
x = an/ref; //Used to calculate VR
SetColor(WHITE); cout << " Step size is: ";
SetColor(YELLOW); cout << y << endl;
twotothen = pow(2, bit);
//cout << x << endl; //Test if the control value is good
z = .6 * pow(2, bit);
//cout<< z << endl; //test if VR is correct
int binary(z);
SetColor(GREEN); cout << " Step VR Binary Value "; //Header
cout << " ____ _______ ____________ ";
for( j = 1, n =1 ; d!= z; n++, j++ ) //Main loop
{
int k = pow(2, n);
double p;
d = abs(d); //make sure the VR is positive
d += twotothen/ k;
SetColor(WHITE); cout << setw( 5) << right << j << " " << setw(7) << setprecision(6) << abs(d * y) << " ";//Outputs The Step,VR
//Function for decimal to binary
string result;
int rep = d; // the number to convert to binary
if (rep == 0) // if the input is 0, output 0
result = "0";
while (rep != 0)
{
if (rep%2!=0) //if the number is divisible by 2, remainder = 1
{
result += "1";//add 1 to the string
}
else
{
result += "0";//add 0 to the string
}
rep = rep/2;
}
//This is problamatic however, becuase it writes the string in reverse, so it has to get converted:
string hold;//string to hold the converted value
string::reverse_iterator rev; //calls string rev to beign reversing the data values
for ( rev=result.rbegin() ; rev < result.rend(); rev++ ) //for loop; string rev = Returns the reverse of the beginning value, until reaching the end
{
cout << *rev; //points back to the result string for the next conversion
hold += *rev; //add the converted value to be held (this value is truncated every time a new number is converted)
}
cout << setw(bit) << endl;
if (d == z) //when conversion is complete
{
cout << " Final Bianry Conversion is: ";
SetColor(YELLOW); cout << hold << endl;
}
if( abs(d) > abs(z))//else go to next conversion,
{
d-= twotothen/ k;
}
}
SetColor(WHITE); cout << " Percent error is: ";
SetColor(YELLOW); cout << (1 - (d * y / an))*100;//the VR devided by the expected voltage
SetColor(WHITE); cout << " %" << endl;
cout << " Convert Again?";
SetColor(RED); cout << "[Y/y] ";
SetColor(YELLOW); cin >> answer;
system("cls");
}while ( answer =='y' || 'Y'); //if N or anything else, end the program
return 0;
}
|