代码如下
GPIO定义:
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_11; // SCLK、DIN、START
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // DOUT
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // DRDY
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOB, &GPIO_InitStructure);
对寄存器操作如下:
void ADS1248WREG(unsigned char regaddr,unsigned char databyte)
{
ADS1248SPI(0x40+(regaddr&0x0F));
ADS1248SPI(0X00);
ADS1248SPI(databyte);
}
unsigned char ADS1248RREG(unsigned char regaddr)
{
unsigned char r=0X00;
ADS1248SPI(0x20+(regaddr&0x0F));
ADS1248SPI(0X00);
r=ADS1248SPI(0XFF);
return r;
}
unsigned char ADS1248SPI(unsigned char m)
{
unsigned char i,r=0;
for(i=0;i<8;i++) {
ADS1248_SCLK(1);
__NOP();__NOP();__NOP();__NOP();__NOP();
r<<=1;
if (m & 0x80) ADS1248_SADI(1);
else ADS1248_SADI(0);
__NOP();__NOP();__NOP();__NOP();__NOP();
ADS1248_SCLK(0);
__NOP();__NOP();__NOP();__NOP();__NOP();
if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_9)>0) r++;
__NOP();__NOP();__NOP();__NOP();__NOP();
m<<=1;
}
return r;
}
执行AD寄存器写MUX0:
ADS1248WREG(0X00,0x01); // MUX0=00 000 001 = 0x01 AIN1
iads=ADS1248RREG(0X00);
CLK接GND,VREFOUT和VREFCOM接4.7uF,其它电路参照手册。
问题是,往寄存器写数据后,读出来的iads数为0X00,理论应该是0X01;用表和示波器均测得CLK、DIN有数据时电平变化,DOUT无电平变化,是否和代码有关,求解!
Q群:77203167
------解决思路----------------------
经过几天的调试,测试,IO口定义没问题,读写操作也木问题,可能是ADS1248未工作,关于内部晶振,无法测量,现正继续…
------解决思路----------------------
只要SPI的时序正确就可以
------解决思路----------------------
问题完美解决:
按照官方给出的图纸设计电路,然后按照时序给RESERT、START信号,后续WREG、RREG、READDATA等都正常,一定要注意时序,不同MCU时间间隔不一样!