国产24位ADC SGM58601 (ADS1256替代型号)

       单片机型号:GD32E103    ADC芯片:SGM58601(ADS1256)

         SGM58600/SGM58601是圣邦微推出的一款24位模数转换器,其中SGM58600为2通道(1差分)采集(替代型号ADS1255),SGM58601为8通道(4差分)采集(替代型号ADS1256)。

        调试过程中很不顺利,一开始如何也不能驱动,收到数据全部是错误的。无奈网上买了一个ADS1256模块,店家给了例程(STM32F103)(时钟72M,SPI时钟36M),直接移植后还是无法驱动(GD32F405)(时钟200M,SPI时钟60M)。

        后面直接用STM32F103 开发板烧录店家程序后发现正常驱动,我就开始怀疑时钟有问题,后来降GD32F405主时钟降至25M后,将SPI时钟分频至与STM32  SPI时钟接近后,读数正常。

        新的问题出现了,调整至能驱动的时钟后,使用逻辑分析仪抓取波形发现CLK时钟仅为1K左右,速度远远不能达到要求。后调整主时钟至120M,SPI  256分频,调整采样频率后正常驱动。(这一点我也很迷惑,没有细看数据手册,正常采集后也不想管了)后将程序移植至GD32E103,下面附上代码。

#ifndef _SGM58601_H
#define _SGM58601_H
#include "gd32e10x.h"
#include "systick.h"
#include "spi.h"

#define SGM58601_DRDY_PIN		GPIO_PIN_3
#define SGM58601_DRDY			gpio_input_bit_get(GPIOA, SGM58601_DRDY_PIN)

//define commands 
#define SGM58601_CMD_WAKEUP   0x00 
#define SGM58601_CMD_RDATA    0x01 
#define SGM58601_CMD_RDATAC   0x03 
#define SGM58601_CMD_SDATAC   0x0f 
#define SGM58601_CMD_RREG     0x10 
#define SGM58601_CMD_WREG     0x50 
#define SGM58601_CMD_SELFCAL  0xf0 
#define SGM58601_CMD_SELFOCAL 0xf1 
#define SGM58601_CMD_SELFGCAL 0xf2 
#define SGM58601_CMD_SYSOCAL  0xf3 
#define SGM58601_CMD_SYSGCAL  0xf4 
#define SGM58601_CMD_SYNC     0xfc 
#define SGM58601_CMD_STANDBY  0xfd 
#define SGM58601_CMD_REST    0xfe 
 
//define the SGM58601 register values 
#define SGM58601_STATUS       0x00   
#define SGM58601_MUX          0x01   
#define SGM58601_ADCON        0x02   
#define SGM58601_DRATE        0x03   
#define SGM58601_IO           0x04   
#define SGM58601_OFC0         0x05   
#define SGM58601_OFC1         0x06   
#define SGM58601_OFC2         0x07   
#define SGM58601_FSC0         0x08   
#define SGM58601_FSC1         0x09   
#define SGM58601_FSC2         0x0A 
 
 
//define multiplexer codes 
#define SGM58601_MUXP_AIN0   0x00 
#define SGM58601_MUXP_AIN1   0x10 
#define SGM58601_MUXP_AIN2   0x20 
#define SGM58601_MUXP_AIN3   0x30 
#define SGM58601_MUXP_AIN4   0x40 
#define SGM58601_MUXP_AIN5   0x50 
#define SGM58601_MUXP_AIN6   0x60 
#define SGM58601_MUXP_AIN7   0x70 
#define SGM58601_MUXP_AINCOM 0x80 
 
#define SGM58601_MUXN_AIN0   0x00 
#define SGM58601_MUXN_AIN1   0x01 
#define SGM58601_MUXN_AIN2   0x02 
#define SGM58601_MUXN_AIN3   0x03 
#define SGM58601_MUXN_AIN4   0x04 
#define SGM58601_MUXN_AIN5   0x05 
#define SGM58601_MUXN_AIN6   0x06 
#define SGM58601_MUXN_AIN7   0x07 
#define SGM58601_MUXN_AINCOM 0x08   
 
 
//define gain codes 
#define SGM58601_GAIN_1      0x00 
#define SGM58601_GAIN_2      0x01 
#define SGM58601_GAIN_4      0x02 
#define SGM58601_GAIN_8      0x03 
#define SGM58601_GAIN_16     0x04 
#define SGM58601_GAIN_32     0x05 
#define SGM58601_GAIN_64     0x06 
//#define SGM58601_GAIN_64     0x07 
 
//define drate codes 
#define SGM58601_DRATE_30000SPS   0xF0 
#define SGM58601_DRATE_15000SPS   0xE0 
#define SGM58601_DRATE_7500SPS   0xD0 
#define SGM58601_DRATE_3750SPS   0xC0 
#define SGM58601_DRATE_2000SPS   0xB0 
#define SGM58601_DRATE_1000SPS   0xA1 
#define SGM58601_DRATE_500SPS    0x92 
#define SGM58601_DRATE_100SPS    0x82 
#define SGM58601_DRATE_60SPS     0x72 
#define SGM58601_DRATE_50SPS     0x63 
#define SGM58601_DRATE_30SPS     0x53 
#define SGM58601_DRATE_25SPS     0x43 
#define SGM58601_DRATE_15SPS     0x33 
#define SGM58601_DRATE_10SPS     0x23 
#define SGM58601_DRATE_5SPS      0x13 
#define SGM58601_DRATE_2_5SPS    0x03

// define commands 
#define SGM58601_CMD_WAKEUP   0x00 
#define SGM58601_CMD_RDATA    0x01 
#define SGM58601_CMD_RDATAC   0x03 
#define SGM58601_CMD_SDATAC   0x0f 
#define SGM58601_CMD_RREG     0x10 
#define SGM58601_CMD_WREG     0x50 
#define SGM58601_CMD_SELFCAL  0xf0 
#define SGM58601_CMD_SELFOCAL 0xf1 
#define SGM58601_CMD_SELFGCAL 0xf2 
#define SGM58601_CMD_SYSOCAL  0xf3 
#define SGM58601_CMD_SYSGCAL  0xf4 
#define SGM58601_CMD_SYNC     0xfc 
#define SGM58601_CMD_STANDBY  0xfd 
#define SGM58601_CMD_REST    0xfe 
 
//define the SGM58601 register values 
#define SGM58601_STATUS       0x00   
#define SGM58601_MUX          0x01   
#define SGM58601_ADCON        0x02   
#define SGM58601_DRATE        0x03   
#define SGM58601_IO           0x04   
#define SGM58601_OFC0         0x05   
#define SGM58601_OFC1         0x06   
#define SGM58601_OFC2         0x07   
#define SGM58601_FSC0         0x08   
#define SGM58601_FSC1         0x09   
#define SGM58601_FSC2         0x0A 
 
 
//define multiplexer codes 
#define SGM58601_MUXP_AIN0   0x00 
#define SGM58601_MUXP_AIN1   0x10 
#define SGM58601_MUXP_AIN2   0x20 
#define SGM58601_MUXP_AIN3   0x30 
#define SGM58601_MUXP_AIN4   0x40 
#define SGM58601_MUXP_AIN5   0x50 
#define SGM58601_MUXP_AIN6   0x60 
#define SGM58601_MUXP_AIN7   0x70 
#define SGM58601_MUXP_AINCOM 0x80 
 
#define SGM58601_MUXN_AIN0   0x00 
#define SGM58601_MUXN_AIN1   0x01 
#define SGM58601_MUXN_AIN2   0x02 
#define SGM58601_MUXN_AIN3   0x03 
#define SGM58601_MUXN_AIN4   0x04 
#define SGM58601_MUXN_AIN5   0x05 
#define SGM58601_MUXN_AIN6   0x06 
#define SGM58601_MUXN_AIN7   0x07 
#define SGM58601_MUXN_AINCOM 0x08   
 
 
//define gain codes 
#define SGM58601_GAIN_1      0x00 
#define SGM58601_GAIN_2      0x01 
#define SGM58601_GAIN_4      0x02 
#define SGM58601_GAIN_8      0x03 
#define SGM58601_GAIN_16     0x04 
#define SGM58601_GAIN_32     0x05 
#define SGM58601_GAIN_64     0x06 
//#define SGM58601_GAIN_64     0x07 
 
//define drate codes 
#define SGM58601_DRATE_30000SPS   0xF0 
#define SGM58601_DRATE_15000SPS   0xE0 
#define SGM58601_DRATE_7500SPS   0xD0 
#define SGM58601_DRATE_3750SPS   0xC0 
#define SGM58601_DRATE_2000SPS   0xB0 
#define SGM58601_DRATE_1000SPS   0xA1 
#define SGM58601_DRATE_500SPS    0x92 
#define SGM58601_DRATE_100SPS    0x82 
#define SGM58601_DRATE_60SPS     0x72 
#define SGM58601_DRATE_50SPS     0x63 
#define SGM58601_DRATE_30SPS     0x53 
#define SGM58601_DRATE_25SPS     0x43 
#define SGM58601_DRATE_15SPS     0x33 
#define SGM58601_DRATE_10SPS     0x23 
#define SGM58601_DRATE_5SPS      0x13 
#define SGM58601_DRATE_2_5SPS    0x03

void SGM58601_Gpio_Init(void);												//SGM58601 GPIO初始化

void SGM58601WREG(unsigned char regaddr,unsigned char databyte);			//SGM58601 写寄存器

signed int SGM58601ReadData(unsigned char channel);						//SGM58601 读数据

void SGM58601_Init(void);													//SGM58601采集初始化


int  SGM58601_Read_SingleData(unsigned char channel);						//SGM58601单端ADC读取
int  SGM58601_Read_DiffData(void);											//SGM58601差分ADC读取


#endif


#include "SGM58601.H"

void SGM58601_Gpio_Init(void)
{
	gpio_init(SGM58601_GPIO_RCU,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ, SGM58601_DRDY_PIN);
}

void SGM58601WREG(unsigned char regaddr,unsigned char databyte)			//B
{
    
	SPI_CS_ENABLE();
	while(SGM58601_DRDY);//当SGM58601_DRDY为低时才能写寄存器
	//向寄存器写入数据地址
    SPI0_ReadWriteByte(SGM58601_CMD_WREG | (regaddr & 0x0F));
    //写入数据的个数n-1
    SPI0_ReadWriteByte(0x00);
    //向regaddr地址指向的寄存器写入数据databyte
    SPI0_ReadWriteByte(databyte);
	SPI_CS_DISABLE();
}



//初始化SGM58601  //  差分采集
void SGM58601_Init(void)	//B
{
	//*************自校准****************
//  while(SGM58601_DRDY);
//	SPI_CS_ENABLE();
//	SPI0_ReadWriteByte(SGM58601_CMD_SELFCAL);
//	while(SGM58601_DRDY);
//	SPI_CS_DISABLE();
	//**********************************

	SGM58601WREG(SGM58601_STATUS,0x06);               // 高位在前、使用缓冲
//	SGM58601WREG(SGM58601_STATUS,0x04);               // 高位在前、不使用缓冲

	SGM58601WREG(SGM58601_MUX,0x08);                  // 初始化端口A0为‘+’,AINCOM位‘-’
	SGM58601WREG(SGM58601_ADCON,SGM58601_GAIN_64);                // 放大倍数1
	SGM58601WREG(SGM58601_DRATE,SGM58601_DRATE_2000SPS);  // 数据10sps
	SGM58601WREG(SGM58601_IO,0x00);               

	//*************自校准****************
//	while(SGM58601_DRDY);
//	SPI_CS_ENABLE();
//	SPI0_ReadWriteByte(SGM58601_CMD_SELFCAL);
//	while(SGM58601_DRDY);
//	SPI_CS_DISABLE(); 
	//**********************************
}

//读取单端AD值
signed int SGM58601ReadData(unsigned char channel)  //A
{
    unsigned int sum=0;
	
 	while(SGM58601_DRDY);//当SGM58601_DRDY为低时才能写寄存器 
	SGM58601WREG(SGM58601_MUX,channel);		//设置通道
	SPI_CS_ENABLE();
	SPI0_ReadWriteByte(SGM58601_CMD_SYNC);
	SPI0_ReadWriteByte(SGM58601_CMD_WAKEUP);	               
	SPI0_ReadWriteByte(SGM58601_CMD_RDATA);
   	sum |= (SPI0_ReadWriteByte(0xff) << 16);
	sum |= (SPI0_ReadWriteByte(0xff) << 8);
	sum |= SPI0_ReadWriteByte(0xff);
	SPI_CS_DISABLE();

	if (sum>0x7FFFFF)           // if MSB=1, 
	{
		sum -= 0x1000000;       // do 2's complement

	}
    return sum;
}


int SGM58601_Read_SingleData(unsigned char channel)
{
	return SGM58601ReadData( (channel << 4) | SGM58601_MUXN_AINCOM);
}

int SGM58601_Read_DiffData(void)
{
	return SGM58601ReadData( SGM58601_MUXP_AIN0 | SGM58601_MUXN_AIN1);
}











  • 11
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值