Shop OBEX P1 Docs P2 Docs Learn Events
problem converting pBasic to Javelin; ADC0838 — Parallax Forums

problem converting pBasic to Javelin; ADC0838

verobelverobel Posts: 81
edited 2006-06-02 17:38 in General Discussion
Hi Javeliners;
I am having a problem getting my ADC0838 class to work. It is based on code that is working in a pBasic program (excerpts shown below)

in main..
CS1···· PIN· 5····· ' ADC0838.18 chip select (common data i/0, clk )
DATI PIN 6········· ' ADC0838.17 data in·· (must have sep data line for a/d to work)
DATO PIN 6········· ' ADC0838.14 data out········· "··· "
Clock2· CON· 0····· ' Orange shift clock (74x165.2),(ADC0838.16)·

·IF( FADC ) THEN GOSUB Adc_0838

Adc_0838:

··· 'DEBUG CRSRXY, 0, 1, "Program FADC·· ", CR
··· 'Channel = 0
··· HIGH CS1··········· ' from other working prog··· .. triggers on high to low
··· LOW CS1············ ' ""
··· 'MUXBYTE = %11000 | ((Channel & %001) << 2) | ((Channel & %110) >> 1)
··· MUXBYTE = %11000 | ((0 & %001) << 2) | ((0 & %110) >> 1)

··· SHIFTOUT DATI,Clock2,MSBFIRST,[noparse][[/noparse]MUXBYTE\5]
··· SHIFTIN DATO,Clock2,MSBPOST,[noparse][[/noparse]ADCRes]

··· 'DEBUG CRSRXY, 0, 9, "ADCRes = ", DEC3 ADCRes
··· ADCWord = (ADCRes*100)*/5
··· pot =· ADCWord/5········ ' convert ADC chn0 val to range 0->99 for output

··· 'Channel = 1
··· HIGH CS1··········· ' from other working prog··· .. triggers on high to low
··· LOW CS1············ ' ""
··· 'MUXBYTE = %11000 | ((Channel & %001) << 2) | ((Channel & %110) >> 1)
··· MUXBYTE = %11000 | ((1 & %001) << 2) | ((1 & %110) >> 1)

··· SHIFTOUT DATI,Clock2,MSBFIRST,[noparse][[/noparse]MUXBYTE\5]
··· SHIFTIN DATO,Clock2,MSBPOST,[noparse][[/noparse]ADCRes]

··· ADCWord = (ADCRes*100)*/5
··· lgt =· ADCWord/5····· ' convert ADC chn1 val to range 0->99 for output
··· DEBUG CRSRXY, 0, 9,"
",CR
··· DEBUG CRSRXY, 0, 10," Channel ;··· MUXBYTE·· ;···· ADCResult··· ;··· Voltage*100"· ,CR
··· DEBUG CRSRXY, 0, 11,"
",CR
··· DEBUG CRSRXY, 0, 12, DEC 1," (%",BIN3 1,") ;· ",DEC MUXBYTE, " (%",BIN MUXBYTE,") ;· "
··· 'DEBUG CRSRXY, 0, 12, DEC CHANNEL," (%",BIN3 Channel,") ;· ",DEC MUXBYTE, " (%",BIN MUXBYTE,") ;· "
··· DEBUG CRSRXY, 0, 13, DEC3 ADCRes," (%",BIN8 ADCRes,") ;· ",DEC3 lgt," (%",BIN8 lgt,") " , CR
··· 'DEBUG CRSRXY, 0, 4, "pot = ", DEC pot, CR
··· 'DEBUG CRSRXY, 0, 5, "lgt = ", DEC lgt, CR

· RETURN

this gives debug feedback like (when using BS2pe)
· channel··· muxbyte········ adcResult·········· volt*100
1 (%001), 28 (%11100), 154 (%10011000), 060 (%00111011)

now my new javelin program Multi5.java (attached) shows adcResult = 255... for same condition. Also attached is the ADC0838.java. I have tried to understand and set the parameters the same... and I am using the same ciruit and pin numbers but swapped in Javelin Stamp.

Can anyone spot the problem?
Thanks, John

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-06-02 03:43
    What value does clockIn get after shiftIn?
    Put
    System.out.print("clockIn=");
    System.out.println(clockIn);
    after the shiftIn() in your read() method of the· adc0838 class.

    regards peter
    ·
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-06-02 05:09
    This thread is all about the 0838.
    http://forums.parallax.com/showthread.php?p=519918
    regards peter
    ·
  • verobelverobel Posts: 81
    edited 2006-06-02 13:11
    these are the values I am getting on first 2 calls to ADC0000838:

    muxbyte=24 clockIn=511
    muxbyte=28 clockIn=511
    lgt=-1 pot=-1

    in my pBasic program the equivalent result for clockIn was ADCresult = 154
    you might have noticed some shifts I did on muxbyte << 11
    and clockIn >> 8 (the output above was before I did the shift)
    ..these appear to be required since javelin is using 16 bit words and pBasic uses 8 bit words

    john
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-06-02 13:55
    In that other thread they talked about an extra bit at the start (which leads to 511,
    which cannot be with 8bit data) that must be stripped off.
    I think the right code is
    ··· CPU.writePin(enablePin,false);· //enable, triggers at falling edge
    ··· CPU.shiftOut(dataPin, clockPin, nBits, CPU.SHIFT_MSB, muxbyte <<11);
    ··· clockIn = CPU.shiftIn(dataPin,clockPin,readSize,CPU.POST_CLOCK_MSB);
    ··· clockIn = clockIn&0xFF; //strip off extra bit
    ··· CPU.writePin(enablePin,true);··· // disable, setup for next trigger

    regards peter
  • verobelverobel Posts: 81
    edited 2006-06-02 13:59
    I looked at the 2004 thread on ADC0838 and it is all pBasic.. but there was an interesting discussion on the 9th bit.. and filtering it out with & $00FF .. or & 0x00FF (for java), which I have tried.. on doing this the clockIn value is 255 or %11111111 ... expecting value more like %10011000

    I've double checked the pins
    There is the CPU.writePin(clockPin, false); // to give positive pulses
    There is the hgh to low trigger
    CPU.writePin(enablePin,true);
    CPU.writePin(enablePin,false);

    To my mind it is likely one of the following is wrong orr behaving different to pBasic:

    on CPU.shiftOut(dataPin, clockPin, nBits, CPU.SHIFT_MSB, muxbyte <<11);
    CPU.SHIFT_MSB // MSB sent first, nBits=5
    muxbyte << 11 // I want top 5 bits shifted out, so I shift them to the top first

    on CPU.shiftIn(dataPin,clockPin,readSize,CPU.POST_CLOCK_MSB);
    CPU.POST_CLOCK_MSB // post clock sampling and MSB-first transmission

    john
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-06-02 14:02
    You should also change
    ····· lgt = lgt*100/255-1;·· // -1 added to avoid field overflow at 255
    ····· pot = adc.read(1);···· // read pot from chn 1
    ····· pot = pot*100/255-1;
    to
    ····· lgt = (lgt*100)/255;
    ····· pot = adc.read(1);···· // read pot from chn 1
    ····· pot = (pot*100)/255;

    I think the IDE is smart enough to recognize that mathematically
    ····· lgt = lgt*100/255-1 equals lgt=lgt*constant-1
    where constant is·100/255
    But due to integer math, 100/255 = 0, hence lgt=-1 using the current formula.

    It is always wise to use brackets in your formula. That way you have full control
    on how calculation is done.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-06-02 14:06
    The adc0831 class uses
    ··· CPU.writePin(enablePin,false);
    ··· clockIn = CPU.shiftIn(dataPin,clockPin,readSize,CPU.POST_CLOCK_MSB);
    ··· CPU.writePin(enablePin,true);
    ··· CPU.delay(100);
    ··· lastRaw = clockIn;······························ // raw is protected

    (there is no 9th bit there)
    so I guess the shift modes are correct.

    regards peter
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-06-02 14:16
    Also, initialize CS pin high in the 0838 constructor.
    Then in read method, make CS low, then read data, then make CS high again.

    That is the order used in the BS2 program from that other thread.

    Read_0838:
    · LOW CS
    · ' setup mux bits for single-ended mode
    · LOOKUP chan, [noparse][[/noparse]%1000, %1100, %1001, %1101,
    ··············· %1010, %1110, %1011, %1111], mux

    · SHIFTOUT Dio, CLk, MSBFIRST, [noparse][[/noparse]1\1, mux\4]···· ' send start, mux
    · SHIFTIN Dio, Clk, MSBPOST, [noparse][[/noparse]adc(chan)\9]····· ' read selected input into byte·variable
    · HIGH CS

    · RETURN

    regards peter
  • verobelverobel Posts: 81
    edited 2006-06-02 17:38
    hi Peter...thanks for all the feedback

    I have incorporated all your suggestions and get clockIn=255 ..want values in range approx: 30->230·.. so it still is not working as far as I can tell.

    debug shows:
    muxbyte=24 dataPin=6 clockPin=0 enablePin=5 readSize=9 clockIn=255

    latest programs attached
    I plugged by BS2pe back in and it still works..

    john
Sign In or Register to comment.