+ Post New Thread
Results 1 to 2 of 2
  1. #1
    Newbie level 1
    Points: 14, Level: 1

    Join Date
    Jul 2018
    Posts
    1
    Helped
    0 / 0
    Points
    14
    Level
    1

    Interfacing AD7490 with PIC16F1789

    I am trying to read data from AD7490 via SPI but I keep getting FF. Can you please help me find out what is wrong with my code? The code is given below:

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    
    #include <16F1789.h>
    #fuses NOWDT, NOBROWNOUT, NOMCLR
    #use delay(internal=8MHz, clock=8MHz)
    #include <PIC16F1789_registers.h>
     
     
    #use rs232(baud=9600,parity=N,xmit=PIN_B6,rcv=PIN_B7,bits=8,stream=PORT1)
     
    #use spi(MASTER, CLK = PIN_C3, DI = PIN_C4, DO = PIN_C5, BAUD = 9600, BITS = 16, STREAM = PORT2, MODE = 2 )
     
     
    void main()
    {
     
    TRISC2 = 0;
     
     
     
       while(True)
       {
       
             RC2 = 0;
             ////////////////////////////////////////////
             spi_write(PORT2, 0xAB);
             spi_write(PORT2, 0x30);
             
              RC2 = 1;
              delay_us(5);
              
              RC2 = 0;
             
             
             spi_write(PORT2, 0x00);
             spi_write(PORT2, 0x00);
             
             delay_us(5);
             
                  
             int16 vol_X = spi_xfer(PORT2);
             
            
            
             /////////////////////////////////////////////
             RC2 = 1;
     
     
             fputc(vol_X, PORT1);
              
             delay_ms(500);
       
      
       }
     
     
    }
    Last edited by bassa; 18th July 2018 at 13:48. Reason: add code syntax

    •   AltAdvertisement

        
       

  2. #2
    Full Member level 1
    Points: 2,388, Level: 11
    Achievements:
    7 years registered

    Join Date
    Jan 2009
    Posts
    105
    Helped
    16 / 16
    Points
    2,388
    Level
    11

    Re: Interfacing AD7490 with PIC16F1789

    Hmmm, not familiar with code syntax, but I think you try to make single conversions of Vin10.
    checkout the sequence described in datasheet, the conversion output is interleaved, so you need one dummy write in front:

    Code C - [expand]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    
    void main()
    {
      // Setup
      TRISC2 = 0;
      RC2 = 1;
      delay_us(5);
     
      // Dummy write to trigger first conversion - data read is ignored
      RC2 = 0;
      // not sure if Port2 is 8 or 16 bit register, maybe try spi_write(PORT2, 0xAB30)
      spi_write(PORT2, 0xAB);
      spi_write(PORT2, 0x30);
      RC2 = 1;
      delay_ms(500);
     
      while(True)
      {
        // write to trigger next conversion - data read is result from previous write
        RC2 = 0;
        // not sure if Port2 is 8 or 16 bit register, maybe try spi_write(PORT2, 0xAB30)
        spi_write(PORT2, 0xAB);
        spi_write(PORT2, 0x30);
        RC2 = 1;
        
        int16 vol_X = spi_xfer(PORT2);
        
        // Output read data to UART
        fputc(vol_X, PORT1);
        delay_ms(500);
      }
    }



--[[ ]]--