Shop OBEX P1 Docs P2 Docs Learn Events
problem with writing register to ADC AD7730 — Parallax Forums

problem with writing register to ADC AD7730

renweirenwei Posts: 1
edited 2005-06-27 21:24 in BASIC Stamp
Hi,
·· I am doing a project on blood pressure measurement. currently i am doing signal conditioning part. i am trying to write register into AD7730(or AD7714 or any other chip with serial interface). it is very strange that sometimes it works only for some certain register. that means i can see RDY goes low and i can start reading register. i am using SHIFTOUT command. i am afraid of time requirement cannot be matched with AD7730. can anyone help to look at my code below? i try with clock polarity =1 but still cannot.

thank you very much



' P0 -- SCLK, P1 -- RDY, P2 -- DIN, P3 -- DOUT, P4 -- CS
' {$STAMP BS2}

OUTPUT 0
OUTPUT 3
OUTPUT 4
INPUT· 1
INPUT· 2

LOW 0
HIGH 3
HIGH 4

LOW 4

SHIFTOUT 3,0, MSBFIRST, [noparse][[/noparse]reset1\16, reset2\16]
HIGH 3
HIGH 4
LOW 0

PAUSE 1
LOW 4
SHIFTOUT 3,0, MSBFIRST, [noparse][[/noparse]WRITEmode\8]
HIGH 3
HIGH 4
LOW 0

PAUSE 1
LOW 4
SHIFTOUT 3,0, MSBFIRST, [noparse][[/noparse]regmode\16]
HIGH 3
HIGH 4
LOW 0

PAUSE 1
LOW 4
SHIFTOUT 3,0, MSBFIRST, [noparse][[/noparse]READmode\8]
HIGH 3
HIGH 4
LOW 0

AGAIN:
IF IN1=0 THEN below
GOTO AGAIN


below:
LOW 4
SHIFTIN 2,0, MSBPOST, [noparse][[/noparse]result\16]
SHIFTIN 2,0, MSBPOST, [noparse][[/noparse]dummi\16]
LOW 0
HIGH 4
DEBUG HEX result

'
code below cannot work
PAUSE 1
LOW 4
SHIFTOUT 3,0, MSBFIRST, [noparse][[/noparse]readstatus\8]
HIGH 3
HIGH 4
LOW 0

wieder:
IF IN1=0 THEN unter
GOTO wieder
unter:

LOW 4
SHIFTIN 2,0, MSBPOST, [noparse][[/noparse]result2\8]
SHIFTIN 2,0, MSBPOST, [noparse][[/noparse]dummi\16]
LOW 0
HIGH 4


DEBUG " status register is "
DEBUG HEX result2

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2005-06-23 12:55
    Just a few comments.

    1. PBASIC programs almost never need to use the OUTPUT and INPUT keywords -- the commands (like·HIGH, LOW, etc.)·handle this automatically.· There are a few exceptions, but as I just stated, they are exceptions.

    2. Using named pins will help others help you.· Since you didn't provide a schematic, the only thing we can deduce from your code is that P3 is the data out, and P0 is the clock out.

    3. You definitely want to use POL = 1 because that's how the SHIFTIN and SHIFTOUT clock works.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • Bruce BatesBruce Bates Posts: 3,045
    edited 2005-06-23 13:30
    renwei -

    A good deal needs to be done to the supplied program before it will pass through the Stamp Editor without errors. I suspect even more will need to be done before you are successful in using it with the ADC7730. I found no reference to the ADC7714 on the Analog Devices web site, so I reserve comment on that chip.

    Here is a copy of the program with the undefined areas outlined, and at least some rudimentary comments added. You will find that troubleshooting ANY program will be much easier if you add comments to it.

    ' P0 -- SCLK, P1 -- RDY, P2 -- DIN, P3 -- DOUT, P4 -- CS
    ' {$STAMP BS2}

    OUTPUT 0 'CLK
    INPUT 1 'RDY
    INPUT 2 'DIN
    OUTPUT 3 'DOUT
    OUTPUT 4 'CS

    '*****************************************************
    reset1 VAR Word 'UNDEFINED in supplied program! *
    reset2 VAR Word 'UNDEFINED in supplied program! *
    READmode VAR Word 'UNDEFINED in supplied program! *
    WRITEmode VAR Word 'UNDEFINED in supplied program! *
    regmode VAR Word 'UNDEFINED in supplied program! *
    result VAR Word 'UNDEFINED in supplied program! *
    result2 VAR Byte 'UNDEFINED in supplied program *
    dummi VAR Word 'UNDEFINED in supplied program! *
    readstatus VAR Byte 'UNDEFINED in supplied program! *
    '*****************************************************

    'Begin Chip Initialization Sequence
    LOW 0 'Bring CLK LOW
    HIGH 3 'Bring DOUT HIGH
    HIGH 4 'Bring CS HIGH
    LOW 4 'Bring CS LOW

    'End Chip Initialization Sequence

    SHIFTOUT 3,0, MSBFIRST, [noparse][[/noparse]reset1\16, reset2\16] 'Send unknown data to ADC
    HIGH 3 'Set DOUT HIGH
    HIGH 4 'Set CS HIGH
    LOW 0 'Set CLK LOW

    PAUSE 1 'Wait for chip to settle

    LOW 4 'Set CS LOW
    SHIFTOUT 3,0, MSBFIRST, [noparse][[/noparse]WRITEmode\8] 'Send unknown data to ADC
    HIGH 3 'Set DOUT HIGH
    HIGH 4 'Set CS HIGH
    LOW 0 'Set CLK LOW

    PAUSE 1 'Wait for chip to settle

    LOW 4 'Set CS LOW
    SHIFTOUT 3,0, MSBFIRST, [noparse][[/noparse]regmode\16] 'Send unknown data to ADC
    HIGH 3 'Set DOUT HIGH
    HIGH 4 'Set CS HIGH
    LOW 0 'Set CLK LOW

    PAUSE 1 'Wait for chip to settle

    LOW 4 'Set CS LOW
    SHIFTOUT 3,0, MSBFIRST, [noparse][[/noparse]READmode\8] 'Send unknown data to ADC
    HIGH 3 'Set DOUT HIGH
    HIGH 4 'Set CS HIGH
    LOW 0 'Set CLK LOW

    AGAIN:
    IF IN1=0 THEN below 'Check RDY pin on chip
    GOTO AGAIN


    below:
    LOW 4 'Set CS LOW
    SHIFTIN 2,0, MSBPOST, [noparse][[/noparse]result\16] 'Receive data into UNDEFINED location
    SHIFTIN 2,0, MSBPOST, [noparse][[/noparse]dummi\16] 'Bypass 16 additional bits of data to UNDEFINED location
    LOW 0 'Set CLK LOW
    HIGH 4 'Set CS HIGH
    DEBUG HEX result 'Display result on DEBUG terminal in HEX - 2 bytes of data

    '
    code below cannot work
    PAUSE 1 'Wait for chip to settle
    LOW 4 'Set CS LOW
    SHIFTOUT 3,0, MSBFIRST, [noparse][[/noparse]readstatus\8] 'Send unknown data to ADC - 8 bits ONLY
    HIGH 3 'Set DOUT HIGH
    HIGH 4 'Set CS HIGH
    LOW 0 'Set CLK LOW


    wieder:
    IF IN1=0 THEN unter 'Check RDY pin on chip
    GOTO wieder

    unter:

    LOW 4 'Set CS HIGH
    SHIFTIN 2,0, MSBPOST, [noparse][[/noparse]result2\8] 'Receive data into UNDEFINED location
    SHIFTIN 2,0, MSBPOST, [noparse][[/noparse]dummi\16] 'Bypass 16 additional bits of data to UNDEFINED location
    LOW 0 'Set CLK LOW
    HIGH 4 'Set CS HIGH

    ' Display Status Regster on DEBUG Terminal
    DEBUG " status register is "
    DEBUG HEX result2

    END

    I made no attempt to check the logic of the program since the data areas indicated were not defined. I did note that some commands require 8 bits of data, some use 16 bits, and others use 24 bits, so be careful to supply the appropriate number of bits of data for the operation involved. Note also that an external master clock is required for this chip.

    The chip can be used in SPI mode using only three wires, which is probably the way to go, at least during the early development stage. All that is required to communicate with the chip are SCLK, DIN, and DOUT. _CS can be tied LOW, and POL tied as appropriate. _SYNC and _RESET can both be tied HIGH.

    Regards,

    Bruce Bates
  • Mohamed RefkyMohamed Refky Posts: 47
    edited 2005-06-27 21:24
    Hello,
    I'm using AD7730 in weigh scale applications,here is example codes of writing and reading of registers:
    ADdata······· var··· word······· 'variable to hold 16 bit result.
    DATAin····· con····· 2············'AD data input pin.
    DATAout····con···· 3··········· 'AD data output pin.
    SCKL··········· con···· 4··········· 'clock to AD.
    RDY············· con···· 5··········· 'RDY input .
    input 5
    Initial:
    SHIFTOUT DATAin,SCLK,MSBFIRST,[noparse][[/noparse]$FFFF\16,$FFFF\16] 'write 32 ones will reset the AD7730 to the default state.
    SHIFTOUT DATAin,SCLK,MSBFIRST,[noparse][[/noparse]$02] 'write to communication register setting next operation as write to mode register.
    SHIFTOUT DATAin,SCLK,MSBFIRST,[noparse][[/noparse]$3080\16] 'write to mode register starting continuous conversion for 10mV input range,unipolar,16 bit data word and 5V reference.
    SHIFTOUT DATAin,SCLK,MSBFIRST,[noparse][[/noparse]$21] 'write to communication register setting next operation as continuous read from data register.
    low 2············· ' set DIN line low to insure part is not reset while in continuous reade mode.
    ReadData:
    waitRDY:
    IF RDY = 1 THEN waitRDY············ 'wait for RDY to go low to indicate output update.
    SHIFTIN DATAout,SCLK,MSBPOST,[noparse][[/noparse]ADdata\16]······ 'read conversion result from data register.
    DEBUG DEC ADdata,cr········· ·'display data in decimal.
    pause 500·································· 'wait 0.5 second between reading.
    GOTO ReadData
    Regards
    Mohamed Refky
Sign In or Register to comment.