Author Topic: Arduino SPI Problems  (Read 7135 times)

0 Members and 1 Guest are viewing this topic.

Offline aep9690Topic starter

  • Regular Contributor
  • *
  • Posts: 67
  • Country: us
Arduino SPI Problems
« on: November 17, 2013, 10:21:34 pm »
I am trying to create a BMS using an Arduino Uno and the LTC6802-2 (Datasheet Here: http://cds.linear.com/docs/en/datasheet/68022fa.pdf).  I was able to get a free demo board from Linear (Schematic Here: http://cds.linear.com/docs/en/demo-board-schematic/1393bsch.pdf) along with the USB interface board and can talk to the demo board just fine using the USB interface board.  I have also been able to read from the devices registers through the SPI bus on the Arduino.

The problem is that I can't seem to be able to write to the registers at all.  I send a write command and then send out the data one byte at a time but when I go to read from it afterwards it doesn't update.  Here is the code I wrote:

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

#define READ 0x02
#define WRITE 0x01
#define CS 10

byte result[7];
byte address = B10000000;
byte transmit1[6] = {244, 1, 1, 1, 125, 181};
byte transmit2[6] = {0xE0, 1, 1, 1, 1, 1};

void setup(){
  pinMode(CS, OUTPUT);
  digitalWrite(CS, HIGH);
 
  Serial.begin(9600);
 
  SPI.begin();
  SPI.setDataMode(SPI_MODE3);
  SPI.setClockDivider(SPI_CLOCK_DIV16);
  SPI.setBitOrder(MSBFIRST);
}

void loop() {
 
  // Read 1
  digitalWrite(CS, LOW);
  SPI.transfer(address);
  SPI.transfer(READ);
  for(int i = 0; i < 7; i++){
    result[i] = SPI.transfer(0x00);
  }
  digitalWrite(CS, HIGH);
 
  Serial.println("READ 1");
  for(int i = 0; i < 7; i++){
    Serial.print(result[i], HEX);
    Serial.print(' ');
  }
  Serial.println(' ');
 
  delay(100);
 
  // Write 1
  digitalWrite(CS, LOW);
  SPI.transfer(address);
  SPI.transfer(WRITE);
  for(int i = 0; i < 6; i++){
    SPI.transfer(transmit1[i]);
  }
  digitalWrite(CS,HIGH);
 
  Serial.println("WRITE1");
  for(int i = 0; i < 6; i++){
    Serial.print(transmit1[i], HEX);
    Serial.print(' ');
  }
  Serial.println(' ');
 
  delay(100);
 
  // Read 2
  digitalWrite(CS, LOW);
  SPI.transfer(address);
  SPI.transfer(READ);
  for(int i = 0; i < 7; i++){
    result[i] = SPI.transfer(0x00);
  }
  digitalWrite(CS, HIGH);
 
  Serial.println("READ 2");
  for(int i = 0; i < 7; i++){
    Serial.print(result[i], HEX);
    Serial.print(' ');
  }
  Serial.println(' ');
 
  delay(100);
 
   // Write 2
  digitalWrite(CS, LOW);
  SPI.transfer(address);
  SPI.transfer(WRITE);
  for(int i = 0; i < 6; i++){
    SPI.transfer(transmit2[i]);
  }
  digitalWrite(CS, HIGH);

  Serial.println("WRITE 2");
  for(int i = 0; i < 6; i++){
    Serial.print(transmit2[i], HEX);
    Serial.print(' ');
  }
  Serial.println(' ');
 
  delay(2000);
}

There shouldn't be anything wrong with the SPI bus because I can read from the device just fine, so I think there has to be something wrong with the software but it is so basic I don't see any problems.  I have hooked up my oscilliscope to the bus lines and the only thing that seems like it light cause this problem is that the CS line is pulled high about 5us after the data transmission has finished.  The datasheet says the devices latches the data on the rising edge of the CS line so if it is low for too long it might cause problems.  The thing is I don't know how to reduce the time between after the transmission has finished and before the CS line is pulled high.

I know other people have used the Arduino and this chip to make a BMS but one of the people isn't going open source on it and hasn't returned my messages and the other person published code that doesn't work and hasn't returned my messages either.

I would really appreciate any help I can get.  I'm ready to throw my workbench out a window.
 

Offline Maxlor

  • Frequent Contributor
  • **
  • Posts: 565
  • Country: ch
Re: Arduino SPI Problems
« Reply #1 on: November 18, 2013, 12:05:43 am »
Try not sending the address byte. I didn't read all of the data sheet, but it seems that command 1, WRCFG, is a broadcast command. See also page 24 of the datasheet you linked.
 

Offline aep9690Topic starter

  • Regular Contributor
  • *
  • Posts: 67
  • Country: us
Re: Arduino SPI Problems
« Reply #2 on: November 18, 2013, 12:17:00 am »
I tried using both the address command and the broadcast command but neither worked.  I also wrote the code using the examples on page 24.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Arduino SPI Problems
« Reply #3 on: November 18, 2013, 12:26:31 am »
Quote
The problem is that I can't seem to be able to write to the registers at all.

What output did you get from your code that made you think that way?
================================
https://dannyelectronics.wordpress.com/
 

Offline aep9690Topic starter

  • Regular Contributor
  • *
  • Posts: 67
  • Country: us
Re: Arduino SPI Problems
« Reply #4 on: November 18, 2013, 12:37:26 am »
I'm not at the lab now so I can't get the exact print out but it was along these lines.

Code: [Select]
READ 1
E0 0 0 0 0 0 A1 (This is the original values in the register according to the PC software, the A1 is the PEC packet)
WRITE 1
F4 1 1 1 7D B5 ( I am not writing any 1's because the Arduino SPI library interprets a byte of 0's as a read)
READ 2
F4 1 1 1 7D B5 CD (I think the PEC packet was CD in this, I will need to double check.)
WRITE 2
E0 1 1 1 1 1 ( I wanted to write again to make sure it was repeatable)
READ 1
E0 0 0 0 0 0 A1

The last line is why I think it isn't working, if the write was successful then the values in the last read should be E0 1 1 1 1 1, not E0 0 0 0 0 0.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Arduino SPI Problems
« Reply #5 on: November 18, 2013, 12:43:00 am »
Quote
it was along these lines.

...


The last line is why I think it isn't working,

It is probably more helpful to you if you post the exact and actual print out from the terminal.
================================
https://dannyelectronics.wordpress.com/
 

Offline aep9690Topic starter

  • Regular Contributor
  • *
  • Posts: 67
  • Country: us
Re: Arduino SPI Problems
« Reply #6 on: November 18, 2013, 12:55:54 am »
Here is the exact readout:

Code: [Select]
READ 1
E0 0 0 0 0 0 A1 
WRITE1
F4 1 1 1 7D B5 
READ 2
F4 1 1 1 7D B5 CD 
WRITE 2
E0 1 1 1 1 1 
READ 1
E0 0 0 0 0 0 A1 
WRITE1
F4 1 1 1 7D B5 
READ 2
F4 1 1 1 7D B5 CD 
WRITE 2
E0 1 1 1 1 1 
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Arduino SPI Problems
« Reply #7 on: November 18, 2013, 01:14:26 am »
1) there are only 6 CFG registers;
2) WRITE1 and READ2 are identical -> the code is working;
3) WRITE2 and READ1 are not working -> maybe the data written is invalid; maybe you are trying to write too many registers, etc. Check the datasheet to see if the data are indeed valid.

To be safe, I would have pack the write / read process into functions.
================================
https://dannyelectronics.wordpress.com/
 

Offline aep9690Topic starter

  • Regular Contributor
  • *
  • Posts: 67
  • Country: us
Re: Arduino SPI Problems
« Reply #8 on: November 18, 2013, 01:22:18 am »
I know there are only 6 CFG registers, the 7th bit is for checking the PEC.

I can't really find any information in the datasheet that restricts the values in the CFG registers.

Byte 1 I am not changing, byte 2, 3, and 4 handle the discharge and interrupts for the cells, each bit is for a specific cell, byte 5 specifies the undervoltage, which defaults to 0, and byte 6 handles the overvoltage, which defaults to 0.

I don't see a problem with the values I am writing.

I plan on packing the functions once I get them working.
« Last Edit: November 18, 2013, 01:32:30 am by aep9690 »
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Arduino SPI Problems
« Reply #9 on: November 18, 2013, 01:35:31 am »
Quote
I can't really find any information in the datasheet that restricts the values in the CFG registers.

Right there in the datasheet.

Also the wdt expires in 2.5 seconds. After that, the CFGs are reset to default values. The long delay at end of the loop runs quite close to that, especially if you factor in the time to send the strings. What if you shorten it? :)
================================
https://dannyelectronics.wordpress.com/
 

Offline aep9690Topic starter

  • Regular Contributor
  • *
  • Posts: 67
  • Country: us
Re: Arduino SPI Problems
« Reply #10 on: November 18, 2013, 01:51:22 am »
It looks like reducing the delays fixed the problem.  I will just need to figure out a way to make sure the watchdog timer never expires.  Thanks a lot for the help everyone.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Arduino SPI Problems
« Reply #11 on: November 18, 2013, 11:30:16 am »
Or form a habit of reading the datasheet thoroughly before attempting to code to it. Or you will always end of spending more time trying to debug your code.
================================
https://dannyelectronics.wordpress.com/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf