新手入门
- 积分
- 13
- 金钱
- 13
- 注册时间
- 2021-10-9
- 在线时间
- 57 小时
|
楼主 |
发表于 2023-2-16 16:26:59
|
显示全部楼层
unsigned char SPI_WriteByte(unsigned char TxData)
{
unsigned char RxData=0;
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI1,TxData);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_RXNE)==RESET);
RxData=SPI_I2S_ReceiveData(SPI1);
return RxData;
}
//-----------------------------------------------------------------//
// 功 能:ADS1256 写数据
// 入口参数: /
// 出口参数: /
// 全局变量: /
// 备 注: 向ADS1256中地址为regaddr的寄存器写入一个字节databyte
//-----------------------------------------------------------------//
void ADS1256WREG(unsigned char regaddr,unsigned char databyte)
{
CS_0();
while(ADS1256_DRDY);//当ADS1256_DRDY为低时才能写寄存器
//向寄存器写入数据地址
SPI_WriteByte(ADS1256_CMD_WREG | (regaddr & 0x0F));
//写入数据的个数n-1
SPI_WriteByte(0x00);
//向regaddr地址指向的寄存器写入数据databyte
SPI_WriteByte(databyte);
CS_1();
}
//初始化ADS1256
void ADS1256_Init(void)
{
//*************自校准****************
while(ADS1256_DRDY);
CS_0();
SPI_WriteByte(ADS1256_CMD_SELFCAL);
while(ADS1256_DRDY);
CS_1();
//**********************************
// ADS1256WREG(ADS1256_STATUS,0x06); // 高位在前、使用缓冲
ADS1256WREG(ADS1256_STATUS,0x04); // 高位在前、不使用缓冲
//ADS1256WREG(ADS1256_MUX,0x00|0x01); // 初始化端口A0为‘+’,AINCOM位‘-’
ADS1256WREG(ADS1256_ADCON,ADS1256_GAIN_1); // 放大倍数1
ADS1256WREG(ADS1256_DRATE,ADS1256_DRATE_15000SPS); // 数据10sps
ADS1256WREG(ADS1256_IO,0x00);
//*************自校准****************
while(ADS1256_DRDY);
CS_0();
SPI_WriteByte(ADS1256_CMD_SELFCAL);
while(ADS1256_DRDY);
CS_1();
//**********************************
}
//读取AD值
signed int ADS1256ReadData(unsigned char channel)
{
unsigned int sum=0;
while(ADS1256_DRDY);//当ADS1256_DRDY为低时才能写寄存器
ADS1256WREG(ADS1256_MUX,channel); //设置通道
CS_0();
SPI_WriteByte(ADS1256_CMD_SYNC);
SPI_WriteByte(ADS1256_CMD_WAKEUP);
SPI_WriteByte(ADS1256_CMD_RDATA);
sum |= (SPI_WriteByte(0xff) << 16);
sum |= (SPI_WriteByte(0xff) << 8);
sum |= SPI_WriteByte(0xff);
CS_1();
if (sum>0x7FFFFF) // if MSB=1,
{
sum -= 0x1000000; // do 2's complement
}
return sum;
}
|
|