-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c.c
More file actions
45 lines (38 loc) · 794 Bytes
/
i2c.c
File metadata and controls
45 lines (38 loc) · 794 Bytes
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
33
34
35
36
37
38
39
40
41
42
43
44
45
/* Author: Steve Gunn
* Licence: This work is licensed under the Creative Commons Attribution License.
* View this license at http://creativecommons.org/about/licenses/
*/
#include "i2c.h"
void init_i2c_master(void)
{
/* F_SCL = F_CPU/120 = 100kHz */
TWBR = 0x34;
TWSR = 0x00;
}
void i2c_start(void)
{
TWCR = _BV(TWINT) | _BV(TWSTA) | _BV(TWEN);
while(!(TWCR & _BV(TWINT)));
}
void i2c_stop(void)
{
TWCR = _BV(TWINT) | _BV(TWSTO) | _BV(TWEN);
}
void i2c_tx(uint8_t b)
{
TWDR = b;
TWCR = _BV(TWINT) | _BV(TWEN);
while(!(TWCR & _BV(TWINT)));
}
uint8_t i2c_rx_ack(void)
{
TWCR = _BV(TWINT) | _BV(TWEN) | _BV(TWEA);
while(!(TWCR & _BV(TWINT)));
return TWDR;
}
uint8_t i2c_rx_nack(void)
{
TWCR = _BV(TWINT) | _BV(TWEN);
while(!(TWCR & _BV(TWINT)));
return TWDR;
}