今天把在TI申请的样片ADS1110焊接上,开始从头开始写测试程序,
一边看数据手册一边写,写完了在对照逻辑分析仪,验证自己的时序,今天太幸运了,一步到位搞定了。我用的芯片是000地址的。这个一定要注意哦。
测试了芯片数据稳定性,觉得这个芯片很完美,1倍放大15sps速度 16bit数据很稳定。
写这篇文章的目的是回报阿莫论坛的无私精神。希望更多的莫友发表自己的成果,给正在挣扎的朋友一点帮助,
下面我用的是STM32F205芯片读取的,废话少说,上代码
/**
******************************************************************************
* @File :ADS1110.c
* @Author :Flame
* @Email :zhcj66@163.com
* @Version :V1.0.0
* @FileNewDate :2013-04-20
* @FileChangeDate :2013-04-20
* @Brief :16Bit_AD,自带2.048V ±.05%的电压基准,采样率:15/30/60/240sps;放大倍数:1/2/4/8
:2013-04-20程序测试成功
******************************************************************************
*/
#include "includes.h"
#define ADS1110_SCL_CLR() GPIO_ResetBits(GPIOC,GPIO_Pin_7);
#define ADS1110_SCL_SET() GPIO_SetBits(GPIOC,GPIO_Pin_7);
#define ADS1110_SDA_CLR() GPIO_ResetBits(GPIOC,GPIO_Pin_8);
#define ADS1110_SDA_SET() GPIO_SetBits(GPIOC,GPIO_Pin_8);
#define ADS1110_SDA_Get() GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8)
#define ADS1110Addr 0x90 //ADS1110地址
#define ADS1110ReadAddr ADS1110Addr+1 //ADS1110读地址
#define ADS1110WriteAddr ADS1110Addr+0 //ADS1110写地址
#define ADS1110Reg 0x0C //连续,15sps,1倍
void ADS1110_GPIO_In(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void ADS1110_GPIO_Out(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void ADS1110_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
ADS1110_GPIO_Out();
}
void ADS1110_Delays(unsigned int count)//6us
{
uint16_t i,j;
for(i=count;i>0;i--)
for(j=12;j>0;j--);
}
void ADS_delay(void)
{
unsigned char i;
for(i=0;i<5;i++);
}
void ADS1110_Start(void)
{
ADS1110_GPIO_Out();
ADS1110_SDA_SET();
ADS1110_SCL_SET();
ADS1110_Delays(1);
ADS1110_SDA_CLR();
ADS1110_Delays(1);
ADS1110_SCL_CLR();
}
void ADS1110_Stop(void)
{
ADS1110_GPIO_Out();
ADS1110_SDA_CLR();
ADS1110_SCL_CLR();
ADS1110_Delays(1);
ADS1110_SCL_SET();
ADS1110_Delays(1);
ADS1110_SDA_SET();
ADS1110_Delays(1);
}
//等待应答信号到来 返回值:1,接收应答失败 ;0,接收应答成功
unsigned char ADS1110_Wait_Ack(void)
{
u8 ucErrTime=0;
ADS1110_GPIO_In(); //SDA设置为输入
ADS1110_SDA_SET();
ADS1110_Delays(1);
ADS1110_SCL_SET();
ADS1110_Delays(1);
while(ADS1110_SDA_Get())
{
ucErrTime++;
if(ucErrTime>250)
{
ADS1110_Stop();
return 1;
}
}
ADS1110_SCL_CLR();//时钟输出0
return 0;
}
//产生ACK应答
void ADS1110_Ack(void)
{
ADS1110_SCL_CLR();
ADS1110_GPIO_Out();
ADS1110_SDA_CLR();
ADS1110_Delays(2);
ADS1110_SCL_SET();
ADS1110_Delays(2);
ADS1110_SCL_CLR();
}
//不产生ACK应答
void ADS1110_NAck(void)
{
ADS1110_SCL_CLR();
ADS1110_GPIO_Out();
ADS1110_SDA_SET();
ADS1110_Delays(2);
ADS1110_SCL_SET();
ADS1110_Delays(2);
ADS1110_SCL_CLR();
}
//IIC发送一个字节
void ADS1110_Send_Byte(u8 txd)
{
u8 t;
ADS1110_GPIO_Out();
ADS1110_SCL_CLR();//拉低时钟开始数据传输
for(t=0;t<8;t++)
{
//IIC_SDA=(txd&0x80)>>7;
if (txd&0x80) {ADS1110_SDA_SET();}
else {ADS1110_SDA_CLR();}
txd<<=1;
ADS1110_Delays(2);
ADS1110_SCL_SET();
ADS1110_Delays(2);
ADS1110_SCL_CLR();
ADS1110_Delays(2);
}
}
//读1个字节,ack=1时,发送ACK,ack=0,发送nACK
unsigned char ADS1110_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
ADS1110_GPIO_In();//SDA设置为输入
ADS1110_SDA_SET();
for(i=0;i<8;i++ )
{
ADS1110_SCL_CLR();
ADS1110_Delays(2);
ADS1110_SCL_SET();
receive<<=1;
if(ADS1110_SDA_Get())receive++;
ADS1110_Delays(1);
}
if (!ack)
ADS1110_NAck();//发送nACK
else
ADS1110_Ack(); //发送ACK
return receive;
}
void ADS_WriteCfgReg(unsigned char CfgData) //Write ADS1110 Congiguration Register
{
ADS1110_Start();
ADS1110_Send_Byte(ADS1110WriteAddr);
ADS1110_Wait_Ack();
ADS1110_Send_Byte(CfgData);
ADS1110_Wait_Ack();
ADS1110_Stop();
}
unsigned int ReadDataCfgReg(unsigned int ReadType) //Read data from ADS1110 Data register ReadType=0x01返回AD值,ReadType=0x00返回寄存器值
{
unsigned int temp,temp1;
ADS1110_Start();
ADS1110_Send_Byte(ADS1110ReadAddr);
ADS1110_Wait_Ack();
temp=ADS1110_Read_Byte(1)<<8;
ADS1110_Ack();
temp+=(ADS1110_Read_Byte(1));
ADS1110_Ack();
temp1 = ADS1110_Read_Byte(1);
ADS1110_Ack();
ADS1110_Stop();
if(ReadType == 0x01)//返回AD值
{
return(temp);
}
else return(temp1);//返回寄存器值
}
void ADS1110_Configuration(void)
{
ADS1110_GPIO_Configuration();
ADS_WriteCfgReg(ADS1110Reg);
}
void GetADS1110Data(void)
{
unsigned char Temp[8];
unsigned int Temp24;
Temp24 = ReadDataCfgReg(1);
Temp[0] = Temp24/10000%10+0x30;
Temp[1] = Temp24/1000%10+0x30;
Temp[2] = Temp24/100%10+0x30;
Temp[3] = Temp24/10%10+0x30;
Temp[4] = Temp24%10+0x30;
Temp[5] = 0x0D;
Temp[6] = 0x0A;
Temp[7] = 0;
USART1_Puts(Temp);
}转载
|