求助!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条回答
狄胜倩
1楼-- · 2019-08-23 01:21
请问你的STM32的SPI与ATT7022是直连的吗?
xuyan021
2楼-- · 2019-08-23 03:47
感觉还是你的SPI设置有问题。明天我把我的程序放上来。我6月份时写过ATT7022E的程序。
xuyan021
3楼-- · 2019-08-23 06:14
[mw_shl_code=c,true]void SPI1_Init(void) { RCC->APB2ENR|=1<<12; //SPI1时钟使能 //这里只针对SPI口初始化 GPIOA->CRL&=0X000FFFFF; GPIOA->CRL|=0XB8B00000; //PA5.6.7?? GPIOC->CRL&=0XFFF0FFFF; //PC4(CS)设置成输出,上拉 GPIOC->CRL|=0X00030000; GPIOC->ODR|=0X1<<4; SPI1->CR1|=0<<10; //全双工模式 SPI1->CR1|=1<<9; //软件nss管理 SPI1->CR1|=1<<8; SPI1->CR1|=1<<2; //SPI主机 SPI1->CR1|=0<<11; //8bit数据格式 SPI1->CR1|=1<<0; //数据采样从第二个时间边沿开始,CPHA=1 SPI1->CR1|=1<<3; //Fsck=Fpclk1/4 SPI1->CR1|=0<<7; //MSBfirst SPI1->CR1|=1<<6; //SPI设备使能 SPI1_ReadWriteByte(0xff);//启动传输 } u8 SPI1_ReadWriteByte(u8 TxData) { while((SPI1->SR&1<<1)==0); //等待发送区空 SPI1->DR=TxData; //发送一个byte while((SPI1->SR&1<<0)==0); //等待接收完一个byte return SPI1->DR; //返回收到的数据 } u32 readSPI(u8 SPI_CMD) { u32 temp; u8 i,a[3]; SPI_CS = 0 ; SPI1_ReadWriteByte(SPI_CMD); delay_us(3); for(i=0;i<3;i++){ a=SPI1_ReadWriteByte(0xff); } temp=a[0]; temp=(temp<<8)+a[1]; temp=(temp<<8)+a[2]; SPI_CS = 1 ; return temp; } void WriteSPI(u8 SPI_CMD,u32 dwData) { u8 i,b[3]; SPI_CS = 0 ; b[0]=dwData>>16; b[1]=dwData>>8; b[2]=dwData; SPI1_ReadWriteByte(SPI_CMD); for(i=0;i<3;i++) b=SPI1_ReadWriteByte(b); SPI_CS = 1 ; } u8 Read7022d(u8 Cmd,u32 *CH) { u32 Temp; *CH=readSPI(Cmd); Temp=readSPI(0x2D); if(Temp==(*CH)) return 1; else return 0; } u8 Write7022d(u8 Cmd,u32 dwData) { u32 Temp; WriteSPI(Cmd,dwData); delay_us(3); Temp=readSPI(0x2D); if((Temp==dwData)) return 1; else return 0; } void ATT7022E_Init(void) { u8 a[36]; u32 temp[12]; GPIOB->CRL&=0XFFFFFF00; //PB0/PB1(REST,WP)设置成输出,默认下拉 GPIOB->CRL|=0X00000015; GPIOC->CRL&=0XFF0FFFFF; //PC5(SIG)设置成输入 GPIOC->CRL|=0X00400000; REST=0; //复位ATT7022E delay_us(25); REST=1; delay_ms(25); while(SIG); Result=readSPI(0); AT24CXX_Read(0x00,a,36); while(!Write7022d(0x81,0xB9FE)); //设置配置寄存器 while(!Write7022d(0x83,0xF804)); //设置EMU单元配置寄存器 while(!Write7022d(0xB1,0x3427)); //模拟模块使能,开通高能滤波 while(!Write7022d(0x9E,0x1B6)); //高频脉冲输出参数 while(!Write7022d(0xB0,0x0002)); //设置数据更新中断使能 while(!Write7022d(0x9d,0x418)); //设置启动功率 temp[0]=(u32)(a[2])*0x10000+(u32)(a[1])*0x100+a[0]; //A Power temp[1]=(u32)(a[5])*0x10000+(u32)(a[4])*0x100+a[3]; //B Power temp[2]=(u32)(a[8])*0x10000+(u32)(a[7])*0x100+a[6]; //C Power temp[3]=(u32)(a[11])*0x10000+(u32)(a[10])*0x100+a[9]; //A PA temp[4]=(u32)(a[14])*0x10000+(u32)(a[13])*0x100+a[12]; //B PA temp[5]=(u32)(a[17])*0x10000+(u32)(a[16])*0x100+a[15]; //C PA temp[6]=(u32)(a[20])*0x10000+(u32)(a[19])*0x100+a[18]; //A Voltage temp[7]=(u32)(a[23])*0x10000+(u32)(a[22])*0x100+a[21]; //B Voltage temp[8]=(u32)(a[26])*0x10000+(u32)(a[25])*0x100+a[24]; //C Voltage temp[9]=(u32)(a[29])*0x10000+(u32)(a[28])*0x100+a[27]; //A Current temp[10]=(u32)(a[32])*0x10000+(u32)(a[31])*0x100+a[30]; //B Current temp[11]=(u32)(a[35])*0x10000+(u32)(a[34])*0x100+a[33]; //C Current while(!Write7022d(0x84,temp[0])); //A相功率校验寄存器 while(!Write7022d(0x87,temp[0])); //B相功率校验寄存器 while(!Write7022d(0x8a,temp[0])); //C相功率校验寄存器 while(!Write7022d(0x85,temp[1])); //A相相位补偿寄存器 while(!Write7022d(0x88,temp[1])); //B相相位补偿寄存器 while(!Write7022d(0x8b,temp[1])); //C相相位补偿寄存器 while(!Write7022d(0x86,temp[2])); //A while(!Write7022d(0x89,temp[2])); while(!Write7022d(0x8c,temp[2])); while(!Write7022d(0x8d,temp[3])); while(!Write7022d(0x90,temp[3])); while(!Write7022d(0x8e,temp[4])); //B while(!Write7022d(0x91,temp[4])); while(!Write7022d(0x9f,temp[5])); //C while(!Write7022d(0x92,temp[5])); while(!Write7022d(0x97,temp[6])); //A/B/C电压 while(!Write7022d(0x98,temp[7])); while(!Write7022d(0x99,temp[8])); while(!Write7022d(0x9a,temp[9])); //A/B/C电流 while(!Write7022d(0x9b,temp[10])); while(!Write7022d(0x9c,temp[11])); while(!Write7022d(0x00C5,0x000002)); //开启同步采样数据缓冲功能 while(!Write7022d(0x00C9,0x000001)); TIM4_Int_Init(1139,35999); } void ProcessATT7022E(void) { while(!Read7022d(CurrentA_ADDR,&Data.CurrentA)); while(!Read7022d(CurrentB_ADDR,&Data.CurrentB)); while(!Read7022d(CurrentC_ADDR,&Data.CurrentC)); Data.CurrentA=Data.CurrentA*100/8192; Data.CurrentB=Data.CurrentB*100/8192; Data.CurrentC=Data.CurrentC*100/8192; while(!Read7022d(VoltageA_ADDR,&Data.VoltageA)); while(!Read7022d(VoltageB_ADDR,&Data.VoltageB)); while(!Read7022d(VoltageC_ADDR,&Data.VoltageC)); Data.VoltageA=Data.VoltageA*100/8192; Data.VoltageB=Data.VoltageB*100/8192; Data.VoltageC=Data.VoltageC*100/8192; while(!Read7022d(Power_ADDR, &Data.Energy[0])); while(!Read7022d(PowerA_ADDR,&Data.Energy[1])); while(!Read7022d(PowerB_ADDR,&Data.Energy[2])); while(!Read7022d(PowerC_ADDR,&Data.Energy[3])); if(Data.Energy[0]>0x800000){ Data.Energy[0]=~Data.Energy[0]+1; Data.Energy[0]=Data.Energy[0]&0x00ffffff; } Data.Energy[0]=Data.Energy[0]*K*2000; if(Data.Energy[1]>0x800000){ Data.Energy[1]=~Data.Energy[1]+1; Data.Energy[1]=Data.Energy[1]&0x00ffffff; } Data.Energy[1]=Data.Energy[1]*K*1000; if(Data.Energy[2]>0x800000){ Data.Energy[2]=~Data.Energy[2]+1; Data.Energy[2]=Data.Energy[2]&0x00ffffff; } Data.Energy[2]=Data.Energy[2]*K*1000; if(Data.Energy[3]>0x800000){ Data.Energy[3]=~Data.Energy[3]+1; Data.Energy[3]=Data.Energy[3]&0x00ffffff; } Data.Energy[3]=Data.Energy[3]*K*1000; while(!Read7022d(Repower_ADDR, &Data.ReEnergy[0])); while(!Read7022d(RepowerA_ADDR,&Data.ReEnergy[1])); while(!Read7022d(RepowerB_ADDR,&Data.ReEnergy[2])); while(!Read7022d(RepowerC_ADDR,&Data.ReEnergy[3])); if(Data.ReEnergy[0]>0x800000){ Data.ReEnergy[0]=~Data.ReEnergy[0]+1; Data.ReEnergy[0]=Data.ReEnergy[0]&0x00ffffff; } Data.ReEnergy[0]=Data.ReEnergy[0]*K*2000; if(Data.ReEnergy[1]>0x800000){ Data.ReEnergy[1]=~Data.ReEnergy[1]+1; Data.ReEnergy[1]=Data.ReEnergy[1]&0x00ffffff; } Data.ReEnergy[1]=Data.ReEnergy[1]*K*1000; if(Data.ReEnergy[2]>0x800000){ Data.ReEnergy[2]=~Data.ReEnergy[2]+1; Data.ReEnergy[2]=Data.ReEnergy[2]&0x00ffffff; } Data.ReEnergy[2]=Data.ReEnergy[2]*K*1000; if(Data.ReEnergy[3]>0x800000){ Data.ReEnergy[3]=~Data.ReEnergy[3]+1; Data.ReEnergy[3]=Data.ReEnergy[3]&0x00ffffff; } Data.ReEnergy[3]=Data.ReEnergy[3]*K*1000; while(!Read7022d(Frequency_C_ADDR,&Data.Frequency)); Data.Frequency=Data.Frequency*100/8192.0; while(!Read7022d(PF_ADDR, &Data.Phasy[0])); while(!Read7022d(PF_A_ADDR,&Data.Phasy[1])); while(!Read7022d(PF_B_ADDR,&Data.Phasy[2])); while(!Read7022d(PF_C_ADDR,&Data.Phasy[3])); if(Data.Phasy[0]>0x800000){ Data.Phasy[0]=~Data.Phasy[0]+1; Data.Phasy[0]=Data.Phasy[0]&0x00ffffff; } Data.Phasy[0]=(Data.Phasy[0]/8388608.0)*1000; if(Data.Phasy[1]>0x800000){ Data.Phasy[1]=~Data.Phasy[1]+1; Data.Phasy[1]=Data.Phasy[1]&0x00ffffff; } Data.Phasy[1]=(Data.Phasy[1]/8388608.0)*1000; if(Data.Phasy[2]>0x800000){ Data.Phasy[2]=~Data.Phasy[2]+1; Data.Phasy[2]=Data.Phasy[2]&0x00ffffff; } Data.Phasy[2]=(Data.Phasy[2]/8388608.0)*1000; if(Data.Phasy[3]>0x800000){ Data.Phasy[3]=~Data.Phasy[3]+1; Data.Phasy[3]=Data.Phasy[3]&0x00ffffff; } Data.Phasy[3]=(Data.Phasy[3]/8388608.0)*1000; readADC(); while(!Write7022d(0x00C5,0)); //关闭同步采样数据缓冲功能 while(!Write7022d(0x00C5,2)); //开启同步采样数据缓冲功能 } void readADC(void) { unsigned char i; for(i=0;i<64;i++){ ADC_BUF.ADC_VoltageA=readSPI(0x7f); ADC_BUF.ADC_VoltageB=readSPI(0x7f); ADC_BUF.ADC_VoltageC=readSPI(0x7f); ADC_BUF.ADC_CurrentA=readSPI(0x7f); ADC_BUF.ADC_CurrentB=readSPI(0x7f); ADC_BUF.ADC_CurrentC=readSPI(0x7f); ADC_BUF.ADC_CurrentN=readSPI(0x7f); } } [/mw_shl_code]
正点原子
4楼-- · 2019-08-23 06:22
 精彩回答 2  元偷偷看……
狄胜倩
5楼-- · 2019-08-23 08:38
回复【13楼】masosam:
---------------------------------
请问您调出来了吗?求指导
只是那个明
6楼-- · 2019-08-23 11:07
xuyan021 发表于 2015-11-30 00:17
[mw_shl_code=c,true]void SPI1_Init(void)
{         
                        RCC-&gt;APB2ENR|=1&lt;&lt;12;           //SPI1时钟使能

首先感谢你奉献你的代码!请问你用粘贴出来的程序读出了ATT7022E中的电压、电流、功率数值吗?

一周热门 更多>