微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 硬件电路设计 > TI模拟硬件电路设计 > 用stc121c5a60s2 的ads1118一直显示错误数据

用stc121c5a60s2 的ads1118一直显示错误数据

时间:10-02 整理:3721RD 点击:

#include "ads1118.h"

void delay_ms(uint m)
{
uint i, j,k;
for(i=m;i>0;i--)
for(k=6;k>0;k--)
for(j=140;j>0;j--);
}
void delay100us() // 11.0592MHz ?100us
{
uint j;
for(j=78;j>0;j--)
_nop_();
}
void ADS1118_Init(void)
{
CS_H;
SCLK_H;
MOSI_L;
}
void delay10us() // 11.0592MHz ?10us
{
uint j;
for(j=1;j>0;j--)
_nop_();
}
union aa{
uint dat1[2];
ulong dat2;
};
uint Write_ADS1118(uint dat,uchar mode)
{
// uint temp;
union aa temp;
union aa temp1;
// ulong Value;
uchar i;
if(mode == 1)dat |= 0x8000;
temp.dat1[1] = dat;
temp.dat1[0] = dat;
SCLK_L;
CS_L;
delay_ms(1);delay_ms(1);
for(i=0;i<32;i++)
{
if(temp.dat2 & 0x80000000)MOSI_H;
else MOSI_L;
temp.dat2 <<= 1;
delay100us();
SCLK_H;
delay100us();
SCLK_L;
delay100us();

temp1.dat2 <<= 1;delay10us();
if(READ_MISO)temp1.dat2 |= 0x01;

}
CS_H;
SCLK_L;
MOSI_L;
MISO_L;
printf("temp %d\n\r",temp1.dat1[0]);
printf("temp %#X\n\r",temp1.dat1[1]);
return temp1.dat1[0];
}

/*******************************************************************************
//oˉêyÃû3Æ£oADS_SEL_Read£¨£©
//oˉêy1|Äü£o¶á衸÷·μçÑ1£¬í¨1yὸöswitchÑ¡Ôñ¶áè¡2»í¬μÄí¨μà
//êä èë£oroad:Ôöòæ·Å′óÆ÷ὶËμÄμçÑ1Ñ¡Ôñ£¬2¢Ñ¡Ôñ2a¼¸Â·μçÑ1
// Ref: Ñ¡Ôñ2ο¼μçÑ1£¬óD6ÖÖÑ¡Ôñ
//êä 3ö£odat£o16λad×a»»êy¾Y
//±¸ ×¢£oÕaò»′ζá3öμÄ×a»»êy¾YêÇéÏò»′ÎμÄ×a»»êy¾Y£¬2»òa»ìÏy.ÕaàïÑ¡ÔñμÄêÇμ¥′Î
×a»»μçÑ1Ö죬μ±è»£¬ò2¿éòÔÑ¡Ôñ¶à′Î×a»»,í¨1y¼Ä′æÆ÷μÄμú8λ¿éòÔéèÖÃ
//èÕ Æú£o2013.6.8
*******************************************************************************/
int ADS_SEL_Read(uchar road,uchar Ref) //2a¼¸Â·μçÑ1
{
int dat = 0;
uint Config_Value = 0x008b; //ĬèÏμí8룬DOUT′øéÏà-μç×è
switch(road)
{
case 0: Config_Value += 0x0000;break; //AINP = AIN0 and AINN = AIN1 (default)
case 1: Config_Value += 0x1000;break; //AINP = AIN0 and AINN = AIN3
case 2: Config_Value += 0x2000;break; //AINP = AIN1 and AINN = AIN3
case 3: Config_Value += 0x3000;break; //AINP = AIN2 and AINN = AIN3
case 4: Config_Value += 0x4000;break; //AINP = AIN0 and AINN = GND
case 5: Config_Value += 0x5000;break; //AINP = AIN1 and AINN = GND
case 6: Config_Value += 0x6000;break; //AINP = AIN2 and AINN = GND
case 7: Config_Value += 0x7000;break; //AINP = AIN3 and AINN = GND
default : break;
}
switch(Ref)
{
case 0: Config_Value += 0x0000;break; //000 : FS = ¡à6.144V(1)
case 1: Config_Value += 0x0200;break; //001 : FS = ¡à4.096V(1)
case 2: Config_Value += 0x0400;break; //002 : FS = ¡à2.048V(1)
case 3: Config_Value += 0x0600;break; //003 : FS = ¡à1.024V(1)
case 4: Config_Value += 0x0800;break; //004 : FS = ¡à0.512V(1)
case 5: case 6: case 7: Config_Value += 0x0a00;break; //005 : FS = ¡à0.256V(1)
default : break;
}
CS_L;
dat = Write_ADS1118(Config_Value,1);
delay100us();
CS_H;
return dat;
}

无论有没有输入始终是这个问题,是时序问题,可时序到底哪里错了呀??

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top