twi 1.0
AVR mega series twi library
Loading...
Searching...
No Matches
twi.h
Go to the documentation of this file.
1
19
20#ifndef TWI_H_
21#define TWI_H_
22
23 #ifndef F_CPU
31 #define F_CPU 12000000UL
32 #endif
33
34 #ifndef F_TWI
42 #define F_TWI 100000UL
43 #endif
44
45 #ifndef TWI_PRESCALER
57 #define TWI_PRESCALER 0
58 #endif
59
60 #ifndef TWI_BITRATE
61 #ifdef TWI_PRESCALER
71 #define TWI_BITRATE ((F_CPU/F_TWI) - 16UL) / (2 * (1<<TWI_PRESCALER) * (1<<TWI_PRESCALER))
72 #endif
73 #endif
74
82 #ifndef TWI_STATUS_START
87 #define TWI_STATUS_START 0x08
88 #endif
89
90 #ifndef TWI_STATUS_REPEATED_START
95 #define TWI_STATUS_REPEATED_START 0x10
96 #endif
97
98 #ifndef TWI_STATUS_ADDRESS_WRITE_ACK
103 #define TWI_STATUS_ADDRESS_WRITE_ACK 0x18
104 #endif
105
106 #ifndef TWI_STATUS_ADDRESS_WRITE_NACK
111 #define TWI_STATUS_ADDRESS_WRITE_NACK 0x20
112 #endif
113
114 #ifndef TWI_STATUS_DATA_WRITE_ACK
119 #define TWI_STATUS_DATA_WRITE_ACK 0x28
120 #endif
121
122 #ifndef TWI_STATUS_DATA_WRITE_NACK
127 #define TWI_STATUS_DATA_WRITE_NACK 0x30
128 #endif
129
130 #ifndef TWI_STATUS_ARBITRATION_LOST
135 #define TWI_STATUS_ARBITRATION_LOST 0x38
136 #endif
137
138 #ifndef TWI_STATUS_ADDRESS_READ_ACK
143 #define TWI_STATUS_ADDRESS_READ_ACK 0x40
144 #endif
145
146 #ifndef TWI_STATUS_ADDRESS_READ_NACK
151 #define TWI_STATUS_ADDRESS_READ_NACK 0x48
152 #endif
153
154 #ifndef TWI_STATUS_DATA_READ_ACK
159 #define TWI_STATUS_DATA_READ_ACK 0x50
160 #endif
161
162 #ifndef TWI_STATUS_DATA_READ_NACK
167 #define TWI_STATUS_DATA_READ_NACK 0x58
168 #endif
170
171 #ifndef TWI_TWIE
183 // #define TWI_TWIE
184 #endif
185
186 #include <avr/io.h>
187
188 #ifdef TWI_TWIE
189 #include <avr/interrupt.h>
190 #endif
191
194
195 unsigned char twi_init(void);
196 void twi_disable(void);
197 unsigned char twi_status(void);
198
199 #ifndef TWI_TWIE
200 TWI_Error twi_start(void);
201 void twi_stop(void);
202 TWI_Error twi_address(unsigned char address, TWI_Operation operation);
203 TWI_Error twi_set(unsigned char data);
204 TWI_Error twi_get(unsigned char *data, TWI_Acknowledge acknowledge);
205 #endif
206
207
208#endif /* TWI_H_ */
Macro definitions for TWI/I2C communication flags and operations.
Enumerations for TWI/I2C interface modes, operations, acknowledgment, and error codes.
enum TWI_Acknowledge_t TWI_Acknowledge
Alias for enum TWI_Acknowledge_t representing TWI acknowledge mode.
Definition TWI_enums.h:66
enum TWI_Operation_t TWI_Operation
Alias for enum TWI_Operation_t representing TWI operation modes.
Definition TWI_enums.h:48
enum TWI_Error_t TWI_Error
Alias for enum TWI_Error_t representing TWI error status.
Definition TWI_enums.h:89
TWI_Error twi_address(unsigned char address, TWI_Operation operation)
Send a TWI (I2C) slave address combined with the read/write operation bit.
Definition twi.c:147
void twi_disable(void)
Disable the TWI (I2C) interface.
Definition twi.c:56
unsigned char twi_init(void)
Initialize the TWI (I2C) hardware interface in master mode.
Definition twi.c:36
unsigned char twi_status(void)
Retrieves the current TWI status byte.
Definition twi.c:78
TWI_Error twi_set(unsigned char data)
Transmit a data byte over the TWI (I2C) bus.
Definition twi.c:203
void twi_stop(void)
Stop the TWI (I2C) transmission by generating a stop condition.
Definition twi.c:120
TWI_Error twi_get(unsigned char *data, TWI_Acknowledge acknowledge)
Receive a data byte from the TWI (I2C) bus.
Definition twi.c:247
TWI_Error twi_start(void)
Initiates a TWI (I2C) (repeated) start condition on the bus.
Definition twi.c:96