Shop OBEX P1 Docs P2 Docs Learn Events
A/D and D/A converter problem — Parallax Forums

A/D and D/A converter problem

jojo Posts: 15
edited 2005-12-19 21:21 in Learn with BlocklyProp
Dear Sir

I try to build a PID control project but i have a problem to choose the A/D and D/A converter.
My project is

a sinusiodal wave input from funtion generater·pass through A/D to BS2 then D/A.
I am first time user. Can you guys give me some information about the A/D· and D/A converter and how to connect to each other?

PS : I try ADC0831 but i don't get much of it.

·

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2005-09-07 19:32
    jo,

    ·· Please see the link below to the Stampworks manual for an example· of using the ADC0831.· You can download this book free in PDF format from the bottom of the page.

    http://www.parallax.com/detail.asp?product_id=27220

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • edited 2005-09-07 23:23
    Basic Analog and Digital also has examples of both A/D and D/A conversion. You can downlaod it from http://www.parallax.com/dl/docs/books/edu/baad.pdf.· See chapter 3 for A/D with the ADC0831 and chapter 7 for D/A conversion with an RC circuit.

    After you have tried those two chapters, come back and look at this statement: In your program, all you will have to do is store your value from the ADC0831 in a variable, and then use that same variable in your PWM output.
  • jojo Posts: 15
    edited 2005-09-23 22:01
    Dear

    Maybe I ask a stupid question.
    I follow the experiment #27 Voltage Measurement in Stamp Work and build the circult.


    I put a square wave or sine wave from function generator as input.
    But I only see the positive voltage value appear on the debug terminal.
    I can't get the negative voltage value and it show 0 on the debug terminal.

    How can I modify this program to get both positive and negative voltage?


    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    ADdata CON 0
    ADclock CON 1
    ADcs CON 2

    result VAR Byte
    mvolts VAR Word

    Initialize:
    ·HIGH ADcs

    Main:
    ·GOSUB Read0831
    ·mvolts = result */ $139C
    ·DEBUG HOME
    ·DEBUG "adc... ", DEC result," ", CR
    ·DEBUG "volts... ",DEC mvolts DIG 3, ".", DEC3 mvolts
    ·PAUSE 100
    ·GOTO Main
    ·END

    Read0831:
    ·LOW ADcs
    ·SHIFTIN ADdata,ADclock, MSBPOST, [noparse][[/noparse]result\9]
    ·HIGH ADcs
    ·RETURN



    Post Edited (jo) : 9/23/2005 10:00:34 PM GMT
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-09-23 22:04
    You can't; the ADC0831 will only measure positive voltages. You can use the MAX1270 (we carry it) to measure positive and negative voltages.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • jojo Posts: 15
    edited 2005-09-23 22:19
    Max1270 have 8 analog input pin but I only need one input pin for my funtion generater
    Can you give me more information?
  • edited 2005-09-23 23:40
    Do your input and output sine waves both have to be AC coupled? If not, you can use your function generator's DC offset to bring the input signal above 0 V. For example, if your function generator is giving you a sine wave that goes from -2.5 V to + 2.5 V, adding 2.5 V of DC offset will move the sine wave up (from 0 to 5 V).

    If your input signal does have to be AC coupled, the Max1270 is one option. Another option would be to use the op amp circuit in Chapter 8 of Understanding Signals. You can use this circuit to apply DC offset to an AC coupled signal. The example in Chapter 8 uses an inverting op amp. You will need to do some math on the input values so that your program corrects for signal inversion and DC offset. It would be possible to skip the math if you were to send your D/A output to the input of another inverting op amp with (negative) DC offset to bring the signal back down.

    If you try an op amp, tune it first while monitoring the input and output with an oscilloscope. When you know the output is in the 0 to 5 V range for all possible inputs, then connect it to the ADC0831's input.

    Understanding Signals is available for download from www.parallax.com -> Downloads -> Stamps in Class Tutorials.
  • jojo Posts: 15
    edited 2005-09-28 17:12
    Dear Andy,

    I build the circuit in chapter 8.
    When I adjust 10k resistor, I only see the clipping signal back to normal signal. I didn't see output signal(negative part)·move above 0V .

    What does the output signal look like?
  • edited 2005-09-29 04:22
    I tested the circuit just to make sure, and it does work as advertised. I was able to view the bottom part of an ac coupled signal by adjusting the 10 k pot. It clipped the top, but I could see the bottom. By reducing the op amp's gain, (swap the 20 k for something smaller) you should be able to see the whole signal without clipping anything.

    Sorry, I'm out of time tonight, but I will post screen captures tomorrow so you can see what it looks like.
  • edited 2005-09-29 21:31
    Alright, attached is a screen capture of measurements on that op amp circuit taken with the Parallax USB Oscilloscope.·

    The lower sine wave (CH1) is the 3 V pk-pk AC coupled signal going into the Op Amp circuit (where P15 was).· The 0 V marker is the blue triangle on the left of the screen, and as you can see, the sine wave is pretty much equally above and below 0 V.· You can·also view the specs for this signal in the Auto Measurements panel.

    The upper sine wave (CH2) is the·op amp's output after some tinkering with the potentiometer.· It's got just a little over 1.5 V·DC offset.· The zero volts marker for this signal, is the red triangle on the left of the screen, and you can view the signal specs in the Cursors panel.· The reason the frequencies are a little different is because I didn't place the cursors accurately enough.

    I made·four changes to the circuit shown in the book:
    1. Replaced P15 with a function generator output.·
    2. Removed the 1 uF capacitor.· Since the signal is coming from a function generator, it probably doesn't change things much.· It's only necessary·to filter·the BASIC Stamp's FREQOUT signal, which digitally synthesizes a sine wave.
    3. Replaced the 20 k resistor with a 10 k resistor.··This takes the gain from·-2 to -1.·
    4. Replaced the 10 k potentiometer with a 100 k.· Either·value should work fine, I just didn't happen to have a 10 k pot handy.

    Post Edited (Andy Lindsay (Parallax)) : 9/29/2005 10:09:33 PM GMT
  • jojo Posts: 15
    edited 2005-10-03 05:42
    Dear Andy

    Finally, I put DC offser circuit, ADC0831, stamp, and MAX522 together.

    I sucessfully·get the·output signal which is same as input signal.

    But, I have to turn input signal·frequency below 10Hz to get the this result.

    It looks like the conversion speed is not fast enough to convert the input signal.

    Is some thing going wrong with·circuit, stamp, chips or·program that causes this kind of problem?



    The·input signal frequency range that I need is 100K Hz to 1 Hz.

    Can stamp or chips convert this kind of signal?

    By the way, I give you my program.



    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    A2Ddata· CON 0
    A2Dclock CON 1
    A2Dcs···· CON 2

    Din········ CON 6
    D2Acs···· CON 5
    D2Asclk·· CON 4

    result····· VAR Byte
    mVolts···· VAR Word

    SetDacB· CON %00100010
    SetDacA· CON %00100001

    'initialize

    LOW D2Asclk
    LOW A2Dclock
    HIGH A2Dcs
    HIGH D2Acs


    Main:
    ·GOSUB READ0831

    ·mVolts =result*/$139c
    ·DEBUG HOME
    ·DEBUG "ADC...", DEC result," ",CR
    ·DEBUG "volts...", DEC mVolts DIG 3,"." DEC3 mVolts

    ·GOSUB WriteDac
    ·PAUSE 100

    ·GOTO Main
    ·END


    Read0831:
    ·LOW A2Dcs
    ·SHIFTIN A2Ddata,A2Dsclk, MSBPOST,[noparse][[/noparse]result\9]
    ·HIGH A2Dcs
    ·result=result*1··························································· ·'change proportional gain
    RETURN

    WriteDac:
    ·LOW D2Acs
    ·SHIFTOUT Din,D2Asclk, MSBFIRST,[noparse][[/noparse]SetDacA,result]
    ·HIGH D2Acs
    RETURN














    Post Edited (jo) : 10/3/2005 5:45:29 AM GMT
  • edited 2005-10-04 01:23
    Jo,

    It looks like you've made some good progress.

    After removing the PAUSE command and all the DEBUG commands, it appears that your maximum sampling rate is about 160 Hz. To see this for yourself, remove the PAUSE and DEBUG commands, and then measure the frequency of the ADC enable signal on P2. For a 10 Hz sine wave, 16 samples per cycle that you get from a 160 Hz sampling rate is probably enough to reconstruct the waveform so that it is recognizable on your oscilloscope.

    To sample and reconstruct the a 100 kHz sine wave, you will need a sampling rate in the MHz range. For example, if the 16X sampling rate is sufficient, you would need a 1.6 MHz sampling rate. If you want the output waveform to look better, will need an even faster sampling rate.

    The BASIC Stamp is not the right microcontroller for this kind of job. You should instead use the SX microcontroller. The SX Microcontroller can be programmed in SX BASIC, which is very similar to PBASIC, so some of your programming work may already be done.· Also, SX Microcontroller development kits are inexpensive. For example, go to the SX Products Overview page·and follow the SX Tech Toolkit LITE link ($89 US). This kit has almost everything you need to get started with the SX microcontroller.· The only·item not included is a good power supply.· I would recommend a·7.5 V, 1000 mA, DC Supply.

    In order to ensure that your project is successful, I would also buy and start prototyping with parallel A/D and D/A converters.

    So, are there any other specifications for this project? In the interest of not having to go and buy yet a third microcontroller, it would probably be a good idea to fully explain the project specification before we go much further.

    Regards, Andy

    Post Edited (Andy Lindsay (Parallax)) : 10/4/2005 3:13:24 AM GMT
  • jojo Posts: 15
    edited 2005-10-04 07:43
    Dear Andy

    Thank you for everything.

    This control system project is to find out the plant's system and mathematic model.
    First, I build different kind of plants which like 1st order system 6280/(s+6280) , 2nd order system 800000000/s(s+23256) ... by using op-amp.
    Then, by giving same input signal and passing through proportional, PI or PID controller to get different output signal.
    Since I have these output signal. Next time, maybe I have an unknown plant, I can test this unknown plant by doing the same experiment. Then I probaby will know what kind of system is.


    I already built the analog controller by using op-amp.
    So, I work on using basic stamp to build proportional, PI and PID controller.
    In fact, right now my plant's cutoff frequency is around 3K Hz.
    Therefore, the input signal probably only need 10K Hz

    Anyway, right now I use BS2 rev G microcontrller and Board of education.
    That is not capable to convert this high frequency signal.
    So, I will talk to my professor tomorrow.

    But there is one thing I don't understand.
    In your 2004 product catalog , BS2-IC's processor speed is 20MHz.
    I think·BS2-IC is fast enough to deal with the signal.
    Can you tell me how to pick the microprocessor?

    Post Edited (jo) : 10/4/2005 7:54:51 AM GMT
  • edited 2005-10-05 00:12
    Jo,

    I'm sorry about the 20 MHz misunderstanding. The Interpreter chip on the BASIC Stamp 2 fetches and executes instructions from a separate serial EEPROM chip. Even though the Interpreter chip's clock is 20 MHz, the execution time for each higher level PBASIC instruction is slower. The reason for this is because it takes the Interpreter chip time to send a serial message to the EEPROM chip, and then receive a serial reply message with the next token. To see the actual instruction execution rates, take a look at the BASIC Stamp Comparison Chart.· When you review the specifications, it claims about 4000 instructions per second. This instruction execution does not take into account the amount of time it takes for commands to send serial messages to the D/A converter, and so on.

    Now, back to the SX microcontroller. There's no external interpreter; all the program memory is internal flash. You can clock it up to 75 MHz, and with the SX chip, that's 75-million machine language instructions per second. It can handle the digital sampling of a 10 kHz signal with ease. With a 10 kHz signal, you may not have to worry about switching to parallel A/D and D/A converters. I think the serial ones you have will probably work fine.

    If you are automating to perform a frequency sweep on an unknown system, you may also need a RAM chip to store the measurements. The SX chip is an 8-bit microcontroller. If you are going to try to make the microcontroller guess the transfer function based on a frequency sweep, you might find yourself writing some custom routines for higher math functions. Another option would be to add a Floating Point Coprocessor.

    Andy

    Post Edited (Andy Lindsay (Parallax)) : 10/5/2005 12:15:27 AM GMT
  • jojo Posts: 15
    edited 2005-10-05 18:45
    Dear Andy

    I already had a 9V 300mA DC supply which is used on board of education.
    I think I can use this one.
    Therefore, I need to buy SX Tech Toolkit Lite and Floating Point Coprocessor.
    What else part I need?

    jo
  • edited 2005-10-06 00:09
    Hi Jo,

    To get started, all you really need is the SX-Tech Toolkit LITE and the 7.5 V, 1000 mA DC supply. The reason I recommend the 7.5 V, 1000 mA supply is because the SX-Key tool pulls a significant amount of current (more than 300 mA) when you are in the Debug environment and clocking the chip at high speeds. I think it's a very important part.

    If you do anticipate needing to store and manipulate large numeric values, then also get the Floating Point Coprocessor. While it is possible to write the routines you will need in either assembly or BASIC, it might take a lot of time. The Floating Point Coprocessor should save you that time.

    At some point, you may need to find a RAM chip to store lots of A/D measurements. I would wait until you have a really good idea of how much data you need to store before searching for a RAM chip.

    Andy
  • jojo Posts: 15
    edited 2005-10-10 19:07
    Dear Andy

    I ordered SX Tech Toolkit Lite, Floating Point Coprocessor and 7.5V power supply last Friday.
    I hope that I can receive them this Wednesday.

    Right now I work on writing the proportional and integral control program.

    I read the Experiment 6 in your Industrial Control book but I don't get much of it.
    Do you have any other example of integral control program?

    Jo
  • edited 2005-10-13 19:47
    Hi Jo,

    Try this:·PID Control Intro with the BASIC Stamp.

    Regards, Andy
  • jojo Posts: 15
    edited 2005-11-03 17:33
    Dear Andy



    Thank you for your help.

    I built a proportional control. But result is not what I think.



    Attached file is the block diagram of my design.

    System is 6280/(S+6280)

    attachment.php?attachmentid=73757attachment.php?attachmentid=73758attachment.php?attachmentid=73759attachment.php?attachmentid=73760

    attachment.php?attachmentid=73761attachment.php?attachmentid=73762attachment.php?attachmentid=73763attachment.php?attachmentid=73764

    attachment.php?attachmentid=73765attachment.php?attachmentid=73766attachment.php?attachmentid=73767attachment.php?attachmentid=73768

    Stamp Program

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}

    'Proportional control

    A2Ddata CON 0
    A2Dclock CON 1
    A2Dcs CON 2

    Din CON 6
    D2Acs CON 5
    D2Asclk CON 4


    SetDacA· CON %00100001
    SP CON 51······················ 'setpoint= 1 Volt
    result VAR Byte················ 'A/D input signal
    Sign VAR Word
    Datain VAR Word···············'error
    P VAR Word····················· 'D/A output signal
    Kp VAR Byte···················· 'proportional gain


    Initialize:
    LOW D2Asclk
    LOW A2Dclock
    HIGH A2Dcs
    HIGH D2Acs

    Main:
    ·GOSUB Read_0831
    ·GOSUB Sum
    ·GOSUB CalcInput
    ·GOSUB WriteDac
    ·GOTO Main


    Read_0831:····························· 'ADC0831
    ·LOW A2Dcs
    ·SHIFTIN A2Ddata, A2Dclock, MSBPOST, [noparse][[/noparse]result\9]
    ·HIGH A2Dcs
    ·RETURN

    Sum:
    ·Datain=(SP-result)
    ·Sign=Datain
    ·GOSUB SetSign
    ·Datain=ABS Datain
    ·Datain=Datain*Sign

    CalcInput:
    ·Sign=Datain
    ·GOSUB SetSign
    ·Kp=1
    ·P=ABS Datain*Kp
    ·P=P*Sign
    ·RETURN

    WriteDac:······························· 'MAX522
    ·LOW D2Acs
    ·SHIFTOUT Din,D2Asclk, MSBFIRST,[noparse][[/noparse]SetDacA,P]
    ·HIGH D2Acs
    ·RETURN

    SetSign:
    ·IF Sign.BIT15=0 THEN SignPos
    ·Sign=-1
    ·RETURN
    ·SignPos:
    ·Sign=1
    ·SignDone:
    ·RETURN







    When Kp=1(Gain=1), output signal is a serial square wave.

    But if I change the Kp,·the output signal(square wave)·goes up and down.

    I don't know what happen to my program. So, can·anyone check this program?






    Post Edited (jo) : 11/3/2005 5:53:07 PM GMT
    439 x 411 - 10K
    45 x 25 - 128B
    107 x 25 - 241B
    50 x 25 - 136B
    102 x 25 - 238B
    45 x 25 - 128B
    107 x 25 - 241B
    50 x 25 - 136B
    102 x 25 - 238B
    45 x 25 - 128B
    107 x 25 - 241B
    50 x 25 - 136B
    102 x 25 - 238B
  • edited 2005-11-05 06:47
    Your Sum subroutine is missing a RETURN command.
  • edited 2005-11-05 06:52
    Jo,

    If putting that RETURN command in doesn't fix the problem, make a copy of the program and replace the READ_0831 and WriteDac subroutines with DEBUGIN and DEBUG commands. Then, hand enter values and verify that the displayed output is correct.

    Regards, Andy
  • jojo Posts: 15
    edited 2005-11-09 18:30
    Andy Lindsay (Parallax) said...
    Do your input and output sine waves both have to be AC coupled? If not, you can use your function generator's DC offset to bring the input signal above 0 V. For example, if your function generator is giving you a sine wave that goes from -2.5 V to + 2.5 V, adding 2.5 V of DC offset will move the sine wave up (from 0 to 5 V).

    If your input signal does have to be AC coupled, the Max1270 is one option. Another option would be to use the op amp circuit in Chapter 8 of Understanding Signals. You can use this circuit to apply DC offset to an AC coupled signal. The example in Chapter 8 uses an inverting op amp. You will need to do some math on the input values so that your program corrects for signal inversion and DC offset. It would be possible to skip the math if you were to send your D/A output to the input of another inverting op amp with (negative) DC offset to bring the signal back down.

    If you try an op amp, tune it first while monitoring the input and output with an oscilloscope. When you know the output is in the 0 to 5 V range for all possible inputs, then connect it to the ADC0831's input.

    Understanding Signals is available for download from www.parallax.com -> Downloads -> Stamps in Class Tutorials.
    Dear Andy


    "It would be possible to skip the math if you were to send your D/A output to the input of another inverting op amp with (negative) DC offset to bring the signal back down."confused.gif

    I put a DC offset circuit(attached file)·to move the input signal above 0V. After D/A, how do I build another DC offset to bring the signal (negative part) back down? Because,if I use the same DC offset circuit,·the signal will not go below 0V.

    Jo

    Post Edited (jo) : 11/9/2005 7:05:09 PM GMT
    525 x 395 - 147K
  • edited 2005-11-10 06:49
    Jo,

    If you have some electronics textbooks, look for a "summing amplifier". A Google search will also give you example circiuts with the design equations. The idea is to feed your DC offset (positive or negative) into one of the summing amplifier's inputs and the signal into the other.

    Regards, Andy

    ·
  • jojo Posts: 15
    edited 2005-11-14 18:24
    Dear Andy


    My output signal has 2ms to 5ms time delay.
    First, the delay time is from 2ms to 5ms which is unstable. Why?
    Second, Is there any circuit that can fix this problem?
    Because I need this feedback output signal to add up the input signal(from function generator).
    So, I need both input and output signal are in the same page.

    jo
  • edited 2005-11-15 04:24
    Hi Jo,

    There are three different factors that cause a phase shift between your input and output signal.

    1) It takes time for the ADC0831 to send its·message to the BASIC Stamp
    2) The BASIC Stamp takes time running the program
    3) It takes time for the BASIC Stamp to send the new message to the DAC.

    2 to 5 ms is a reasonable delay given the existing serial components (the ADC and DAC) and BASIC Stamp.·

    I am sorry to say that the only "circuit" I can think of that will eliminate your phase delay is an SX chip working with a parallel ADC and a parallel DAC.· Just as the SX chip would allow you to sample and process signals at higher frequencies (see earlier posts in this thread), it will also allow you to process the signals quickly enough to that the phase delay will be very small.

    Regards, Andy
  • vzhzjtvzhzjt Posts: 5
    edited 2005-12-19 20:27
    Andy Lindsay,

    Like Jo, I have been working on converting an analog PD conttroller to a digital PD controller (Magnetic Levitator).

    I bought the Basic Stamp 2, purchased 2 A/D's and was going to use the PWM output to drive the analog FET (more thermally efficient than using the analog DC voltage) which regulates the current through the coil.

    1st issue, integer math ... solved using the floating point co-processor

    2nd issue, the PWM command doesn't execute in the "back-ground" and for a duration of 255 ms took 350 ms to execute ... partially soved using the PWMPAL ... this allows the PWM to execute in the "Back-Ground" but, everytime I update the duty-cycle, the signal goes low (for up to 20ms) ... this "Drop-Out" will cause the object I am levitating to FALL.

    3rd issue, Speed ... Speed ... Speed, with the on-board interpretter, this system is not very good for fast-real-time control, all the algorithms are correct, but with a Loop-Time of 80 ms and the PWMPAL "Drop-Outs", I am not sure this scheme will work.

    I am now considering what JO is looking at, using the MAX522 serial D/A and driving the FET with a normal analog voltage.

    You mention using the SX microcontroller, this will certainly increase the speed, but what about the following:

    1. Does it perform floating point math?
    2. Can the PWM signal execute in the "Back-Ground"?

    I am thinking about the ATOM ... but it too looks like it can not perform multi-tasking ... running the PWM signal in the "Back-Ground".

    I am told Parralax will be coming out with a Revolutionary Controller/Approach in mid March of 2006 to address issue like this.

    Any help would be greatly appreicated.

    Thanks.

    Jim
  • edited 2005-12-19 21:21
    Jim,

    Yes, definitely take a closer look at the SX microcontroller; I think it will make your project very doable. It's got the speed, and you can do background PWM with it. You can also use it with the floating point coprocessor you already have, which has been optimized for speed and will work very nicely with the SX. I'm not so sure you'll actually need the floating point in the end, but if you do, it'll be at your disposal.

    Regards, Andy

    Andy Lindsay
    Applications Engineer
    Parallax, Inc.
Sign In or Register to comment.