Shop OBEX P1 Docs P2 Docs Learn Events
Interfacing the MAX1270 ADC - Newb SX'er — Parallax Forums

Interfacing the MAX1270 ADC - Newb SX'er

skynuggetskynugget Posts: 172
edited 2009-03-02 21:15 in General Discussion
hey all, im a fairly proficient stamper, and i have finally made the leap to the sx, im still a little overwhelmed, but i thin im wrapping my head around it all.
I am trying to read the max1270 ADC with the following code that doesn't seem to work... has anyone used this chip with the sx before, might you see anything wrong with this code?



' -------------------------------------------------------------------------
' Device Settings
' -------------------------------------------------------------------------

DEVICE          SX28, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ            4_000_000
ID        "Max1270"


' -------------------------------------------------------------------------
' IO Pins
' -------------------------------------------------------------------------

SClock          VAR     RA.0                    ' shift clock
SIn                VAR     RA.1                    ' shiftin data
Sout        VAR     RA.2                    ' shiftout data
CS1270        var    RA.3            ' max1270 chip select

' -------------------------------------------------------------------------
' Variables
' -------------------------------------------------------------------------
ADChan        var    byte
ADRaw        var    byte
config        var    byte

' =========================================================================
  PROGRAM Start
' =========================================================================


' -------------------------------------------------------------------------
' Subroutine Declarations
' -------------------------------------------------------------------------

GET_ADC     SUB    1                         ' get value from ADC


' -------------------------------------------------------------------------
' Program Code
' -------------------------------------------------------------------------

Start:
      Low sclock
    high cs1270
Main:
      get_adc 0             ' get adc channel 1
    watch adraw            'debug adc 
    goto main            'loop

' -------------------------------------------------------------------------
' Subroutine Declarations
' -------------------------------------------------------------------------

' Use: aVar = GET_ADC
' -- reads ADC0831 and places value into 'aVar'

GET_ADC:
    ADChan = __Param1
      READ AdcCfg + ADchan, config     ' read config... maybe

    config = %11110000     ' set config t +5chanel 0    
    low cs1270                                              
    SHIFTOUT SOut, SClock, MSBFIRST, config     'set config and channel     
    High Cs1270                            
    adRaw = 0                            
      low cs1270
    SHIFTIN Sin, sClock, MSBPRE, adRaw\12                                                      ' deactivate ADC0831
      high cs1270

    RETURN ADRaw




AdcCfg:         
    DATA    %11110000, %11100000, %11010000, %11000000, %10110000, %10100000, %10010000, %10000000 ' 0-5





thanks to all in advance!

Comments

  • JonnyMacJonnyMac Posts: 9,412
    edited 2009-02-28 16:21
    You're using an older style of SX/B syntax that doesn't allow you to return a word (subs can only return a byte). I've updated your code -- give this a shot.
  • skynuggetskynugget Posts: 172
    edited 2009-03-01 12:24
    hey thanks for your help jonnymac, i appreciate the comments in your source. hop.gif
  • skynuggetskynugget Posts: 172
    edited 2009-03-01 22:16
    hey another silly question
    how in sx/b do you handle multiplying a variable by 1.2207?

    in stamp basic i would do

    mVolts = adcLevel + (adcLevel ** $3880)

    thanks!
  • JonnyMacJonnyMac Posts: 9,412
    edited 2009-03-01 23:11
    You can do the same thing (**) in SX/B, but you'll have to break that compound expression into simpler lines:

    tmpW1 = adcLevel ** $3880
    mVolts = adcLevel + tmpW1
    


    You could also use */ like this:

    mVolts = adcLevel */ $0138
    


    ...with a little less resolution, but it may be adequate for your needs.
  • skynuggetskynugget Posts: 172
    edited 2009-03-01 23:59
    thanks again jonny, hope i can return the favor some day.

    This sx/b is pritty fun. though i did spend a good day thinking my key was broke, till i realized you have to pull the local oscillator in debug mode cant wait to learn all the neat math quirks:>
  • JonnyMacJonnyMac Posts: 9,412
    edited 2009-03-02 00:36
    SX/B was designed with BASIC Stamp users in mind; many of the things you've done in the past with BASIC Stamps have analogs in the SX. Though some aspects require a bit more effort, it's worth it. Having the ability to add "virtual peripherals" to a project using the SX's speed is really a lot of fun.

    Be sure to read through the SX/B help file and the "What's New" file for SX/B 2.0.
  • skynuggetskynugget Posts: 172
    edited 2009-03-02 05:14
    hey jonnnymac, does this look right to you...

    chlevel = 852 in debug when im expecting like 4_950..
    without the math it works ok.

    Main:
      FOR chan = 0 TO 7
        chLevel = GET_ADC chan
        
        tmpW1 = chLevel ** $3880
        chLevel = chLevel + tmpW1
    
        WATCH chan, 8, udec                ' use Debug --> Run
        WATCH chLevel, 12, udec
        
        BREAK
    
      NEXT
    
    



    danke very much
  • JonnyMacJonnyMac Posts: 9,412
    edited 2009-03-02 05:16
    You should add another variable so that you can see what the raw input is versus the final calculated value. Your raw input should be from $000 to $FFF.
  • skynuggetskynugget Posts: 172
    edited 2009-03-02 19:38
    like this?
    Main:
      FOR chan = 0 TO 7
        chLevel = GET_ADC chan
        
        tmpW1 = chLevel ** $3880
        mvolts = chLevel + tmpW1
    
        WATCH chan, 8, udec                ' use Debug --> Run
        WATCH chLevel, 12, udec
        
        watch mvolts,12, udec
        BREAK
    
      NEXT
    
      GOTO Main
    
    



    debug results
    chan = 8
    chlevel= 4_054
    mvolts= 852
  • JonnyMacJonnyMac Posts: 9,412
    edited 2009-03-02 20:33
    See attached.
  • skynuggetskynugget Posts: 172
    edited 2009-03-02 21:15
    duh, watch 16 bits! thanks man , really appreciate it.
Sign In or Register to comment.