-
Notifications
You must be signed in to change notification settings - Fork 4
/
TWI.c
51 lines (43 loc) · 934 Bytes
/
TWI.c
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
46
47
48
49
50
51
// Library taken from http://www.embedds.com/programming-avr-i2c-interface/
// Respective licence applies.
#include "TWI.h"
// Set SCL to 400kHz and enable TWI.
void TWI_Init(void)
{
TWSR = 0x00;
TWBR = 0x0C;
TWCR = (1<<TWEN);
}
void TWI_Start(void)
{
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0);
}
void TWI_Stop(void)
{
TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);
}
void TWI_Write(uint8_t data)
{
TWDR = data;
TWCR = (1<<TWINT) | (1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0);
}
uint8_t TWI_ReadACK(void)
{
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
while ((TWCR & (1<<TWINT)) == 0);
return TWDR;
}
// Read byte with NACK.
uint8_t TWI_ReadNACK(void)
{
TWCR = (1<<TWINT) | (1<<TWEN);
while ((TWCR & (1<<TWINT)) == 0);
return TWDR;
}
uint8_t TWI_GetStatus(void)
{
uint8_t status = TWSR & 0xF8; // Mask status.
return status;
}