一:
最近实验室有几个项目都需要用到高分辨率AD转换,于是就开始了ADS1256的开发。
新手,焊得丑,见谅(能用就行)
二:
本以为很容易就能做完,结果被采样速率的问题困扰了很久。
代码如下,使用2.5V基准源进行测试,结果在读ADS时经常出现读出0xFFFFFF的情况,只能忍住悲伤开始查找资料,后来在TI论坛上看到一位大哥说这种情况一般是SPI没有读取到数据于是直接将ADS初始化中的10SPS改成30000SPS,结果一测试OK了。
void ADS1256_Init(void)
{
ADS1256WREG(ADS1256_STATUS,0x04);
ADS1256WREG(ADS1256_MUX,0x01);
ADS1256WREG(ADS1256_ADCON,0x00);
ADS1256WREG(ADS1256_DRATE,ADS1256_DRATE_10SPS);
ADS1256WREG(ADS1256_IO,0x00);
}
三:
可能因为ADS1256是外部ADC,数据转换速率慢于单片机时就会造成SPI上没有数据导致单片机读取不到吧,模数转换很多东西的确比较复杂,以前只注重简单的应用,还是需要以后继续学习,也请各位大哥多多指正。
最后附上代码:
ADS1256.c
#include "ADS1256.h"
#define FILTER_NUM 10
unsigned int ads_value;
unsigned int ads_flitter[FILTER_NUM+1];
long double ads_voltage;
void SPI1_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/****Initial SPI1******************/
/* Enable SPI1 and GPIOA clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
/* Configure SPI1 pins: NSS, SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//SPI1 NSS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA, GPIO_Pin_4);
/* SPI1 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(SPI1, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);
}
void Init_ADS1256_GPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI1_Init();
}
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;
}
void ADS1256WREG(unsigned char regaddr,unsigned char databyte)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));
SPI_WriteByte(ADS1256_CMD_WREG | (regaddr & 0x0F));
SPI_WriteByte(0x00);
SPI_WriteByte(databyte);
GPIO_SetBits(GPIOA, GPIO_Pin_4);
}
void ADS1256_Init(void)
{
ADS1256WREG(ADS1256_STATUS,0x04);
ADS1256WREG(ADS1256_MUX,0x01);
ADS1256WREG(ADS1256_ADCON,0x00);
ADS1256WREG(ADS1256_DRATE,ADS1256_DRATE_30000SPS);
ADS1256WREG(ADS1256_IO,0x00);
}
unsigned int ADS1256ReadData(unsigned char channel)
{
unsigned int data;
ADS1256WREG(ADS1256_MUX, channel | ADS1256_MUXN_AIN1);
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));
SPI_WriteByte(ADS1256_CMD_SYNC);
SPI_WriteByte(ADS1256_CMD_WAKEUP);
SPI_WriteByte(ADS1256_CMD_RDATA);
data |= (SPI_WriteByte(0xff)<<16);
data |= (SPI_WriteByte(0xff)<<8);
data |= SPI_WriteByte(0xff);
return data;
}
unsigned int ADS_Flitter()
{
unsigned char i=0;
ads_flitter[FILTER_NUM+1] = 0;
for(i=0;i<FILTER_NUM;i++)
{
ads_flitter[i] = ADS1256ReadData(0);
ads_flitter[FILTER_NUM+1] += ads_flitter[i];
}
ads_flitter[FILTER_NUM+1] = ads_flitter[FILTER_NUM+1]/FILTER_NUM;
ads_value = ads_flitter[FILTER_NUM+1];
return ads_value;
}
long double ADS_Voltage()
{
ads_voltage = (long double)ADS_Flitter()*5/0x7FFFFF;
return ads_voltage;
}
ADS1256.h文章来源:https://www.toymoban.com/news/detail-525544.html
#ifndef __ADS1256_H_
#define __ADS1256_H_
#include "stm32f10x.h"
#define RCC_ADS1256DRDY RCC_APB2Periph_GPIOA
#define GPIO_ADS1256DRDY_PORT GPIOA
#define GPIO_ADS1256DRDY GPIO_Pin_0
// define commands
#define ADS1256_CMD_WAKEUP 0x00
#define ADS1256_CMD_RDATA 0x01
#define ADS1256_CMD_RDATAC 0x03
#define ADS1256_CMD_SDATAC 0x0f
#define ADS1256_CMD_RREG 0x10
#define ADS1256_CMD_WREG 0x50
#define ADS1256_CMD_SELFCAL 0xf0
#define ADS1256_CMD_SELFOCAL 0xf1
#define ADS1256_CMD_SELFGCAL 0xf2
#define ADS1256_CMD_SYSOCAL 0xf3
#define ADS1256_CMD_SYSGCAL 0xf4
#define ADS1256_CMD_SYNC 0xfc
#define ADS1256_CMD_STANDBY 0xfd
#define ADS1256_CMD_REST 0xfe
// define the ADS1256 register values
#define ADS1256_STATUS 0x00
#define ADS1256_MUX 0x01
#define ADS1256_ADCON 0x02
#define ADS1256_DRATE 0x03
#define ADS1256_IO 0x04
#define ADS1256_OFC0 0x05
#define ADS1256_OFC1 0x06
#define ADS1256_OFC2 0x07
#define ADS1256_FSC0 0x08
#define ADS1256_FSC1 0x09
#define ADS1256_FSC2 0x0A
// define multiplexer codes
#define ADS1256_MUXP_AIN0 0x00
#define ADS1256_MUXP_AIN1 0x10
#define ADS1256_MUXP_AIN2 0x20
#define ADS1256_MUXP_AIN3 0x30
#define ADS1256_MUXP_AIN4 0x40
#define ADS1256_MUXP_AIN5 0x50
#define ADS1256_MUXP_AIN6 0x60
#define ADS1256_MUXP_AIN7 0x70
#define ADS1256_MUXP_AINCOM 0x80
#define ADS1256_MUXN_AIN0 0x00
#define ADS1256_MUXN_AIN1 0x01
#define ADS1256_MUXN_AIN2 0x02
#define ADS1256_MUXN_AIN3 0x03
#define ADS1256_MUXN_AIN4 0x04
#define ADS1256_MUXN_AIN5 0x05
#define ADS1256_MUXN_AIN6 0x06
#define ADS1256_MUXN_AIN7 0x07
#define ADS1256_MUXN_AINCOM 0x08
// define gain codes
#define ADS1256_GAIN_1 0x00
#define ADS1256_GAIN_2 0x01
#define ADS1256_GAIN_4 0x02
#define ADS1256_GAIN_8 0x03
#define ADS1256_GAIN_16 0x04
#define ADS1256_GAIN_32 0x05
#define ADS1256_GAIN_64 0x06
//#define ADS1256_GAIN_64 0x07
//define drate codes
#define ADS1256_DRATE_30000SPS 0xF0
#define ADS1256_DRATE_15000SPS 0xE0
#define ADS1256_DRATE_7500SPS 0xD0
#define ADS1256_DRATE_3750SPS 0xC0
#define ADS1256_DRATE_2000SPS 0xB0
#define ADS1256_DRATE_1000SPS 0xA1
#define ADS1256_DRATE_500SPS 0x92
#define ADS1256_DRATE_100SPS 0x82
#define ADS1256_DRATE_60SPS 0x72
#define ADS1256_DRATE_50SPS 0x63
#define ADS1256_DRATE_30SPS 0x53
#define ADS1256_DRATE_25SPS 0x43
#define ADS1256_DRATE_15SPS 0x33
#define ADS1256_DRATE_10SPS 0x23
#define ADS1256_DRATE_5SPS 0x13
#define ADS1256_DRATE_2_5SPS 0x03
void SPI1_Init(void);
void Init_ADS1256_GPIO(void);
unsigned char SPI_WriteByte(unsigned char TxData);
void ADS1256WREG(unsigned char regaddr,unsigned char databyte);
void ADS1256_Init(void);
unsigned int ADS1256ReadData(unsigned char channel);
unsigned int ADS_Flitter(void);
long double ADS_Voltage(void);
#endif
main.h文章来源地址https://www.toymoban.com/news/detail-525544.html
/**
******************************************************************************
* @file Project/STM32F10x_StdPeriph_Template/main.c
* @author MCD Application Team
* @version V3.5.0
* @date 08-April-2011
* @brief Main program body
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "ADS1256.h"
#include <stdio.h>
extern unsigned int ads_value;
extern long double ads_voltage;
int main(void)
{
Init_ADS1256_GPIO();
ADS1256_Init();
while (1)
{
ADS_Voltage();
}
}
到了这里,关于STM32F103硬件SPI驱动ADS1256的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!