STM32F103 与ADS8331 SPI读写不通,求解答,谢谢!~
#include "stm32f10x.h"
#include "delay.h"
#include "spi.h"
#include "ads8331_function.h"
/*******************************************************************************
******************************************************************************/
void ADS8331_SPI_Config(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
//*****************************************************************************************
//³õʼ»¯ADS8331¿ØÖÆÒý½Å ¿ØÖÆIO ·ÇSPIͨѶ
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //ÉÏÀÊäÈë
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOB, &GPIO_InitStructure); //³õʼ»¯GPIOB.0 ΪÉÏÀÊäÈëÒý½Å END-DRDY
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //ÍÆÍìÊä³ö
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure); //³õʼ»¯PB.1 ÍÆÍìÊä³öÒý½Å START
//*****************************************************************************************
//SPI ͨѶÅäÖÃ
/* Enable SPI clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* Configure SPI pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = ADS8331_SCK | ADS8331_DO | ADS8331_DIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ADS8331_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = ADS8331_CS;
GPIO_Init(ADS8331_PORT, &GPIO_InitStructure);
ADS8331_CS_H();
/* SPI configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
// SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; //SPI Ö¡½á¹¹16b
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
// SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
// SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //ÔÀ´
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; //PCLK1/32=24/16=1.5MHz,
// SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(ADS8331_SPI, &SPI_InitStructure);//ÉèÖÃSPI ÍâÉè #define ADS1248_SPI SPI1
/* Enable SPI */
SPI_Cmd(ADS8331_SPI, ENABLE); //ʹÄÜSPI #define ADS1248_SPI SPI1
}
/*******************************************************************************
*******************************************************************************/
u16 ADS8331_ReadWord(void)
{
return (ADS8331_WriteWord(ADS_NOP));
}
*******************************************************************************/
u16 ADS8331_WriteWord(u16 data)
{
/* Loop while DR register in not emplty */
while(SPI_I2S_GetFlagStatus(ADS8331_SPI, SPI_I2S_FLAG_TXE) == RESET);
/* Send u8 through the SPI bus peripheral */
SPI_I2S_SendData(ADS8331_SPI, data);
/* Wait to receive a u8 */
while(SPI_I2S_GetFlagStatus(ADS8331_SPI, SPI_I2S_FLAG_RXNE) == RESET);
/* Return the u8 read from the SPI bus */
return SPI_I2S_ReceiveData(ADS8331_SPI);
}
void ADS_Wtite_Reg(u16 reg)
{
reg = Write_CFR_CMD | reg;
ADS8331_CS_L();
ADS8331_WriteWord(reg);
ADS8331_CS_H();
}
//*****************************
//¶ÁÅäÖüĴæÆ÷
u16 ADS_Read_Reg(void)
{ u16 reg;
ADS8331_CS_L();
ADS8331_WriteWord(Read_CFR_CMD);
reg = ADS8331_ReadWord();
ADS8331_CS_H();
return (reg&0x0fff);
}
//ÅäÖÃADS8331
u16 ADS8331_CONFIG(void)
{ u16 config_REG ;
config_REG = Nomal_Operation
| Deep_Power_Down_DIS
| Nap_Power_Down_DIS
| Auto_Nap_Power_Down_DIS
| Pin10_As_EOC_INT
| Pin10_as_EOC
| EOC_INT_Active_Low
| SPS_250k_42CCLKs
| Manual_Trigger
| Conversion_Clock_Full_SCLK
// | Auto_Channel_Select_EN
;
ADS_Wtite_Reg(config_REG) ; //дÅäÖüĴæÆ÷
delay_us(200);
return ADS_Read_Reg(); //·µ»Ø¶ÁÈ¡µÄ ÅäÖüĴæÆ÷½øÐжԱÈ
}
调用 u16 ADS8331_CONFIG(void) 函数,采样位置和时钟极性也调整过,什么也读不到。。。不知道错哪里了,求指点,不胜感激。
#ifndef __ADS8331_FUNCTION_H
#define __ADS8331_FUNCTION_H
#include "sys.h"
#define Vref 2.048f
//#define ADS_NOP 0x8000
#define ADS_NOP 0x9000
//#define ADS_NOP 0xa000
//ADͨ·ѡÔñ**************
#define ADIN1 0x0000
#define ADIN2 0x1000
#define ADIN3 0x3000
#define ADIN4 0x4000
//*************************************
//AD¿ØÖÆÃüÁî***************************
#define WakeUp_CMD 0xb000
#define Defaule_CMD 0xf000
//*************************************
//ADÒý½Å¿ØÖÆ***************************
#define ADS8331_EOC GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)
#define ADS8331_EOC_high GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==1 //
#define ADS8331_EOC_low GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==0 //Data ready (active low)
#define ADS8331_CONVST_H GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_SET) //ÉèÖÃCONVST_H 1 ²»¿ªÊ¼×ª»» (1£ºBit_SET;0:Bit_RESET)
#define ADS8331_CONVST_L GPIO_WriteBit(GPIOB, GPIO_Pin_1, Bit_RESET); //ÉèÖÃCONVST_H 0 ¿ªÊ¼×ª»» (1£ºBit_SET;0:Bit_RESET)
//****************************************
#define Read_CFR_CMD 0xc000
#define Read_data_CMD 0xd000
#define Write_CFR_CMD 0xe000
//****************************************
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#define Nomal_Operation 0x0001 //SoftWare_Rest
#define TAG_Bit_Out_En 0x0002 //TAG_Bit_Out_DIS
#define Deep_Power_Down_DIS 0x0004 //Deap_Power_Down_EN
#define Nap_Power_Down_DIS 0x0008 //Nap_Power_Down_EN
#define Auto_Nap_Power_Down_DIS 0x0010 //Auto_Nap_Power_Down_EN
#define Pin10_As_EOC_INT 0x0020 //Pin10_as_CDI
#define Pin10_as_EOC 0x0040 //Pin10_as_INT
#define EOC_INT_Active_Low 0x0080 //EOC_INT_Active_Hign
#define SPS_250k_42CCLKs 0x0100 //SPS_500k_21CCLKs
#define Manual_Trigger 0x0200 //Auto_Trigger
#define Conversion_Clock_Full_SCLK 0x0400 //Conversion_Clock_Half_SCLK
#define Auto_Channel_Select_EN 0x0800 //Manual_Channel_Select_EN
/* | Nomal_Operation //D0
| TAG_Bit_Out_En //D1
| Deep_Power_Down_DIS //D2
| Nap_Power_Down_DIS //D3
| Auto_Nap_Power_Down_DIS //D4
| Pin10_As_EOC_INT //D5
| Pin10_as_EOC //D6
| EOC_INT_Active_Low //D7
| SPS_250k_42CCLKs //D8
| Manual_Trigger //D9
| Conversion_Clock_Full_SCLK //D10
| Manual_Channel_Select_EN //D11
*/
void ADS_Wtite_Reg(u16 reg);//д¼Ä´æÆ÷
u16 ADS_Read_Reg(void);//¶Á¼Ä´æÆ÷
u16 ADS8331_CONFIG(void) ;
void ADS_Channel_Select(u16 channal) ;//ADͨµÀÑ¡Ôñ//ÊäÈë ADIN1£»ADIN2£»ADIN3£»ADIN4
void ADS_Start(void);//¿ªÊ¼×ª»»
u16 ADS_Read_Data(void);//¶ÁÈ¡ADCת»»ÊýÖµ
float Get_voltage(u16 channal);
void ADS_Default(void) ;
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
#endif
什么也读不到, 该怎么理解
SPI 在写的时候总会自动返回. 总是有数据的, 全0 的结果还是全1 的结果, 如果是这种情况, 先检查硬件吧.包括常见的供电, 焊接和连接等等
我觉得你先示波器捉波形看看 再看看寄存器配置有没有问题
请问你的问题解决了吗?我现在也是搞这个芯片,可以分享下代码吗?非常感谢。