久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费
標(biāo)題:
國(guó)產(chǎn)8通道24位ADC SGM58601(替代ADS1256)GD32單片機(jī)驅(qū)動(dòng)程序調(diào)試
[打印本頁(yè)]
作者:
弓長(zhǎng)張
時(shí)間:
2024-1-19 20:49
標(biāo)題:
國(guó)產(chǎn)8通道24位ADC SGM58601(替代ADS1256)GD32單片機(jī)驅(qū)動(dòng)程序調(diào)試
調(diào)試過(guò)程中遇到的問(wèn)題:
最大的問(wèn)題,使用SPI驅(qū)動(dòng)時(shí)讀數(shù)為0 -1 或者其他很大的數(shù)(都是不對(duì)的數(shù)),經(jīng)過(guò)反復(fù)驗(yàn)證后發(fā)現(xiàn)與SPI時(shí)鐘有關(guān),參考STM32 驅(qū)動(dòng)ADS1256程序 硬件SPI 時(shí)鐘分頻為256,燒錄后驅(qū)動(dòng)正常,修改分頻系數(shù)后,讀數(shù)異常。但是256分頻后 時(shí)鐘頻率約1.幾KHz 速度太慢。有反復(fù)調(diào)試了一會(huì),發(fā)現(xiàn)SPI時(shí)鐘速度 ,ADC采樣頻率都可能會(huì)導(dǎo)致讀數(shù)錯(cuò)誤,具體問(wèn)題沒(méi)有細(xì)究。經(jīng)過(guò)調(diào)試后可以使用該程序驅(qū)動(dòng)SGM58601。下面附上代碼,單片機(jī)為GD32E103。
單片機(jī)源程序如下:
#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 SGM58601AWREG(unsigned char regaddr,unsigned char databyte); //SGM58601A 寫(xiě)寄存器
void SGM58601BWREG(unsigned char regaddr,unsigned char databyte); //SGM58601B 寫(xiě)寄存器
signed int SGM58601AReadData(unsigned char channel); //SGM58601 讀數(shù)據(jù)
signed int SGM58601BReadData(unsigned char channel); //SGM58601 讀數(shù)據(jù)
void SGM58601A_Init(void); //SGM58601A 單端采集初始化
void SGM58601B_Init(void); //SGM58601B 差分采集初始化
int SGM58601_Read_SingleData(unsigned char channel); //SGM58601A 單端ADC讀取
int SGM58601_Read_DiffData(void); //SGM58601B 差分ADC讀取
#endif
復(fù)制代碼
#include "SGM58601.H"
void SGM58601_Gpio_Init(void)
{
gpio_init(SGM58601_GPIO_RCU,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ, SGM58601_DRDY_PIN);
}
void SGM58601AWREG(unsigned char regaddr,unsigned char databyte) //A
{
SPI_CS1_ENABLE();
while(SGM58601_DRDY);//當(dāng)SGM58601_DRDY為低時(shí)才能寫(xiě)寄存器
//向寄存器寫(xiě)入數(shù)據(jù)地址
SPI0_ReadWriteByte(SGM58601_CMD_WREG | (regaddr & 0x0F));
//寫(xiě)入數(shù)據(jù)的個(gè)數(shù)n-1
SPI0_ReadWriteByte(0x00);
//向regaddr地址指向的寄存器寫(xiě)入數(shù)據(jù)databyte
SPI0_ReadWriteByte(databyte);
SPI_CS1_DISABLE();
}
void SGM58601BWREG(unsigned char regaddr,unsigned char databyte) //B
{
SPI_CS2_ENABLE();
while(SGM58601_DRDY);//當(dāng)SGM58601_DRDY為低時(shí)才能寫(xiě)寄存器
//向寄存器寫(xiě)入數(shù)據(jù)地址
SPI0_ReadWriteByte(SGM58601_CMD_WREG | (regaddr & 0x0F));
//寫(xiě)入數(shù)據(jù)的個(gè)數(shù)n-1
SPI0_ReadWriteByte(0x00);
//向regaddr地址指向的寄存器寫(xiě)入數(shù)據(jù)databyte
SPI0_ReadWriteByte(databyte);
SPI_CS2_DISABLE();
}
//初始化SGM58601 // 單端采集
void SGM58601A_Init(void) //A
{
//*************自校準(zhǔn)****************
// while(SGM58601_DRDY);
// SPI_CS1_ENABLE();
// SPI0_ReadWriteByte(SGM58601_CMD_SELFCAL);
// while(SGM58601_DRDY);
// SPI_CS1_DISABLE();
//**********************************
SGM58601AWREG(SGM58601_STATUS,0x06); // 高位在前、使用緩沖
// SGM58601AWREG(SGM58601_STATUS,0x04); // 高位在前、不使用緩沖
// SGM58601AWREG(SGM58601_MUX,0x08); // 初始化端口A0為‘+’,AINCOM位‘-’
SGM58601AWREG(SGM58601_ADCON,SGM58601_GAIN_1); // 放大倍數(shù)1
SGM58601AWREG(SGM58601_DRATE,SGM58601_DRATE_7500SPS); // 數(shù)據(jù)10sps
SGM58601AWREG(SGM58601_IO,0x00);
//*************自校準(zhǔn)****************
// while(SGM58601_DRDY);
// SPI_CS1_ENABLE();
// SPI0_ReadWriteByte(SGM58601_CMD_SELFCAL);
// while(SGM58601_DRDY);
// SPI_CS1_DISABLE();
//**********************************
}
//初始化SGM58601 // 差分采集
void SGM58601B_Init(void) //B
{
//*************自校準(zhǔn)****************
// while(SGM58601_DRDY);
// SPI_CS2_ENABLE();
// SPI0_ReadWriteByte(SGM58601_CMD_SELFCAL);
// while(SGM58601_DRDY);
// SPI_CS2_DISABLE();
//**********************************
SGM58601BWREG(SGM58601_STATUS,0x06); // 高位在前、使用緩沖
// SGM58601BWREG(SGM58601_STATUS,0x04); // 高位在前、不使用緩沖
SGM58601BWREG(SGM58601_MUX,0x08); // 初始化端口A0為‘+’,AINCOM位‘-’
SGM58601BWREG(SGM58601_ADCON,SGM58601_GAIN_64); // 放大倍數(shù)1
SGM58601BWREG(SGM58601_DRATE,SGM58601_DRATE_2000SPS); // 數(shù)據(jù)10sps
SGM58601BWREG(SGM58601_IO,0x00);
//*************自校準(zhǔn)****************
// while(SGM58601_DRDY);
// SPI_CS2_ENABLE();
// SPI0_ReadWriteByte(SGM58601_CMD_SELFCAL);
// while(SGM58601_DRDY);
// SPI_CS2_DISABLE();
//**********************************
}
//讀取單端AD值
signed int SGM58601AReadData(unsigned char channel) //A
{
unsigned int sum=0;
while(SGM58601_DRDY);//當(dāng)SGM58601_DRDY為低時(shí)才能寫(xiě)寄存器
SGM58601AWREG(SGM58601_MUX,channel); //設(shè)置通道
SPI_CS1_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_CS1_DISABLE();
if (sum>0x7FFFFF) // if MSB=1,
{
sum -= 0x1000000; // do 2's complement
}
return sum;
}
signed int SGM58601BReadData(unsigned char channel) //B
{
unsigned int sum=0;
while(SGM58601_DRDY);//當(dāng)SGM58601_DRDY為低時(shí)才能寫(xiě)寄存器
SGM58601BWREG(SGM58601_MUX,channel); //設(shè)置通道
SPI_CS2_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_CS2_DISABLE();
if (sum>0x7FFFFF) // if MSB=1,
{
sum -= 0x1000000; // do 2's complement
}
return sum;
}
int SGM58601_Read_SingleData(unsigned char channel)
{
return SGM58601AReadData( (channel << 4) | SGM58601_MUXN_AINCOM);
}
int SGM58601_Read_DiffData(void)
{
return SGM58601BReadData( SGM58601_MUXP_AIN0 | SGM58601_MUXN_AIN1);
}
復(fù)制代碼
GD32_SGM58601.7z
2024-1-19 22:16 上傳
點(diǎn)擊文件名下載附件
202.25 KB, 下載次數(shù): 12
作者:
人工置頂員
時(shí)間:
2024-1-20 15:06
頂一下
歡迎光臨 (http://www.zg4o1577.cn/bbs/)
Powered by Discuz! X3.1
主站蜘蛛池模板:
日韩精品网站
|
欧美成人免费在线视频
|
欧美精品一区二区三区在线
|
久久免费小视频
|
99精品久久久
|
精品国产一区二区三区在线观看
|
日中文字幕在线
|
日韩视频在线免费观看
|
中文字幕在线观看一区
|
91香蕉视频在线观看
|
av在线免费网
|
国产欧美精品一区二区三区
|
91精品国产乱码久久久
|
日韩无
|
午夜欧美一区二区三区在线播放
|
97精品一区二区
|
91久久久久
|
在线一区观看
|
亚洲精品欧洲
|
高清色视频
|
福利视频网址
|
日本高清精品
|
一区二区三区四区在线视频
|
福利片在线
|
三级在线视频
|
性国产xxxx乳高跟
|
在线播放第一页
|
亚洲精品二区
|
国产精品成人久久久久
|
欧美 日韩 中文
|
中文字幕成人av
|
精品视频一区二区
|
久久亚洲春色中文字幕久久久
|
小h片免费观看久久久久
|
国产高清91
|
中文字幕第一页在线
|
91最新视频
|
国产精品视频久久久久
|
国产最好的av国产大片
|
狠狠骚
|
亚洲伊人a
|