求助STM32操作ADS1247,向地址为0X04的寄存器写入0XA3,读出不正确(data_Buf[3]要么是0要么是乱码),代码在内容中,请教高手!
void ADS1247_LowLevel_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*!< ADS1247_SPI Periph clock enable */
/*!< ADS1247_SPI_SCL_GPIO_CLK, ADS1247_SPI_SDA_GPIO_CLK
and ADS1247_SPI_SMBUSALERT_GPIO_CLK Periph clock enable */
RCC_APB2PeriphClockCmd(ADS1247_SPI_CLK_GPIO_CLK | ADS1247_SPI_STR_GPIO_CLK | ADS1247_SPI_RDY_GPIO_CLK, ENABLE);
/*!< Configure ADS1247_SPI pins: CLK SDI */
GPIO_InitStructure.GPIO_Pin = ADS1247_SPI_CLK_PIN | ADS1247_SPI_SDI_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(ADS1247_SPI_CLK_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure ADS1247_SPI pins: SDO */
GPIO_InitStructure.GPIO_Pin = ADS1247_SPI_SDO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(ADS1247_SPI_SDO_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure ADS1247_SPI pins: START RESET */
GPIO_InitStructure.GPIO_Pin = ADS1247_SPI_STR_PIN | ADS1247_SPI_RET_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(ADS1247_SPI_STR_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure ADS1247_SPI pins: CS */
GPIO_InitStructure.GPIO_Pin = ADS1247_SPI_CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(ADS1247_SPI_CS_GPIO_PORT, &GPIO_InitStructure);
/*!< Configure ADS1247_SPI pins: READY*/
GPIO_InitStructure.GPIO_Pin = ADS1247_SPI_RDY_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(ADS1247_SPI_RDY_GPIO_PORT, &GPIO_InitStructure);
}
void ADS1247_Init(void)
{
ADS1247_LowLevel_Init();
ADS1247_CS_H;
ADS1247_RET_H;
ADS1247_RET_L;
delay(5000000);
ADS1247_RET_H;
delay(5000000);
ADS1247_STR_L;
delay(DELAY_T);
ADS1247_STR_H;
}
/*******************************************************************************************************************
** 函数名称: void SPI_SendByte() Name: void SPI_SendByte()
** 功能描述: 通过SPI接口发送一个字节 Function: send a byte by SPI interface
** 输 入: INT8U byte: 发送的字节 Input: INT8U byte: the byte that will be send
** 输 出: 无 Output: NULL
** 说 明:
********************************************************************************************************************/
unsigned char SPI_SendByte(unsigned char byte)
{
unsigned char i,j;
j=0;
ADS1247_CS_L;
SPI_CLK_L;
delay(DELAY_T);
for(i=0;i<8;i++) //"发数据
{
SPI_CLK_H;
if(byte&0x80)SPI_SDI_H;
else SPI_SDI_L;
byte=byte<<1;
delay(DELAY_T);
j=j<<1;
if(GPIO_ReadInputDataBit(ADS1247_SPI_SDO_GPIO_PORT,ADS1247_SPI_SDO_PIN))j=(j|0x01); //"读数据
SPI_CLK_L;//
delay(DELAY_T);
}
//ADS1247_CS_H;
return j;
}
/*******************************************************************************************************************
** 函数名称: INT8U SPI_RecByte() Name: INT8U SPI_RecByte()
** 功能描述: 从SPI接口接收一个字节 Function: receive a byte from SPI interface
** 输 入: 无 Input: NULL
** 输 出: 收到的字节 Output: the byte that be received
********************************************************************************************************************/
unsigned char SPI_RecByte(void)
{
return SPI_SendByte(0XFF);
}ADS1247_Init();
//while(aabb)
//{
//
//
main()
{
ADS1247_Init();
SPI_SendByte(ADS1247_WREG|0X04);
SPI_SendByte(0X01);
SPI_SendByte(0XA3);
SPI_SendByte(ADS1247_RREG|0X04);
SPI_SendByte(0X01);
data_Buf[3] = SPI_RecByte();
}