|
调试过程中遇到的问题:
最大的问题,使用SPI驱动时读数为0 -1 或者其他很大的数(都是不对的数),经过反复验证后发现与SPI时钟有关,参考STM32 驱动ADS1256程序 硬件SPI 时钟分频为256,烧录后驱动正常,修改分频系数后,读数异常。但是256分频后 时钟频率约1.几KHz 速度太慢。有反复调试了一会,发现SPI时钟速度 ,ADC采样频率都可能会导致读数错误,具体问题没有细究。经过调试后可以使用该程序驱动SGM58601。下面附上代码,单片机为GD32E103。
单片机源程序如下:
- #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);//当SGM58601_DRDY为低时才能写寄存器
- //向寄存器写入数据地址
- SPI0_ReadWriteByte(SGM58601_CMD_WREG | (regaddr & 0x0F));
- //写入数据的个数n-1
- SPI0_ReadWriteByte(0x00);
- //向regaddr地址指向的寄存器写入数据databyte
- SPI0_ReadWriteByte(databyte);
- SPI_CS1_DISABLE();
- }
- void SGM58601BWREG(unsigned char regaddr,unsigned char databyte) //B
- {
-
- SPI_CS2_ENABLE();
- while(SGM58601_DRDY);//当SGM58601_DRDY为低时才能写寄存器
- //向寄存器写入数据地址
- SPI0_ReadWriteByte(SGM58601_CMD_WREG | (regaddr & 0x0F));
- //写入数据的个数n-1
- SPI0_ReadWriteByte(0x00);
- //向regaddr地址指向的寄存器写入数据databyte
- SPI0_ReadWriteByte(databyte);
- SPI_CS2_DISABLE();
- }
- //初始化SGM58601 // 单端采集
- void SGM58601A_Init(void) //A
- {
- //*************自校准****************
- // 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); // 放大倍数1
- SGM58601AWREG(SGM58601_DRATE,SGM58601_DRATE_7500SPS); // 数据10sps
- SGM58601AWREG(SGM58601_IO,0x00);
- //*************自校准****************
- // while(SGM58601_DRDY);
- // SPI_CS1_ENABLE();
- // SPI0_ReadWriteByte(SGM58601_CMD_SELFCAL);
- // while(SGM58601_DRDY);
- // SPI_CS1_DISABLE();
- //**********************************
- }
- //初始化SGM58601 // 差分采集
- void SGM58601B_Init(void) //B
- {
- //*************自校准****************
- // 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); // 放大倍数1
- SGM58601BWREG(SGM58601_DRATE,SGM58601_DRATE_2000SPS); // 数据10sps
- SGM58601BWREG(SGM58601_IO,0x00);
- //*************自校准****************
- // 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);//当SGM58601_DRDY为低时才能写寄存器
- SGM58601AWREG(SGM58601_MUX,channel); //设置通道
- 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);//当SGM58601_DRDY为低时才能写寄存器
- SGM58601BWREG(SGM58601_MUX,channel); //设置通道
- 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);
- }
复制代码 |
|