void I2CInit(void)
{
RCC->APB1ENR |= RCC_APB1Periph_I2C1; //wlacz zegar dla I2C1
GPIOB->CRH |= 0x00000000;
GPIOB->CRL |= 0xFF000000;
I2C1->CR2 |= 0x36; //36MHz
I2C1->TRISE = 0x26; //1us
I2C1->CCR = 0xB4; //100kHz
I2C1->CR1 = 0x01; //wlacz I2C
}
void I2CStart(void)
{
uint16_t dummy;
while((((I2C1->SR2) >> 1)&0x01)!=0x00); //czekaj poki BUSY
I2C1->CR1 |= (uint16_t)(0x01 << 8);
while((((I2C1->SR1) >> 0)&0x01)!=0x01); //czekaj az ustawi sie flaga SB
dummy = I2C1->SR1;
dummy = I2C1->SR2; //skasuj ewentualne flagi
}
void I2CAddr(uint8_t address)
{
uint16_t dummy;
I2C1->DR = address;
while((((I2C1->SR1) >> 1)&0x01)!=0x00); //czekaj az wyzeruje sie flaga ADDR
dummy = I2C1->SR1;
dummy = I2C1->SR2; //skasuj ewentualne flagi
}
void I2CSend(uint8_t data)
{
uint16_t dummy;
I2C1->DR = data;
while((((I2C1->SR1) >> 7)&0x01)!=0x01);
dummy = I2C1->SR1;
dummy = I2C1->SR2; //skasuj ewentualne flagi
}
uint8_t I2CRead(uint8_t ack)
{
uint16_t dummy;
if(ack)
I2C1->CR1 |= (uint16_t)(0x01 << 10);
else
I2C1->CR1 &= ~((uint16_t)(0x01 << 10));
while((((I2C1->SR1) >> 6)&0x01)!=0x00);
dummy = I2C1->SR1;
dummy = I2C1->SR2; //skasuj ewentualne flagi
return((uint8_t)(I2C1->DR));
}
void I2CStop(void)
{
uint16_t dummy;
I2C1->CR1 |= (uint16_t)(0x01 << 9);
dummy = I2C1->SR1;
dummy = I2C1->SR2; //skasuj ewentualne flagi
}