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; // ÍÆÃâÊä³ö
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // ¸´ÓÃÍÆÃâÊä³ö
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; // ¸´ÓÃÍÆÃâÊä³ö
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; // ¸´ÓÃÍÆÃâÊä³ö
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; //Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼
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 Òý½ÅÉèÖÃ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_4 | GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode= GPIO_Mode_Out_PP; //--ÍÆÃâÊä³öģʽ
//GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed= GPIO_Speed_10MHz; //--GPIO·×ªËÙ¶ÈΪ50MHz
GPIO_Init(GPIOA , &GPIO_InitStructure); //--½«ÅäÖÃдÈë¶ÔÓ¦µÄGPIO¼Ä´æÆ÷ÖÐ
// GPIO_InitTypeDef GPIO_InitStructure;
///////// DRDYÒý½ÅÉèÖÃ
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·×ªËÙ¶ÈΪ50MHz
GPIO_Init(GPIOA , &GPIO_InitStructure); //--½«ÅäÖÃдÈë¶ÔÓ¦µÄGPIO¼Ä´æÆ÷ÖÐ
GPIO_Init(GPIOA , &GPIO_InitStructure); //--½«ÅäÖÃдÈë¶ÔÓ¦µÄGPIO¼Ä´æÆ÷ÖÐ
}
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ΪµÍʱ²ÅÄÜд¼Ä´æÆ÷
SPI_SendByte(ADS1256_CMD_SYNC); //ͬ²½ÃüÁî
SPI_SendByte(ADS1256_CMD_WAKEUP); //ͬ²½»½ÐÑ
while(ADS_DRDY);
SPI_SendByte(ADS1256_CMD_WREG | ADS1256_STATUS);//Á¬ÐøдÈë4¸ö¼Ä´æÆ÷
SPI_SendByte(3);
SPI_SendByte(0x04); //¸ßλÔÚÇ°£¬Ê¹ÓÃÄÚ²¿Ð£×¼£¬²»Ê¹Óûº´æ
SPI_SendByte(ADS1256_MUXP_AIN2 | ADS1256_MUXN_AIN3);//¶Ë¿ÚÊäÈëA2ΪÕý£¬A3λ¸º
SPI_SendByte(ADS1256_GAIN_1); //·Å´ó±¶ÊýÉèÖÃ
SPI_SendByte(ADS1256_DRATE_2_5SPS); //²É¼¯ËÙ¶ÈÉèÖÃ
delay_us(100);
while(ADS_DRDY); //µ±ADS1256_DRDYΪµÍʱ²ÅÄÜд¼Ä´æÆ÷
SPI_SendByte(ADS1256_CMD_SELFCAL); //Æ«ÒƺÍÔöÒæ×Ô¶¯Ð£×¼
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ΪµÍʱ²ÅÄÜд¼Ä´æÆ÷
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;
}
|