程序如下,我直接用LCD12864显示转换的结果
//main.c
#include "sys.h"
#include "usart.h"
#include "ads1118.h"
#include "lcd12864.h"
#include "delay.h"
int main(void)
{
ADS1118_Init();
LCD12864_Init();
delay_init(72);
while(1)
{
ADS1118_WriteBytes(0x85FB);
while(DOUT_Set);
LCD12864_Display_Integer(0,0,ADS1118_ReadBytes(),"7×8");
} }
//ads1118.c
#include "ADS1118.h"
#include "stm32f10x.h"
#include "delay.h"
void ADS1118_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(ADS1118_CLOCK, ENABLE);
GPIO_InitStructure.GPIO_Pin = Pin_ADS1118_CS;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIO_ADS1118_CS, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Pin_ADS1118_SCLK;
GPIO_Init(GPIO_ADS1118_SCLK, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = Pin_ADS1118_DIN;
GPIO_Init(GPIO_ADS1118_DIN, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = Pin_ADS1118_DOUT;
GPIO_Init(GPIO_ADS1118_DOUT, &GPIO_InitStructure);
ADS1118_CS_1();
ADS1118_SCLK_1();
ADS1118_DIN_1();
}
void ADS1118_WriteBytes( u16 data)
{
u8 i = 16;
ADS1118_CS_0();
ADS1118_SCLK_1();
while(i–)
{
delay_us(10);
if(data&0x8000)
ADS1118_DIN_1();
else
ADS1118_DIN_0();
ADS1118_SCLK_0();
data <<= 1;
delay_us(10);
ADS1118_SCLK_1();
}
ADS1118_SCLK_1();
}
u16 ADS1118_ReadBytes( void )
{
u8 i = 16;
u16 data;
// if(DOUT_Set)
// return 0;
ADS1118_SCLK_0();
while(i–)
{
delay_us(10);
ADS1118_SCLK_1();
if(DOUT_Set)
data |= 1;
else data &= 0;
data <<= 1;
delay_us(10);
ADS1118_SCLK_0();
}
return data;
}
//ads1118.h
#ifndef _ADS1118_H
#define _ADS1118_H
#include "stm32f10x.h"
#define GPIO_ADS1118_CS GPIOA
#define GPIO_ADS1118_SCLK GPIOA
#define GPIO_ADS1118_DIN GPIOA
#define GPIO_ADS1118_DOUT GPIOA
#define Pin_ADS1118_CS GPIO_Pin_3
#define Pin_ADS1118_SCLK GPIO_Pin_0
#define Pin_ADS1118_DIN GPIO_Pin_1
#define Pin_ADS1118_DOUT GPIO_Pin_2
#define ADS1118_CLOCK (RCC_APB2Periph_GPIOA)
#define ADS1118_CS_1() GPIO_SetBits(GPIO_ADS1118_CS,Pin_ADS1118_CS)
#define ADS1118_CS_0() GPIO_ResetBits(GPIO_ADS1118_CS,Pin_ADS1118_CS)
#define ADS1118_SCLK_1() GPIO_SetBits(GPIO_ADS1118_SCLK,Pin_ADS1118_SCLK)
#define ADS1118_SCLK_0() GPIO_ResetBits(GPIO_ADS1118_SCLK,Pin_ADS1118_SCLK)
#define ADS1118_DIN_1() GPIO_SetBits(GPIO_ADS1118_DIN,Pin_ADS1118_DIN)
#define ADS1118_DIN_0() GPIO_ResetBits(GPIO_ADS1118_DIN,Pin_ADS1118_DIN)
#define ADS1118_DOUT_1() GPIO_SetBits(GPIO_ADS1118_DOUT,Pin_ADS1118_DOUT)
#define ADS1118_DOUT_0() GPIO_ResetBits(GPIO_ADS1118_DOUT,Pin_ADS1118_DOUT)
#define DOUT_Reset GPIO_ReadInputDataBit(GPIO_ADS1118_DOUT,Pin_ADS1118_DOUT)==RESET
#define DOUT_Set GPIO_ReadInputDataBit(GPIO_ADS1118_DOUT,Pin_ADS1118_DOUT)==SET
#define SingleSart 1<<15
#define FS_6114 0<<9
#define FS_4096 1<<9
#define FS_2048 2<<9
#define FS_1024 3<<9
#define FS_0512 4<<9
#define FS_0256 5<<9
#define ConMode 0<<8
#define SinMode 1<<8
#define Rate_8SPS 0<<5
#define Rate_16SPS 1<<5
#define Rate_32SPS 2<<5
#define Rate_64SPS 3<<5
#define Rate_128SPS 4<<5
#define Rate_250SPS 5<<5
#define Rate_475SPS 6<<5
#define Rate_860SPS 7<<5
#define TemMode 1<<4
#define ADCMode 0<<4
#define EN_Pullup 1<<3
#define Dis_Pullup 0<<3
#define Write_Config 1<<1
void ADS1118_WriteBytes( u16 data);
void ADS1118_Init(void);
u16 ADS1118_ReadBytes( void );
#endif
FENGSHENG XIA:
在main函数中
int main(void){ ADS1118_Init(); LCD12864_Init(); delay_init(72); while(1) { ADS1118_WriteBytes(0x85FB); while(DOUT_Set); LCD12864_Display_Integer(0,0,ADS1118_ReadBytes(),"7×8"); } }
加入 while(DOUT_Set); 发现直接卡住了
如果去掉 while(DOUT_Set); 用延时200ms来默认已经转换完成
发现输出结果很不稳定,是不是我软件的读写程序写错了?
FENGSHENG XIA:
在main函数中,我如果一直等待DOUT的低电平,程序就会一直在等待
如果用延时200ms来默认转换完成,那么DOUT的输出值就很不稳定
是我SPI时序写错了吗?
SurfaceBook:
有可能是你对ADS1118设置不正确导致的DOUT输出不稳定。
user3917511:
回复 FENGSHENG XIA:
楼主有没有好好审阅下你的SPI时序呢?根据datasheet时序图来检查下。。
JiaChuang Chen:
STM32内部不是硬件集成 的有SPI协议嘛?为什么还要用IO口模拟?
使用硬件SPI比IO口模拟的可靠性要高,你使用硬件SPI再试试会不会有这个问题?
WenJie Chen1:
最近也在弄这个ADS1118,可以加个QQ交流一下吗? 2265251083,qq。
Weixiong Yu:
你好,作者,我最近也在试着用32IO软件方式模拟出spi来与ads1118通信,借鉴你的资料,觉得很有收获,但我对你的代码仍有几处看不太清,想问问你。
例如,在子函数u16 ADS1118_ReadBytes( void )中的
“ // if(DOUT_Set) // return 0; ” ,这是备注吗?还是它本身也是代码一部分