情况这样,
我的STM32 和ADE7753通信,
连接如下:
MOSI---DOUT
MISO ----DIN
SCLK---SCLK
CS------CS
程序测试如下:
void SPI_Configuration(void)//SPI1使能
{
SPI_InitTypeDef SPI_InitStructure;
SPI_StructInit(&SPI_InitStructure);
/* SPI1 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //SPI设置为双线双向全双工
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;//设置为主SPI
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;//SPI发送接收8位帧结构
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;//时钟悬空低 参考了ADE7753芯片手册,芯片空闲或者发送完毕处于低电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;//数据捕获于第一个时钟沿
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//SPI_NSS_Hard;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //256分频
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//SPI数据高位在前
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
//SPI_CalculateCRC( SPI2, DISABLE);//不启动硬件CRC计算
SPI_SSOutputCmd(SPI2, ENABLE);
/*使能*/
SPI_Cmd(SPI2,ENABLE);
}
unsigned char SPI2_ReadWrite(unsigned char writedat)
{
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
/* Send byte through the SPI1 peripheral */
SPI_I2S_SendData(SPI2, writedat);
/* Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the byte read from the SPI bus */
return SPI_I2S_ReceiveData(SPI2);
}
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15|GPIO_Pin_14;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
void WritingDatatoAD(u8 addr, u16 multi_byte)
{
AD_CS_L();
Delay(1);
SPI2_ReadWrite(0x80|addr);
Delay(1);
SPI2_ReadWrite(((multi_byte >> 8) & 0xff));
Delay(1);
SPI2_ReadWrite(multi_byte & 0xff);
Delay(1);
AD_CS_H();
}
//读ADE7753
u32 ReadingDatafromAD(u8 addr)
{
u32 data = 0;
AD_CS_L();
Delay(2);
SPI2_ReadWrite(0x80|addr);
data = (SPI1_Write2_Byte(0xff)) << 16;
Delay(1);
data |= (SPI1_Write2_Byte(0xff)) << 8;
Delay(1);
data |= SPI1_Write2_Byte(0xff);
data &= 0x00ffffff;
Delay(1);
AD_CS_H();
return data;
}
//******************************
测试部分
***********************************/
WritingDatatoAD(REG_MODE,0x01);
ck = ReadingDatafromAD(REG_MODE);
现在的情况是 我发一个数 收到的也是这个数 很不理解啊 ,请大家帮帮忙 查查了 麻烦了 |