STM32结合AD1256调试出现问题,忘大神给予指导
时间:10-02
整理:3721RD
点击:
STM32结合AD1256进行调试,始终不能出正常结果,不知道有没有大神调过,能否帮我看下程序,我可能没有分数,初来这个论坛,还望大神给予指导。
示波器显示波形也正确,无论模拟输入多少,输出变量值始终为0x10453附近变动,请教各位大神,有了解的吗?
以下是程序:
int main(void)
{
// unsigned long results = 0;
unsigned char i = 0;
SystemInit(); //时钟初始化
delay_init(72); //延迟初始化
SPI_ADS1256_Init(); //SPI初始化
ADS1256_GPIO_init(); //与AD相关GPIO初始化
ADS1256_Init();//AD1256初始化
while(1)
{
results = ADS1256ReadData();
}
以下SPI_ADS1256_Init()-----------------------------------
void SPI_ADS1256_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SPI1 and GPIO clocks */
/*!< SPI_FLASH_SPI_CS_GPIO, SPI_FLASH_SPI_MOSI_GPIO,
SPI_FLASH_SPI_MISO_GPIO, SPI_FLASH_SPI_DETECT_GPIO
and SPI_FLASH_SPI_SCK_GPIO Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD, ENABLE);
/*!< SPI_FLASH_SPI Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/*!< AFIO Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/*!< Configure SPI_FLASH_SPI pins: SCK */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // í??aê?3?
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ?′ó?í??aê?3?
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: DIN */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ?′ó?í??aê?3?
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: DOUT */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //é?à-ê?è?
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ?′ó?í??aê?3?
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //′
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //êy?Y′?ê?′óMSB???aê?
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC?μ????μ??à??ê?
SPI_Init(SPI1, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);
}
void ADS1256_GPIO_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/////////CS reset òy??éè??
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_4 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP; //--í??aê?3??£ê?
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_10MHz; //--GPIO·-×a?ù?è?a50MHz
GPIO_Init(GPIOA , &GPIO_InitStructure); //--??????D′è???ó|μ?GPIO??′??÷?D
// GPIO_InitTypeDef GPIO_InitStructure;
///////// DRDYòy??éè??
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN_FLOATING; //--??μ?ê?è??£ê?
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_10MHz; //--GPIO·-×a?ù?è?a50MHz
GPIO_Init(GPIOA , &GPIO_InitStructure); //--??????D′è???ó|μ?GPIO??′??÷?D
GPIO_Init(GPIOA , &GPIO_InitStructure); //--??????D′è???ó|μ?GPIO??′??÷?D
}
void ADS1256_Init(void)
{
ADS_CS_LOW();
RESET_HIGH();
delay_us(100);
SPI_SendByte(ADS1256_CMD_REST);
delay_ms(10);
while(ADS_DRDY); //μ±ADS1256_DRDY?aμíê±2??üD′??′??÷
SPI_SendByte(ADS1256_CMD_SYNC); //í?2??üá?
SPI_SendByte(ADS1256_CMD_WAKEUP); //í?2???D?
while(ADS_DRDY);
SPI_SendByte(ADS1256_CMD_WREG | ADS1256_STATUS);//á?D?D′è?4????′??÷
SPI_SendByte(3);
SPI_SendByte(0x04); //?????ú?°£?ê1ó??ú2?D£×?£?2?ê1ó??o′?
SPI_SendByte(ADS1256_MUXP_AIN2 | ADS1256_MUXN_AIN3);//???úê?è?A2?a?y£?A3???o
SPI_SendByte(ADS1256_GAIN_1); //·?′ó±?êyéè??
SPI_SendByte(ADS1256_DRATE_2_5SPS); //2é?ˉ?ù?èéè??
delay_us(100);
while(ADS_DRDY); //μ±ADS1256_DRDY?aμíê±2??üD′??′??÷
SPI_SendByte(ADS1256_CMD_SELFCAL); //??ò?oí??ò?×??ˉD£×?
ADS_CS_HIGH();
delay_ms(100);
}
unsigned long ADS1256ReadData(void)
{
unsigned char i=0;
unsigned long sum=0;
unsigned long r=0;
ADS_CS_LOW();
while(ADS_DRDY); //μ±ADS1256_DRDY?aμíê±2??üD′??′??÷
SPI_SendByte(ADS1256_CMD_SYNC);
SPI_SendByte(ADS1256_CMD_WAKEUP);
SPI_SendByte(ADS1256_CMD_RDATA);
delay_us(10); //min=50*(1/fCLKIN)=50*(1/7.68MHZ)=6500ns;max=whatever
for(i=0;i<3;i++)
{
sum = sum << 8;
r = SPI_SendByte(0);
sum |= r;
}
ADS_CS_HIGH();
return sum;
}
示波器显示波形也正确,无论模拟输入多少,输出变量值始终为0x10453附近变动,请教各位大神,有了解的吗?
以下是程序:
int main(void)
{
// unsigned long results = 0;
unsigned char i = 0;
SystemInit(); //时钟初始化
delay_init(72); //延迟初始化
SPI_ADS1256_Init(); //SPI初始化
ADS1256_GPIO_init(); //与AD相关GPIO初始化
ADS1256_Init();//AD1256初始化
while(1)
{
results = ADS1256ReadData();
}
以下SPI_ADS1256_Init()-----------------------------------
void SPI_ADS1256_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SPI1 and GPIO clocks */
/*!< SPI_FLASH_SPI_CS_GPIO, SPI_FLASH_SPI_MOSI_GPIO,
SPI_FLASH_SPI_MISO_GPIO, SPI_FLASH_SPI_DETECT_GPIO
and SPI_FLASH_SPI_SCK_GPIO Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD, ENABLE);
/*!< SPI_FLASH_SPI Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/*!< AFIO Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
/*!< Configure SPI_FLASH_SPI pins: SCK */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // í??aê?3?
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ?′ó?í??aê?3?
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: DIN */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ?′ó?í??aê?3?
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*!< Configure SPI_FLASH_SPI pins: DOUT */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //é?à-ê?è?
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ?′ó?í??aê?3?
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //′
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //êy?Y′?ê?′óMSB???aê?
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC?μ????μ??à??ê?
SPI_Init(SPI1, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);
}
void ADS1256_GPIO_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/////////CS reset òy??éè??
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_4 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP; //--í??aê?3??£ê?
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_10MHz; //--GPIO·-×a?ù?è?a50MHz
GPIO_Init(GPIOA , &GPIO_InitStructure); //--??????D′è???ó|μ?GPIO??′??÷?D
// GPIO_InitTypeDef GPIO_InitStructure;
///////// DRDYòy??éè??
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_IN_FLOATING; //--??μ?ê?è??£ê?
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_10MHz; //--GPIO·-×a?ù?è?a50MHz
GPIO_Init(GPIOA , &GPIO_InitStructure); //--??????D′è???ó|μ?GPIO??′??÷?D
GPIO_Init(GPIOA , &GPIO_InitStructure); //--??????D′è???ó|μ?GPIO??′??÷?D
}
void ADS1256_Init(void)
{
ADS_CS_LOW();
RESET_HIGH();
delay_us(100);
SPI_SendByte(ADS1256_CMD_REST);
delay_ms(10);
while(ADS_DRDY); //μ±ADS1256_DRDY?aμíê±2??üD′??′??÷
SPI_SendByte(ADS1256_CMD_SYNC); //í?2??üá?
SPI_SendByte(ADS1256_CMD_WAKEUP); //í?2???D?
while(ADS_DRDY);
SPI_SendByte(ADS1256_CMD_WREG | ADS1256_STATUS);//á?D?D′è?4????′??÷
SPI_SendByte(3);
SPI_SendByte(0x04); //?????ú?°£?ê1ó??ú2?D£×?£?2?ê1ó??o′?
SPI_SendByte(ADS1256_MUXP_AIN2 | ADS1256_MUXN_AIN3);//???úê?è?A2?a?y£?A3???o
SPI_SendByte(ADS1256_GAIN_1); //·?′ó±?êyéè??
SPI_SendByte(ADS1256_DRATE_2_5SPS); //2é?ˉ?ù?èéè??
delay_us(100);
while(ADS_DRDY); //μ±ADS1256_DRDY?aμíê±2??üD′??′??÷
SPI_SendByte(ADS1256_CMD_SELFCAL); //??ò?oí??ò?×??ˉD£×?
ADS_CS_HIGH();
delay_ms(100);
}
unsigned long ADS1256ReadData(void)
{
unsigned char i=0;
unsigned long sum=0;
unsigned long r=0;
ADS_CS_LOW();
while(ADS_DRDY); //μ±ADS1256_DRDY?aμíê±2??üD′??′??÷
SPI_SendByte(ADS1256_CMD_SYNC);
SPI_SendByte(ADS1256_CMD_WAKEUP);
SPI_SendByte(ADS1256_CMD_RDATA);
delay_us(10); //min=50*(1/fCLKIN)=50*(1/7.68MHZ)=6500ns;max=whatever
for(i=0;i<3;i++)
{
sum = sum << 8;
r = SPI_SendByte(0);
sum |= r;
}
ADS_CS_HIGH();
return sum;
}
首先确定硬件连接,STM32的SPI模块通信测试程序通过没有,STM32管脚的初始化和时钟的配置……一般这个都没错
然后是AD1256器件的配置啦,必须要读手册,确认每个寄存器的配置位是否正确
用到的delay函数应该是systic写的吧,
我觉得主要还是器件的操作问题~~~
检查通信的时序是不是与手册里面要求的一致,该延时的地方有没有延时。