[求助]
采用AD7746+stm32,使用I2C模拟,但一直没有ACK
- #define SDA_IN {RCC->APB2ENR|=(unsigned int)1<<3;GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(unsigned int)8<<28;GPIOB->ODR|=(unsigned int)1<<7;}
- #define SDA_OUT {RCC->APB2ENR|=(unsigned int)1<<3;GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(unsigned int)3<<28;GPIOB->ODR|=(unsigned int)1<<7;}
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB,GPIO_Pin_6);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
- GPIO_SetBits(GPIOB,GPIO_Pin_7);
- void IIC_SendByte(char ch)
- {
- char i;
- 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;
- if(ch&0x80)
- AD7746_SDAout=1;
- else
- AD7746_SDAout=0;
- ch = ch<<1;
- delay(5);
- AD7746_SCLK = 1; //clock to send bit
- delay(5);
- AD7746_SCLK = 0; //clear clock
- delay(5);
- //move ch's bit
- }
- SDA_IN; //release data line for acknowledge
-
- AD7746_SCLK = 1; //send clock for acknowledge
- delay(2);
- //AD7746_SDAout=1;
- //delay(2);
- if(!AD7746_SDAin) //no acknowledge, set flag
- {
-
- IIC_Flag |= 1<<NOACK;
- }
-
- delay(20);
- AD7746_SCLK = 0; //clear clock
- }
- 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;
- }