• AVR Freaks

Hot!SPI A/D Convertor read issue PIC32MK

Author
Edwardsan2
New Member
  • Total Posts : 7
  • Reward points : 0
  • Joined: 2019/04/05 11:30:20
  • Location: 0
  • Status: offline
2019/04/22 22:10:54 (permalink)
0

SPI A/D Convertor read issue PIC32MK

Hey guys, i'm reading 0's with SPI from TI's ADS8344N chip. Not sure what the issue is. Pg.14 of the datasheet says one method is to "use 32 clock cycles per conversion; the last seven clock cycles simply shift out zeros on the DOUT line." So I configured MODE_32 to 1 and the send 32 control bits, but receive back a single 0 each time. Any help is greatly appreciated!
 
Im using a PIC32MK1024GPE100 development board
 
 
#include <SOLAR_ASSIST.h> //configure UART, pins, and disable interrupts
#include <stdio.h>

// send a 32 bits via spi and return the response
unsigned int spi_io(unsigned int o) {
    SPI2BUF = o; //send data to the slave
    while(!SPI2STATbits.SPIRBF) { //(wait while recieve buffer is not full)
        ;
    }
    int t;
    for( t=0 ; t < 100000 ; t++);
    //every time you read from SPI2BUF you should also read from it to prevent overflow
    return SPI2BUF; //read the data sent back by the slave
}
//initialize the spi2 and ADC module
void ad_init(){
 
 
 
     ANSELAbits.ANSA14=0; //disable analog functionality, on pins
     ANSELBbits.ANSB7=0;
     ANSELBbits.ANSB9=0;
 
 
 
    
    //CS=1; // NO DATA being sent right now so set CS to high, when data is 
                                                //going to be sent drive CS TO LOW(0);
    //Master SPI2 pins(Dev Board) are: SCK2(Pin70), SDI2(RPA14)(pin66), SDO2(RPB7)(pin 72), SS/CS=RB9(pin 76)
    //Master SPI1 pins(Final Circuit) are: SCK1(72), SDI1(RPC7), SDO1(RPC7), SS
    SDI2R=0b1101; //configure pin rpa14 as sdi2
    RPB7R=0b00100; //configure pin rpb7 as sdo2
    RPB9R=0b00100; //configure pin rpb9 as ss2
    
    SPI2CON=0; //turn off the SPI module and reset it
    SPI2BUF; // clear the rx buffer by reading from it
    SPI2BRG=0x0009; //baud rate to 2 MHz [SPI2BRG = (40000000/(2*desired))-1]
    SPI2STATbits.SPIROV=0; //clear the overflow bit
    SPI2CONbits.MODE32=1; //32 bits of data to be transferred
    SPI2CONbits.MSTEN=1; //Pic is the Master
    SPI2CONbits.MSSEN=1; //Enable use of SS2, so pic32 can drive SS high and low as needed
    SPI2CONbits.SSEN=0; //Pic is not a slave
    SPI2CONbits.CKP=0; //clock is low when no data is being transmitted 
    SPI2CONbits.CKE=1; //Dout data changes on falling edge
    
    SPI2CONbits.ON=1; //turn SPI on, note: in slave mode you do not turn on baud
    
    //set desired clock mode for ADC1
    //CS=0; //enable the ADC chip
    spi_io(0x87); //When power is first applied user must set desired clock mode
                                                  //0b10000111 is sent:No power down between conversion
}


unsigned int adc_rw(int channel){
    unsigned int adcvalue = 0;
    unsigned int commandbits = 0b00000000; // Read nothing....
    char bufa[100]={}; // buffer for comm. with the user
    switch(channel) // Switch case to select channel
    {
      case 1: {
        commandbits = 0b10000111000000000000000000000000; // Select channel 0
        }
        break;
      case 2: {
        commandbits = 0b11000111000000000000000000000000; // Select channel 1 
        }
        break;
      case 3: {
        commandbits = 0b10010111000000000000000000000000; // Select channel 2 
        }
        break;
      case 4: {
        commandbits = 0b11010111000000000000000000000000; // Select channel 3 
        }
        break;
      case 5: {
        commandbits = 0b10100111000000000000000000000000; // Select channel 4 
        }
        break;
    }
    adcvalue=spi_io(commandbits); 
    sprintf(bufa,"%d\r\n",adcvalue);
    serialTransmit(bufa);  //read 23 bit value returned by ADS
    return adcvalue>>7; //return adc value shifted to right by 7 since,7 cycles are 0's, making 16 bits
}


int main(void){
    char buf[100]={}; // buffer for comm. with the user
    picConfigure(9600);
    ad_init();
    float readValue=0;
    float readVoltage=0;
    int iChannel=1;
    while(1){
        for(iChannel=1;iChannel<=3;iChannel++){
            readValue=adc_rw(iChannel);      //get 16 bits from ads8344
            readVoltage = (readValue*(3.3))/(65535); //convert 16 bits to analog value
            sprintf(buf,"Channel %d Voltage: %6.4f V\r\n",iChannel,readVoltage);
            serialTransmit(buf);
        }
    }
    return 0;
}

post edited by Edwardsan2 - 2019/04/23 16:30:37
#1

6 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9966
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: SPI A/D Convertor read issue 2019/04/23 00:17:41 (permalink)
    0
    You left out the most important detail.
    What PIC are you using?
    Is the SPI data input pin analog capable?
    If yes, have you switched it to digital mode?
     
    #2
    Edwardsan2
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/04/05 11:30:20
    • Location: 0
    • Status: offline
    Re: SPI A/D Convertor read issue 2019/04/23 05:38:24 (permalink)
    0
    My bad, Im using a PIC32MK1024GPE100 development board,
    I dont see anything in the datasheet about SPI2 being analog capable, or how to switch it to digital mode.
     
    #3
    mbrowning
    Just a Member
    • Total Posts : 1315
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: SPI A/D Convertor read issue 2019/04/23 06:10:11 (permalink)
    0
    SDI2R=0b1101; //configure pin rpa14 as sdi2

    perhaps you could look at pin 66 in table 5
    And then risk studying table 13-3

    Oh well - there's always next year
    #4
    Edwardsan2
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/04/05 11:30:20
    • Location: 0
    • Status: offline
    Re: SPI A/D Convertor read issue 2019/04/23 06:37:15 (permalink)
    0
    Sorry Im still a beginner, but doesn't the datasheet say this.
    "
    When a remappable peripheral is active on a given I/O pin, it takes priority over all other digital I/O and digital communication peripherals associated with the pin. Priority is given regardless of the type of peripheral that is mapped. Remappable peripherals never take priority over any analog functions associated with the pin."
     
    once i configure sdi2 on PPS, doesnt it take precedence over Port RA12, since its not an analog pin.
    And the ADC module is off by default so I think that line of code is setup correctly.
     
    EDIT:
    Forgot to set the ANSELA14 pin to 0,QHB and mbrowning were right. Thanks guys
    post edited by Edwardsan2 - 2019/04/23 07:45:46
    #5
    mbrowning
    Just a Member
    • Total Posts : 1315
    • Reward points : 0
    • Joined: 2005/03/16 14:32:56
    • Location: Melbourne, FL
    • Status: offline
    Re: SPI A/D Convertor read issue 2019/04/23 09:02:12 (permalink)
    0
    That analog inputs need to be configured (turned off) for digital inputs to work is probably the most common problem I've seen on this forum. It's very easy to miss this requirement, especially for those new to Microchip devices.

    Oh well - there's always next year
    #6
    Edwardsan2
    New Member
    • Total Posts : 7
    • Reward points : 0
    • Joined: 2019/04/05 11:30:20
    • Location: 0
    • Status: offline
    Re: SPI A/D Convertor read issue 2019/04/23 09:54:45 (permalink)
    0
    Thanks for the reply the 2 pic learning resources I'm using never had ANSEL, so I never knew it existed. I'll make sure to double check the data sheet before configuring next time.
     
    After adding these lines at the beginning of ad_init(), I still get 0's.
     
        ANSELAbits.ANSA14=0; //disable analog functionality, on pins
        ANSELBbits.ANSB7=0;
        ANSELBbits.ANSB9=0;

     
    Im sending 32 bits(MSB first), and expect 32 bits per transaction but am just getting 0's, any ideas.
    #7
    Jump to:
    © 2019 APG vNext Commercial Version 4.5