Gast
#2386184
Hallo.
Ich versuche vom SRF02 (Ultraschallsensor) aus den Registern zu lesen
oder drauf zu schreiben, aber irgendwie funktioniert das nicht.
Vielleicht kann jemand nen Tip geben, wo der Fehler liegt.
Vielen Dank
uint8_t Lob ;
uint8_t Hib ;
/**
* @brief Initialize all stuff you need for I2C
* @retval None.
*/
void GPIO_Configuration( void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure I2C1 pins: SCL and SDA to communicate with the sensor */
GPIO_InitStructure. GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure. GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
// Initialise the GPIOB Peripheral
GPIO_Init(GPIOB, &GPIO_InitStructure );
// Configure the GPIO ports as alternate functions (PB6,7)
GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE);
}
void I2C_Configuration( void)
{
I2C_InitTypeDef I2C_InitStructure;
/* I2C configuration */
I2C_InitStructure. I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure. I2C_OwnAddress1 = I2C1_SLAVE_ADDRESS7;
I2C_InitStructure. I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress =
I2C_AcknowledgedAddress_7bit;
I2C_InitStructure. I2C_ClockSpeed = I2C_Speed;
/* Apply I2C configuration after enabling it */
I2C_Init(I2C1, &I2C_InitStructure) ;
/* I2C Peripheral Enable */
I2C_Cmd(I2C1, ENABLE);
}
void I2C_Sensor_Init(void)
{
/* I2C configuration */
I2C_Configuration( );
I2C_DeInit(I2C1);
/* GPIO Periph clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* I2C Periph clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
/* GPIO configuration */
GPIO_Configuration( );
}
/**
* @brief Write one byte to the selected slave device.
* @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
* @param Address:8bit slave address of the slave register
* @param Data: byte to write to the specified register
* @retval None.
*/
void I2C_Master_Write__Byte(I2C_TypeDef* I2Cx, uint8_t Register_Adress,
uint8_t DataByte)
{
// if(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
I2C_GenerateSTOP(I2Cx, ENABLE);
/* Send STRAT condition */
I2C_GenerateSTART(I2Cx, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
/* Send Slave address for write */
I2C_Send7bitAddress(I2Cx, I2C1_SLAVE_ADDRESS7,
I2C_Direction_Transmitter);
I2C_AcknowledgeConfig(I2Cx, ENABLE);
/* Test on EV6 and clear it */
while(!I2C_CheckEvent(I2Cx,
I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Send the Slave´s internal register address to write to */
I2C_SendData(I2Cx,Register_Adress);
I2C_AcknowledgeConfig(I2Cx, ENABLE);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send the byte to be written */
I2C_SendData( I2Cx, DataByte);
I2C_AcknowledgeConfig(I2Cx, ENABLE);
/* Test on EV8 and clear it */
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/* Send STOP condition */
I2C_GenerateSTOP(I2Cx, ENABLE);
}
uint8_t I2C_Master_Read_2Byte(I2C_TypeDef* I2Cx)
{
uint8_t Lob ;
uint8_t Hib ;
uint8_t temp;
uint8_t I2C_ReceivedData=0;
//Communication start
// Messvorgang in starten
//I2C Interface ready?
if(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY))
I2C_GenerateSTOP(I2Cx, ENABLE);
I2C_GenerateSTART(I2Cx, ENABLE);
/* Test on EV5 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
//After checking on EV5,the master sends the address of the
slave(s) with which it will communicate
I2C_Send7bitAddress(I2Cx, I2C1_SLAVE_ADDRESS7,
I2C_Direction_Transmitter);
// The master has to wait that a slave acknowledges his
address
I2C_AcknowledgeConfig(I2Cx, ENABLE);
// Master Transmitter mode: The master has to send data (0x00)
while(!I2C_CheckEvent(I2Cx,
I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//send the reg select COMMAND byte
I2C_SendData(I2Cx, 0x00);
//The master has to wait that a slave acknowledges
I2C_AcknowledgeConfig(I2Cx, ENABLE);
// Test on EV8 and clear it
while(!I2C_CheckEvent(I2Cx,
I2C_EVENT_MASTER_BYTE_TRANSMITTED));
// Master Transmitter mode: The master has to send data (0x81)
while(!I2C_CheckEvent(I2Cx,
I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
//send the reg select COMMAND byte
I2C_SendData(I2Cx, 0x51);
//The master has to wait that a slave acknowledges
I2C_AcknowledgeConfig(I2Cx, ENABLE);
// Test on EV8 and clear it
while(!I2C_CheckEvent(I2Cx,
I2C_EVENT_MASTER_BYTE_TRANSMITTED));
//Communication restart
I2C_GenerateSTART(I2Cx, ENABLE);
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
//After checking on EV5,the master sends the address of the
slave(s) with which it will communicate
I2C_Send7bitAddress(I2Cx, I2C1_SLAVE_ADDRESS7,
I2C_Direction_Receiver);
//The master has to wait that a slave acknowledges
I2C_AcknowledgeConfig(I2Cx, ENABLE);
//Next check EV6:
while(!I2C_CheckEvent(I2Cx,
I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_AcknowledgeConfig(I2C1, DISABLE);
/*!< Test on EV7 and clear it */
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED));
//Read MSB Byte
Lob = I2C_ReceiveData(I2Cx);
while(!I2C_CheckEvent(I2Cx,
I2C_EVENT_SLAVE_BYTE_TRANSMITTED));
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED));
//Read LSB Byte
Hib = I2C_ReceiveData(I2Cx);
while(!I2C_CheckEvent(I2Cx,
I2C_EVENT_SLAVE_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2Cx, ENABLE);
I2C_AcknowledgeConfig(I2C1, DISABLE);
I2C_ReceivedData =(Lob+256)+ Hib ;
return I2C_ReceivedData;
}
////////////////////////////////////////////////////////////////////////
/
int main(void) {
SystemInit();// this sets up the oscilator
CAN_inits();
I2C_Sensor_Init();
SRF02_init();
NVIC_Configuration();
I2C_Master_Read_2Byte(I2C1);
return 0;
}