LTC6804讀寫配置暫存器
一、寫配置暫存器步驟及函數封裝
寫配置暫存器
1.把CSB拉低至低電平;
2.發送WRCFG命令(0x00 0x01)及其PEC(0x3D 0x6E);
3.發送配置暫存器的CFGR0位元組,然後繼續發送CFGR1….CFGR5;
4.發送CFGR0….CFGR5的PEC校驗碼;
5.把CSB拉至高電平,數據在CSB的上升沿上被鎖定至所有的器件中。
配置暫存器封裝函數
void LTC6804_wrcfg(Uint8 total_ic,Uint8 config[6]){
const Uint8 BYTES_IN_REG = 6;
const Uint8 CMD_LEN = 4+(8*total_ic);
Uint8 cmd[12];
Uint16 cfg_pec;
int i;
Uint8 cmd_index; //命令計數器
Uint8 current_byte;
//1
cmd[0] = 0x00;
cmd[1] = 0x01;
cmd[2] = 0x3d;
cmd[3] = 0x6e;
//2
cmd_index = 4;
for(current_byte = 0; current_byte < BYTES_IN_REG; current_byte++){ // 對CFGR暫存器中的6個位元組中的每一個執行// current_byte是位元組計數
cmd[cmd_index] = config[current_byte]; //將配置數據添加到要發送的陣列
cmd_index = cmd_index + 1;
}
//3
cfg_pec = (Uint16)pec15_calc(BYTES_IN_REG, &config[0]); // 計算每個IC配置暫存器數據的PEC
cmd[cmd_index] = (Uint8)(cfg_pec >> 8);
cmd[cmd_index + 1] = (Uint8)cfg_pec;
cmd_index = cmd_index + 2;
wakeup_idle (); //這將確保LTC6804 isoSPI埠處於喚醒狀態。可以刪除此命令。
output_low(LTC6804_CS); //將片選(CSB)拉低,寫入配置資訊
for(i = 0;i<12;i++){
SPIAData(cmd[i]);
}
output_high(LTC6804_CS); //將片選(CSB)拉低,寫入配置資訊
}
二、讀配置暫存器步驟及函數封裝
讀配置暫存器
1.把CSB拉低至低電平;
2.發送RDCFG命令(0x00 0x02)及其PEC(0x2b 0x0A);
3.發送無效位元組(0xFF),回讀配置資訊
4.把CSB拉至高電平,數據在CSB的上升沿上被鎖定至所有的器件中。
配置暫存器封裝函數
int LTC6804_rdcfg(Uint8 total_ic, Uint8 r_config[8]){
const Uint8 BYTES_IN_REG = 8;
Uint8 cmd[4];
Uint8 rx_data[8];
int pec_error = 0;
Uint16 data_pec;
int i;
Uint16 received_pec;
Uint8 current_byte;
//1
cmd[0] = 0x00;
cmd[1] = 0x02;
cmd[2] = 0x2b;
cmd[3] = 0x0A;
//2
wakeup_idle (); //這將確保LTC6804 isoSPI埠處於喚醒狀態。 可以刪除此命令。
//3
output_low(LTC6804_CS); //將配置資訊寫入
for(i = 0;i<4;i++){
SPIAData(cmd[i]);
}
for(i = 0;i<8;i++){
rx_data[i] = SPIAData(0xFF);
}
output_high(LTC6804_CS);
for (current_byte = 0; current_byte < BYTES_IN_REG; current_byte++){
r_config[current_byte] = rx_data[current_byte];
}
received_pec = (r_config[6]<<8) + r_config[7];
data_pec = pec15_calc(6,&r_config[0]);
if(received_pec != data_pec){
pec_error = -1;
}
return(pec_error);
}