mochi12345's blog

日々のことをつらつらと綴っていきます

ltc6804-1のステータスレジスタの読み取り

電圧を読みだして過電圧と過放電を処理しようかと考えていたのだが,設定レジスタで過電圧と過放電の電圧値を設定できて,自動で判定してくれるようだったので実装してみる.

設定レジスタの1~3で過電圧と過放電の電圧を設定できる.

設定レジスタ1と2の下位4bitで過放電電圧,設定レジスタ2の上位4bitと設定レジスタ3で過充電電圧を設定できる.

これを読み取るためにltc6804ライブラリにia新しいコードを追加した.

uint8_t LTC6804_rdstatb(uint8_t total_ic, //Number of ICs in the system
				     uint8_t r_statb[][8] //A two dimensional array that the function stores the read configuration data.
					 )
{
  const uint8_t BYTES_IN_REG = 8;
  
  uint8_t cmd[4];
  uint8_t *rx_data;
  int8_t pec_error = 0; 
  uint16_t data_pec;
  uint16_t received_pec;
  
  rx_data = (uint8_t *) malloc((8*total_ic)*sizeof(uint8_t));
  
  //1
  cmd[0] = 0x00;
  cmd[1] = 0x12; //変更
  cmd[2] = 0x70; //変更
  cmd[3] = 0x24; //変更
 
  //2
  wakeup_idle (); //This will guarantee that the LTC6804 isoSPI port is awake. This command can be removed.
  //3
  output_low(LTC6804_CS);
  spi_write_read(cmd, 4, rx_data, (BYTES_IN_REG*total_ic));         //Read the configuration data of all ICs on the daisy chain into 
  output_high(LTC6804_CS);													//rx_data[] array			
 
  for (uint8_t current_ic = 0; current_ic < total_ic; current_ic++) 			//executes for each LTC6804 in the daisy chain and packs the data
  { 																			//into the r_config array as well as check the received Config data
																				//for any bit errors	
	//4.a																		
    for (uint8_t current_byte = 0; current_byte < BYTES_IN_REG; current_byte++)					
    {
      r_statb[current_ic][current_byte] = rx_data[current_byte + (current_ic*BYTES_IN_REG)]; //変更
    }
    //4.b
    received_pec = (r_statb[current_ic][6]<<8) + r_statb[current_ic][7];
    data_pec = pec15_calc(6, &r_statb[current_ic][0]);
    if(received_pec != data_pec)
    {
      pec_error = -1;
    }  
  }
  
  free(rx_data);
  //5
  return(pec_error);
}

そしてarduinoのプログラムに以下を追加して過電圧の判定を行った.

uint8_t rx_status[TOTAL_IC][8];
/*
 ステータス・レジスタ読み出し用配列
 データ(6バイト)+PEC(2バイト)
*/

//セル電圧過電圧エラーチェック
boolean cellcheckOV(){
  
  OV = false;
  int8_t error = 0;
 
  //配列内の電圧をすべて比較
  wakeup_sleep();
  error = LTC6804_rdstatb(TOTAL_IC,rx_status);
  if (error == -1)
  {
   Serial.println(F("A PEC error was detected in the received data"));
  }

  for(uint8_t current_ic=0; current_ic<TOTAL_IC; current_ic++){
   for(uint8_t i=2; i<4; i++){
      if(rx_status[current_ic][i]==0xAA) OV = true;     
    }
  }
  Serial.print(F("Over Voltave : "));
  Serial.println(OV);
  return OV; 
}