Important alert: (current site time 7/28/2014 6:10:27 PM EDT)
 

VB icon

A 16 bit successive comparison ADC

Email
Submitted on: 5/28/2002 2:21:46 PM
By: Ali Ezad Abbas  
Level: Intermediate
User Rating: By 1 Users
Compatibility: Java (JDK 1.2)
Views: 13251
(About the author)
 
     It simulates a 16 bit successive comparison ADC
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: A 16 bit successive comparison ADC
// Description:It simulates a 16 bit successive comparison ADC
// By: Ali Ezad Abbas
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2922&lngWId=2//for details.//**************************************

/*********************************************
 * This program simulates the working of 
 * a 16-bit successive comparison anaolog
 * to digital converter. The simulation 
 * has been purposely slowed down to
 * illustrate the various comparisons 
 * taking place in the ADC. Feel free
 * to play around the code but donot
 * remove the credits if you redistribute
 * the code.
 * 
 * AUTHOR: Ali Ezad Abbas (ali_ezad@ieee.org)
 * ********************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ADC extends JFrame implements Runnable {
public static JTextField input;
public static JButton begin;
public static JLabel output, inf, lab, inf2, anaout;
public static int flag = 0, flag2 = 0;
public static Thread t;
public static void main (String [] args) {
 t = new Thread(new ADC ());
}
public ADC () {
 setSize(300,300);
 setTitle("16-Bit Successive Comparison ADC");
 getContentPane().setLayout(null);
 addWindowListener(new windowListener () );
 input = new JTextField();
 input.addActionListener (new buttonListener () );
 output = new JLabel ("0000000000000000");
 anaout = new JLabel ("0");
 inf = new JLabel ("Ready");
 inf2 = new JLabel ("Analog Output: ");
 lab = new JLabel ("Digital Output: ");
 begin = new JButton ("START");
 begin.addActionListener (new buttonListener () );
 getContentPane().add(input);
 getContentPane().add(begin);
 getContentPane().add(output);
 getContentPane().add(inf);
 getContentPane().add(lab);
 getContentPane().add(inf2);
 getContentPane().add(anaout);
 input.setBounds(25,50,120,20);
 begin.setBounds(25,80,100,20);
 lab.setBounds(25,150,100,20);
 inf2.setBounds(25,190,100,20);
 anaout.setBounds(125,190,120,20);
 output.setBounds(125,150,120,20);
 inf.setBounds(25,252,150,20);
 show();
}
public void run () {
 while (true) {
 if (flag == 0) {
lab.setText("Digital Output: ");
int reference = -32200;
String out = "0000000000000000";
int in = Integer.parseInt(input.getText());
int index = 0;
if (in<0) {
in = Math.abs(in);
lab.setText("Digital Output:-");
}
if (in>=65536) {
in = -32199;
reference = in;
}
while (reference != in) {
if (in < reference) {
 out = changeBitToZero(out,index-1);
 out = changeBitToOne(out,index);
} else {
 out = changeBitToOne(out,index);
}
output.setText(out);
index++;
reference = analog(out);
anaout.setText(""+reference);
try {
 t.sleep(500);
} catch (Exception ion) {}
}
flag++;
if (in == -32199) {
inf.setText("Error: Overflow");
} else {
inf.setText("Done");
}
 }
 }
}
public static int analog (String s) {
 int o = 0;
 int j=0;
 for (int i=(s.length()-1);i>=0;i--) {
 int b = Integer.parseInt( ( ( s.charAt(i) ) + "" ) );
 o = o + (int)(b*(Math.pow(2,j)));
 j++;
 }
 return o;
}
public static String changeBitToZero (String a , int index) {
 String b = "";
 for (int i=0;i<a.length();i++) {
 if (i == index) {
b = b + "0";
 } else {
b = b + a.charAt(i);
 }
 }
 return b;
}
public static String changeBitToOne (String a, int index) {
 String b = "";
 for (int i=0;i<a.length();i++) {
 if (i == index) {
b = b + "1";
 } else {
b = b + a.charAt(i);
 }
 }
 return b;
}
private class buttonListener implements ActionListener {
 public void actionPerformed (ActionEvent e) {
 if (flag2 == 0) {
t.start();
flag2++;
 } else {
flag = 0;
 }
 inf.setText("Processing...");
 }
}
private class windowListener extends WindowAdapter {
 public void windowClosing (WindowEvent e) {
 System.exit(0);
 }
}
}


Other 18 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.