Shop OBEX P1 Docs P2 Docs Learn Events
Looking for example code with the MCP3208 analog to digital converter — Parallax Forums

Looking for example code with the MCP3208 analog to digital converter

turbosupraturbosupra Posts: 1,088
edited 2009-10-06 00:51 in Propeller 1
I saw these

http://obex.parallax.com/objects/481/
http://obex.parallax.com/objects/488/

But they are a little complicated for a beginner such as myself and I was looking for a simplistic starter so that I could experiment with my ADC and led/testboard where I could change a few values and see what it does.

My ultimate goal is to output a specific voltage value to my multimeter, and be able to manipulate that in the code.

Thanks for reading.

Comments

  • StefanL38StefanL38 Posts: 2,292
    edited 2009-10-05 18:14
    Hello TurboSupra,

    to OUTPUT a voltage you don't need a analog digital converter you need the oposite
    a digital to analog converter

    There are two basic principles to do that

    first: PWM (pulse-width-modulation (needs one IO-PIN and some additional hardware R-C eventual OP-Amp as a voltagefollower)

    second: R-2R-resistor ladder needs one IO-Pin per bit resolution

    If you don't need high speed on changing the voltage
    the easiest way for PWM would be a loop
    with two adjustable waittimes for IO-Pin high and io-pin low
    and a resistor and condensator connected to the outputpin to convert the PWM DC alternating between 0V and 3.3V to a (more or less) flat DC-voltage

    post which direction you would like to go or what more questions you have about coding this

    best regards

    Stefan
  • turbosupraturbosupra Posts: 1,088
    edited 2009-10-05 18:23
    Hi Stefan,

    Thanks for the reply!

    I thought about using a resistor ladder, and some spin code to tell it what to do, with multiple input pins, and 1 output pin.

    I don't think I need high speed on changing, maybe .1 seconds? I would like to do the adjustment through software/pwm (as I think that would give me the most flexibility)

    The resistor/condensator sounds like the best solution for me. I need to vary from as low as 1.4vdc to as high as 2.1vdc . Thanks for the help!




    StefanL38 said...
    Hello TurboSupra,

    to OUTPUT a voltage you don't need a analog digital converter you need the oposite
    a digital to analog converter

    There are two basic principles to do that

    first: PWM (pulse-width-modulation (needs one IO-PIN and some additional hardware R-C eventual OP-Amp as a voltagefollower)

    second: R-2R-resistor ladder needs one IO-Pin per bit resolution

    If you don't need high speed on changing the voltage
    the easiest way for PWM would be a loop
    with two adjustable waittimes for IO-Pin high and io-pin low
    and a resistor and condensator connected to the outputpin to convert the PWM DC alternating between 0V and 3.3V to a (more or less) flat DC-voltage

    post which direction you would like to go or what more questions you have about coding this

    best regards

    Stefan
  • StefanL38StefanL38 Posts: 2,292
    edited 2009-10-05 18:32
    with pwm you can vary from 0V to 3.3V on an IO-Pin

    0.00 V means always off

    1.65 V means 50% of the time on 50% of the time off

    3.30V means 100% of the time on

    1.40V means 1.4/3.3=42.4% time on 57.6% time off

    2.10V means 2.1/3.3=63.4% time on 36.6% time off

    depending on the values of R and C the adjusting time to a new voltage changes
    and the current you can deliver to a load changes

    digital multimeters (DMM) have 1MOhm-input resistance minimun. This means as long as you just
    connect the voltage to a DMM you can use values from 1kOhm to 100 kOhm and condensators
    from 0.1 µF to 100µF

    For speed perfomance and lowest rest"ripples" you have to do more (OP-Amp as a voltagefollower with
    "flatening capacitors on input and output of the OP-AMP

    best regards

    Stefan
  • ry.davidry.david Posts: 63
    edited 2009-10-05 18:37
    I have posted a MCP4922 object in OBEX - It's a 2-channel, 12-bit DAC. It comes in several package sizes, including DIP
  • turbosupraturbosupra Posts: 1,088
    edited 2009-10-05 19:12
    Thanks Stefan

    So if I set up my crystal and time settings, I can tell spin to have the voltage on for 42.4% and then flatten it with the resistor/capacitor?

    Can you tell me what happens if I used 1k and .01uF vs if I used 100k and 100uF? How would the two extremes affect my output? As I'm connecting them to an automobile ecu once I get it working.



    StefanL38 said...
    with pwm you can vary from 0V to 3.3V on an IO-Pin

    0.00 V means always off

    1.65 V means 50% of the time on 50% of the time off

    3.30V means 100% of the time on

    1.40V means 1.4/3.3=42.4% time on 57.6% time off

    2.10V means 2.1/3.3=63.4% time on 36.6% time off

    depending on the values of R and C the adjusting time to a new voltage changes
    and the current you can deliver to a load changes

    digital multimeters (DMM) have 1MOhm-input resistance minimun. This means as long as you just
    connect the voltage to a DMM you can use values from 1kOhm to 100 kOhm and condensators
    from 0.1 µF to 100µF

    For speed perfomance and lowest rest"ripples" you have to do more (OP-Amp as a voltagefollower with
    "flatening capacitors on input and output of the OP-AMP

    best regards

    Stefan
  • Mike GreenMike Green Posts: 23,101
    edited 2009-10-05 20:12
    Download and read the application note on the Propeller's counters (AN001). Among other things, it discusses the use of the built-in counters to generate PWM.
  • turbosupraturbosupra Posts: 1,088
    edited 2009-10-05 20:28
    Thanks!

    https://www.parallax.com/Portals/0/Downloads/appnt/prop/AN001-PropellerCountersv1.1.zip

    I will read up on this!

    Mike Green said...
    Download and read the application note on the Propeller's counters (AN001). Among other things, it discusses the use of the built-in counters to generate PWM.
  • turbosupraturbosupra Posts: 1,088
    edited 2009-10-06 00:19
    How do I know which pin this is using for input and output?


    
    {{
    Demonstration of scaling Duty Cycle
    10kω
    APIN ─┳── Out
    │
    .1μF
    
    Delta modulation has no fundamental freq but has quantization noise
    }}
    CON _clkmode = xtal1 + pll16x
    _xinfreq = 5_000_000
    VAR long parameter
    
    
    PUB go | x
    cognew(@entry, @parameter) 'startup DAC cog and point to DAC value
    repeat
    repeat x from 0 to period 'loop over the entire scale
    parameter := $20C49B * x '$1_0000_0000 / period * x <- provides full scale voltage
    waitcnt(1000 +cnt) 'wait awhile before changing the value
    DAT
    org
    entry mov dira, diraval 'set APIN to output
    mov ctra, ctraval 'establish counter A mode and APIN
    mov time, cnt 'record current time
    add time, period 'establish next period
    :loop rdlong value, par 'get an up to date duty cycle
    waitcnt time, period 'wait until next period
    mov frqa, value 'update the duty cycle
    jmp #:loop 'do it again
    diraval long |< 0 'APIN direction
    ctraval long %00111 << 26 + 0 'NCO/PWM APIN=0 {BPIN=1} <-not used
    period long 2000 '40kHz period (_clkfreq / period)
    time res 1
    value res 1
    
  • AJMAJM Posts: 171
    edited 2009-10-06 00:32
    Per the documentation APIN=1

    I'm actually working on a similar project. I would highly recommend going through all of the PE Labs. All of this would make much more sense after finishing them.
  • turbosupraturbosupra Posts: 1,088
    edited 2009-10-06 00:36
    Ok good, because it doesn't make sense at all right now [noparse]:)[/noparse]

    Are you talking about this line in the code?

    ctraval long %00111 << 26 + 0 'NCO/PWM APIN=0 {BPIN=1} <-not used




    Which labs are you talking about, the ones in the book that come with the chip/kit? Or the data sheet? http://www.parallax.com/Portals/0/Downloads/docs/prod/prop/PropellerDatasheet-v1.2.pdf


    AJM said...
    Per the documentation APIN=1

    I'm actually working on a similar project. I would highly recommend going through all of the PE Labs. All of this would make much more sense after finishing them.
  • AJMAJM Posts: 171
    edited 2009-10-06 00:43
    Check out this Link

    They're very helpful
  • turbosupraturbosupra Posts: 1,088
    edited 2009-10-06 00:51
    Sweet, thanks!

    I'm trying to read the AN001 document and it really makes no sense to me at all, I am so lost and confused!

    AJM said...
    Check out this Link

    They're very helpful
Sign In or Register to comment.