1,初始化
接口:
/********************************************
SPI2 STM32 ADS1256
NSS -- PB12 -- CS
SCK -- PB13 -- SCLK
MISO -- PB14 -- DOUT
MOSI -- PB15 -- DIN
PB10 -- DRDY
PB11 -- RESET
********************************************/
2,ADS_sum(a,b):函数功能,测量电压a-b
ADS1256_MUXP_AIN(0~7)代表通道0~7,差分正 P:positive
ADS1256_MUXN_AIN(0~7)代表通道0~7,差分负 N:negative
例:差分测量通道0和1:ADS_sum(ADS1256_MUXP_AIN0 | ADS1256_MUXN_AIN1); 返回通道0-通道1的电压值
单端测量通道0: ADS_sum(ADS1256_MUXP_AIN0 | ADS1256_MUXN_AINCOM); 返回通道0对地的电压值
#include
#include "stm32f10x_gpio.h"
#include "ADS1256.h"
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 NSS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
/* 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);
}
//ADS1256 GPIO
void Init_ADS1256_GPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_ADS1256Reset | RCC_ADS1256DRDY, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_RCC_ADS1256Reset;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_RCC_ADS1256Reset_PORT, &GPIO_InitStructure);
GPIO_ResetBits(GPIO_RCC_ADS1256Reset_PORT, GPIO_RCC_ADS1256Reset );
GPIO_InitStructure.GPIO_Pin = GPIO_ADS1256DRDY;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_ADS1256DRDY_PORT, &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)
{
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));
SPI_WriteByte(ADS1256_CMD_WREG | (regaddr & 0x0F));
SPI_WriteByte(0x00);
SPI_WriteByte(databyte);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
//ADS1256
void ADS1256_Init(void)
{
ADS1256WREG(ADS1256_STATUS,0x06);
// ADS1256WREG(ADS1256_MUX,0x08);
ADS1256WREG(ADS1256_ADCON,0x00);
ADS1256WREG(ADS1256_DRATE,ADS1256_DRATE_10SPS);
ADS1256WREG(ADS1256_IO,0x00);
}
unsigned int ADS1256ReadData()
{
unsigned char i=0;
unsigned int sum=0;
unsigned int r=0;
GPIO_ResetBits(GPIOB, GPIO_Pin_12);;
while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));
// ADS1256WREG(ADS1256_MUX,channel);
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);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
return sum;
}
unsigned int ADS_sum(unsigned char channel)
{
ADS1256WREG(ADS1256_MUX,channel);
return ADS1256ReadData();
}
2主函数
int main(void)
{
unsigned char i=0;
long ulResult;
long double ldVolutage;
uart_init(9600);
Init_ADS1256_GPIO();
ADS1256_Init();
while(1)
{
for(i = 0;i < 8;i++)
{
//ulResult = ADS_sum( (i << 4) | ADS1256_MUXN_AINCOM); //8路测量时放开
ulResult = ADS_sum( ADS1256_MUXP_AIN0 | ADS1256_MUXN_AINCOM); //单测0路
if( ulResult & 0x800000 )
{
ulResult = ~(unsigned long)ulResult;
ulResult &= 0x7fffff;
ulResult += 1;
ulResult = -ulResult;
}
ldVolutage = (long double)ulResult*0.59604644775390625;
// printf("第%d路:",(i & 0x07)?(i & 0x07) - 1:7);
printf("第1路");
printf("%lf",ldVolutage);
printf("uV\r\n");
delay_ms(1000);
//printf("%x",(unsigned long)ulResult);//16
Delay(0x3fFFF);
}
}
}
3.中值滤波
#define MEDIAN_LEN 5 //中直滤波的总长度,一般选取奇数
#define MEDIAN 2 //中值在滤波数组中的位置
unsigned long AD_buffer[MEDIAN_LEN]; //ad采集数组缓存
//unsigned long MED_buffer[MEDIAN_LEN]; //中值滤波数组缓存
unsigned char medleng = 0; //存入缓存的数据个数
/**********************************************************/
// 函数名:中值滤波函数
// 描述 :提取前9次采集的数据,去掉高3个,去掉低3个,然后中间的
// 描述 :3个数据求平均值,该算法可尽可能的滤掉干扰数据,并不影响采集速度。
// 输入 :9个数据的数组
// 输出 :中间3个数据的平均值
/*********************************************************/
unsigned long medina_filter(unsigned long *MED_buffer) //xad - ADC转换值
{
unsigned char i,j;
unsigned long xd;
u32 xxd;
for(i = 0; i < MEDIAN_LEN; i ++)
{
for(j = 0; j < MEDIAN_LEN - i; j ++)
{
if( MED_buffer[i] > MED_buffer[i + 1]) // 轮询到的当前元素>AD值,则交换它们的值
{ xd = MED_buffer[i]; MED_buffer[i] = MED_buffer[i + 1]; MED_buffer[i + 1] = xd;}
}
}
xxd = MED_buffer[MEDIAN - 1] + MED_buffer[MEDIAN] + MED_buffer[MEDIAN + 1];
xd = xxd/3;
return xd; //中值
}