AD7746 STM32 测电容值

2019-03-23 16:21发布

IIC通信时,SDL SDA一直处于高电平,没有出现高低电平的变化,这个怎么解决??下边是AD7746的配置,求指教
void IIC_StartBit(void)
{
RCC->APB1ENR|=(unsigned int)1<<3;
GPIOB->MODER&=0X0FFFFFFF;
GPIOB->MODER|=(unsigned int)8<<28;
// SDA_OUT;//enable SDATA pin as an output
IIC_Flag = 0;
AD7746_SDAout=1;

AD7746_SCLK = 1;

AD7746_SDAout  = 0;//low O/P on SDATA
delay(10);//delay 5 Machine cycles
AD7746_SCLK = 0;//start bit

}
//&sup2;&Ugrave;×÷IIC&Ecirc;±&ETH;ò&Ecirc;±&micro;&Auml;&Iacute;&pound;&Ouml;&sup1;&Ecirc;±&Acirc;&szlig;&frac14; &sup2;&Ugrave;×÷
void IIC_StopBit(void)
{
RCC->APB1ENR|=(unsigned int)1<<3;
GPIOB->MODER&=0X0FFFFFFF;
GPIOB->MODER|=(unsigned int)8<<28;
// SDA_OUT;//to enable SDATA pin as an output
AD7746_SDAout  = 0;//get SDATA ready for stop

AD7746_SCLK = 1;//set clock for stop
delay(20);
AD7746_SDAout  = 1;//this is the stop bit


}
//&Iuml;ò&frac14;&Auml;&acute;&aelig;&AElig;÷·&cent;&Euml;&Iacute;×&Ouml;&frac12;&Uacute;&ordm;&macr;&Ecirc;&yacute;
void IIC_SendByte(u8 ch)
{

char i;

RCC->APB1ENR|=(unsigned int)1<<3;
GPIOB->MODER&=0X0FFFFFFF;
GPIOB->MODER|=(unsigned int)8<<28;
// SDA_OUT;//to enable SDATA pin as an output
AD7746_SCLK = 0;//make sure that the clock line is low
delay(20);
for(i=0;i<8;i++)
{

AD7746_SDAout  = 0;//put data bit on SDATA line
if(ch&0x80)
AD7746_SDAout  = 1;
delay(10);
AD7746_SCLK = 1;//clock to send bit
delay(20);
AD7746_SCLK = 0;//clear clock
delay(20);
ch = ch<<1;//move ch's bit

}
RCC->AHB1ENR|=(unsigned int)1<<1; //1×ó&Ograve;&AElig;&para;&macr;1&Icirc;&raquo;
GPIOB->MODER&=0X0FFFFFFF;
GPIOB->MODER|=(unsigned int)8<<28;
// SDA_IN;//release data line for acknowledge     

AD7746_SCLK = 1;//send clock for acknowledge

if(AD7746_SDAin)//no acknowledge, set flag
IIC_Flag |= 1<<NOACK;
delay(20);
AD7746_SCLK = 0;//clear clock

}
u8 IIC_RcvByte(u8 ak)
{

u8 i,rr=0;

RCC->AHB1ENR|=(unsigned int)1<<1; //1×ó&Ograve;&AElig;&para;&macr;1&Icirc;&raquo;
GPIOB->MODER&=0X0FFFFFFF;
GPIOB->MODER|=(unsigned int)8<<28;
//SDA_IN;//to enable SDATA pin as an input

AD7746_SCLK = 0;//make sure the clock line is low
delay(20);
for(i=0;i<8;i++)
{

AD7746_SCLK = 1;//clock to recieve bit
delay(20);
rr = rr<<1;//read data bit
rr |= AD7746_SDAin;
AD7746_SCLK = 0;//clear clock
delay(20);

}
RCC->APB1ENR|=(unsigned int)1<<3;
GPIOB->MODER&=0X0FFFFFFF;
GPIOB->MODER|=(unsigned int)8<<28;
// SDA_OUT;//Data pin of the master must be an..

               //..output for the acknowledge
AD7746_SDAout = ak;//Send (no) acknowledge bit

AD7746_SCLK = 1;
delay(20);
AD7746_SCLK = 0X0;//clear clock
return rr;

}

void CDC_Reset(void)
{

u8 i;
IIC_StartBit();
IIC_SendByte(SLAVEADD);
if(!(IIC_Flag&(1<<NOACK)))
IIC_SendByte(CDC_RESET_ADDR);
else
IIC_Flag |= 1<<ERR;
IIC_StopBit();
for(i=0;i<254;i++);//wait for CDC reset
for(i=0;i<254;i++);
for(i=0;i<254;i++);

}
void CDC_WRREG(char addr, char reg)
{

IIC_StartBit();
IIC_SendByte(SLAVEADD);
if(!(IIC_Flag&(1<<NOACK)))
IIC_SendByte(addr);
if(!(IIC_Flag&(1<<NOACK)))
IIC_SendByte(reg);
IIC_StopBit();
if(IIC_Flag&(1<<NOACK))
IIC_Flag |= 1<<ERR;

}

char CDC_RDREG(char addr)
{

char rr;
IIC_StartBit();
IIC_SendByte(SLAVEADD);
if(!(IIC_Flag&(1<<NOACK)))
IIC_SendByte(addr);
if(!(IIC_Flag&(1<<NOACK)))
{

IIC_StartBit();//re-start
IIC_SendByte(SLAVEADD+1);//read function

}
if(!(IIC_Flag&(1<<NOACK)))
rr = IIC_RcvByte(1);//read 1 byte, no ack
IIC_StopBit();
if(IIC_Flag&(1<<NOACK))
IIC_Flag |= 1<<ERR;
return rr;

}
void CDC_RDCAP(void)
{

IIC_StartBit();
IIC_SendByte(SLAVEADD);
if(!(IIC_Flag&(1<<NOACK)))
IIC_SendByte(CDC_CAPDATA_ADDR);
if(!(IIC_Flag&(1<<NOACK)))
{

IIC_StartBit();//re-start
IIC_SendByte(SLAVEADD+1);//read function

}
if(!(IIC_Flag&(1<<NOACK)))
CapResult_H = IIC_RcvByte(0);//read cap data reg
if(!(IIC_Flag&(1<<NOACK)))
CapResult_M = IIC_RcvByte(0);
if(!(IIC_Flag&(1<<NOACK)))
CapResult_L = IIC_RcvByte(1);
IIC_StopBit();
if(IIC_Flag&(1<<NOACK))
IIC_Flag |= 1<<ERR;

}
float got_7746data(void)
{
// unsigned long SUMPID;
long DATA,H,M,L;
float ma;
H=CapResult_H;
M=CapResult_M;
L=CapResult_L;
DATA=(H<<16)|(M<<8)|L;
SUMPID=DATA;
ma=(float)(DATA-0x80)*99.2/0x75;

return ma;
}

void AD7746Init(void)
{
// float cappos;
CDC_Reset();
//REG SETUP refs to AD7746EVB's software
CDC_WRREG(0x0A,0xA1);//CON REG,CAPF=111,MD=001,9.1Hz
CDC_WRREG(0x0B,0x75);//CAP DAC A
CDC_WRREG(0x09,0x1B);//EXC REG,EXCA,EXCLVL=11
CDC_WRREG(0x07,0x80);//CAP SET-UP,start
// CDC_WRREG(0x07,0x80);
if(IIC_Flag&(1<<ERR))
{

IIC_Flag = 0;
//send_achar('#');   //if there's any error

}
//cappos=(float)17*CAPDAC/0x7F;
//


}
此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
木各各木
1楼-- · 2019-03-23 17:38
/ IIC通信时,SDL SDA一直处于高电平,没有出现高低电平的变化,这个怎么解决??下边是AD7746的配置,求指教
void IIC_StartBit(void)
{
RCC->APB1ENR|=(unsigned int)1<<3;
GPIOB->MODER&=0X0FFFFFFF;
GPIOB->MODER|=(unsigned int)8<<28;
// SDA_OUT;//enable SDATA pin as an output
IIC_Flag = 0;
AD7746_SDAout=1;

AD7746_SCLK = 1;

AD7746_SDAout  = 0;//low O/P on SDATA
delay(10);//delay 5 Machine cycles
AD7746_SCLK = 0;//start bit

}
//&sup2;&Ugrave;×÷IIC&Ecirc;±&ETH;ò&Ecirc;±&micro;&Auml;&Iacute;&pound;&Ouml;&sup1;&Ecirc;±&Acirc;&szlig;&frac14; &sup2;&Ugrave;×÷
void IIC_StopBit(void)
{
RCC->APB1ENR|=(unsigned int)1<<3;
GPIOB->MODER&=0X0FFFFFFF;
GPIOB->MODER|=(unsigned int)8<<28;
// SDA_OUT;//to enable SDATA pin as an output
AD7746_SDAout  = 0;//get SDATA ready for stop

AD7746_SCLK = 1;//set clock for stop
delay(20);
AD7746_SDAout  = 1;//this is the stop bit


}
//&Iuml;ò&frac14;&Auml;&acute;&aelig;&AElig;÷·&cent;&Euml;&Iacute;×&Ouml;&frac12;&Uacute;&ordm;&macr;&Ecirc;&yacute;
void IIC_SendByte(u8 ch)
{

char i;

RCC->APB1ENR|=(unsigned int)1<<3;
GPIOB->MODER&=0X0FFFFFFF;
GPIOB->MODER|=(unsigned int)8<<28;
// SDA_OUT;//to enable SDATA pin as an output
AD7746_SCLK = 0;//make sure that the clock line is low
delay(20);
for(i=0;i<8;i++)
{

AD7746_SDAout  = 0;//put data bit on SDATA line
if(ch&0x80)
AD7746_SDAout  = 1;
delay(10);
AD7746_SCLK = 1;//clock to send bit
delay(20);
AD7746_SCLK = 0;//clear clock
delay(20);
ch = ch<<1;//move ch's bit

}
RCC->AHB1ENR|=(unsigned int)1<<1; //1×ó&Ograve;&AElig;&para;&macr;1&Icirc;&raquo;
GPIOB->MODER&=0X0FFFFFFF;
GPIOB->MODER|=(unsigned int)8<<28;
// SDA_IN;//release data line for acknowledge     

AD7746_SCLK = 1;//send clock for acknowledge

if(AD7746_SDAin)//no acknowledge, set flag
IIC_Flag |= 1<<NOACK;
delay(20);
AD7746_SCLK = 0;//clear clock

}
u8 IIC_RcvByte(u8 ak)
{

u8 i,rr=0;

RCC->AHB1ENR|=(unsigned int)1<<1; //1×ó&Ograve;&AElig;&para;&macr;1&Icirc;&raquo;
GPIOB->MODER&=0X0FFFFFFF;
GPIOB->MODER|=(unsigned int)8<<28;
//SDA_IN;//to enable SDATA pin as an input

AD7746_SCLK = 0;//make sure the clock line is low
delay(20);
for(i=0;i<8;i++)
{

AD7746_SCLK = 1;//clock to recieve bit
delay(20);
rr = rr<<1;//read data bit
rr |= AD7746_SDAin;
AD7746_SCLK = 0;//clear clock
delay(20);

}
RCC->APB1ENR|=(unsigned int)1<<3;
GPIOB->MODER&=0X0FFFFFFF;
GPIOB->MODER|=(unsigned int)8<<28;
// SDA_OUT;//Data pin of the master must be an..

               //..output for the acknowledge
AD7746_SDAout = ak;//Send (no) acknowledge bit

AD7746_SCLK = 1;
delay(20);
AD7746_SCLK = 0X0;//clear clock
return rr;

}

void CDC_Reset(void)
{

u8 i;
IIC_StartBit();
IIC_SendByte(SLAVEADD);
if(!(IIC_Flag&(1<<NOACK)))
IIC_SendByte(CDC_RESET_ADDR);
else
IIC_Flag |= 1<<ERR;
IIC_StopBit();
for(i=0;i<254;i++);//wait for CDC reset
for(i=0;i<254;i++);
for(i=0;i<254;i++);

}
void CDC_WRREG(char addr, char reg)
{

IIC_StartBit();
IIC_SendByte(SLAVEADD);
if(!(IIC_Flag&(1<<NOACK)))
IIC_SendByte(addr);
if(!(IIC_Flag&(1<<NOACK)))
IIC_SendByte(reg);
IIC_StopBit();
if(IIC_Flag&(1<<NOACK))
IIC_Flag |= 1<<ERR;

}

char CDC_RDREG(char addr)
{

char rr;
IIC_StartBit();
IIC_SendByte(SLAVEADD);
if(!(IIC_Flag&(1<<NOACK)))
IIC_SendByte(addr);
if(!(IIC_Flag&(1<<NOACK)))
{

IIC_StartBit();//re-start
IIC_SendByte(SLAVEADD+1);//read function

}
if(!(IIC_Flag&(1<<NOACK)))
rr = IIC_RcvByte(1);//read 1 byte, no ack
IIC_StopBit();
if(IIC_Flag&(1<<NOACK))
IIC_Flag |= 1<<ERR;
return rr;

}
void CDC_RDCAP(void)
{

IIC_StartBit();
IIC_SendByte(SLAVEADD);
if(!(IIC_Flag&(1<<NOACK)))
IIC_SendByte(CDC_CAPDATA_ADDR);
if(!(IIC_Flag&(1<<NOACK)))
{

IIC_StartBit();//re-start
IIC_SendByte(SLAVEADD+1);//read function

}
if(!(IIC_Flag&(1<<NOACK)))
CapResult_H = IIC_RcvByte(0);//read cap data reg
if(!(IIC_Flag&(1<<NOACK)))
CapResult_M = IIC_RcvByte(0);
if(!(IIC_Flag&(1<<NOACK)))
CapResult_L = IIC_RcvByte(1);
IIC_StopBit();
if(IIC_Flag&(1<<NOACK))
IIC_Flag |= 1<<ERR;

}
float got_7746data(void)
{
// unsigned long SUMPID;
long DATA,H,M,L;
float ma;
H=CapResult_H;
M=CapResult_M;
L=CapResult_L;
DATA=(H<<16)|(M<<8)|L;
SUMPID=DATA;
ma=(float)(DATA-0x80)*99.2/0x75;

return ma;
}

void AD7746Init(void)
{
// float cappos;
CDC_Reset();
//REG SETUP refs to AD7746EVB's software
CDC_WRREG(0x0A,0xA1);//CON REG,CAPF=111,MD=001,9.1Hz
CDC_WRREG(0x0B,0x75);//CAP DAC A
CDC_WRREG(0x09,0x1B);//EXC REG,EXCA,EXCLVL=11
CDC_WRREG(0x07,0x80);//CAP SET-UP,start
// CDC_WRREG(0x07,0x80);
if(IIC_Flag&(1<<ERR))
{

IIC_Flag = 0;
//send_achar('#');   //if there's any error

}
//cappos=(float)17*CAPDAC/0x7F;
//


}
此帖出自小平头技术问答
littleshrimp
2楼-- · 2019-03-23 20:01
/ 看你的问题像是I2C没有配置成功,I2C建议你看一下STM32的官方例程
先把I2C调通了,然后再去看AD7746的官方例程比如使用下边这个
ad7746_generic.zip (7.55 KB, 下载次数: 13) 2018-3-21 20:54 上传 点击文件名下载附件
再用你STM32对应的I2C函数替放进Communication文件里

一周热门 更多>