Shop OBEX P1 Docs P2 Docs Learn Events
A/d ic — Parallax Forums

A/d ic

Macgman2000Macgman2000 Posts: 59
edited 2008-09-30 20:47 in General Discussion
Hello,

I am looking for an 8 bit A/D IC. Can you recommend one that uses SXb instruction set? IE, simple command to start sampling and acquire data from A/D?

thanks,
Nick

Comments

  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-09-28 19:57
    The ADC0831 is common and easy to use with SX/B. See attached.

    Post Edited (JonnyMac) : 9/29/2008 7:15:49 PM GMT
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-09-28 20:15
    Great! Thanks for the help!

    I am still trying to wrap my head around the differences between melabs Picbasic and the SXb.
    I found an example for shiftin in the sxb help section. Can you walk me through what it is doing in the shiftin statements?

    GET_ADC:
    CS = 0 ' activate ADC0831
    SHIFTIN Dpin, Cpin, MSBPOST, tmpB1\1, 4 <--what is going on here?
    SHIFTIN Dpin, Cpin, MSBPOST, tmpB1, 4 <--- and here?
    CS = 1 ' deactivate ADC0831
    RETURN tmpB1

    Nick

    Post Edited (Macgman2000) : 9/28/2008 8:35:50 PM GMT
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-09-28 20:46
    SHIFTIN is explained in the help file (hint, hint). Note that my version of the program is updated a bit from that shown in the help file -- use the newer version.
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-09-28 21:04
    Ok fair enough. Read through the help file, let me see if I understand this correctly..

    SHIFTIN Din, Clk, MSBPOST, tmpB1\9, 4

    tmpB1 is a byte variable
    \9 is how many bits clocked in (why 9bits, start + 8bits? why isn't there 8 other A/D channels, how do they clock in?)
    4 is 4x speed (4x speed of what? not sure what the native clock rate is....still hunting for it)

    Thanks again for your help.

    Nick
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-09-28 23:31
    Well, if you take the time to read the ADC0831 data sheet (hint, hint) you will learn that the ADC needs a pulse on its clock line to start the conversion -- that's what the ninth bit does. The ninth bit, in effect, gets shifted through the byte (tmpB1) and is dropped off in the process -- no harm, no foul. I know this because I've actually spent time experimenting (yet another hint) with the ADC0831 and different variations of code to access it.

    The help file very clearly states that the default speed of SHIFTIN is 83K bits/second. You find that in the section very deceptively (wink) labeled SHIFTIN Timing.

    Time to build a circuit and start experimenting -- that's how the rest learned this stuff.
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-09-29 01:14
    Agreed, I need to dive back into the hardware/software. I have become so used to Picbasic Pro and Pics, it's going to take some time to refocus. I bought the SX key kit about 3 yrs ago, at that time I was cranking out projects that didn't need more than 5 MIPS and the on board peripherals more than made up for the lack of horse power so I shelved the SX to my regret.

    Nick
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-09-29 02:35
    The SX is clean and lean -- once you get used to it you'll have a lot of fun. I'm presently working on a DMX lighting controller for a major SoCal amusement park -- using an SX28 and some fun code; about half SX/B and the rest in Assembly. We (EFX-TEK) use the SX programmed with SX/B in all of our accessory products so we feel very confident in it (but then, I've been involved with SX/B since its creation). If you look in the resources section on the Parallax site you'll see that my "Stamp Applications" column has used the SX and SX/B for the past two years; there's probably enough material there to catch you up quickly.
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2008-09-29 09:05
    Hey,

    what about SX/B's ANALOGIN command? The SX, two resistors, and one capacitor make a nice 8-bit ADC without the need for "talking" to an external chip.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Greetings from Germany,

    G
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-09-29 15:51
    Hello Guenther,

    I was thinking about using that method, but realized there may be a problem with it. The problem I forsee is a variance in resistors and capacitors. Let me explain my application. I have 4 potentiometers 8 bit 0~255. I need resistors (possibly 1%) and high quality caps. So center position on the potentiometer should correspond to 127. This may not be true across all 4 potentiometers. So maybe I force all pots mounted to a transmitter case to center position and run an internal calibration to zero out offsets upon power on. I don't know if this is going to be less time consuming....any thoughts?



    Nick
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-09-29 16:17
    I can't give you the code because I wrote it for a client, but I recently did a SX/B joystick application that uses an ADC0838 (four of the channels are for two joysticks). At start up the sticks are read and offsets from 127 are stored in the program for later use. It's actually pretty easy.
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-09-29 18:03
    Jonny,

    I came across this page......I can probably port the code almost as is...some changes. But this should run correct?

    http://maelabs.ucsd.edu/mae_ds/stamp/how/hardware/atod_page/basic_atod.html


    Nick
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-09-29 19:09
    I've attached my ADC0838 demo -- this is what I wrote to play with the chip before dropping it into an application. What I meant so say is that I can't give you the application code in which I used the joystick auto-centering.

    Post Edited (JonnyMac) : 9/29/2008 7:15:15 PM GMT
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-09-29 22:27
    Jonny,

    Thanks for the code!! I read through it, makes perfect sense! I am going to modify it·to pack 4 bytes +header+checksum·and TX every 20ms.·I bought an old 2 stick aircraft transmitter in great shape a few weeks ago on ebay circa...1990's (didn't power up). I paid $12, I am gutting it out and keeping the pots. I will drop in my own boards with a 2.4Ghz radio. Large housing, cool analog dial, BAM! instant radio for my financially draining hobby.

    Nick
  • Guenther DaubachGuenther Daubach Posts: 1,321
    edited 2008-09-29 22:43
    Nick,

    one of my "early" SX projects handled reading the position of 16 potentiometers. You can find a description of this project in my book. For a recent project, I used the same method for reading 10 potentiometers. In both cases, I'm sending the "raw" pot data to a PC via RS-232 where a VB application takes care of calibrating, and normalizing such "raw" data.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Greetings from Germany,

    G
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-09-30 00:42
    Guenther, I will do a search, thanks for the info.

    Nick

    Post Edited (Macgman2000) : 9/30/2008 1:05:27 AM GMT
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-09-30 03:58
    Jonny,


    OK, I am confused. I see in your code that ADC0838 routine return "result". Result as a variable, but nothing is done with this variable. I would think that since "result" has the 8 bit magnitude of the ADC at whatever channel 0~7 that it would be used directly. Looking at the list file I see that result is put into _PARAM1, when the func returns it puts _PARAM1 into LEDS. HELP!

    Nick
  • ZootZoot Posts: 2,227
    edited 2008-09-30 05:18
    Check out the SX/B help file for information about FUNCTIONS and SUBS. Functions allow a "return" value to be assigned to any other variable in the main program upon return. "Behind the scenes" the function loads param1 (and 2, 3, 4, 5 where applicable) and then moves those values to the variable chosen in the main program. In action it might be something like this:

    
    Loop_Me:
    ADCvalue1 = GET_ADC 0  ' get channel 0 value and drop it into ADCvalue1
    ADCvalue2 = GET_ADC 1 ' etc
    ADCvalue3 = GET_ADC 2 ' etc
    GOTO Loop_Me
    
    FUNC GET_ADC
       'do some ADC work 
       'shift channel (__param1 passed by function) out to ADC
       ' get value into a "temporary" work variable, called "result"
       ' error check the value (optional)
       IF result > 128 THEN
          result = 128
       ENDIF
       RETURN result  ' 
       ENDFUNC
    
    
    



    It's the same, really, as doing this, but is visually neater (and clearer) and follows other languages rough construction for assigned "returned" value from functions...

    Loop_Me:
    channel = 0
    GOSUB GET_ADC
    ADCvalue1 = result
    
    channel = 1
    GOSUB GET_ADC
    ADCvalue2 = result
    
    channel = 2
    GOSUB GET_ADC
    ADCvalue2 = result
    
    
    GOTO Loop_Me
    
    SUB GET_ADC
       'do some ADC work 
       'shift channel (__param1 passed by function) out to ADC
       ' get value into a "temporary" work variable, called "result"
       ' error check the value (optional)
       IF result > 128 THEN
          result = 128
       ENDIF
       RETURN
       ENDSUB
    
    
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-09-30 10:25
    Hello Zoot,

    I have found guides for older versions of the compiler. I can not seem to find one that tells me the basic mechanics of what is assumed, passed and why.

    From your example the inference I draw is that if you return 1 value it is assumed _param1 holds this value? If it is 2 then _param1 and _param2 in some order hold the value? This is regardless of the variable name, so just calling something a variable and passing it in a SUB or FUNC sets up the _paramX passing?

    Maybe I am not looking in the right places for info, I feel I am missing a good chunk of info on how the compiler works in this situation. I appreciate your help!

    Nick
  • BeanBean Posts: 8,129
    edited 2008-09-30 11:33
    Nick,
    Yes __PARAM1 holds the first byte returned, then __PARAM2 holds the 2nd byte returned, and so on.
    If a WORD variable is returned you can use the alias __WPARAM12.

    FUNC's must be declared so the compiler knows how many bytes you are returning. As in:

    MyFunc FUNC 2 ' A function that returns 2 bytes (1 WORD)

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson

    "It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter

    www.iElectronicDesigns.com

    ·
  • BeanBean Posts: 8,129
    edited 2008-09-30 11:33
    Nick,
    Yes __PARAM1 holds the first byte returned, then __PARAM2 holds the 2nd byte returned, and so on.
    If a WORD variable is returned you can use the alias __WPARAM12.

    FUNC's must be declared so the compiler knows how many bytes you are returning. As in:

    MyFunc FUNC 2 ' A function that returns 2 bytes (1 WORD)

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson

    "It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter

    www.iElectronicDesigns.com

    ·
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-09-30 12:43
    Bean, thanks for the confirmation. I have one more question. In returning more than 1 byte, what determines the order of _param1, _param2....paramX?

    Nick
  • ZootZoot Posts: 2,227
    edited 2008-09-30 12:48
    If you are using SX/B, look in the HELP menu when you have the compiler/IDE up. It'll be to the right of RUN/PROGRAM in the menubar pulldowns. Explains all of this in detail.

    But since you asked... (tho' I'm not Bean)....

    Values are assigned to params LSB first if a Word value, and PARAM1 is always first. When calling subs or funcs that allow for a different numbers of params, __PARAMCNT gets set with the number of params filled before calling.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • BeanBean Posts: 8,129
    edited 2008-09-30 13:42
    As zoot say it is the order that you give them on the RETURN line.
    So if you have a function that returns three bytes (value1, value2, value3) you would use:

    RETURN value1, value2, value3

    This will automatically put value1 into __PARAM1, value2 into __PARAM2, and value3 into __PARAM3.

    Then in the main program you can do:

    temp1 = MyFunc ' This will put __PARAM1 into temp1
    temp2 = __PARAM2
    temp3 = __PARAM3

    Bean

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson

    "It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter

    www.iElectronicDesigns.com

    ·
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-09-30 15:41
    Bean,

    Thanks for clarifying this issue!!!
    Is there a section in HELP that has all this stuff spelled out? I have looked through HELP and everything in it at least 3x and I have not seen anywhere that this is addressed. I looked up FUNC and SUB, brief mention only of _param. It would be nice to have a detailed nuts and bolts explanation of what goes on in the compiler. Like I said a few posts ago, am I looking at the wrong info? The only info I have is the Practical SX/B and the HELP section of the compiler...I still feel I am missing something.

    Nick


    Post Edited (Macgman2000) : 9/30/2008 3:46:35 PM GMT
  • JonnyMacJonnyMac Posts: 9,214
    edited 2008-09-30 15:58
    It's spelled out in this doc: www.parallax.com/Portals/0/Downloads/docs/prod/sx/PracticalSXB.pdf

    I'm working hard to finish this -- fully updated to SX/B 2.0; I know a lot of people have been waiting a long time and I continue to appreciate your patience; I've learned the hard way that writing a big book is more difficult than writing a magazine column.
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-09-30 16:04
    Jonny,



    Thanks for being patient with me all! I must be suffering from tunnel vision or something. I printed out a copy this morning of practical SX/B....flipped through the pages and there staring me in the face page.98.....I need a vacation.



    Nick
  • ZootZoot Posts: 2,227
    edited 2008-09-30 16:55
    Also, when you are in the SX-Key IDE w/SX/B compiler installed ----

    HELP -> SX/B Help -> DEFINITIONS -> SUBROUTINE DECLARATIONS
    and
    HELP -> SX/B Help -> DEFINITIONS -> FUNCTION DECLARATIONS

    The function help in particular contains some good examples of checking paramcnt (number of parameters passed to a function when it is called) and multi-byte assignments back from the function.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    When the going gets weird, the weird turn pro. -- HST

    1uffakind.com/robots/povBitMapBuilder.php
    1uffakind.com/robots/resistorLadder.php
  • Macgman2000Macgman2000 Posts: 59
    edited 2008-09-30 20:47
    Zoot,

    Thanks for the info [noparse]:)[/noparse] I have plenty to read now, should be fun!

    Nick
Sign In or Register to comment.