Arduino Uno and LTC6803-4 problem with communication using SPI
good day everyone!
i have problem our battery management system using ltc6803-4. used measure 5 series cells. problem when read configuration of ltc6803-4, receive 0 0 0 0 0 0, seems not communicating each other. doesn't match configuration have sent it. don't know what's problem code , schematic. here attach schematic , arduino code , output in serial monitor. , also, copied getpec code other source have checked , seems correct. if can double check me , point out if wrong code makes communication not working, pls need help. datasheet ltc6803-4 can found here http://cds.linear.com/docs/en/datasheet/680324fa.pdf
i have problem our battery management system using ltc6803-4. used measure 5 series cells. problem when read configuration of ltc6803-4, receive 0 0 0 0 0 0, seems not communicating each other. doesn't match configuration have sent it. don't know what's problem code , schematic. here attach schematic , arduino code , output in serial monitor. , also, copied getpec code other source have checked , seems correct. if can double check me , point out if wrong code makes communication not working, pls need help. datasheet ltc6803-4 can found here http://cds.linear.com/docs/en/datasheet/680324fa.pdf
code: [select]
#include "spi.h"
#define wrcfg 0x01 // write configuration register
#define rdcfg 0x02 // read configuration register
#define rdcv 0x04 // read cell voltages
#define stcvad 0x10 // start adc poll status
// #define ltc6804addr1 0x80 // board address 1st ltc6803-4
#define ltc6804addr2 0x81 // board address 2nd ltc6803-4
#define total 5 // total connected cells
byte byteholder;
// configuration 1st module of ltc6803-4 (negate ltc6803-4)
//byte cffmvmc[6] = {0x09, 0x00, 0x00, 0x00, 0x00, 0x00};
// configuration 1st module assert ltc6803-4
byte asltc6803[6] = {0x29, 0x00, 0x00, 0xfc, 0xa4, 0xba};
byte ltc2[1] = {0x81};
byte wrfg[1] = {0x01};
void setup() {
// put setup code here, run once:
pinmode(10, output);
pinmode(11, output);
pinmode(12, input);
pinmode(13, output);
digitalwrite(10, high);
spi.begin();
spi.setclockdivider(spi_clock_div64);
spi.setdatamode(spi_mode3);
spi.setbitorder(msbfirst);
serial.begin(9600);
writecfgr();
}
void loop() {
// put main code here, run repeatedly:
readcfgrtencells();
// spistart();
// spi.transfer(getpec(wrfg,1));
// spiend();
delay(2000);
}
// calculate pec, n size of din
byte getpec(byte * din, int n) {
byte pec, in0, in1, in2;
pec = 0x41;
for(int j=0; j<n; j++) {
for(int i=0; i<8; i++) {
in0 = ((din[j] >> (7 - i)) & 0x01) ^ ((pec >> 7) & 0x01);
in1 = in0 ^ ((pec >> 0) & 0x01);
in2 = in0 ^ ((pec >> 1) & 0x01);
pec = in0 | (in1 << 1) | (in2 << 2) | ((pec << 1) & ~0x07);
}
}
return pec;
}
void spistart() {
spi.begin();
if(digitalread(10) == low) {
digitalwrite(10,high);
delaymicroseconds(5); //ensure valid pulse
}
digitalwrite(10, low);
//delaymicroseconds(20);
}
void spiend(uint8_t flag) { //wait poll status
spi.end();
if(flag > 0) {
delaymicroseconds(10);
digitalwrite(10, high);
}
}
void spiend(void) {
spiend(1);
}
// write configuration registers ltc6803-4 using broadcast write
void writecfgr(){
serial.println("writing configuration cells..");
// open spi communication
spistart();
// send wrcfg command , pec byte (2 bytes)
spi.transfer(wrcfg);
spi.transfer(0xc7);
// send command cfgr0 cfgr5 (6 bytes)
sendmultiplebytespi(asltc6803, 6);
// send computed pec byte of cfgr0 cfgr5
// spi.transfer(getpec(asltc6803,6));
spi.transfer(0xf7);
// close spi communication
spiend();
}
//// read cell configuration registers ltc6803-4 using addressable read
void readcfgrtencells(){
serial.println("reading configuration 5 cells..");
// open spi communication
spistart();
// send address , pec byte (2 bytes)
spi.transfer(ltc6804addr2);
spi.transfer(getpec(ltc2,1));
// send rdcfg command , pec byte (2 bytes)
spi.transfer(rdcfg);
spi.transfer(0xce);
// gather read data configuration registers (6 bytes)
for(int = 0; < 6; i++){
byteholder = spi.transfer(rdcfg);
serial.println(byteholder, hex);
}
// close spi communication
spiend();
}
// send multiple data ltc
void sendmultiplebytespi(byte * data, int n){
for (int = 0; i<n; i++){
spi.transfer(data[i]);
}
}
that doesn't expected:
to send data block have change code to:
code: [select]
// send multiple data ltc
void sendmultiplebytespi(byte * data, int n){
(int = 0; i<n; i++){
spi.transfer(data);
}
}
to send data block have change code to:
code: [select]
// send multiple data ltc
void sendmultiplebytespi(byte * data, int n){
(int = 0; i<n; i++){
spi.transfer(data[i]);
}
}
Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > Arduino Uno and LTC6803-4 problem with communication using SPI
arduino
Comments
Post a Comment