ADS1256+STM32程序详解
ADS1256是TI公司推出的微功耗、高精度、8通道、24位高性能模数转换器(ADC)。该器件提供高达23比特的无噪声精度、数据速率高达30kSPS(次采样/秒)、十万分之一的非线性特性以及具备众输入模拟多路开关、输入缓冲器、可编程增益放大器和可编程数字滤波器等,,有完善的自校正和系统校正系统,SPI串行数据传输接口。可为设计人员带来完整而高分辨率的量测解决方,如应变计、气体分析、仪器仪表、压力传感器、血液分析、医疗科学仪器等应用,ADS1256采用SSOP-2封装。
ADS1256特点
1、8通道输入:可同时采集8路信号。
2、测量范围广:0-5V输入电压采样
3、采集频率高,精度高:采集卡的采集速率为30K/s。
ADS1256示意图
1. 时序图
STM32SPI可以直接连接,ADS1256的CS线仅仅只是做片选使用数据输出结束需要发送一次脉冲。在SCLK第一脉冲DIN开始接受数据输入,数据输入完毕的T6内SCLK需要保持低电平,然后调制一个周期的脉冲信号,数据即可全部到达DOUT总线,一次读写完毕。
核心源码:
#include <stdio.h>
#include "stm32f10x_gpio.h"
#include "ADS1256.h"
//***************************
// Pin assign
// STM32 ADS1256
// GPIOB_Pin_11 <--- DRDY
// GPIOB_Pin_12 ---> CS
// GPIOB_Pin_13 ---> SCK
// GPIOB_Pin_14(MISO) <--- DOUT
// GPIOB_Pin_15(MOSI) ---> DIN
//***************************
#define RCC_DRDY RCC_APB2Periph_GPIOB
#define PORT_DRDY GPIOB
#define PIN_DRDY GPIO_Pin_11
#define PORT_CS GPIOB
#define PIN_CS GPIO_Pin_12
#define CS_0() GPIO_ResetBits(PORT_CS, PIN_CS);
#define CS_1() GPIO_SetBits(PORT_CS, PIN_CS);
#define ADS1256_DRDY (PORT_DRDY->IDR & PIN_DRDY)
void SPI2_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/****Initial SPI2******************/
/* Enable SPI2 and GPIOB clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
/* Configure SPI2 pins: NSS, SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* SPI2 configuration */
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_256; //
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
/* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE);
}
void Init_ADS1256_GPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_DRDY, ENABLE);
GPIO_InitStructure.GPIO_Pin = PIN_DRDY;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(PORT_DRDY, &GPIO_InitStructure);
//SPI2 NSS
CS_1();
GPIO_InitStructure.GPIO_Pin = PIN_CS;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(PORT_CS, &GPIO_InitStructure);
SPI2_Init();
}
unsigned char SPI_WriteByte(unsigned char TxData)
{
unsigned char RxData=0;
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2,TxData);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET);
RxData=SPI_I2S_ReceiveData(SPI2);
return RxData;
}
void ADS1256WREG(unsigned char regaddr,unsigned char databyte)
{
CS_0();
while(ADS1256_DRDY);
SPI_WriteByte(ADS1256_CMD_WREG | (regaddr & 0x0F));
SPI_WriteByte(0x00);
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,0x08); ¡¯
ADS1256WREG(ADS1256_ADCON,ADS1256_GAIN_1);
ADS1256WREG(ADS1256_DRATE,ADS1256_DRATE_10SPS);
ADS1256WREG(ADS1256_IO,0x00);
//*************************
while(ADS1256_DRDY);
CS_0();
SPI_WriteByte(ADS1256_CMD_SELFCAL);
while(ADS1256_DRDY);
CS_1();
//**********************************
}