-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI2C1_Write
32 lines (25 loc) · 1.03 KB
/
I2C1_Write
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void I2C1_Write(uint8_t address, uint8_t command, int n, uint8_t* data)
{
volatile int tmp;
int i;
while(I2C1->SR2 & 2){} //wait until bus not busy
I2C1->CR1 &= ~0x800; //Acknowledge clear p.682
I2C1->CR1 |= 0x100; //generate start p.694
while(!(I2C1->SR1&1)){} //wait until start condition generated
I2C1->DR=address << 1; //transmit slave address
while(!(I2C1->SR1 & 2)){} //wait until end of address transmission p.690
tmp=I2C1->SR2; //Reading I2C_SR2 after reading I2C_SR1 clears the ADDR flag p691
while(!(I2C1->SR1 & 0x80)){} //wait until data register empty p.689
I2C1->DR = 0x20;
while(!(I2C1->SR1 & 0x80)){} //wait until data register empty p.689
I2C1->DR = command; //send command
while(!(I2C1->SR1 & 0x80)){} //wait until data register empty p.689
//write data
for(i=0;i<n;i++)
{
while(!(I2C1->SR1 & 0x80)){} //wait until data register empty p.689
I2C1->DR=*data++; //send command
}
while(!(I2C1->SR1 & 4)){} //wait until byte transfer finished p.690
I2C1->CR1 |= (1<<9); //generate stop
}