void RN8209D_CalibrateInit(uint8_t ch)
{
uint8_t regbuf[4],Addr[1],TEMP[1]={0};
uint8_t status = 1;
RN8209_REST(ch,GPIO_PIN_SET); //RN8209D_CS_CLR();
delay_us(100);//
RN8209_REST(ch,GPIO_PIN_RESET); //RN8209D_CS_CLR();
delay_us(500);//
RN8209_REST(ch,GPIO_PIN_SET); //RN8209D_CS_CLR();
//读取RN8209 DeviceID
//Addr[0]=Reg_DeviceID;
RN8209D_ReadReg(Reg_DeviceID,regbuf,3,ch);
//printf("Reg_DeviceID:%X %X %X\r\n",regbuf[0],regbuf[1],regbuf[2]);
if(RN8209D_ReadReg(Reg_DeviceID,regbuf,3,ch) == 0){
if((regbuf[0]==0x82)&&(regbuf[1]==0x09)&&(regbuf[2]==0x00)){
status = 0;
printf("Reg_DeviceID:%X %X %X\r\n",regbuf[0],regbuf[1],regbuf[2]);
}
else{
status = 1;
}
}
//status = 0;
if(status == 0){
//写使能
regbuf[0] = 0xE5;
RN8209D_WriteReg(WREN,regbuf,1,ch);
regbuf[0]=0;
//复位
regbuf[0] = 0xFA;
RN8209D_WriteReg(WREN,regbuf,1,ch);
HAL_Delay(20);
//写使能
regbuf[0] = 0xE5;
RN8209D_WriteReg(WREN,regbuf,1,ch);
//系统控制寄存器
regbuf[0] = 0x00;
regbuf[1] = 0x43; //PGA=4 开启通道B 0x51
RN8209D_WriteReg(Reg_SYSCON,regbuf,2,ch);
memset(regbuf,0,2);
//memcpy(regbuf,TEMP,2);
RN8209D_ReadReg(Reg_SYSCON,regbuf,2,ch);
//printf("SysStatusAddr:%X %X %X\r\n",regbuf[2],regbuf[1],regbuf[0]);
//写HFCONST
regbuf[0] = 0x14;
regbuf[1] = 0x25;
RN8209D_WriteReg(Reg_HFCONST,regbuf,2,ch);
memset(regbuf,0,2);//memcpy(regbuf,TEMP,2);
RN8209D_ReadReg(Reg_HFCONST,regbuf,2,ch);
//printf("SysStatusAddr:%X %X %X\r\n",regbuf[2],regbuf[1],regbuf[0]);
//启动功率
regbuf[0] = 0x00;//0x01;
regbuf[1] = 0x60;//0x2D;
RN8209D_WriteReg(Reg_PStart,regbuf,2,ch);
regbuf[0] = 0x01;
regbuf[1] = 0x20;//0x2D;
RN8209D_WriteReg(Reg_DStart,regbuf,2,ch);
//计量控制
regbuf[0] = 0x00; //80电能读后清零
regbuf[1] = 0x03;
RN8209D_WriteReg(Reg_EMUCON,regbuf,2,ch);
//计量控制2
regbuf[0] = 0x00;
regbuf[1] = 0xB0; //自定义电能寄存器为B通道
RN8209D_WriteReg(Reg_EMUCON2,regbuf,2,ch);
//B通道电流增益
regbuf[0] = 0;
regbuf[1] = 0;
RN8209D_WriteReg(Reg_IBGain,regbuf,2,ch); //
//A通道有功功率增益
regbuf[0] = 0;
regbuf[1] = 0;
RN8209D_WriteReg(Reg_GPQA,regbuf,2,ch);
//B通道有功功率增益
regbuf[0] = 0;
regbuf[1] = 0;
RN8209D_WriteReg(Reg_GPQB,regbuf,2,ch);
//A通道相位校正
regbuf[0] = 0;
RN8209D_WriteReg(Reg_PhsA,regbuf,1,ch);
// regbuf[0] = 0;
// RN8209D_ReadReg(Reg_PhsA,regbuf,1,1);
// printf("Reg:%X\r\n",regbuf[0]);
//B通道相位校正
regbuf[0] = 0;
RN8209D_WriteReg(Reg_PhsB,regbuf,1,ch);
//A通道有功功率offset
regbuf[0] = 0;
regbuf[1] = 0;
RN8209D_WriteReg(Reg_APOSA,regbuf,2,ch);
//B通道有功功率offset
regbuf[0] = 0;
regbuf[1] = 0;
RN8209D_WriteReg(Reg_APOSB,regbuf,2,ch);
//A通道无功功率offset
regbuf[0] = 0;
regbuf[1] = 0;
RN8209D_WriteReg(Reg_RPOSA,regbuf,2,ch);
//B通道无功功率offset
regbuf[0] = 0;
regbuf[1] = 0;
RN8209D_WriteReg(Reg_RPOSB,regbuf,2,ch);
//A通道电流offset
regbuf[0] = 0;
regbuf[1] = 0;
RN8209D_WriteReg(Reg_IARMSOS,regbuf,2,ch);
//B通道电流offset
regbuf[0] = 0;
regbuf[1] = 0;
RN8209D_WriteReg(Reg_IBRMSOS,regbuf,2,ch);
//校表时不要写保护
regbuf[0] = 0;
regbuf[1] = 0;
regbuf[2] = 0;
memset(regbuf,0,3);
//RN8209D_ReadReg(EMUStatus,regbuf,3,ch);
//printf("SysStatusAddr:%X %X %X\r\n",regbuf[2],regbuf[1],regbuf[0]);
}
}
|