[PIC] Interfacing AD7490 with PIC16F1789

Status
Not open for further replies.

Poochi

Newbie level 1
Joined
Jul 18, 2018
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
12
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 a moderator:

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);
  }
}

 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…