This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

opt3001没有反应

Other Parts Discussed in Thread: OPT3001

大家好我使用单片机读取OPT3001得数据,一点反应也没有ACK信号也没有,ADDR接地地址是0x44,下面我贴出我的代码请大神指点

void I2CStart( void ) 

GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = E2PROM_SDA;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


HAL_GPIO_WritePin(GPIOA, E2PROM_SCL, GPIO_PIN_SET); 
HAL_GPIO_WritePin(GPIOA, E2PROM_SDA, GPIO_PIN_SET); 
DelayMs(10); 
// GPIO_SetBits( GPIOA, E2PROM_SCL ); 
HAL_GPIO_WritePin(GPIOA, E2PROM_SCL, GPIO_PIN_SET); 
DelayMs(10); 
// GPIO_ResetBits( GPIOA, E2PROM_SDA ); 
HAL_GPIO_WritePin(GPIOA, E2PROM_SDA, GPIO_PIN_RESET); 
DelayMs(10); 
HAL_GPIO_WritePin(GPIOA, E2PROM_SCL, GPIO_PIN_RESET); 
DelayMs(10); 
// GPIO_ResetBits( GPIOA, E2PROM_SCL ); 
}

void I2CStop( void ) 

GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = E2PROM_SDA;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);


HAL_GPIO_WritePin(GPIOA, E2PROM_SCL, GPIO_PIN_RESET); 
DelayMs(10); 
HAL_GPIO_WritePin(GPIOA, E2PROM_SCL, GPIO_PIN_SET); 
HAL_GPIO_WritePin(GPIOA, E2PROM_SDA, GPIO_PIN_RESET); 
DelayMs(10); 

HAL_GPIO_WritePin(GPIOA, E2PROM_SDA, GPIO_PIN_SET); 
DelayMs(10);

HAL_GPIO_WritePin(GPIOA, E2PROM_SDA, GPIO_PIN_RESET); 
DelayMs(10);

}

unsigned char I2c_getAck(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pin = E2PROM_SDA;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
char ack; 
HAL_GPIO_WritePin(GPIOA, E2PROM_SDA, GPIO_PIN_SET); 
HAL_GPIO_WritePin(GPIOA, E2PROM_SCL, GPIO_PIN_SET); 
DelayMs(10); 
ack = HAL_GPIO_ReadPin( GPIOA, E2PROM_SDA ); 
HAL_GPIO_WritePin(GPIOA, E2PROM_SCL, GPIO_PIN_RESET); 
return ack;
}

void I2CWriteByte( unsigned char byte ) 

unsigned char i;

for( i=0; i<8; i++ ) 

if( 0X80 & byte ) 
{
HAL_GPIO_WritePin(GPIOA, E2PROM_SDA, GPIO_PIN_SET); 
}
else
{
HAL_GPIO_WritePin(GPIOA, E2PROM_SDA, GPIO_PIN_RESET); 


byte <<= 1; 
DelayMs(10);

HAL_GPIO_WritePin(GPIOA, E2PROM_SCL, GPIO_PIN_SET); 
DelayMs(10); 
HAL_GPIO_WritePin(GPIOA, E2PROM_SCL, GPIO_PIN_RESET); 
DelayMs(10); 

// HAL_GPIO_WritePin(GPIOA, E2PROM_SCL, GPIO_PIN_SET); 
// DelayMs(10); 
I2c_getAck();
}

unsigned char I2CReadByte( void ) 

unsigned char i; 
unsigned char ReadValue = 0; 
GPIO_InitTypeDef GPIO_InitStruct; 
unsigned char bit; 

GPIO_InitStruct.Pin = E2PROM_SDA;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
for( i=0; i<8; i++ ) 

HAL_GPIO_WritePin(GPIOA, E2PROM_SCL, GPIO_PIN_SET); 
DelayMs(10); 
if( SET == HAL_GPIO_ReadPin( GPIOA, E2PROM_SDA ) ) 
bit = 0X01; 
else 
bit = 0x00; 

ReadValue = (ReadValue<<1)|bit; 
HAL_GPIO_WritePin(GPIOA, E2PROM_SCL, GPIO_PIN_RESET); 
// DelayMs(10); 

// HAL_GPIO_WritePin(GPIOA, E2PROM_SCL, GPIO_PIN_SET);
// DelayMs(10); 
// GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; 
// GPIO_Init( GPIOB, &GPIO_InitStruct ); 
GPIO_InitStruct.Pin = E2PROM_SDA;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
return ReadValue; 
}

void I2c_toDevice(unsigned char deviceAdd,unsigned char datAdd,unsigned char dat)
{
I2CStart();
I2CWriteByte(deviceAdd);
I2CWriteByte(datAdd);
I2CWriteByte(dat);
I2CWriteByte(0x10);
I2CStop();
}
unsigned short I2c_fromDevice(unsigned char deviceAdd,unsigned char datAdd )
{
unsigned short dat;
I2CStart();
I2CWriteByte(deviceAdd);
I2CWriteByte(datAdd);
// I2CStart();
I2CWriteByte(deviceAdd+1);
dat=I2CReadByte();
I2c_getAck();
dat=dat<<8;
dat|=I2CReadByte();
I2CStop();
return dat;
}