附驱动部分程序:
目前存在的问题,部分配置和状态寄存器可以正确读到默认值,有些就不行,希望用过的或正在用的可以一起交流一下
static uchar Rn8302_RX_Buf[10];
static uchar Rn8302_TX_Buf[10];
/******************************************************************************
RN8302全波计量参数定义及初始化
****************************************************************************/
Full_wave_data Fullwave_data = {
.Ua_wave = {bank0,0x00,3,RO,0},
.Ub_wave = {bank0,0x01,3,RO,0},
.Uc_wave = {bank0,0x02,3,RO,0},
.Ia_wave = {bank0,0x03,3,RO,0},
.Ib_wave = {bank0,0x04,3,RO,0},
.Ic_wave = {bank0,0x05,3,RO,0},
.In_wave = {bank0,0x06,3,RO,0},
.Ua = {bank0,0x07,4,RO,0},
.Ub = {bank0,0x08,4,RO,0},
.Uc = {bank0,0x09,4,RO,0},
.USum_vector = {bank0,0x0A,4,RO,0},
.Ia = {bank0,0x0B,4,RO,0},
.Ib = {bank0,0x0C,4,RO,0},
.Ic = {bank0,0x0D,4,RO,0},
.In = {bank0,0x0E,4,RO,0},
.Reserved1 = {bank0,0x0F,4,RO,0},
.ISum_vector = {bank0,0x10,4,RO,0},
.Ia_NVM1 = {bank0,0x11,3,RO,0},
.Ib_NVM1 = {bank0,0x12,3,RO,0},
.Ic_NVM1 = {bank0,0x13,3,RO,0},
.Pa = {bank0,0x14,4,RO,0},
.Pb = {bank0,0x15,4,RO,0},
.Pc = {bank0,0x16,4,RO,0},
.Pt = {bank0,0x17,4,RO,0},
.Qa = {bank0,0x18,4,RO,0},
.Qb = {bank0,0x19,4,RO,0},
.Qc = {bank0,0x1A,4,RO,0},
.Qt = {bank0,0x1B,4,RO,0},
.Sa = {bank0,0x1C,4,RO,0},
.Sb = {bank0,0x1D,4,RO,0},
.Sc = {bank0,0x1E,4,RO,0},
.St = {bank0,0x1F,4,RO,0},
.Pfa = {bank0,0x20,3,RO,0},
.Pfb = {bank0,0x21,3,RO,0},
.Pfc = {bank0,0x22,3,RO,0},
.Pft = {bank0,0x23,3,RO,0},
.PAfCnt = {bank0,0x24,3,RW,0},
.PBfCnt = {bank0,0x25,3,RW,0},
.PCfCnt = {bank0,0x26,3,RW,0},
.PTfCnt = {bank0,0x27,3,RW,0},
.QAfCnt = {bank0,0x28,3,RW,0},
.QBfCnt = {bank0,0x29,3,RW,0},
.QCfCnt = {bank0,0x2A,3,RW,0},
.QTfCnt = {bank0,0x2B,3,RW,0},
.SAfCnt = {bank0,0x2C,3,RW,0},
.SBfCnt = {bank0,0x2D,3,RW,0},
.SCfCnt = {bank0,0x2E,3,RW,0},
.STfCnt = {bank0,0x2F,3,RW,0},
.EPa = {bank0,0x30,3,RO,0},
.EPb = {bank0,0x31,3,RO,0},
.EPc = {bank0,0x32,3,RO,0},
.EPt = {bank0,0x33,3,RO,0},
.PosEPa = {bank0,0x34,3,RO,0},
.PosEPb = {bank0,0x35,3,RO,0},
.PosEPc = {bank0,0x36,3,RO,0},
.PosEPt = {bank0,0x37,3,RO,0},
.NegEPa = {bank0,0x38,3,RO,0},
.NegEPb = {bank0,0x39,3,RO,0},
.NegEPc = {bank0,0x3A,3,RO,0},
.NegEPt = {bank0,0x3B,3,RO,0},
.EQa = {bank0,0x3C,3,RO,0},
.EQb = {bank0,0x3D,3,RO,0},
.EQc = {bank0,0x3E,3,RO,0},
.EQt = {bank0,0x3F,3,RO,0},
.PosEQa = {bank0,0x40,3,RO,0},
.PosEQb = {bank0,0x41,3,RO,0},
.PosEQc = {bank0,0x42,3,RO,0},
.PosEQt = {bank0,0x43,3,RO,0},
.NegEQa = {bank0,0x44,3,RO,0},
.NegEQb = {bank0,0x45,3,RO,0},
.NegEQc = {bank0,0x46,3,RO,0},
.NegEQt = {bank0,0x47,3,RO,0},
.ESa = {bank0,0x48,3,RO,0},
.ESb = {bank0,0x49,3,RO,0},
.ESc = {bank0,0x4A,3,RO,0},
.ESt = {bank0,0x4B,3,RO,0},
.STV = {bank0,0x4C,4,RO,0},
.PfTV = {bank0,0x4D,3,RO,0},
.STFVCnt = {bank0,0x4E,3,RW,0},
.ESTV = {bank0,0x4F,3,RO,0}
};
/******************************************************************************
RN8302基波谐波计量参数定义及初始化
****************************************************************************/
Fundamental_wave_data Fundwave_data ={
.YUa = {bank0,0x50,3,RO,0},
.YUb = {bank0,0x51,3,RO,0},
.YUc = {bank0,0x52,3,RO,0},
.YIa = {bank0,0x53,3,RO,0},
.YIb = {bank0,0x54,3,RO,0},
.YIc = {bank0,0x55,3,RO,0},
.YIn = {bank0,0x56,3,RO,0},
.UFrequence = {bank0,0x57,3,RO,0},
.FUa = {bank0,0x58,4,RO,0},
.FUb = {bank0,0x59,4,RO,0},
.FUc = {bank0,0x5A,4,RO,0},
.FIa = {bank0,0x5B,4,RO,0},
.FIb = {bank0,0x5C,4,RO,0},
.FIc = {bank0,0x5D,4,RO,0},
.FPa = {bank0,0x5E,4,RO,0},
.FPb = {bank0,0x5F,4,RO,0},
.FPc = {bank0,0x60,4,RO,0},
.FPt = {bank0,0x61,4,RO,0},
.FQa = {bank0,0x62,4,RO,0},
.FQb = {bank0,0x63,4,RO,0},
.FQc = {bank0,0x64,4,RO,0},
.FQt = {bank0,0x65,4,RO,0},
.FSa = {bank0,0x66,4,RO,0},
.FSb = {bank0,0x67,4,RO,0},
.FSc = {bank0,0x68,4,RO,0},
.FSt = {bank0,0x69,4,RO,0},
.FPfa = {bank0,0x6A,3,RO,0},
.FPfb = {bank0,0x6B,3,RO,0},
.FPfc = {bank0,0x6C,3,RO,0},
.FPft = {bank0,0x6D,3,RO,0},
.FPAFCnt = {bank0,0x6E,3,RW,0},
.FPBFCnt = {bank0,0x6F,3,RW,0},
.FPCFCnt = {bank0,0x70,3,RW,0},
.FPTFCnt = {bank0,0x71,3,RW,0},
.FQAFCnt = {bank0,0x72,3,RW,0},
.FQBFCnt = {bank0,0x73,3,RW,0},
.FQCFCnt = {bank0,0x74,3,RW,0},
.FQTFCnt = {bank0,0x75,3,RW,0},
.FSAFCnt = {bank0,0x76,3,RW,0},
.FSBFCnt = {bank0,0x77,3,RW,0},
.FSCFCnt = {bank0,0x78,3,RW,0},
.FSTFCnt = {bank0,0x79,3,RW,0},
.FEPa = {bank0,0x7A,3,RO,0},
.FEPb = {bank0,0x7B,3,RO,0},
.FEPc = {bank0,0x7C,3,RO,0},
.FEPt = {bank0,0x7D,3,RO,0},
.PoSFEPa = {bank0,0x7E,3,RO,0},
.PoSFEPb = {bank0,0x7F,3,RO,0},
.PoSFEPc = {bank0,0x80,3,RO,0},
.PoSFEPt = {bank0,0x81,3,RO,0},
.NegFEPa = {bank0,0x82,3,RO,0},
.NegFEPb = {bank0,0x83,3,RO,0},
.NegFEPc = {bank0,0x84,3,RO,0},
.NegFEPt = {bank0,0x85,3,RO,0},
.FEQa = {bank0,0x86,3,RO,0},
.FEQb = {bank0,0x87,3,RO,0},
.FEQc = {bank0,0x88,3,RO,0},
.FEQt = {bank0,0x89,3,RO,0},
.PoSFEQa = {bank0,0x8A,3,RO,0},
.PoSFEQb = {bank0,0x8B,3,RO,0},
.PoSFEQc = {bank0,0x8C,3,RO,0},
.PoSFEQt = {bank0,0x8D,3,RO,0},
.NegFEQa = {bank0,0x8E,3,RO,0},
.NegFEQb = {bank0,0x8F,3,RO,0},
.NegFEQc = {bank0,0x90,3,RO,0},
.NegFEQt = {bank0,0x91,3,RO,0},
.FESa = {bank0,0x92,3,RO,0},
.FESb = {bank0,0x93,3,RO,0},
.FESc = {bank0,0x94,3,RO,0},
.FESt = {bank0,0x95,3,RO,0},
.HUa = {bank0,0x96,4,RO,0},
.HUb = {bank0,0x97,4,RO,0},
.HUc = {bank0,0x98,4,RO,0},
.HIa = {bank0,0x99,4,RO,0},
.HIb = {bank0,0x9A,4,RO,0},
.HIc = {bank0,0x9B,4,RO,0},
.FSTV = {bank0,0x9C,4,RO,0},
.FPfTV = {bank0,0x9D,3,RO,0},
.FSTVFCnt = {bank0,0x9E,3,RW,0},
.FESTV = {bank0,0x9F,3,RO,0},
};
/******************************************************************************
RN8302配置及状态参数定义及初始化
****************************************************************************/
Config_Stutas_REG CFG_StatusREG = {
//EMM校表寄存器 I
.HFConst1 = {bank1,0x00,2,RW,0},
.HFConst2 = {bank1,0x01,2,RW,0},
.IStart_PS = {bank1,0x02,2,RW,0},
.IStart_Q = {bank1,0x03,2,RW,0},
.LostVoltT = {bank1,0x04,2,RW,0},
.ZXOT = {bank1,0x05,2,RW,0},
.PRTH1L = {bank1,0x06,2,RW,0},
.PRTH1H = {bank1,0x07,2,RW,0},
.PRTH2L = {bank1,0x08,2,RW,0},
.PRTH2H = {bank1,0x09,2,RW,0},
.IRegion3L = {bank1,0x0A,2,RW,0},
.IRegion3H = {bank1,0x0B,2,RW,0},
.PHSUA = {bank1,0x0C,1,RW,0},
.PHSUB = {bank1,0x0D,1,RW,0},
.PHSUC = {bank1,0x0E,1,RW,0},
.PHSIA = {bank1,0x0F,3,RW,0},
.PHSIB = {bank1,0x10,3,RW,0},
.PHSIC = {bank1,0x11,3,RW,0},
.PHSIN = {bank1,0x12,1,RW,0},
.GUA = {bank1,0x13,2,RW,0},
.GUB = {bank1,0x14,2,RW,0},
.GUC = {bank1,0x15,2,RW,0},
.GIA = {bank1,0x16,2,RW,0},
.GIB = {bank1,0x17,2,RW,0},
.GIC = {bank1,0x18,2,RW,0},
.GIN = {bank1,0x19,2,RW,0},
.DCOS_UA = {bank1,0x1A,2,RW,0},
.DCOS_UB = {bank1,0x1B,2,RW,0},
.DCOS_UC = {bank1,0x1C,2,RW,0},
.DCOS_IA = {bank1,0x1D,2,RW,0},
.DCOS_IB = {bank1,0x1E,2,RW,0},
.DCOS_IC = {bank1,0x1F,2,RW,0},
.DCOS_IN = {bank1,0x20,2,RW,0},
.UA_OS = {bank1,0x21,2,RW,0},
.UB_OS = {bank1,0x22,2,RW,0},
.UC_OS = {bank1,0x23,2,RW,0},
.IA_OS = {bank1,0x24,2,RW,0},
.IB_OS = {bank1,0x25,2,RW,0},
.IC_OS = {bank1,0x26,2,RW,0},
.IN_OS = {bank1,0x27,2,RW,0},
.GPA = {bank1,0x28,2,RW,0},
.GPB = {bank1,0x29,2,RW,0},
.GPC = {bank1,0x2A,2,RW,0},
.GQA = {bank1,0x2B,2,RW,0},
.GQB = {bank1,0x2C,2,RW,0},
.GQC = {bank1,0x2D,2,RW,0},
.GSA = {bank1,0x2E,2,RW,0},
.GSB = {bank1,0x2F,2,RW,0},
.GSC = {bank1,0x30,2,RW,0},
.PA_PHSL = {bank1,0x31,2,RW,0},
.PB_PHSL = {bank1,0x32,2,RW,0},
.PC_PHSL = {bank1,0x33,2,RW,0},
.QA_PHSL = {bank1,0x34,2,RW,0},
.QB_PHSL = {bank1,0x35,2,RW,0},
.QC_PHSL = {bank1,0x36,2,RW,0},
.PA_OS = {bank1,0x37,2,RW,0},
.PB_OS = {bank1,0x38,2,RW,0},
.PC_OS = {bank1,0x39,2,RW,0},
.QA_OS = {bank1,0x3A,2,RW,0},
.QB_OS = {bank1,0x3B,2,RW,0},
.QC_OS = {bank1,0x3C,2,RW,0},
.FUA_OS = {bank1,0x3D,2,RW,0},
.FUB_OS = {bank1,0x3E,2,RW,0},
.FUC_OS = {bank1,0x3F,2,RW,0},
.FIA_OS = {bank1,0x40,2,RW,0},
.FIB_OS = {bank1,0x41,2,RW,0},
.FIC_OS = {bank1,0x42,2,RW,0},
.GFPA = {bank1,0x43,2,RW,0},
.GFPB = {bank1,0x44,2,RW,0},
.GFPC = {bank1,0x45,2,RW,0},
.GFQA = {bank1,0x46,2,RW,0},
.GFQB = {bank1,0x47,2,RW,0},
.GFQC = {bank1,0x48,2,RW,0},
.GFSA = {bank1,0x49,2,RW,0},
.GFSB = {bank1,0x4A,2,RW,0},
.GFSC = {bank1,0x4B,2,RW,0},
.FPA_PHS = {bank1,0x4C,2,RW,0},
.FPB_PHS = {bank1,0x4D,2,RW,0},
.FPC_PHS = {bank1,0x4E,2,RW,0},
.FQA_PHS = {bank1,0x4F,2,RW,0},
.FQB_PHS = {bank1,0x50,2,RW,0},
.FQC_PHS = {bank1,0x51,2,RW,0},
.FPA_OS = {bank1,0x52,2,RW,0},
.FPB_OS = {bank1,0x53,2,RW,0},
.FPC_OS = {bank1,0x54,2,RW,0},
.FQA_OS = {bank1,0x55,2,RW,0},
.FQB_OS = {bank1,0x56,2,RW,0},
.FQC_OS = {bank1,0x57,2,RW,0},
.SAGCFG = {bank1,0x58,2,RW,0},
.OVLVL = {bank1,0x59,2,RW,0},
.OILVL = {bank1,0x5A,2,RW,0},
//EMM配置和状态寄存器
.CFCFG = {bank1,0x60,3,RW,0},
.EMUCFG = {bank1,0x61,3,RW,0},
.EMUCON = {bank1,0x62,3,RW,0},
.WSAVECON = {bank1,0x63,1,RW,0},
.EMMIE = {bank1,0x64,2,RW,0},
.EMMIF = {bank1,0x65,2,RO,0},
.PQSign = {bank1,0x66,2,RO,0},
.Noload = {bank1,0x67,2,RO,0},
.IRegionS = {bank1,0x68,2,RO,0},
.PHASES = {bank1,0x69,2,RO,0},
.CheckSum1 = {bank1,0x6A,2,RO,0},
//NVM1配置和状态寄存器
.NVM1CFG = {bank1,0x70,1,RW,0},
.NVM1IF = {bank1,0x71,1,RO,0},
//NVM2配置和状态寄存器
.NVM2CFG = {bank1,0x72,2,RW,0},
.NVM2CMPA = {bank1,0x73,2,RW,0},
.NVM2CMPB = {bank1,0x74,2,RW,0},
.NVM2CMPC = {bank1,0x75,2,RW,0},
.NVM2IF = {bank1,0x76,1,RO,0},
//系统配置寄存器
.WREN = {bank1,0x80,1,RW,0},
.WMSW = {bank1,0x81,1,RW,0},
.SOFT_RST = {bank1,0x82,1,RW,0},
.ADCREG = {bank1,0x83,2,RW,0},
.MODSEL = {bank1,0x86,1,RW,0},
//系统状态寄存器
.SYSSR = {bank1,0x8A,2,RO,0},
.CheckSum2 = {bank1,0x8B,2,RO,0},
.RData = {bank1,0x8C,4,RO,0},
.WData = {bank1,0x8D,3,RO,0},
.LRBufAddr = {bank1,0x8E,2,RO,0},
.DeciceID = {bank1,0x8F,3,RO,0},
//EMM校表寄存器 II
.AUOTDC_EN = {bank1,0x91,2,RW,0},
.IN_WaveEN = {bank1,0x94,2,RW,0},
.PA_PHSM = {bank1,0xB0,3,RW,0},
.PA_PHSH = {bank1,0xB1,3,RW,0},
.PB_PHSM = {bank1,0xB2,3,RW,0},
.PB_PHSH = {bank1,0xB3,3,RW,0},
.PC_PHSM = {bank1,0xB4,3,RW,0},
.PC_PHSH = {bank1,0xB5,3,RW,0},
.QA_PHSM = {bank1,0xB6,3,RW,0},
.QA_PHSH = {bank1,0xB7,3,RW,0},
.QB_PHSM = {bank1,0xB8,3,RW,0},
.QB_PHSH = {bank1,0xB9,3,RW,0},
.QC_PHSM = {bank1,0xBA,3,RW,0},
.QC_PHSH = {bank1,0xBB,3,RW,0},
};
/******************************************************************************
* * 函数名称: RN8302_TX_Frame(uchar num)
* * 函数说明: 向8302中发送一帧数据,Rn8302_TX_Buf发送缓冲区
* * 输入参数: num 帧字节数
* * 输出参数: 无
* * 返回值: 无
****************************************************************************/
static void RN8302_TX_Frame(uchar num)
{
uchar i;
uchar checksum = 0;
uchar* p = Rn8302_TX_Buf;
RN8302_ENABLE;
for(i=0;i<num;i++)
{
Spi_RN8302_SendByte(*p);
checksum += *p;
p++;
}
checksum = ~checksum;
Spi_RN8302_SendByte(checksum);
RN8302_DISABLE;
}
/******************************************************************************
* * 函数名称: RN8302_RX_Frame(uchar num)
* * 函数说明: 从8302中读取一帧数据,Rn8302_RX_Buf接收缓冲区
* * 输入参数: num 帧字节数
* * 输出参数: 无
* * 返回值: 校验结果,正确返回1,错误返回0
****************************************************************************/
static uchar RN8302_RX_Frame(uchar num)
{
uchar i;
uchar checksum = 0;
uchar* p = Rn8302_RX_Buf;
RN8302_ENABLE;
Spi_RN8302_SendByte(Rn8302_TX_Buf[0]);
Spi_RN8302_SendByte(Rn8302_TX_Buf[1]);
checksum = Rn8302_TX_Buf[0]+Rn8302_TX_Buf[1];
for(i=0;i<num;i++)
{
*p++ = Spi_RN8302_ReadByte();
checksum += *p;
}
checksum =~checksum;
if(checksum == Spi_RN8302_ReadByte())
{
RN8302_DISABLE;
return 1;
}
else
{
RN8302_DISABLE;
return 0;
}
}
/******************************************************************************
* * 函数名称: RN8302WR(DataStructure* dat)
* * 函数说明: 向RN8302指定的寄存器中写数据
* * 输入参数: DataStructure* dat,指定的寄存器结构指针
* * 输出参数: 无
* * 返回值: 无
****************************************************************************/
void RN8302WR(DataStructure* dat)
{
uchar data1,data2,data3;
if(dat->rw == RO)return;
switch(dat->bytenum)
{
case 1:
data1 = dat->data & 0xff;
Rn8302_TX_Buf[0] = dat->addr;
Rn8302_TX_Buf[1] = 0x80|(dat->bank<<4);
Rn8302_TX_Buf[2] = data1;
RN8302_TX_Frame(3);
break;
case 2:
data1 = (dat->data>>8) & 0xff;
data2 = dat->data & 0xff;
Rn8302_TX_Buf[0] = dat->addr;
Rn8302_TX_Buf[1] = 0x80|(dat->bank<<4);
Rn8302_TX_Buf[2] = data1;
Rn8302_TX_Buf[3] = data2;
RN8302_TX_Frame(4);
break;
case 3:
data1 = (dat->data>>16) & 0xff;
data2 = (dat->data>>8) & 0xff;
data3 = dat->data & 0xff;
Rn8302_TX_Buf[0] = dat->addr;
Rn8302_TX_Buf[1] = 0x80|(dat->bank<<4);
Rn8302_TX_Buf[2] = data1;
Rn8302_TX_Buf[3] = data2;
Rn8302_TX_Buf[4] = data3;
RN8302_TX_Frame(5);
break;
default:break;
}
}
/******************************************************************************
* * 函数名称: RN8302RD(DataStructure* dat)
* * 函数说明: 从RN8302指定的寄存器中读取数据
* * 输入参数: DataStructure* dat,指定的寄存器结构指针
* * 输出参数: 无
* * 返回值: 校验结果,正确返回1,错误返回0
****************************************************************************/
uchar RN8302RD(DataStructure* dat)
{
uchar check;
if(dat->rw != RO && dat->rw != RW)return 0;
switch(dat->bytenum)
{
case 1:
Rn8302_TX_Buf[0] = dat->addr;
Rn8302_TX_Buf[1] = 0x00|(dat->bank<<4);
check = RN8302_RX_Frame(1);
dat->data = Rn8302_RX_Buf[0];
break;
case 2:
Rn8302_TX_Buf[0] = dat->addr;
Rn8302_TX_Buf[1] = 0x00|(dat->bank<<4);
check = RN8302_RX_Frame(2);
dat->data = Rn8302_RX_Buf[0]<<8|Rn8302_RX_Buf[1];
break;
case 3:
Rn8302_TX_Buf[0] = dat->addr;
Rn8302_TX_Buf[1] = 0x00|(dat->bank<<4);
check = RN8302_RX_Frame(3);
dat->data = Rn8302_RX_Buf[0]<<16|Rn8302_RX_Buf[1]<<8|Rn8302_RX_Buf[2];
break;
case 4:
Rn8302_TX_Buf[0] = dat->addr;
Rn8302_TX_Buf[1] = 0x00|(dat->bank<<4);
check = RN8302_RX_Frame(3);
dat->data = Rn8302_RX_Buf[0]<<24|Rn8302_RX_Buf[1]<<16|Rn8302_RX_Buf[2]<<8|Rn8302_RX_Buf[3];
break;
default:break;
} |