• PicŪ Basic


  • Working with the AD7705 / MX7705 - contributed by Marco D'Onofrio.

    Working with the AD7705 / MX7705

    The codes have been developed for reading/writing operation to the Analog Device AD7705 and Maxim MX7705 (not MAX7705:it is a different chip) 16 bit Sigma Delta AD converter with on chip digital filter and programmable gain amplifier. The AD7705 is a very good analog to digital converter but the output data rate is limited to 500Hz.

    The AD7705 and MX7705 are pin to pin compatible.

    Datasheet for AD7705. Attach:AD7705.pdf

    Datasheet for MX7705. Attach:MX7705.pdf



    The reset pin has been tied to VCC (+5V) with 4k7 resistor. The device can be reset connecting this pin to GND or via software.

    The DRDY pin is the device ready flag. When DRDY is set to +5V then the device is busy. The Ready flag can be checked also via software reading the first MSB bit of the communication register. If the bit is set to 1 then the 7705 is busy.

    Pin 10 and 9 are for the external voltage reference. I have used the MicroChip MCP1525 in TO92 package. This chip is set to 2.5V.

    I used the 7705 in one channel unipolar mode. So I tied the AN1- pin (pin 8) to GND. AN1+ pin (pin 7) was connected to a potentiometer.

    Pin 1 is the serial clock input pin while pin 4 is the Chip Select Pin. When CS is 0, the device is selected. This pin can be connected to GND if only one device is used on the serial bus.

    In pin 14 (DIN) a serial data is sent from PICŪ while pin 13 (DOUT) is the output from the 7705 to uC. To save PICŪ pins these ports can be connected together using a 4k7 pull up (to VCC) resistor. See datasheet for connection hints.

    The PICŪ pins configuration for MX7705_GLCD.bas code are described with the following definitions:

    Code:
    Symbol SCK=PORTA.5 'Clock pin
          Symbol SI=PORTA.3 ' AD7705 Data in pin
          Symbol SO=PORTA.2 ' AD7705 Data out pin
          Symbol CS = PORTA.4 'AD7705 Chip Select pin
          Symbol ready=PORTA.1 'AD7705 Ready pin
    5 PICŪ pins have been used. This configuration can be easily modified to use only 2 PICŪ pins (serial clock and common dataIn/dataOut pin) as in MX7705_2W.bas.

    For this code the definitions are:

    Symbol SCK=PORTA.5 'Clock pin
    Symbol SI=PORTA.2 ' AD7705 Data in pin
    Symbol SO=PORTA.2 ' AD7705 Data out pin. Is the same pin of SI
    The 7705 needs an external clock to work. The clock can be generated from an oscillator (square wave clock) or from a crystal. In oscillator mode it is possible to use the ClockOut pin (pin 3) to output a clock signal while the pin 2 is used as clock source. In crystal mode you have to connect a crystal and two capacitors as normally done with PICŪ. See 7705 datasheet for frequency range. In my circuit I have used an external 4Mhz crystal with two 27pF capacitors.

    The following picture shows how I made the connection to AD7705 using Proton Board and 16F877 running @ 4Mhz. In this picture the AD7705 channel 1 was connected to a potentiometer. On the GLCD are shown the AD converter word value (21922), the AD value in 16 bit format and the scaled value in volts format.



    Thanks to GLCD and Proton basic, it is very easy to plot the voltage trend.



    The two wires connection is shown below. Note the pull up resistor connected to 7705 pin 13 and the jumper between pin 13 and pin 14. CS signal from PICŪ is not present (CS connected to GND). The Ready pin of 7705 is still connected to PICŪ but is not used. The ready flag is checked via serial interface.



    The software is divided into two sections. The first section is for 7705 set-up. In fact at power-up or after a reset the 7705 waits for set-up configuration commands from the uC. The channel, the clock source and the operating mode are selected writing to three registers:the communication, the clock and the set-up registers. For writing operation two bytes must be sent. The first one is always sent to communication register and it is used for register (for example clock or set-up register) and operation (write/read) selection. The byte is also used to select the conversion channels and if it is required a read or write mode to the selected register. The second byte is the command sent to the register selected in the first byte. The 7705 always expects bytes in MSB format.

    Example:

    First byte: Send=$20 ' send command $20 to communication register. $20 hex is 00100000 in binary format. Look at the communication register in the table below.



    Let’s analysize the byte 00100000

    DRDY bit set to 0: this bit must be set to 0 for write operation. During read operation it indicates if the device in ready (0)/busy(1) condition.

    RS2-1-0:register selection. 010 select the clock register.



    R/W: set to 0 for write operation. Set to 1 for read operation. In this example is set to 0 because we are sending a write command to the clock register (selected with RS2-1-0 bits).

    STBY:set to 1 to put the device in power save mode. Set to 0 for normal operation.

    CH1-0: channel selection. Set to 00 to select channel 1.

    Second byte: rec=$C 'write on clock register': clock division=1, clock bit=1, master clock enabled (bit set to 0), bit output rate set to 50hz (maximum is 500Hz). The binary code is 00001100. See clock register table below. Note that in MX7705 the table has the first bit set always to 1 (is the Maxim identification) also if you sent a 0 bit.



    For reading operation one byte is sent from the PICŪ to the communication register. The byte includes the register to be read and the R/W bit always set to 1. After the command is sent, the 7705 send back to the PICŪ a byte or word with the requested information.

    The below picture shows the timing diagrams for read and write operation.




    The second part of the software is free running conversion with trend plot.

    The code for read/write operation is the following (related to 5 pin interface. The two wires code is very similar. Only CS command are no more present):

    AD_write:

    'AD7705 write routine

    'first byte is for channel selection and register selection (R/W bit is set to 0)

    'second byte is the value to be sent

    'this routine is valid only for 8 bit register as communication,clock and set-up register.

    Low CS ' Enable device

    SHOut SI, SCK, msbfirst, [Send\8] ' Send command to communication register

    SHOut SI, SCK, msbfirst, [rec\8] ' Send value to the selecte register

    High CS ' Disable device

    Print At 2,1,"Send:",HEX Send," Dat:",HEX rec," " 'print command

    Return

    AD_read:

    'Read from AD7705

    'first byte is the register address (R/W bit is set to 1)

    'second byte is register content

    Low CS 'Enable device

    SHOut SI, SCK, msbfirst, [Send\8] 'Send read command and address

    SHIn SO, SCK, msbpre, [rec\8] ' Read data

    High CS ' Disable device

    Print At 3,1,"Rec:",HEX Send," Dat:",HEX rec," "

    Return

    AD_read2:

    'Read AD7705 conversion result

    'A 16 bit value is expected

    Low CS 'Enable device

    SHOut SI, SCK, msbfirst, [Send\8] ' Send read command and data register address

    SHIn SO, SCK, msbpre, [ad\16] ' Read 16 bit data

    High CS ' Disable device

    Print At 0,1,"Rec:",HEX Send," AD:",Dec ad," "

    Print At 1,1,"Ad:",BIN ad

    value= (ad*2.5)/65535

    Print At 2,1,"Volts:",DEC8 value," "

    Return

    The Ad_Read2 subroutine is for reading the converted value (a word value) while the Ad_read subroutine is for register reading.

    The 7705 Ads have many features not described in this short page. My code is just a kick start code that I hope can save you some time. If you have any question you can send me an email.

    Two codes are provided.

    The first one is MX7705_GLCD.bas and it uses 5 PICŪ pins (CS,SI,SO,Ready,SCLK).
    Code:
          '****************************************************************
          '*  Name    : MX7705 16f877.BAS                                 *
          '*  Author  : Marco D'Onofrio                                   *
          '*  Notice  : Copyright (c) 2004 Donomark                       *
          '*          : All Rights Reserved                               *
          '*  Date    : 26/11/2004                                        *
          '*  Version : 1.0                                               *
          '*  Notes   : version with 16f877,GLCD and trend plot           *
          '*          : Code working properly.                            *
          '****************************************************************
          Include "PROTON_G4.INT"
           
          ' Demo software for AD7705 (Analog device) and Maxim MX7705 (the chips are pin to pin compatible) 16 bits Sigma delta AD converter with digital filtering
          ' In this demo 5 PICŪ pins are used. However it is possible to reduce the line to two (Cs connected to GND, ready flasg
          ' read from register and not from pin, SI and SO pin tied togheter with a pull-up resistor. See AD7705 or MX7705 datasheet
          ' Draw a scale with X and Y lines
          ' Incremental tick marks are also added
           
          ' Using a GRAPHIC LCD with an external FONT            
           
                  Symbol SCK=PORTA.5 'Clock pin
                  Symbol SI=PORTA.3 ' AD7705 Data in pin
                  Symbol SO=PORTA.2 ' AD7705 Data out pin
                  Symbol CS = PORTA.4 'AD7705 Chip Select pin
                  Symbol ready=PORTA.1 'AD7705 Ready pin
           
           'Variables for scale drawing (the code for scale drawing is from Proton demo file)
           
                  Dim S_XSTART As Byte
                  Dim S_XEND As Byte
                  Dim S_YSTART As Byte
                  Dim S_YEND As Byte
                  Dim S_YTICK As Byte
                  Dim S_XTICK As Byte
                  Dim XPOS As Byte
                  Dim YPOS As Byte  
           
                  Dim time_unit As Byte 'variable used for ascissa                
           
                  Dim Send As Byte   'byte sent to communication register of AD7705
                  Dim rec As Byte    'byte received from AD7705:only for communication register, set-up register and clock register (8 bit registers)
                  Dim I As Byte      'general purpose variable
                  Dim ad As Word     'ad converter value read from AD7705
                  Dim value As Float  'variable used for Y drawing
                  Dim value_old As Float 'previous value
           
          Clear 'clear all variables
          Cls   'clear LCD
          S_XSTART = 0                    ' XPOS start position
          S_XEND = 119                    ' XPOS end position
          S_YSTART = 0                    ' YPOS start position
          S_YEND = 59                             ' YPOS end position
          S_XTICK = 10                    ' X increment of TICKS
          S_YTICK = 10                    ' Y increment of TICKS
           
          start:
          Print At 0,1,"Mx7705 AD Config"
          DelayMS 250
           
          Send=$20 ' send command $20 to communication register: channel 1 selected, next operation is a write on clock register
          rec=$C 'write on clock register: clock division=1, clock bit=1, master clock enabled (bit set to 0), bit output rate set to 50hz (maximum is 500Hz)
          GoSub AD_write 'call the subroutine for AD write
          Send=$28 'send command to communication register:next operation is a read from clock register to check if previous command succeded
          GoSub AD_read
           
          calib: 'self-calibration
          Send=$10 ' send command to communication register:channel 1 selected, next operation is a write on set-up register
          rec=$46  ' write $46 (bin 01000110) on set-up register:enable self calibration,gain 1, unipolar operation, buffer control on, enable filter synchronization
          'note that after the self calibration, AD7705 start free conversion cycle
          GoSub AD_write 'send command to AD7705
          Send=$18 'read back to check command has been accepted
          GoSub AD_read
           
          While ready=1 'check if ready pin is low. If low AD7705 is ready for next operation
          Print At 4,1,"wait"  
          High PORTD.0
          Wend
          DelayMS 100
          Low PORTD.0
           
          'AD7705 set-up finished. Start conversions
          Cls 'clear LCD
          GoSub Draw_Scale' Draw the scale
          loop:
          Send=$38 'send command to communication register:next operation is reading the 16bit data register where conversion result are stored
          GoSub AD_read2
          While ready=1 'wait AD7705 until the conversion is finished
          Print At 4,1,"wait"  
          Wend
                          Inc time_unit   ' draw the trend
                          Line 1,time_unit-1,(60-value_old*10), time_unit,(60-value*10)
                          If time_unit=119 Then 'if time unit is 119, clear GLCD and start again
                          Cls
                  GoSub Draw_Scale        ' Draw the scale
                          Clear time_unit
                          EndIf
                          value_old=value
          If InKey=0 Then GoTo calib 'if SW1 is pressed, perform self-calibration and starts againg conversion
          If InKey=1 Then 'if SW2 is pressed, put PICŪ in pause mode
          Snooze 5
          While InKey=16
              Snooze 5
          Wend
          EndIf
          GoTo loop
           
          AD_write:
          'AD7705 write routine
          'first byte is for channel selection and register selection (R/W bit is set to 0)
          'second byte is the value to be sent
          'this routine is valid only for 8 bit register as communication,clock and set-up register.
           
                          Low CS                         ' Enable serial Dac
                  SHOut SI, SCK, msbfirst, [Send\8]       ' Send write enable command
                  SHOut SI, SCK, msbfirst, [rec\8]       ' Send write enable command
                  High CS                          ' Disable to execute command
                  Print At 2,1,"Send:",HEX Send," Dat:",HEX rec,"  "  'print command        
                  Return
           
          AD_read:
          'Read from AD7705
    
          'first byte is the register address  (R/W bit is set to 1)
          'second byte is register content
                  Low CS
                  SHOut SI, SCK, msbfirst, [Send\8]       ' Send read command and address
                  SHIn SO, SCK, msbpre, [rec\8]   ' Read data
                  High CS                          ' Disable
                  Print At 3,1,"Rec:",HEX Send," Dat:",HEX rec,"  "
                  Return
           
          AD_read2:
          'Read AD7705 conversion result
          'A 16 bit value is expected
                  Low CS
                  SHOut SI, SCK, msbfirst, [Send\8]       ' Send read command and address
                  SHIn SO, SCK, msbpre, [ad\16]   ' Read 16 bit data
                  High CS                          ' Disable
                  Print At 0,1,"Rec:",HEX Send," AD:",Dec ad,"   "
                  Print At 1,1,"Ad:",BIN ad
                  value= (ad*2.5)/65535
                  Print At 2,1,"Volts:",DEC8 value,"  "
                  Return        
           
          '---------------------------------------------------------------------------      
          ' Draw_Scale subroutine which draws an XY scale with tick marks
          ' Assumes values assigned to S_XPOS,S_YPOS,S_XTICK,X_XEND,S_YEND and S_YTICK
           
          Draw_Scale:
          ' Draw vertical Y axis
                          I = 0
                  YPOS = S_YSTART
                          Repeat
                          Plot YPOS,S_XSTART
                                  Inc I
                                  Inc YPOS
                  Until YPOS > S_YEND
           
          ' Add the tick marks to the Y axis
                          YPOS = S_YSTART
                          Repeat
                          Plot YPOS,S_XSTART + 1
                                  YPOS = YPOS + S_YTICK
                  Until YPOS > S_YEND
           
          ' Draw horizontal X axis
                          XPOS = S_XSTART
                          Repeat
                                  Plot S_YEND,XPOS
                                  Inc XPOS
                  Until XPOS > S_XEND
    
          ' Draw X axis tick marks
                          XPOS = S_XSTART
                          Repeat
                                  Plot S_YEND - 1,XPOS
                                  XPOS = XPOS + S_XTICK
                  Until XPOS > S_XEND
    
                          Return        
    
           Include "FONT.INC"
    Code:
    '****************************************************************
    '*  Name    : MX7705 GLCD 2 WIRES                               *
    '*  Author  : Marco D'Onofrio                                   *
    '*  Notice  : Copyright (c) 2004 Donomark                       *
    '*          : All Rights Reserved                               *
    '*  Date    : 10/12/2004                                        *
    '*  Version : 1.0                                               *
    '*  Notes   : version with 16f877,GLCD and trend plot           *
    '*          : 2 wires interface with serial ready flag reading  *
    '****************************************************************
    Include "PROTON_G4.INT"
    
    ' Demo software for AD7705 (Analog device) and Maxim MX7705 (the chips are pin to pin compatible) 16 bits Sigma delta AD converter with digital filtering
    ' In this demo 2 PICŪ pins are used. Cs is connected to GND, ready flag is read via serial line
    ' SI and SO pin are tied togheter with a 4k7 resistor pull-up resistor to VCC. See AD7705 or MX7705 datasheet
    ' Only Si/SO pin a clock pin are used
    ' Draw a scale with X and Y lines
    ' Incremental tick marks are also added
    ' Code working properly
    
    ' Using a GRAPHIC LCD with an external FONT        
    
            Symbol SCK=PORTA.5 'Clock pin
            Symbol SI=PORTA.2 ' AD7705 Data in pin
            Symbol SO=PORTA.2 ' AD7705 Data out pin. Is the same pin of SI
    
     'Variables for scale drawing (the code for scale drawing is from Proton demo file)
    
            Dim S_XSTART As Byte
            Dim S_XEND As Byte
            Dim S_YSTART As Byte
            Dim S_YEND As Byte
            Dim S_YTICK As Byte
            Dim S_XTICK As Byte
            Dim XPOS As Byte 
            Dim YPOS As Byte   
    
            Dim time_unit As Byte 'variable used for ascissa
    
    
            Dim Send As Byte   'byte sent to communication register of AD7705
            Dim rec As Byte    'byte received from AD7705:only for communication register, set-up register and clock register (8 bit registers)
            Dim ready As Byte  'AD7705 Ready BIT
            Dim I As Byte      'general purpose variable
            Dim ad As Word     'ad converter value read from AD7705
            Dim value As Float  'variable used for Y drawing
            Dim value_old As Float 'previous value 
    
    Clear 'clear all variables
    Cls   'clear LCD
    S_XSTART = 0            ' XPOS start position
    S_XEND = 119            ' XPOS end position
    S_YSTART = 0            ' YPOS start position
    S_YEND = 59                ' YPOS end position
    S_XTICK = 10            ' X increment of TICKS
    S_YTICK = 10             ' Y increment of TICKS
    
    start:
    Print At 0,1,"Mx7705 AD Config"
    DelayMS 250
    
    Send=$20 ' send command $20 to communication register: channel 1 selected, next operation is a write on clock register
    rec=$C 'write on clock register: clock division=1, clock bit=1, master clock enabled (bit set to 0), bit output rate set to 50hz (maximum is 500Hz)
    GoSub AD_write 'call the subroutine for AD write
    Send=$28 'send command to communication register:next operation is a read from clock register to check if previous command succeded
    GoSub AD_read
    
    calib: 'self-calibration
    Send=$10 ' send command to communication register:channel 1 selected, next operation is a write on set-up register
    rec=$46  ' write $46 (bin 01000110) on set-up register:enable self calibration,gain 1, unipolar operation, buffer control on, enable filter synchronization
    'note that after the self calibration, AD7705 start free conversion cycle
    GoSub AD_write 'send command to AD7705
    Send=$18 'read back to check command has been accepted
    GoSub AD_read
    
    Send=$8 'send read request to communication register
    GoSub AD_read
    ready=rec & %10000000 'check if the MSB BIT is =1
    While ready=%10000000    'If MSB bit (bit 8) is 1 AD7705 is busy
    Print At 4,1,"wait"      'Wait until ready bit is 0
    High PORTD.0
    GoSub AD_read
    ready=rec & %10000000
    Wend
    DelayMS 100
    Low PORTD.0
    
    'AD7705 set-up finished. Start conversions
    Cls 'clear LCD
    GoSub Draw_Scale' Draw the scale
    loop:
    Send=$38 'send command to communication register:next operation is reading the 16bit data register where conversion result are stored
    GoSub AD_read2
    Send=$8 'send read request to communication register
    GoSub AD_read
        ready=rec & %10000000 'check if the MSB BIT is =1
        While ready=%10000000    'wait AD7705 until the conversion is finished
        Print At 4,1,"wait"     
        High PORTD.0
        GoSub AD_read
        ready=rec & %10000000
        Wend
    
            Inc time_unit   ' draw the trend
               Line 1,time_unit-1,(60-value_old*10), time_unit,(60-value*10)
            If time_unit=119 Then 'if time unit is 119, clear GLCD and start again
            Cls
            GoSub Draw_Scale    ' Draw the scale
            Clear time_unit
            EndIf
            value_old=value
    If InKey=0 Then GoTo calib 'if SW1 is pressed, perform self-calibration and starts againg conversion
    If InKey=1 Then 'if SW2 is pressed, put PICŪ in pause mode
    Snooze 5
    While InKey=16
        Snooze 5
    Wend
    EndIf
    GoTo loop
    
    AD_write:
    'AD7705 write routine
    'first byte is for channel selection and register selection (R/W bit is set to 0)
    'second byte is the value to be sent
    'this routine is valid only for 8 bit register as communication,clock and set-up register.
    
    
            SHOut SI, SCK, msbfirst, [Send\8]       ' Send command to communication register
            SHOut SI, SCK, msbfirst, [rec\8]       ' Send value to the selecte register
            Print At 2,1,"Send:",HEX Send," Dat:",HEX rec,"  "  'print command        
            Return
    
    AD_read:
    'Read from AD7705
    'first byte is the register address  (R/W bit is set to 1)
    'second byte is register content
    
            SHOut SI, SCK, msbfirst, [Send\8] 'Send read command and address
            SHIn SO, SCK, msbpre, [rec\8]   ' Read data
            Print At 3,1,"Rec:",HEX Send," Dat:",HEX rec,"  "
            Return
    
    AD_read2:
    'Read AD7705 conversion result
    'A 16 bit value is expected
            SHOut SI, SCK, msbfirst, [Send\8] ' Send read command and data register address
            SHIn SO, SCK, msbpre, [ad\16]   ' Read 16 bit data
            Print At 0,1,"Rec:",HEX Send," AD:",Dec ad,"   "
            Print At 1,1,"Ad:",BIN ad
            value= (ad*2.5)/65535
            Print At 2,1,"Volts:",DEC8 value,"  "
            Return        
    
    '---------------------------------------------------------------------------       
    ' Draw_Scale subroutine which draws an XY scale with tick marks
    ' Assumes values assigned to S_XPOS,S_YPOS,S_XTICK,X_XEND,S_YEND and S_YTICK
    
    Draw_Scale:
    ' Draw vertical Y axis
            I = 0
            YPOS = S_YSTART
            Repeat
                Plot YPOS,S_XSTART
                Inc I
                Inc YPOS
            Until YPOS > S_YEND
    
    ' Add the tick marks to the Y axis
            YPOS = S_YSTART
            Repeat
                Plot YPOS,S_XSTART + 1
                YPOS = YPOS + S_YTICK
            Until YPOS > S_YEND
    
    ' Draw horizontal X axis
            XPOS = S_XSTART
            Repeat
                Plot S_YEND,XPOS
                Inc XPOS
            Until XPOS > S_XEND
    
    ' Draw X axis tick marks
            XPOS = S_XSTART
            Repeat
                Plot S_YEND - 1,XPOS
                XPOS = XPOS + S_XTICK
            Until XPOS > S_XEND
    
            Return        
    
     Include "FONT.INC"