用的407的开发板,AD用的是TI的ADS1299的评估板,SPI都设置好的了,但是每次读寄存器数据都是失败。
源码如下:
#include "ADS1299.h"
/*SPI ReadWriteByte*/
uint8_t SPI1_ReadWriteByte(uint8_t TxData)
{
uint8_t RxData;
int count;
HAL_SPI_Transmit(&hspi1,&TxData,1,10);
HAL_SPI_Receive(&hspi1,&RxData,1,10);
//HAL_SPI_TransmitReceive(&hspi1,&TxData,&RxData,1,1000);
return RxData;
}
/*SPI Write registers*/
void ADS1299_WriteReg(uint8_t RegAddr,uint8_t RegData)
{
uint8_t WriteAddr;
int count;
ADS1299_CS_Low;
WriteAddr=WREG|RegAddr;
SPI1_ReadWriteByte(WriteAddr);
for(count=0;count<168;count++);
SPI1_ReadWriteByte(0x00);
for(count=0;count<168;count++);
SPI1_ReadWriteByte(RegData);
for(count=0;count<168;count++);
ADS1299_CS_High;
}
/*SPI Read registers*/
uint8_t ADS1299_ReadReg(uint8_t RegAddr)
{
uint8_t ReadAddr;
uint8_t ReadData;
int count;
ADS1299_CS_Low;
ReadAddr=RREG|RegAddr;
SPI1_ReadWriteByte(ReadAddr);
for(count=0;count<168;count++);
SPI1_ReadWriteByte(0x00);
for(count=0;count<168;count++);
ReadData=SPI1_ReadWriteByte(0xff);
for(count=0;count<168;count++);
ADS1299_CS_High;
return ReadData;
}
/*SPI Write command*/
void ADS1299_WriteCommand(uint8_t Command)
{
ADS1299_CS_Low;
SPI1_ReadWriteByte(Command);
ADS1299_CS_High;
}
/*ADS1299 Read ADC data*/
uint8_t ADS1299_ReadAdcData(uint8_t *AdcData)
{
uint8_t i=0;
ADS1299_CS_Low;
while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4)!=SET);
for(i=0;i<27;i++)
{
AdcData[i]=SPI1_ReadWriteByte(0xff);
}
ADS1299_CS_High;
return SUCCESS;
}
/*ADS1299 Initial floww at Power-Up*/
void ADS1299_PowerUpInit(void)
{
uint8_t RegValue;
// Delay for Power-On Reset and Oscillator Start-Up;
ADS1299_PDWN_High;
ADS1299_RESET_High;
ADS1299_START_Low;
HAL_Delay(1000);//Delay 1s
//Issue Reset Pulse
ADS1299_RESET_Low;
HAL_Delay(100);//Delay 100ms
ADS1299_RESET_High;
HAL_Delay(100);//Delay 100ms
//Send SDATAC Command
ADS1299_WriteCommand(SDATAC);
//Set Reference
ADS1299_WriteReg(ADS1299_REG_CONFIG3,0xE0);//Using internal Reference
HAL_Delay(10);//Delay 10ms
RegValue=ADS1299_ReadReg(ADS1299_REG_CONFIG3);//Debug !!!
printf("CONFIG3 :%04X
",RegValue);
//Write certain Registers
ADS1299_WriteReg(ADS1299_REG_CONFIG1,0x96);//Set output data rate =fMOD/4096(250 SPS)
ADS1299_WriteReg(ADS1299_REG_CONFIG2,0xC0);//Set test signal are generated internally and signal amplitude
//is 1x-(VREFP-VREFN)/2400, the frequency is fCLK/2^21
RegValue=ADS1299_ReadReg(ADS1299_REG_CONFIG1);//Debug !!!
printf("CONFIG1 :%04X
",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CONFIG2);//Debug !!!
printf("CONFIG2 :%04X
",RegValue);
ADS1299_WriteReg(ADS1299_REG_CH1SET,0x01);//Set All channels to input short
ADS1299_WriteReg(ADS1299_REG_CH2SET,0x01);
ADS1299_WriteReg(ADS1299_REG_CH3SET,0x01);
ADS1299_WriteReg(ADS1299_REG_CH4SET,0x01);
ADS1299_WriteReg(ADS1299_REG_CH5SET,0x01);
ADS1299_WriteReg(ADS1299_REG_CH6SET,0x01);
ADS1299_WriteReg(ADS1299_REG_CH7SET,0x01);
ADS1299_WriteReg(ADS1299_REG_CH8SET,0x01);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH1SET);//Debug !!!
printf("CH1SET :%04X
",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH2SET);//Debug !!!
printf("CH2SET :%04X
",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH3SET);//Debug !!!
printf("CH3SET :%04X
",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH4SET);//Debug !!!
printf("CH4SET :%04X
",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH5SET);//Debug !!!
printf("CH5SET :%04X
",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH6SET);//Debug !!!
printf("CH6SET :%04X
",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH7SET);//Debug !!!
printf("CH7SET :%04X
",RegValue);
RegValue=ADS1299_ReadReg(ADS1299_REG_CH8SET);//Debug !!!
printf("CH8SET :%04X
",RegValue);
//Active Conversion
ADS1299_START_High;//Set START=1
//Put the device back in RDATAC mode
ADS1299_WriteCommand(RDATAC);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
哎,可能是用Cube MX生成的初始化代码,我用寄存器初始化了一遍,能用
一周热门 更多>