已回答假定已回答

急,急,急 ADE7878 SPI通讯问题

Solo 在 2017-10-10 詢問的問題
最後回覆由JerryYU於2017-11-7提供

各位好:

     参考官网的ADE7878示例代码,然后发现SPI读任何寄存器都全是0XFFFFFFFF,感觉应该是没通讯上,但是又不知道问题在哪,有哪位用过的可以帮忙看下吗? 附上示例代码, 诚心请教! 有好心人可以加我的QQ:79444528或者微信solo_wen,求指导. 已经弄了两周了. 下面是我的一些详细信息:

 

1.硬件原理图:

已经量VREF(C28)两端电压为1.19V

 

2.软件:

SPI配置:

void MX_SPI1_Init(void)

{

  hspi1.Instance = SPI1;

  hspi1.Init.Mode = SPI_MODE_MASTER;

  hspi1.Init.Direction = SPI_DIRECTION_2LINES;

  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;

  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;

  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;

  hspi1.Init.NSS = SPI_NSS_SOFT;

  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; // 80M/64

  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;

  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;

  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

  hspi1.Init.CRCPolynomial = 7;

  if (HAL_SPI_Init(&hspi1) != HAL_OK)

  {

    _Error_Handler(__FILE__, __LINE__);

  }

}

然后CLKPolarity  HIGH,LOW都试过, CLKPhase 1,2EDGE也都试过,都没用.

 

ADE78XX_SPI.C

 

/************************************************************************************************

 

 

Author        : CAC (China Applications Support Team)        

 

 

Date          : October 2012

 

 

File          : ADE7878_SPI.h

 

 

Hardware      : ADE7878, ADuC7060

 

 

Description   : Test with ADE7878

       Config ADE7878 to read phase A current rms, voltage rms, energy, period, in linecycle mode

 

 

*************************************************************************************************/

#include "ADE78xx_spi.h"

#include "extern.h"

 

 

//extern unsigned char szTxData[7];

unsigned char DummyRd;

SPI_HandleTypeDef* ADE_hspi =  &hspi1;

 

 

uint8_t SPI_comm(uint8_t s_byte);

 

 

/******************************************************************************

**  Abstract:  

** short delay

**  Parameters:

** none 

**  Returns:

** none

*******************************************************************************/

void SPIDelay(void)

{

int i;

for(i=0; i<20; i++);

}

 

 

/******************************************************************************

**  Abstract:  

** write 4 bytes to ADE7878

**  Parameters:

** unsigned int address: register address

** long int sendtemp   : configuration value 

**  Returns:

** none

*******************************************************************************/

void SPIWrite4Bytes(unsigned int address , long int sendtemp)

{

char i;

unsigned char szTxData[7];

CLR_PIN00();

szTxData[0] = 0x00;

szTxData[1] = (char)(address>>8);

szTxData[2] = (char)(address);

szTxData[3] = (char)(sendtemp>>24);

szTxData[4] = (char)(sendtemp>>16);

szTxData[5] = (char)(sendtemp>>8);

szTxData[6] = (char)(sendtemp);

for(i=0;i<7;i++)

{

SPI_comm(szTxData[i]);

SPIDelay();

}

SET_PIN00();

}

 

 

/******************************************************************************

**  Abstract:  

** write 2 bytes to ADE7878

**  Parameters:

** unsigned int address: register address

** long int sendtemp   : configuration value 

**  Returns:

** none

*******************************************************************************/

void SPIWrite2Bytes(unsigned int address , int sendtemp)

{

char i;

unsigned char szTxData[7];

CLR_PIN00();

szTxData[0] = 0x00;

szTxData[1] = (char)(address>>8);

szTxData[2] = (char)(address);

szTxData[3] = (char)(sendtemp>>8);

szTxData[4] = (char)(sendtemp);

 

 

for(i=0;i<5;i++)

{

SPI_comm(szTxData[i]);

SPIDelay();

}

SET_PIN00();

 

 

}

 

 

/******************************************************************************

**  Abstract:  

** write 1 byte to ADE7878

**  Parameters:

** unsigned int address: register address

** long int sendtemp   : configuration value 

**  Returns:

** none

*******************************************************************************/

void SPIWrite1Byte(unsigned int address , char sendtemp)

{

char i;

unsigned char szTxData[7];

CLR_PIN00();

szTxData[0] = 0x00;

szTxData[1] = (char)(address>>8);

szTxData[2] = (char)(address);

szTxData[3] = sendtemp;

 

 

for(i=0;i<4;i++)

{

SPI_comm(szTxData[i]);

SPIDelay();

}

 

SET_PIN00();

}

 

 

/******************************************************************************

**  Abstract:  

** read 4 bytes from ADE7878

**  Parameters:

** unsigned int address: register address

**  Returns:

** readout             : read value

*******************************************************************************/

long int SPIRead4Bytes(unsigned int address)

{

long int readout;

unsigned char szTxData[7];

char spireadbuf[4];

char i;

 

 

CLR_PIN00();

szTxData[0] = 0x01;

szTxData[1] = (char)(address>>8);

szTxData[2] = (char)(address);

 

for(i=0;i<3;i++)

{

SPI_comm(szTxData[i]);

SPIDelay();

}

// SPICON |= BIT12; // Flush Rx FIFO

// SPICON &= ~BIT12;

 

 

// DummyRd = SPI_comm(0x00);

for(i=0;i<4;i++)

{

SPIDelay();

spireadbuf[i] = SPI_comm(0x00);

}

 

readout = (spireadbuf[0]<<24)+(spireadbuf[1]<<16)+(spireadbuf[2]<<8)+spireadbuf[3];

 

 

SET_PIN00();

return(readout);

}

 

 

/******************************************************************************

**  Abstract:  

** read 2 bytes from ADE7878

**  Parameters:

** unsigned int address: register address

**  Returns:

** readout             : read value

*******************************************************************************/

int SPIRead2Bytes(unsigned int address)

{

int readout;

unsigned char szTxData[7];

char i;

char spireadbuf[2];

 

 

CLR_PIN00();

szTxData[0] = 0x01;

szTxData[1] = (char)(address>>8);

szTxData[2] = (char)(address);

 

for(i=0;i<3;i++)

{

SPI_comm(szTxData[i]);

SPIDelay();

}

 

 

// SPICON |= BIT12; // Flush Rx FIFO

// SPICON &= ~BIT12;

DummyRd = SPI_comm(0x00);

for(i=0;i<2;i++)

{

// SPITX = 0x00;

SPIDelay();

spireadbuf[i] = SPI_comm(0x00);

}

 

readout = (spireadbuf[0]<<8)+spireadbuf[1];

 

 

SET_PIN00();

return(readout);

}

 

 

/******************************************************************************

**  Abstract:  

** read 1 byte from ADE7878

**  Parameters:

** unsigned int address: register address

**  Returns:

** readout             : read value

*******************************************************************************/

char SPIRead1Byte(unsigned int address)

{

char readout;

unsigned char szTxData[7];

char i;

 

 

CLR_PIN00();

szTxData[0] = 0x01;

szTxData[1] = (char)(address>>8);

szTxData[2] = (char)(address);

 

for(i=0;i<3;i++)

{

SPI_comm(szTxData[i]);

SPIDelay();

}

// SPICON |= BIT12; // Flush Rx FIFO

// SPICON &= ~BIT12;

DummyRd = SPI_comm(0x00);

// SPITX = 0x00;

SPIDelay();

readout = SPI_comm(0x00);

 

 

SET_PIN00();

 

return(readout);

}

 

 

 

 

uint8_t SPI_comm(uint8_t s_byte)

{

  uint8_t ret = 0;

  HAL_SPI_TransmitReceive(ADE_hspi, &s_byte, &ret, 1, 0xfff);

 

  return ret;

}

 

以上基本参考的示例程序, 只把发送和接收这里改成了我的库函数.

 

ADE7878.C就是示例程序一模一样了.

 

然后我在Main里面函数这样调用的.

  MX_SPI1_Init();

  ADE7878Reset();

  ADE7878SPICfg();

然后最后执行读写寄存器,如下图:

 

結果