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);
}

猜你喜欢

转载自www.cnblogs.com/fcy1/p/12925921.html