-
Notifications
You must be signed in to change notification settings - Fork 4
/
i2c.c
170 lines (148 loc) · 4.13 KB
/
i2c.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//
// i2c.c
// i2c
//
// Created by Michael Köhler on 09.10.17.
//
//
#include "i2c.h"
#if defined (__AVR_ATmega328__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega168PA__) || defined(__AVR_ATmega88P__) || defined(__AVR_ATmega48P__) || defined(__AVR_ATmega1284P__) || defined (__AVR_ATmega324A__) || defined (__AVR_ATmega324P__) || defined (__AVR_ATmega324PA__) || defined (__AVR_ATmega644__) || defined (__AVR_ATmega644A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__) || defined (__AVR_ATmega1284P__)
#if PSC_I2C != 1 && PSC_I2C != 4 && PSC_I2C != 16 && PSC_I2C != 64
#error "Wrong prescaler for TWI !"
#elif SET_TWBR < 0 || SET_TWBR > 255
#error "TWBR out of range, change PSC_I2C or F_I2C !"
#endif
uint8_t I2C_ErrorCode;
/**********************************************
Public Function: i2c_init
Purpose: Initialise TWI/I2C interface
Input Parameter: none
Return Value: none
**********************************************/
void i2c_init(void){
// set clock
switch (PSC_I2C) {
case 4:
TWSR = 0x1;
break;
case 16:
TWSR = 0x2;
break;
case 64:
TWSR = 0x3;
break;
default:
TWSR = 0x00;
break;
}
TWBR = (uint8_t)SET_TWBR;
// enable
TWCR = (1 << TWEN);
}
/**********************************************
Public Function: i2c_start
Purpose: Start TWI/I2C interface
Input Parameter:
- uint8_t i2c_addr: Adress of reciever
Return Value: none
**********************************************/
void i2c_start(uint8_t i2c_addr){
// i2c start
TWCR = (1 << TWINT)|(1 << TWSTA)|(1 << TWEN);
uint16_t timeout = F_CPU/F_I2C*2.0;
while((TWCR & (1 << TWINT)) == 0 &&
timeout !=0){
timeout--;
if(timeout == 0){
I2C_ErrorCode |= (1 << I2C_START);
return;
}
};
// send adress
TWDR = i2c_addr;
TWCR = (1 << TWINT)|( 1 << TWEN);
timeout = F_CPU/F_I2C*2.0;
while((TWCR & (1 << TWINT)) == 0 &&
timeout !=0){
timeout--;
if(timeout == 0){
I2C_ErrorCode |= (1 << I2C_SENDADRESS);
return;
}
};
}
/**********************************************
Public Function: i2c_stop
Purpose: Stop TWI/I2C interface
Input Parameter: none
Return Value: none
**********************************************/
void i2c_stop(void){
// i2c stop
TWCR = (1 << TWINT)|(1 << TWSTO)|(1 << TWEN);
}
/**********************************************
Public Function: i2c_byte
Purpose: Send byte at TWI/I2C interface
Input Parameter:
- uint8_t byte: Byte to send to reciever
Return Value: none
**********************************************/
void i2c_byte(uint8_t byte){
TWDR = byte;
TWCR = (1 << TWINT)|( 1 << TWEN);
uint16_t timeout = F_CPU/F_I2C*2.0;
while((TWCR & (1 << TWINT)) == 0 &&
timeout !=0){
timeout--;
if(timeout == 0){
I2C_ErrorCode |= (1 << I2C_BYTE);
return;
}
};
}
/**********************************************
Public Function: i2c_readAck
Purpose: read acknowledge from TWI/I2C Interface
Input Parameter: none
Return Value: uint8_t
- TWDR: recieved value at TWI/I2C-Interface, 0 at timeout
- 0: Error at read
**********************************************/
uint8_t i2c_readAck(void){
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);
uint16_t timeout = F_CPU/F_I2C*2.0;
while((TWCR & (1 << TWINT)) == 0 &&
timeout !=0){
timeout--;
if(timeout == 0){
I2C_ErrorCode |= (1 << I2C_READACK);
return 0;
}
};
return TWDR;
}
/**********************************************
Public Function: i2c_readNAck
Purpose: read non-acknowledge from TWI/I2C Interface
Input Parameter: none
Return Value: uint8_t
- TWDR: recieved value at TWI/I2C-Interface
- 0: Error at read
**********************************************/
uint8_t i2c_readNAck(void){
TWCR = (1<<TWINT)|(1<<TWEN);
uint16_t timeout = F_CPU/F_I2C*2.0;
while((TWCR & (1 << TWINT)) == 0 &&
timeout !=0){
timeout--;
if(timeout == 0){
I2C_ErrorCode |= (1 << I2C_READNACK);
return 0;
}
};
return TWDR;
}
#else
#error "Micorcontroller not supported now!"
#endif