|
楼主 |
发表于 2006-7-6 10:31:53
|
显示全部楼层
经过多次实验,终于搞定了ADS1110的读写操作,到这里跟大家分享,
希望与各位朋友常沟通与交流,我的MSN: prmtwujingbin@hotmail.com
//ADS1110 相关操作函数
//P5.0-- AD_SDA
//P5.1-- AD_SCL
void ADS_start(void);
void ADS_delay(void);
void ADS_stop(void);
void ADS_clock(void); //Generate a ACK
void ADS_ack(void); //The master send a ACK to device
void ADS_RcvAck(void); //receive device sending a ack
void ADS_nack(void); //Generate a NACK
void ADS_waitack(void); //Wating for a ACK from device
void ADS_outbyte(unsigned char); //Send one uusigned char data to Bus
unsigned char ADS_inbyte(void); //Read a byte Data from bus
void ADS_WriteCfgReg(unsigned char CfgData); //Write ADS1110 Congiguration Register
unsigned int ReadDataCfgReg(unsigned int ReadType); //Read data from ADS1110 Data register
void ADS_delay(void)
{
unsigned char i;
for(i=0;i<5;i++);
}
void ADS_clock(void) //Generate a ACK
{
//P5.0-- AD_SDA
//P5.1-- AD_SCL
P5OUT |= BIT1;
ADS_delay();
P5OUT &= ~BIT1;
ADS_delay();
}
void ADS_ack(void) //The master send a ACK to device
{
ADS_delay();
P5DIR |= BIT0;
P5OUT &= ~BIT0;
ADS_delay();
P5OUT |= BIT1;
ADS_delay();
P5OUT &= ~BIT1;
P5DIR &= ~BIT0;
}
void ADS_RcvAck(void) //receive device sending a ack
{
P5DIR &= ~BIT0;
ADS_delay();
P5OUT |= BIT1;
ADS_delay();
P5OUT &= ~BIT1;
P5DIR &= ~BIT0;
}
void ADS_nack(void) //Generate a NACK
{
P5DIR |= BIT0;
P5OUT |= BIT0;
ADS_delay();
P5OUT |= BIT1;
ADS_delay();
P5OUT &= ~BIT1;
P5DIR &= ~BIT0;
}
void ADS_waitack(void) //Wating for a ACK from device
{
//P5.0-- AD_SDA
//P5.1-- AD_SCL
unsigned int i;
P5DIR &= ~BIT0;
ADS_delay();
P5OUT |= BIT1;
ADS_delay();
i=0X0FF;
do
{
}
while(--i!=0);
P5OUT &= ~BIT1;
P5DIR |= BIT0;
}
void ADS_outbyte(unsigned char adsdata) //Send one uusigned char data to Bus
{
unsigned char i;
P5DIR |= BIT0;
for(i=0;i<8;i++)
{
if(adsdata & 0x80)
P5OUT |= BIT0;
else
P5OUT &= ~BIT0;
ADS_clock();
adsdata <<=1;
}
}
unsigned char ADS_inbyte(void) //Read a byte Data from bus
{
unsigned char i,adsdata;//,tempin;
P5DIR &= ~BIT0;
adsdata=0;
for(i=0;i<8;i++)
{
adsdata <<=1;
if(P5IN & BIT0)
adsdata |= 0X01;
ADS_clock();
}
P5OUT &= ~BIT0;
return(adsdata);
}
void ADS_WriteCfgReg(unsigned char CfgData) //Write ADS1110 Congiguration Register
{
ADS_start();
ADS_outbyte(0x92);
ADS_waitack();
ADS_outbyte(CfgData);
ADS_waitack();
ADS_stop();
}
unsigned int ReadDataCfgReg(unsigned int ReadType) //Read data from ADS1110 Data register
{
unsigned int temp,temp1;
ADS_start();
ADS_outbyte(0x93);
ADS_waitack();
temp=ADS_inbyte()*0x100;
ADS_ack();
temp+=(ADS_inbyte());
ADS_ack();
temp1 = ADS_inbyte();
ADS_ack();
ADS_stop();
if(ReadType == 0x01)
{
if(temp > 0x7fff)
{
temp = 0x0000;
return(temp);
}
else
return(temp);
}
else
return(temp1);
} |
|