Go Down

Topic: M90E32AS SPI Help (Read 855 times) previous topic - next topic

Asuar

Hello everyone,

I am currently working on a project that is using the M90E32AS Poly-Phase Energy Metering IC and the ATMEGA328P. Currently I'm using the arduino uno as a quick development board to try and get my project started. The problem I am having is with the SPI serial communication, I just can't get a proper response from the chip. My code for reading register 78H is:


#include <SPI.h>

#define PM0 7       //Mode control 0
#define PM1 8       //Mode control l 
#define CS 10       //ChipSelect
#define DATAOUT 11  //MOSI
#define DATAIN  12  //MISO
#define SPICLOCK 13 //Clock

void setup() {

  pinMode(PM1, OUTPUT);
  pinMode(PM0, OUTPUT);

  DDRB |= (1 << PB2); // chipselect pin output

  digitalWrite(PM1, HIGH);
  digitalWrite(PM0, HIGH);

  SPI.setClockDivider( SPI_CLOCK_DIV16); // slow the SPI bus down 16/16 = 1 Mhz
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE2);

  SPI.begin();
  Serial.begin(9600);
}

void loop() {

  byte commandByte1 = B10000000; // start bit, 5 bit "x", and first two address bits 00
  byte commandByte2 = B01111000; // address bits 78H = 0111 1000
  byte commandByte3 = B00000000;
  byte commandByte4 = B00000000;

  byte hiByte, loByte;

  PORTB &= ~(1 << PB2);              // activate Chipselect
 
  SPI.transfer(commandByte1);
  SPI.transfer(commandByte2);
  hiByte = SPI.transfer(commandByte3);
  loByte = SPI.transfer(commandByte4);
             
  PORTB |= (1 << PB2);             //deactivate Chipselect
 
  Serial.println(hiByte, BIN);
  Serial.println(loByte, BIN);
  Serial.println();

  delay(2000);
}

the only response i get is all 1's and I know I have the wiring correct. 
If there are any SPI experts out there that can help I would really appreciate it.
Attached is the datasheet and application notes.

Thanks,
Asuar

dlloyd

#1
Dec 29, 2014, 09:44 pm Last Edit: Dec 29, 2014, 10:14 pm by dlloyd
SPI.begin() needs to be called first, try this order in your setup:

Code: [Select]
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV16); // slow the SPI bus down 16/16 = 1 Mhz
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE2);

Check that SCK and CS are "normally high" ... they should both be HIGH after setup completes.
Also, may need to try LSBFIRST and/or SPI_MODE3.

EDIT:

It's possible that SDO (MOSI) needs a pullup resistor since it does go high impedance at times.

Perhaps this could help with diagnostics:

Code: [Select]
#include <SPI.h>

#define PM0 7       //Mode control 0
#define PM1 8       //Mode control l 
#define CS 10       //ChipSelect
#define DATAOUT 11  //MOSI
#define DATAIN  12  //MISO
#define SPICLOCK 13 //Clock

void setup() {

  pinMode(CS, OUTPUT);
  pinMode(PM1, OUTPUT);
  pinMode(PM0, OUTPUT);
  digitalWrite(CS, HIGH);
  digitalWrite(PM1, HIGH);
  digitalWrite(PM0, HIGH);

  SPI.begin();
  SPI.setClockDivider(SPI_CLOCK_DIV16); // slow the SPI bus down 16/16 = 1 Mhz
  SPI.setBitOrder(MSBFIRST);
  SPI.setDataMode(SPI_MODE2);

  Serial.begin(9600);
}

void loop() {

  byte commandByte1 = B10000000; // start bit, 5 bit "x", and first two address bits 00
  byte commandByte2 = B01111000; // address bits 78H = 0111 1000
  byte commandByte3 = B00000000;
  byte commandByte4 = B00000000;

  digitalWrite(CS, LOW);
  delayMicroseconds(50);
  byte receiveByte1 = SPI.transfer(commandByte1);
  byte receiveByte2 = SPI.transfer(commandByte2);
  byte receiveByte3 = SPI.transfer(commandByte3);
  byte receiveByte4 = SPI.transfer(commandByte4);
  delayMicroseconds(50);
  digitalWrite(CS, HIGH);

  Serial.print("receiveByte1 ");
  Serial.println(receiveByte1, BIN);
  Serial.print("receiveByte2 ");
  Serial.println(receiveByte2, BIN);
  Serial.print("receiveByte3 ");
  Serial.println(receiveByte3, BIN);
  Serial.print("receiveByte4 ");
  Serial.println(receiveByte4, BIN);
  Serial.println();

  delay(2000);
}

Go Up