Trying to get LTC1325 Battery Management chip interfaced with Arduino as a project. I need to send command word of 22 bits to LTC via SPI bus. Little over my head as I am not a great programmer so trying to learn in process. I would appreciate some help with this part. So far my code seems to do something from what I see on Oscope but not sure it is actually doing what I want. Once the command word is sent I should receive 8 bits to confirm all is good. Somehow would be best if serial.print was was used to output what I send and receive from LTC1325. Attached is LTC1325 doc.
#include <SPI.h>
void setup() {
// Enable SPI for LTC1325
digitalWrite(SS, HIGH); // ensure SS stays high for now
// Put SCK, MOSI, SS pins into output mode
// also put SCK, MOSI into LOW state, and SS into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
SPI.begin();
// Slow down the master a bit
//SPI.setClockDivider(SPI_CLOCK_DIV64);
const int CS_Pin = 7;
SPI.beginTransaction(SPISettings(250000, MSBFIRST, SPI_MODE0));
pinMode(CS_Pin, OUTPUT); // The CS_Pin should remain in a high impedance state (INPUT) when it is not in use.
digitalWrite(CS_Pin, LOW); // Activate the CS line (CS is active LOW)
SPI.transfer(0b00000010); // Send First byte
SPI.transfer(0b00100100); // Send Second byte
SPI.transfer(0b00000000); // Send Third byte
SPI.transfer(0b00000000); // Send Forth byte
SPI.transfer(0b00000000); // Send Fifth byte
SPI.transfer(0b00000000); // Send Sixte byte
pinMode(CS_Pin, INPUT); // Set CS_Pin to high impedance to allow pull-up to reset CS to inactive.
digitalWrite(CS_Pin, HIGH); // Enable internal pull-up
}
void loop() {
//Loop Read from LTC1325
const int CS_Pin = 7;
pinMode(CS_Pin, OUTPUT); // The CS_Pin should remain in a high impedance state (INPUT) when it is not in use.
digitalWrite(CS_Pin, LOW); // Activate the CS line (CS is active LOW)
SPI.transfer(0b00000000); // Just send 0's
pinMode(CS_Pin, INPUT); // Set CS_Pin to high impedance to allow pull-up to reset CS to inactive
digitalWrite(CS_Pin, HIGH); // Enable internal pull-up
}
I cannot comment on your code as I am not familiar enough with the SPI libaray.
However:
pinMode commands need to be issued before digitalWrite commands as you first define an IO pin as output and then set its appropriate value.
Typically you would send your command (cyclically) to your device and then wait a bit for the data to leave the Arduino (delay instruction) and then read the result. This happens in the loop() function as the loop function is executed cyclically.
If you want to read the data, you can issue a loop after sending the command to your device. In this loop you can use small delays() and check after each small delay wether you have received an answer or not.
I have adjusted my sketch. I do see something on the scope but best would be to send out serial.println to see what it is sending or if I had a logic analyzer. so far all I see is a single 0 should be 8bits…I will also try with delay(10) and see if it works
Thanks
Luca
#include <SPI.h>
void setup() {
// Enable SPI for LTC1325
//digitalWrite(SS, HIGH); // ensure SS stays high for now
// Put SCK, MOSI, SS pins into output mode
// also put SCK, MOSI into LOW state, and SS into HIGH state.
// Then put SPI hardware into Master mode and turn SPI on
SPI.begin();
Serial.begin(9600);
// Slow down the master a bit
//SPI.setClockDivider(SPI_CLOCK_DIV64);
//const int CS_Pin = 7;
//SPI.beginTransaction(SPISettings(50000, MSBFIRST, SPI_MODE0));
//pinMode(CS_Pin, OUTPUT); // The CS_Pin should remain in a high impedance state (INPUT) when it is not in use.
//digitalWrite(CS_Pin, LOW); // Activate the CS line (CS is active LOW)
//SPI.transfer(0b00000010); // Send First byte
//SPI.transfer(0b00100100); // Send Second byte
//SPI.transfer(0b00000000); // Send Third byte
//SPI.transfer(0b00000000); // Send Forth byte
//val1 = SPI.transfer(0x00); // get RXbyte4 result and place in val 1
//val2 = SPI.transfer(0x00); // get RXbyte5 result and place in val 2
//SPI.transfer(0b00000000); // Send Fifth byte
//SPI.transfer(0b00000000); // Send Sixte byte
//val3 = SPI.transfer(0x00); // get RXbyte6 result and place in val 3
//pinMode(CS_Pin, INPUT); // Set CS_Pin to high impedance to allow pull-up to reset CS to inactive.
//digitalWrite(CS_Pin, HIGH); // Enable internal pull-up
}
void loop() {
//Loop Read from LTC1325
const int CS_Pin = 7;
//Set Variables
int incomingByte1;
int incomingByte2;
int incomingByte3;
int incomingByte4;
int incomingByte5;
int incomingByte6;
SPI.beginTransaction(SPISettings(125000, MSBFIRST, SPI_MODE1));
pinMode(CS_Pin, OUTPUT); // The CS_Pin should remain in a high impedance state (INPUT) when it is not in use.
digitalWrite(CS_Pin, LOW); // Activate the CS line (CS is active LOW)
//Send 6 Bytes
SPI.transfer(0b00000010); // Send First byte
incomingByte1 = SPI.transfer(0x00); // get RXbyte1 result and place in incomingByte 1
SPI.transfer(0b00100100); // Send Second byte
incomingByte2 = SPI.transfer(0x00); // get RXbyte2 result and place in incomingByte 2
SPI.transfer(0b00000000); // Send Third byte
incomingByte3 = SPI.transfer(0x00); // get RXbyte3 result and place in incomingByte 3
SPI.transfer(0b00000000); // Send Forth byte
incomingByte4 = SPI.transfer(0x00); // get RXbyte4 result and place in incomingByte 4
SPI.transfer(0b00000000); // Send Fifth byte
incomingByte5 = SPI.transfer(0x00); // get RXbyte5 result and place in incomingByte 5
SPI.transfer(0b00000000); // Send Sixte byte
incomingByte6 = SPI.transfer(0x00); // get RXbyte6 result and place in incomingByte 6
pinMode(CS_Pin, INPUT); // Set CS_Pin to high impedance to allow pull-up to reset CS to inactive.
digitalWrite(CS_Pin, HIGH); // Enable internal pull-up
//Set SerialPrint 6 Bytes
Serial.println(incomingByte1);
Serial.println(incomingByte2);
Serial.println(incomingByte3);
Serial.println(incomingByte4);
Serial.println(incomingByte5);
Serial.println(incomingByte6);
//delay(1); // keep from flooding the PC screeen
}
Would anyone know if I would see 8 Bits instead of a single 0. Wondering if its a problem with my code or the Arduino cant parse it correctly. I think logic analyzer would help. I noticed on the scope an output which looks like logic signals. The pk-pk voltage is about 500mv, does the Arduino mega require voltage ie. 5 Volts?