1 | void I2C_send(uint8_t data, uint8_t instruction)
|
2 | {
|
3 | // Sending data
|
4 | // Generate start condition
|
5 | I2C1->CR1 |= (1<<I2C_CR1_START_Pos); // Generate repeated start condition
|
6 | while(!(I2C1->SR1 & I2C_SR1_SB)); // Wait until Start bit is set
|
7 | uint8_t tempAddress = 0;
|
8 | uint8_t slaveAddress = 39; // 0x27 = 39dec = 0b0010 0111
|
9 | tempAddress = (slaveAddress<<1); // Make space on the 0th bit for R/W
|
10 | tempAddress &= ~(1<<0); // Clear 0th bit for write
|
11 | I2C1->DR = tempAddress; // Sends slave address
|
12 |
|
13 | while(!(I2C1->SR1 & I2C_SR1_ADDR)); // Wait until address is sent and acknowledged
|
14 | uint32_t temp1 = 0;
|
15 | temp1 = I2C1->SR1;
|
16 | temp1 = I2C1->SR2;
|
17 | (void)temp1;
|
18 |
|
19 | while(!(I2C1->SR1 & I2C_SR1_TXE)); // Wait for transmit ready
|
20 |
|
21 |
|
22 | uint8_t command = 0x00;
|
23 | // uint8_t command = 0x00; // 0b00000000. Sets RS=0, RW=0, EN=0, Backlight=Off
|
24 | if(instruction == 0)
|
25 | {
|
26 | command = 0x08; // 0b00001000. Sets RS=0, RW=0, EN=0, Backlight=On
|
27 | }
|
28 | else if(instruction == 1)
|
29 | {
|
30 | command = 0x09; // 0b00001001. Sets RS=1, RW=0, EN=0, Backlight=On
|
31 | }
|
32 | else
|
33 | {
|
34 | // Error
|
35 | }
|
36 | uint8_t Nibble_H = 0;
|
37 | uint8_t Nibble_L = 0;
|
38 | Nibble_H = data & 0xF0;
|
39 | Nibble_L = data & 0x0F;
|
40 |
|
41 | I2C1->DR = Nibble_H | command; // Sends high Nibble together with command
|
42 |
|
43 | while(!(I2C1->SR1 & I2C_SR1_TXE));
|
44 |
|
45 | while(!(I2C1->SR1 & I2C_SR1_BTF));
|
46 |
|
47 | I2C1->DR = (Nibble_L << 4) | command; // Sends low Nibble
|
48 |
|
49 | while(!(I2C1->SR1 & I2C_SR1_TXE));
|
50 |
|
51 | while(!(I2C1->SR1 & I2C_SR1_BTF));
|
52 |
|
53 | I2C1->DR |= (1<<2); // Enables pulse to secure command
|
54 |
|
55 | while(!(I2C1->SR1 & I2C_SR1_TXE));
|
56 |
|
57 | while(!(I2C1->SR1 & I2C_SR1_BTF));
|
58 |
|
59 | delay_25us(1); // Waits 25us
|
60 |
|
61 | I2C1->DR &= ~(1<<2); // Disables pulse again
|
62 |
|
63 | while(!(I2C1->SR1 & I2C_SR1_TXE));
|
64 |
|
65 | while(!(I2C1->SR1 & I2C_SR1_BTF));
|
66 |
|
67 | I2C1->CR1 |= (1<<I2C_CR1_STOP_Pos); // Generate stop condition
|
68 |
|
69 | // I2C1->CR1 &= ~(1<<I2C_CR1_PE_Pos); // Turn off I2C1
|
70 |
|
71 | }
|