求助!STM32F103+ATT7022EU,读不到电压、电流、频率

2019-08-20 19:23发布

各位大侠,我用STM32F103读取ATT7022E,只读各相电压、电流、频率,为何只能偶尔读取到(10次能读取到1次,显示各相电压为250多V,频率为50Hz);串口调试助手显示的内容也不稳定,有时某些寄存器内容正确,有时又不正确,重启后下一次又正确了。
请用过ATT7022E的朋友帮我看看代码,到底问题出在哪里?
按照《ATT7022EU应用笔记》中的描述,我检查了VCC、AVCC、VDD、REFCAP、RESET、IRQ引脚的电平,全部正常,说明芯片工作正常。
做项目,结尾阶段,最后卡在这里了,急啊!
请用过ATT7022E的朋友帮忙,万分感谢!
ATT7022E.h内容如下: #ifndef _ATT7022EU_H_
#define _ATT7022EU_H_ #define RST_Port GPIOE //ATT7022EU与STM32连接用的RST引脚定义
#define CS_Port  GPIOA //ATT7022EU与STM32连接用的CS引脚定义(STM32输出数据至ATT7022EU)
#define SCL_Port GPIOA //ATT7022EU与STM32连接用的SCL引脚定义(STM32输出数据至ATT7022EU)
#define DATA_Port GPIOA //ATT7022EU与STM32连接用的DATA引脚定义(STM32输出数据至ATT7022EU)
#define DIN_Port GPIOA //ATT7022EU与STM32连接用的DIN引脚定义(ATT7022EU输出数据至STM32) #define RST_Pin  GPIO_Pin_2 //ATT7022EU与STM32连接用的RST引脚定义
#define CS_Pin  GPIO_Pin_4 //ATT7022EU与STM32连接用的CS引脚定义(STM32输出数据至ATT7022EU)
#define SCL_Pin  GPIO_Pin_5 //ATT7022EU与STM32连接用的SCL引脚定义(STM32输出数据至ATT7022EU)
#define DATA_Pin GPIO_Pin_7 //ATT7022EU与STM32连接用的DATA引脚定义(STM32输出数据至ATT7022EU)
#define DIN_Pin  GPIO_Pin_6 //ATT7022EU与STM32连接用的DIN引脚定义(ATT7022EU输出数据至STM32) #define Set_CS   GPIO_SetBits (CS_Port, CS_Pin)
#define Clr_CS   GPIO_ResetBits (CS_Port, CS_Pin)
#define Rev_CS   GPIO_WriteBit(CS_Port, CS_Pin, (BitAction)(1 - GPIO_ReadOutputDataBit(CS_Port, CS_Pin))) #define Set_SCL   GPIO_SetBits (SCL_Port, SCL_Pin)
#define Clr_SCL   GPIO_ResetBits (SCL_Port, SCL_Pin)
#define Rev_SCL   GPIO_WriteBit(SCL_Port, SCL_Pin, (BitAction)(1 - GPIO_ReadOutputDataBit(SCL_Port, SCL_Pin))) #define Set_DATA  GPIO_SetBits (DATA_Port, DATA_Pin)
#define Clr_DATA  GPIO_ResetBits (DATA_Port, DATA_Pin)
#define Rev_DATA  GPIO_WriteBit(DATA_Port, DATA_Pin, (BitAction)(1 - GPIO_ReadOutputDataBit(DATA_Port, DATA_Pin))) #define Set_RST   GPIO_SetBits (RST_Port, RST_Pin)
#define Clr_RST   GPIO_ResetBits (RST_Port, RST_Pin)
#define Rev_RST   GPIO_WriteBit(RST_Port, LED54_Pin, (BitAction)(1 - GPIO_ReadOutputDataBit(RST_Port, RST_Pin))) #define Rd_MISO   GPIO_ReadInputDataBit(DIN_Port, DIN_Pin)==1 #define r_Pflag   0x3D  //功率方向
#define R_Sflag   0x2C  //状态寄存器 #define r_Pa    0x01
#define r_Pb    0x02
#define r_Pc    0x03
#define r_Pt    0x04 //有功功率 #define r_Qa    0x05
#define r_Qb    0x06
#define r_Qc    0x07
#define r_Qt    0x08 //无功功率 #define r_Sa    0x09
#define r_Sb    0x0A
#define r_Sc    0x0B
#define r_St    0x0C //视在功率 #define r_UaRms   0x0D
#define r_UbRms   0x0E
#define r_UcRms   0x0F
#define r_UtRms   0x2B //电压有效值   #define r_IaRms   0x10
#define r_IbRms   0x11
#define r_IcRms   0x12
#define r_ItRms   0x13  //电流有效值 #define r_Pfa    0x14
#define r_Pfb    0x15
#define r_Pfc    0x16
#define r_Pft    0x17 //功率因数 #define r_Freq   0x1C //线网频率 #define r_Epa    0x1E
#define r_Epb    0x1F
#define r_Epc    0x20
#define r_Ept    0x21 //累加型有功电能 #define r_Eqa    0x22
#define r_Eqb    0x23
#define r_Eqc    0x24
#define r_Eqt    0x25 //累加型无功电能 #define r_Epa2   0x31
#define r_Epb2   0x32
#define r_Epc2   0x33
#define r_Ept2   0x34 //清零型有功电能 #define r_Eqa2   0x35
#define r_Eqb2   0x36
#define r_Eqc2   0x37
#define r_Eqt2   0x38 //清零型无功电能 #define HFconst   0xA0 //高频输出参数 #define UADC    0xBF //电压通道增益 #define UgainA   0x9B
#define UgainB   0x9C
#define UgainC   0x9D //分相电压校准 #define IgainA   0x1A
#define IgainB   0x1B
#define IgainC   0x1C //分相电压校准 #define PgainB   0x05 //B相有功功率增益 // // #define Vu     0.190 //电压通道采样电压
// // #define Vi     0.003 //电流通道采样电压
// // #define Un     220  //额定电压
// // #define In     4.15 //额定电流 // // #define Meter_G   1.163 //ATT7022E常数 // // //#define Meter_HFConst ((2.592*1E10*Meter_G*Meter_G*Vu*Vi)/(In*Un*Meter_Ec)) // // #define Meter_HFConst  ((2.592*pow(10,10)*Meter_G*Meter_G*Vu*Vi)/(In*Un*Meter_Ec)) // // #define Meter_K     (2.592*pow(10,10)/(Meter_HFConst*Meter_Ec*pow(2,23))) // // u16 Meter_Ec = 3200;   //电表常数,默认值为3200,应该根据实际测量误差更改!!! typedef struct      //只需要读电压、电流和频率,其它暂不需要
{
// //  u32 P;      //有功功率
// //  float Rp;
// //  u32 Q;      //无功功率
// //  float Rq;
// //  u32 S;      //视在功率
// //  float Rs;
 u32 URms;        //电压有效值
 float Rurms;
 u32 IRms;        //电流有效值
 float Rirms;
// //  u32 Pf;      //功率因数
// //  float Rpf;
 u32 Freq;        //线网频率
 float Rfreq;
}DataTypeDef; DataTypeDef ADataTypeDef,BDataTypeDef,CDataTypeDef,TDataTypeDef; float PhaseA_I_Amp_Factor = 1.0;   //A相电流放大倍数,默认值为1.0,应该根据实际测量误差更改!!!
float PhaseB_I_Amp_Factor = 1.0;   //B相电流放大倍数,默认值为1.0,应该根据实际测量误差更改!!!
float PhaseC_I_Amp_Factor = 1.0;   //C相电流放大倍数,默认值为1.0,应该根据实际测量误差更改!!!
float PhaseT_I_Amp_Factor = 1.0;   //T相电流放大倍数,默认值为1.0,应该根据实际测量误差更改!!!
float PhaseA_V_Amp_Factor = 1.0;   //A相电压放大倍数,默认值为1.0,应该根据实际测量误差更改!!!
float PhaseB_V_Amp_Factor = 1.0;   //B相电压放大倍数,默认值为1.0,应该根据实际测量误差更改!!!
float PhaseC_V_Amp_Factor = 1.0;   //C相电压放大倍数,默认值为1.0,应该根据实际测量误差更改!!!
float PhaseT_V_Amp_Factor = 1.0;   //T相电压放大倍数,默认值为1.0,应该根据实际测量误差更改!!! void delay_us1(u16 time);
u32 SPI_ATT_Read(u8 data);             //  SIG --> Sflag.7
void SPI_ATT_Write(u8 com_add,u32 data2);//  0xD3,0x000000 可进行软件复位
void ATT7022_Init(void);
void ATT_Adjust(void);
void ATT_Test(void);
void Read_ATT_AData(void);
void Read_ATT_BData(void);
void Read_ATT_CData(void);
void Read_ATT_TData(void); void Output_ATT(DataTypeDef output);
void Read_ATT_TestData(void); void Task_Monitor(void *pdata); extern char *itoa(int value, char *string, int radix); #endif   ATT7022E.c内容如下: #include "ATT7022E.h" //7022初始化
void ATT7022_Init(void)
{
 GPIO_InitTypeDef GPIO_InitStructure;  GPIO_InitStructure.GPIO_Pin = CS_Pin|SCL_Pin|DATA_Pin;
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;   //复用推挽输出
 GPIO_Init(GPIOA,&GPIO_InitStructure);  GPIO_InitStructure.GPIO_Pin = RST_Pin;
 GPIO_Init(GPIOE,&GPIO_InitStructure);  Clr_SCL;
 
 GPIO_InitStructure.GPIO_Pin = DIN_Pin;
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   //浮空输入
 GPIO_Init(DIN_Port,&GPIO_InitStructure);  //硬件复位
 Clr_RST;
 delay_us1(50);
 Set_RST;
 delay_us1(600);
 
} /*校表函数*/
void ATT_Adjust(void)
{
 u32 read1=0x55;  SPI_ATT_Write(0xC3, 0x000000);  //清校表数据
 SPI_ATT_Write(0xC9, 0x00005A);  //打开校准数据写
 SPI_ATT_Write(0x01, 0xB9FE);   //填写模式配置寄存器
 SPI_ATT_Write(0x03, 0xF804);   //填写EMU单元配置寄存器
 SPI_ATT_Write(0x31, 0x3437);   //填写模拟模块使能寄存器
 SPI_ATT_Write(0x02, 0x0100);   //电压通道增益为2,其余各路ADC增益均为1
 SPI_ATT_Write(0x6D, 0xFF00);   //
 SPI_ATT_Write(0x6E, 0x0DB8);   //
 SPI_ATT_Write(0x6F, 0xD1DA);   //
 SPI_ATT_Write(UADC, 0x000000);  //电压通道增益为1
// SPI_ATT_Write(HFconst, 0x00016D);   //高频输出参数为1511
///*----------------------------------------
//    分相电压电流校准参数
//-----------------------------------------*/
// SPI_ATT_Write(UgainA, 0x000000);
// SPI_ATT_Write(UgainB, 0x000000);
// SPI_ATT_Write(UgainC, 0x8172F5);  //8483573
  SPI_ATT_Write(0xC9, 0x000001);    //关闭校准数据写
  SPI_ATT_Write(0xC6, 0x00005A);    //校表数据读出使能
  read1=SPI_ATT_Read(0x00);
  printf(" In 0xC6 with 0x5A: 0x00 is %x ! ",read1); //通过串口调试助手看,读到的值有时是5555,有时又是aaaa,为什么?
 printf("It should be 0x00AAAA ! ");
  read1=0x55;
  read1=SPI_ATT_Read(0x01);
  printf(" ModeCfg is %x ! ",read1); //通过串口调试助手看,读到的值有时是7c02,有时又是89aa,为什么?
 printf("It should be 0xB9FE ! ");
  read1=0x55;
  read1=SPI_ATT_Read(0x03);
  printf(" EMUCfg is %x ! ",read1); //通过串口调试助手看,读到的值是f804,这个值读对了,为什么其它寄存器会读错?
 printf("It should be 0xF804 ! ");
  read1=0x55;
  read1=SPI_ATT_Read(0x31);
  printf(" ModuleCfg is %x ! ",read1); //通过串口调试助手看,读到的值有时是0,有时又是3527,为什么?
 printf("It should be 0x3437 ! ");
  read1=0x55;
  SPI_ATT_Write(0xC6, 0x000001);    //计量数据读出使能
  read1=SPI_ATT_Read(0x00);
  printf(" In 0xC6 without 0x5A: 0x00 is %x ! ",read1);  //通过串口调试助手看,读到的值有时是aaaa,有时又是7122a0,为什么?
 printf("We are using ATT7022EU, so it should be 0x7122A0/0x705200 ! ");
} /*试验函数(为了避免main中太多代码而设)*/
void ATT_Test(void)
{
  u32 read=0x55;
  read=SPI_ATT_Read(0x00);
  printf(" Device ID is %x ! ",read);
 printf("ATT7022EU's device ID should be 0x7122A0 ! ");
  read=0x55;
  printf(" Reading A... ");
  Read_ATT_AData();
  delay_ms(1000);
  delay_ms(1000);
  delay_ms(1000);
  printf(" Reading B... ");
  Read_ATT_BData();
  delay_ms(1000);
  delay_ms(1000);
  delay_ms(1000);
  printf(" Reading C... ");
  Read_ATT_CData();
  delay_ms(1000);
  delay_ms(1000);
  delay_ms(1000);
// printf(" Reading T... ");
// Read_ATT_TData();
}
/*SPI读操作*/
u32 SPI_ATT_Read(u8 data)
{
  u8 i;
  u32 temp=0;
  Set_CS;
  Clr_SCL;
  Clr_CS;  //片选为低,开始操作
  for(i=0;i<8;i++)
  {
    Set_SCL;
    delay_us1(50);
    if(data&0x80)
      Set_DATA;
    else
      Clr_DATA;
    delay_us1(3);  //稳定数据
    Clr_SCL;
    delay_us1(50);
    data<<=1;   //左移数据
  }
  delay_us1(3);  //稳定数据
  for(i=0;i<24;i++)
  {
    temp<<=1;
    Set_SCL;
    delay_us1(50);
    if(Rd_MISO)
    temp|=0x01;
    Clr_SCL;
    delay_us1(50);
  }
  Set_CS;
  return (temp);
} /*SPI写操作*/
void SPI_ATT_Write(u8 com_add,u32 data2)
{
  u8 i,data1;
  data1=0x80|com_add;
  Set_CS;
  Clr_SCL;
  Clr_CS;   //片选为低,开始操作
  for(i=0;i<8;i++)
  {
    Set_SCL;
    delay_us1(50);
    if(data1&0x80)
      Set_DATA;
    else
      Clr_DATA;
    delay_us1(3);
    Clr_SCL;
    delay_us1(50);
    data1<<=1;   //左移数据
  }
  for(i=0;i<24;i++)
  {
    Set_SCL;
    delay_us1(50);
    if(data2&0x00800000)
      Set_DATA;
    else
      Clr_DATA;
    delay_us1(3);
    Clr_SCL;
    delay_us1(50);
    data2<<=1;
  }
  Set_CS;
} //以下为各相常用数据读取,暂时只需读取电压、电流、频率
void Read_ATT_AData(void)
{
// //   ADataTypeDef.P=SPI_ATT_Read(r_Pa);
// //   ADataTypeDef.Q=SPI_ATT_Read(r_Qa);
// //   ADataTypeDef.S=SPI_ATT_Read(r_Sa);
  ADataTypeDef.URms=SPI_ATT_Read(r_UaRms);
  ADataTypeDef.IRms=SPI_ATT_Read(r_IaRms);
// //   ADataTypeDef.Pf=SPI_ATT_Read(r_Pfa);
// //   ADataTypeDef.Freq=SPI_ATT_Read(r_Freq);
  ADataTypeDef.Rurms=ADataTypeDef.URms/8192.0*PhaseA_V_Amp_Factor;   //   2^10/2^23
  ADataTypeDef.Rirms=ADataTypeDef.IRms/8192.0*PhaseA_I_Amp_Factor;
// //   if(ADataTypeDef.Pf>0x800000)
// //   {
// //     ADataTypeDef.Rpf=0x1000000-ADataTypeDef.Pf;
// //     ADataTypeDef.Rpf=-((ADataTypeDef.Rpf/8388608.0)-((ADataTypeDef.Rpf/8388608.0)*PhaseA.Ph_compensation));
// //   }
// //   else
// //     ADataTypeDef.Rpf=(ADataTypeDef.Pf/8388608.0)-((ADataTypeDef.Pf/8388608.0)*PhaseA.Ph_compensation);
// //   ADataTypeDef.Rfreq=ADataTypeDef.Freq/8192.0; //  Lcd_Clr();
//  sprintf(disp0,"A phase:");
//  Lcd_Puts(7,0,disp0);
//  Output_ATT(ADataTypeDef);
} void Read_ATT_BData(void)
{
// //   BDataTypeDef.P=SPI_ATT_Read(r_Pb);
// //   BDataTypeDef.Q=SPI_ATT_Read(r_Qb);
// //   BDataTypeDef.S=SPI_ATT_Read(r_Sb);
  BDataTypeDef.URms=SPI_ATT_Read(r_UbRms);
  BDataTypeDef.IRms=SPI_ATT_Read(r_IbRms);
// //   BDataTypeDef.Pf=SPI_ATT_Read(r_Pfb);
// //   BDataTypeDef.Freq=SPI_ATT_Read(r_Freq);
// //   if(BDataTypeDef.P>0x800000)
// //   {
// //     BDataTypeDef.Rp=0x1000000-BDataTypeDef.P;
// //     BDataTypeDef.Rp=-((BDataTypeDef.Rp*Meter_K*fabs(PhaseB.I_Amp_Factor)*fabs(PhaseB.V_Amp_Factor))
// //       -((BDataTypeDef.Rp*Meter_K*fabs(PhaseB.I_Amp_Factor)*fabs(PhaseB.V_Amp_Factor))*PhaseB.P_Gain_compensation));  //2^15/2^23
// //   }
// //   else
// //     BDataTypeDef.Rp=(BDataTypeDef.P*Meter_K*fabs(PhaseB.I_Amp_Factor)*fabs(PhaseB.V_Amp_Factor))
// //       -((BDataTypeDef.P*Meter_K*fabs(PhaseB.I_Amp_Factor)*fabs(PhaseB.V_Amp_Factor))*PhaseB.P_Gain_compensation);
// //   if(BDataTypeDef.Q>0x800000)
// //   {
// //     BDataTypeDef.Rq=0x1000000-BDataTypeDef.Q;
// //     BDataTypeDef.Rq=-((BDataTypeDef.Rq*Meter_K*fabs(PhaseB.I_Amp_Factor)*fabs(PhaseB.V_Amp_Factor))
// //       -((BDataTypeDef.Rq*Meter_K*fabs(PhaseB.I_Amp_Factor)*fabs(PhaseB.V_Amp_Factor))*PhaseB.P_Gain_compensation));
// //   }
// //   else
// //     BDataTypeDef.Rq=(BDataTypeDef.Q*Meter_K*fabs(PhaseB.I_Amp_Factor)*fabs(PhaseB.V_Amp_Factor))
// //       -((BDataTypeDef.Q*Meter_K*fabs(PhaseB.I_Amp_Factor)*fabs(PhaseB.V_Amp_Factor))*PhaseB.P_Gain_compensation);
// //   BDataTypeDef.Rs=BDataTypeDef.S/256.0;
  BDataTypeDef.Rurms=BDataTypeDef.URms/8192.0*PhaseB_V_Amp_Factor;   //   2^10/2^23
  BDataTypeDef.Rirms=BDataTypeDef.IRms/8192.0*PhaseB_I_Amp_Factor;
// //   if(BDataTypeDef.Pf>0x800000)
// //   {
// //     BDataTypeDef.Rpf=0x1000000-BDataTypeDef.Pf;
// //     BDataTypeDef.Rpf=-((BDataTypeDef.Rpf/8388608.0)-((BDataTypeDef.Rpf/8388608.0)*PhaseB.Ph_compensation));
// //   }
// //   else
// //     BDataTypeDef.Rpf=(BDataTypeDef.Pf/8388608.0)-((BDataTypeDef.Pf/8388608.0)*PhaseB.Ph_compensation);
// //   BDataTypeDef.Rfreq=BDataTypeDef.Freq/8192.0; //  Lcd_Clr();
//  sprintf(disp0,"B phase:");
//  Lcd_Puts(7,0,disp0);
//  Output_ATT(BDataTypeDef);
} void Read_ATT_CData(void)
{
// //   CDataTypeDef.P=SPI_ATT_Read(r_Pc);
// //   CDataTypeDef.Q=SPI_ATT_Read(r_Qc);
// //   CDataTypeDef.S=SPI_ATT_Read(r_Sc);
  CDataTypeDef.URms=SPI_ATT_Read(r_UcRms);
  CDataTypeDef.IRms=SPI_ATT_Read(r_IcRms);
// //   CDataTypeDef.Pf=SPI_ATT_Read(r_Pfc);
// //   CDataTypeDef.Freq=SPI_ATT_Read(r_Freq);
// //   if(CDataTypeDef.P>0x800000)
// //   {
// //     CDataTypeDef.Rp=0x1000000-CDataTypeDef.P;
// //     CDataTypeDef.Rp=-((CDataTypeDef.Rp*Meter_K*fabs(PhaseC.I_Amp_Factor)*fabs(PhaseC.V_Amp_Factor))
// //       -((CDataTypeDef.Rp*Meter_K*fabs(PhaseC.I_Amp_Factor)*fabs(PhaseC.V_Amp_Factor))*PhaseC.P_Gain_compensation));    //2^15/2^23
// //   }
// //   else
// //     CDataTypeDef.Rp=(CDataTypeDef.P*Meter_K*fabs(PhaseC.I_Amp_Factor)*fabs(PhaseC.V_Amp_Factor))
// //       -((CDataTypeDef.P*Meter_K*fabs(PhaseC.I_Amp_Factor)*fabs(PhaseC.V_Amp_Factor))*PhaseC.P_Gain_compensation);
// //   if(CDataTypeDef.Q>0x800000)
// //   {
// //     CDataTypeDef.Rq=0x1000000-CDataTypeDef.Q;
// //     CDataTypeDef.Rq=-((CDataTypeDef.Rq*Meter_K*fabs(PhaseC.I_Amp_Factor)*fabs(PhaseC.V_Amp_Factor))
// //       -((CDataTypeDef.Rq*Meter_K*fabs(PhaseC.I_Amp_Factor)*fabs(PhaseC.V_Amp_Factor))*PhaseC.P_Gain_compensation));
// //   }
// //   else
// //     CDataTypeDef.Rq=(CDataTypeDef.Q*Meter_K*fabs(PhaseC.I_Amp_Factor)*fabs(PhaseC.V_Amp_Factor))
// //       -((CDataTypeDef.Q*Meter_K*fabs(PhaseC.I_Amp_Factor)*fabs(PhaseC.V_Amp_Factor))*PhaseC.P_Gain_compensation);
// //   CDataTypeDef.Rs=CDataTypeDef.S/256.0;
  CDataTypeDef.Rurms=CDataTypeDef.URms/8192.0*PhaseC_V_Amp_Factor;   //   2^10/2^23
  CDataTypeDef.Rirms=CDataTypeDef.IRms/8192.0*PhaseC_I_Amp_Factor;
// //   if(CDataTypeDef.Pf>0x800000)
// //   {
// //     CDataTypeDef.Rpf=0x1000000-CDataTypeDef.Pf;
// //     CDataTypeDef.Rpf=-((CDataTypeDef.Rpf/8388608.0)-((CDataTypeDef.Rpf/8388608.0)*PhaseC.Ph_compensation));
// //   }
// //   else
// //     CDataTypeDef.Rpf=(CDataTypeDef.Pf/8388608.0)-((CDataTypeDef.Pf/8388608.0)*PhaseC.Ph_compensation);
// //   CDataTypeDef.Rfreq=CDataTypeDef.Freq/8192.0; //  Lcd_Clr();
//  sprintf(disp0,"C phase:");
//  Lcd_Puts(7,0,disp0);
//  Output_ATT(CDataTypeDef);
} void Read_ATT_TData(void)
{
// //   TDataTypeDef.P=SPI_ATT_Read(r_Pt);
// //   TDataTypeDef.Q=SPI_ATT_Read(r_Qt);
// //   TDataTypeDef.S=SPI_ATT_Read(r_St);
  TDataTypeDef.URms=SPI_ATT_Read(r_UtRms);
  TDataTypeDef.IRms=SPI_ATT_Read(r_ItRms);
// //   TDataTypeDef.Pf=SPI_ATT_Read(r_Pft);
  TDataTypeDef.Freq=SPI_ATT_Read(r_Freq);
// //   if(TDataTypeDef.P>0x800000)
// //   {
// //    TDataTypeDef.Rp=0x1000000-TDataTypeDef.P;
// //    TDataTypeDef.Rp=-(TDataTypeDef.Rp/64.0);    //2^17/2^23
// //   }
// //   else
// //    TDataTypeDef.Rp=TDataTypeDef.P/64.0;
// //   if(TDataTypeDef.Q>0x800000)
// //   {
// //    TDataTypeDef.Rq=0x1000000-TDataTypeDef.Q;
// //    TDataTypeDef.Rq=-(TDataTypeDef.Rq/64.0);
// //   }
// //   else
// //    TDataTypeDef.Rq=TDataTypeDef.Q/64.0;
// //   TDataTypeDef.Rs=TDataTypeDef.S/64.0;
  TDataTypeDef.Rurms=TDataTypeDef.URms/8192.0*PhaseT_V_Amp_Factor;   //2^10/2^23
  TDataTypeDef.Rirms=TDataTypeDef.IRms/8192.0*PhaseT_I_Amp_Factor;
// //   if(TDataTypeDef.Pf>0x800000)
// //   {
// //    TDataTypeDef.Rpf=0x1000000-TDataTypeDef.Pf;
// //    TDataTypeDef.Rpf=-(TDataTypeDef.Rpf/8388608.0);
// //   }
// //   else
// //    TDataTypeDef.Rpf=TDataTypeDef.Pf/8388608.0;
  TDataTypeDef.Rfreq=TDataTypeDef.Freq/8192.0; // //   Output_ATT(TDataTypeDef);
} 未完,接下面
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
39条回答
masosam
2019-08-22 19:23
回复【12楼】正点原子:
---------------------------------
原子哥,下面是我的初始化程序,先不考虑电压电流功率校准。除了id不是0,其他都是0.不知哪里出了问题。
void SPI1_Init(void)//spi初始化
{
    GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef SPI_InitStructure;
    
    RCC_APB2PeriphClockCmd(SPI_CLK_CLK|SPI_MISO_CLK|SPI_MOSI_CLK|SPI_CLK,ENABLE);
      
    //CS
    Dioout_Init(OUT_SPI_CS);//CS高

    //CLK MOSI 
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin=SPI_CLK_PIN|SPI_MOSI_PIN|SPI_MISO_PIN;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
    GPIO_Init(GPIOA,&GPIO_InitStructure);
    GPIO_SetBits(GPIOA,SPI_CLK_PIN|SPI_MISO_PIN|SPI_MOSI_PIN);
    
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin=SPI_MISO_PIN;
    GPIO_Init(GPIOA,&GPIO_InitStructure);
    
    SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_256;
    SPI_InitStructure.SPI_CPHA=SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_CPOL=SPI_CPOL_High;
    SPI_InitStructure.SPI_CRCPolynomial=7;
    SPI_InitStructure.SPI_DataSize=SPI_DataSize_8b;
    SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
    SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;
    
    SPI_Init(SPI_ATT,&SPI_InitStructure);
    SPI_Cmd(SPI_ATT,ENABLE);
}

uint32_t SPI_ReadReg(uint8_t reg)//读寄存器
{
    uint32_t regval=0;
    ATT_CHOOSE;       
    SPI_ReadWrite(reg); 
    vDly_Timeus(3);
    regval |= (SPI_ReadWrite(reg)<<16);
    regval |= (SPI_ReadWrite(reg)<<8);
    regval |=  SPI_ReadWrite(reg);
    ATT_RELEASE;
    
    return regval;
}

void SPI_WriteReg(uint8_t reg,uint32_t data)//写寄存器
{   
    ATT_CHOOSE;
    SPI_ReadWrite(reg);
    SPI_ReadWrite((data>>16)&0xff);
    SPI_ReadWrite((data>>8)&0xff);
    SPI_ReadWrite(data&0xff);   
    ATT_RELEASE;
}

void ATT_Init(void)
{
    uint16_t c=1000;
    SPI1_Init();
    Dioout_Init(OUT_ATT_SEL);//三相四线
    Dioout_Init(OUT_S1);
    Dioout_Init(OUT_S2);
    vDly_Timeus(100000);
    
    SPI_WriteReg(W_RESET,0x000000);//复位       
    vDly_Timeus(100000);
    
    SPI_WriteReg(W_CALWREN,0x00005A);//校表写使能
    vDly_Timeus(10000);

    SPI_WriteReg(0x81,0xB9FE);//写模式配置寄存器
    vDly_Timeus(10000);
    SPI_WriteReg(0x83,0xF804);//EMU
    vDly_Timeus(10000);
    SPI_WriteReg(0xb1,0x3d27);//模拟模块
    vDly_Timeus(10000); 
    SPI_WriteReg(0xc6,0xffffa5);//使能计量读
    
    vDly_Timeus(10000);  
    
    id=SPI_ReadReg(R_ID);//读的ID是0x1c49a8
 
   SPI_WriteReg(0xc6,0x00005A);//读校表。读刚配置的寄存器值,都是0
   vDly_Timeus(10000);
   
   id=SPI_ReadReg(0x00);//校表ID还是1c49a8
   vDly_Timeus(10000);
   
   id=SPI_ReadReg(0x01);//0
   vDly_Timeus(10000);
   
   id=SPI_ReadReg(0x02);//0
   vDly_Timeus(10000);
   
   id=SPI_ReadReg(0x03);//0
   vDly_Timeus(10000);
   
   id=SPI_ReadReg(0x31);//0
   vDly_Timeus(10000);
   
   SPI_WriteReg(0xc6,0xffffa5);//0
   vDly_Timeus(10000);
   
   SPI_WriteReg(W_CALWREN,0xFFFF00);//关校表使能
   vDly_Timeus(10000); 
}

一周热门 更多>