1 | #include "I2C.h"
|
2 |
|
3 |
|
4 | /*** Function to send a START Condition ***/
|
5 | void TWI_START(void)
|
6 | {
|
7 | // Send a START condition
|
8 | TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
|
9 | // Wait for TWINT Flag set. This indicates that the START condition has been transmitted
|
10 | while (!(TWCR & (1<<TWINT)));
|
11 | // Check value of TWI statusregister. Mask prescaler bits. If status different from START go to ERROR if ((TWSR & 0xF8) != START)
|
12 | ERROR();
|
13 | }
|
14 |
|
15 | /*** Function to send the Slave Address with ACK in Master Transmit Mode ***/
|
16 | void TWI_MT_SLA_ACK(void)
|
17 | {
|
18 | // Load Slave Address + Write into TWDR Register
|
19 | TWDR = SLA_W;
|
20 | // Clear TWINT bit in TWCR to start transmission
|
21 | TWCR = (1<<TWINT) | (1<<TWEN);
|
22 | // Wait for TWINT Flag set. This indicates that the SLA+W has been transmitted, and ACK/NACK has been received.
|
23 | while (!(TWCR & (1<<TWINT)));
|
24 | // Check value of TWI status register. Mask prescaler bits. If status different from MT_SLA_ACK go to ERROR
|
25 | if ((TWSR & 0xF8) != MT_SLA_ACK)
|
26 | ERROR();
|
27 | }
|
28 |
|
29 | /*** Function to send 8Bit of data with ACK in Master Transmit Mode **/
|
30 | void TWI_MT_DATA_ACK(uint8_t data)
|
31 | {
|
32 | // Load DATA into TWDR register
|
33 | TWDR = data;
|
34 | // Clear TWINT bit in TWCR to start transmission
|
35 | TWCR = (1<<TWINT) | (1<<TWEN);
|
36 | // Wait for TWINT flag set. This indicates that the DATA has been transmitted, and ACK/NACK has been received.
|
37 | while (!(TWCR & (1<<TWINT)));
|
38 | // Check value of TWI status register. Mask prescaler bits. If status different from MT_DATA_ACK go to ERROR
|
39 | if ((TWSR & 0xF8) != MT_DATA_ACK)
|
40 | ERROR();
|
41 | }
|
42 |
|
43 | /*** Function to send a REPEATED START condition **/
|
44 | void TWI_R_START(void)
|
45 | {
|
46 | // Send a START condition
|
47 | TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
|
48 | // Wait for TWINT Flag set. This indicates that the START condition has been transmitted
|
49 | while (!(TWCR & (1<<TWINT)));
|
50 | // Check value of TWI statusregister. Mask prescaler bits. If status different from R_START go to ER-ROR
|
51 | if ((TWSR & 0xF8) != R_START)
|
52 | ERROR();
|
53 | }
|
54 |
|
55 | *** Function to send the Slave Address in Master Receive Mode with Acknowledge **/
|
56 | void TWI_MR_SLA_ACK(void)
|
57 | {
|
58 | // Load Slave Address + Read into TWDR Register
|
59 | TWDR = SLA_R;
|
60 | // Clear TWINT bit in TWCR to start transmission
|
61 | TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
|
62 | // Wait for TWINT Flag set. This indicates that the SLA+W has been transmitted, and ACK/NACK has been received.
|
63 | while (!(TWCR & (1<<TWINT)));
|
64 | // Check value of TWI status register. Mask prescaler bits. If status different from MR_SLA_ACK go to ERROR
|
65 | if ((TWSR & 0xF8) != MR_SLA_ACK)
|
66 | ERROR();
|
67 | }
|
68 |
|
69 | /*** Function to send the Slave Address in Master Receive Mode without Acknowledge **/
|
70 | void TWI_MR_SLA_NACK(void)
|
71 | {
|
72 | // Load Slave Address + Read into TWDR Register
|
73 | TWDR = SLA_R;
|
74 | // Clear TWINT bit in TWCR to start transmission
|
75 | TWCR = (1<<TWINT) | (1<<TWEN);
|
76 | // Wait for TWINT Flag set. This indicates that the SLA+W has been transmitted, and ACK/NACK has been received.
|
77 | while (!(TWCR & (1<<TWINT)));
|
78 | // Check value of TWI status register. Mask prescaler bits. If status different from MR_SLA_NACK go to ERROR
|
79 | if ((TWSR & 0xF8) != MR_SLA_NACK)
|
80 | ERROR();
|
81 | }
|
82 |
|
83 | /*** Function to read one Databyte in Master Receive Mode and send NACK ***/
|
84 | uint8_t TWI_READ_DATABYTE_NACK(void)
|
85 | {
|
86 | // Clear TWINT bit in TWCR to start transmission with NACK
|
87 | TWCR = (1<<TWINT) | (1<<TWEN);
|
88 | // Wait for TWINT flag set. This indicates that the DATA has been received
|
89 | while (!(TWCR & (1<<TWINT)));
|
90 | // Check value of TWI status register. Mask prescaler bits. If status different from MR_SLA_NACK go to ERROR
|
91 | if ((TWSR & 0xF8) != MR_DATA_NACK)
|
92 | ERROR();
|
93 | return TWDR; // Return the value of data register
|
94 | }
|
95 |
|
96 | /*** Function to read one Databyte in Master Receive Mode and send ACK ***/
|
97 | uint8_t TWI_READ_DATABYTE_ACK(void)
|
98 | {
|
99 | // Clear TWINT bit in TWCR to start transmission with ACK
|
100 | TWCR = (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
|
101 | // Wait for TWINT flag set. This indicates that the DATA has been received
|
102 | while (!(TWCR & (1<<TWINT)));
|
103 | // Check value of TWI status register. Mask prescaler bits. If status different from MR_SLA_ACK go to ERROR
|
104 | if ((TWSR & 0xF8) != MR_DATA_ACK)
|
105 | ERROR();
|
106 | return TWDR; // Return the value of data register
|
107 | }
|
108 |
|
109 | /*** function to send a STOP condition ***/
|
110 | void TWI_STOP(void)
|
111 | {
|
112 | TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // Transmit STOP condition
|
113 | }
|
114 |
|
115 | /*** function to show a bus error ***/
|
116 | void ERROR(void)
|
117 | {
|
118 | PORTB |= (1<<PB1); // ERROR LED ON
|
119 | }
|