Jump to content


Photo

SPI between MSP430/432 and MCP3911

#MSP430 energia MCP3911 SPI MSP432

  • Please log in to reply
11 replies to this topic

#1 MSP432

MSP432

    Member

  • Members
  • PipPip
  • 18 posts

Posted 07 December 2016 - 10:44 PM

Hi,

I am trying to read ADC channel values from MCP3911 using MSP launchpad using SPI protocol. When i used Arduino Mega the protocol is working fine. But when i use any TI devices i am having a problem, it always gives me all 0'S or 1's.

I am using energia for MSP devices. Any i have chosen the appropriate pins as defined in the enegia pin layout.
msp430-----
static const uint8_t SS      = 8;  /* P2.7 */
static const uint8_t SCK     = 7;  /* P3.2 */
static const uint8_t MOSI    = 15;  /* P3.0 */
static const uint8_t MISO    = 14;  /* P3.1 */

Can someone help me troubleshoot where i am going wrong.

Thanks in advance.
 

// SPI Stuff here
#include "SPI.h"
const uint8_t MCP3911_CS = SS; // Teensy SPI CS1 = MCP3911


void setup() {

  //SPI Bus setup
  digitalWrite(MCP3911_CS,HIGH); //
  pinMode (MCP3911_CS, OUTPUT); // MCP3911

  
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE0);
  SPI.setClockDivider(SPI_CLOCK_DIV8); //i.e. 6MHz on a Teensy running at 48MHz. 
  SPI.begin();

  //Setup Serial Comms
  Serial.begin(115200);
  Write_MCP3911_Register (0x0D, B11000010); 
}

int32_t adc;
void loop()
{
  //adc = Read_MCP3911_24bit(0x00);
  Read_MCP3911_Register(0x0D);
//  Serial.print("Ch0 :  ");
//  Serial.println(adc);
  delay(200); 
}

uint8_t Write_MCP3911_Register (uint8_t MCP3911_Register_Address, uint8_t Command) {
  Serial.print("Command Register Received: ");
  Serial.print(MCP3911_Register_Address,HEX);
  Serial.print(" - Command Received: ");
  Serial.println(Command,BIN);

  MCP3911_Register_Address <<= 1;   //left shift address one digit
  MCP3911_Register_Address &= B00111110; // and ensure last digit is a zero for write command
  digitalWrite(MCP3911_CS, LOW); // now take CS low to enable SPI device
  SPI.transfer(MCP3911_Register_Address); // send address with write command to MCP3911
  SPI.transfer(Command); //now send payload
  digitalWrite(MCP3911_CS, HIGH); // deselect the CS pin. 

  Serial.print(" Write Command Byte Sent: ");
  Serial.println(MCP3911_Register_Address,BIN); // verify what command was sent (i.e. address and write bit = 0)

  //Now Verify all went well. If so, have the function return value of one,
  //otherwise, alert the user that something is amiss. 
  uint8_t Response = Read_MCP3911_Register (MCP3911_Register_Address>>1);
  if (Response == Command)  return 1;
  else 
  {
    Serial.println("");
    Serial.print("Error for register: ");
    Serial.print(MCP3911_Register_Address>>1,BIN);
    Serial.print(" - Command Sent: ");
    Serial.print(Command,BIN);
    Serial.print(" - Response Received: ");
    Serial.println(Response,BIN);
    Serial.println("");
    return 0;
  }
}

uint8_t Read_MCP3911_Register (uint8_t MCP3911_Register_Address) {
  
  MCP3911_Register_Address <<=1; //left shift address one bit for command byte 
  MCP3911_Register_Address |=1; // Ensure read bit is set
  
  Serial.print("  Read Byte Command Sent: ");
  Serial.print(MCP3911_Register_Address,BIN);
  
  digitalWrite(MCP3911_CS, LOW); 
  SPI.transfer(MCP3911_Register_Address); // send address with read command to MCP3911
  uint8_t Response = SPI.transfer(0x00); 
  digitalWrite(MCP3911_CS, HIGH);
 
  Serial.print(" - Response Received: ");
  Serial.println(Response,BIN);
  return Response;
}

void Reset_ADC()
{
 // Puts ADC into Reset Mode, i.e. stops ADC conversions until setup is complete.
  Write_MCP3911_Register (0x0D, B11000010); 
}

int32_t Read_MCP3911_24bit( uint8_t MCP3911_Register_Address)
  {
    uint8_t HB,MB,LB=0, MCP3911_CTRL=0;;
    int32_t adc0code=0;
    MCP3911_CTRL = 0;
    MCP3911_CTRL =(MCP3911_Register_Address<<1); //left shift address one digit for write command
    MCP3911_CTRL |= 1; //Turn on Read Operation by toggling last bit on
    
    digitalWrite(MCP3911_CS, LOW);
    SPI.transfer(MCP3911_CTRL); // send command byte to MCP3911
    HB = SPI.transfer(0x0);//receive High Byte
    MB = SPI.transfer(0x0);//receive Middle Byte
    LB = SPI.transfer(0x0);//receive Low Byte
    digitalWrite(MCP3911_CS, HIGH);
    
    adc0code = HB;
    adc0code = adc0code<<8;
    adc0code |= MB;
    adc0code = adc0code<<8;
    adc0code |= LB;         //connecting the 3 bytes to one number
    
    
    return adc0code;// returning result
  }


#2 MSP432

MSP432

    Member

  • Members
  • PipPip
  • 18 posts

Posted 09 December 2016 - 10:08 PM

Bump



#3 chicken

chicken

    Level 4

  • Members
  • 854 posts

Posted 09 December 2016 - 10:42 PM

What LaunchPad and what version of Energia are you using?

 

Looking at MSP430F5529 as an example, SS/SCK/MISO/MOSI are on pins 33/34/14/15 for SPI(0) and 8/7/9/10 for SPI(1).


  • energia likes this

#4 MSP432

MSP432

    Member

  • Members
  • PipPip
  • 18 posts

Posted 10 December 2016 - 07:28 PM

Hi,

I am using MSP430FF5529 launchpad, energia 17..

 

But the energia reference shows SPI0 on pins 7/8/14/15  and SPI1 on pins 34/33/9/10 for SPI(1).

 

http://energia.nu/Gu...9LaunchPad.html



#5 Fmilburn

Fmilburn

    Level 3

  • Members
  • 528 posts
  • LocationSeattle

Posted 10 December 2016 - 09:20 PM

Hi @MSP432,

 

The pins listed look correct.  I have used the F5529 successfully with the MCP3008 ADC and Energia V17.  It is hard to say what the problem is without access to your setup - can only guess.  A logic analyzer would certainly help.  You might try changing the SPI bus speed with setClockDivider().



#6 MSP432

MSP432

    Member

  • Members
  • PipPip
  • 18 posts

Posted 12 December 2016 - 02:12 PM

Thanks @Fmilburn.

 

Will there be anyother connections running between MSP and MCP other than SS, SCK, MOSI, MISO ?

Oscilloscope shows the MOSI line from MSP is outputting data as expected. Also will pullup, pulldown states of pins matter?

 

If u have a moment, it would be great if you can try the code I posted above and see if ur having any issues on your setup.



#7 LiviuM

LiviuM

    Level 1

  • Members
  • 53 posts

Posted 12 December 2016 - 03:15 PM

Will there be anyother connections running between MSP and MCP other than SS, SCK, MOSI, MISO ?

GND?


  • chicken likes this

#8 MSP432

MSP432

    Member

  • Members
  • PipPip
  • 18 posts

Posted 12 December 2016 - 03:24 PM

Where to connect GND? and is it required for the SPI?

 

 

Attached Files



#9 chicken

chicken

    Level 4

  • Members
  • 854 posts

Posted 12 December 2016 - 04:02 PM

You will need to connect with DGND / GNDB.

 

GND is the reference point to decide whether a signal is high or low. In electronics, connected GND is almost always required/assumed with only a few exceptions.

 

Did you also check SS with the oscilloscope?

 

Typically, SPI does not require pull-ups/downs. Unless explicitly mentioned in the datasheet of the MCP3911 or related evaluation boards, you probably don't need them.


  • energia and MSP432 like this

#10 MSP432

MSP432

    Member

  • Members
  • PipPip
  • 18 posts

Posted 12 December 2016 - 04:09 PM

Thanks for that information.

 

Yes the SS pin is working as expected.

 

But that raises a new question for me, why Arduino is reading without any trouble with same 4 connections i made?

 

Edit: Thanks for pointing that out. Its working now



#11 chicken

chicken

    Level 4

  • Members
  • 854 posts

Posted 12 December 2016 - 04:40 PM

Grounds are often connected accidentally, e.g. when both devices are powered from the same USB hub. But to avoid any problems, it's better to explicitly make the connection.

 

Looking at your picture, I wonder if there's another MCU in that device that is connected to the MCP3911. If so, it will fight the MSP430 for control over SPI and lead to unexpected results.


  • energia likes this

#12 MSP432

MSP432

    Member

  • Members
  • PipPip
  • 18 posts

Posted 12 December 2016 - 05:23 PM

Yes, there is another micro controller on the board. But we are only using this to develop the model. We ll get rid of it and develop our on Analog front end. Will check and see if i can read the values now. Will post if i see any trouble doing so. I just read the control register and its outputting the setting i write.







Also tagged with one or more of these keywords: #MSP430, energia, MCP3911, SPI, MSP432

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users