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
}
//²Ù×÷IICʱÐòʱµÄֹͣʱÂß¼ ²Ù×÷
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
}
//Ïò¼Ä´æÆ÷·¢ËÍ×Ö½Úº¯Êý
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×óÒƶ¯1λ
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×óÒƶ¯1λ
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;
//
}
此帖出自
小平头技术问答
先把I2C调通了,然后再去看AD7746的官方例程比如使用下边这个
再用你STM32对应的I2C函数替放进Communication文件里
一周热门 更多>