-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtwi.c
168 lines (158 loc) · 3.73 KB
/
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
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
/**
* --------------------------------------------------------------------------------------+
* @desc Two Wire Interface / I2C Communication
* --------------------------------------------------------------------------------------+
* Copyright (C) 2020 Marian Hrinko.
* Written by Marian Hrinko ([email protected])
*
* @author Marian Hrinko
* @datum 06.09.2020
* @file twi.c
* @tested AVR Atmega16, ATmega8, Atmega328
*
* @depend twi.h
* --------------------------------------------------------------------------------------+
* @usage Master Transmit Operation
*/
// include libraries
#include "twi.h"
/**
* @desc TWI init - initialize frequency
*
* @param void
*
* @return void
*/
void TWI_Init (void)
{
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Calculation fclk:
//
// fclk = (fcpu)/(16+2*TWBR*4^Prescaler) m16
// fclk = (fcpu)/(16+2*TWBR*Prescaler) m328p
// -------------------------------------------------------------------------------------
// Calculation TWBR:
//
// TWBR = {(fcpu/fclk) - 16 } / (2*4^Prescaler)
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// @param1 value of TWBR (m16)
// fclk = 400kHz; TWBR = 3
// fclk = 100kHz; TWBR = 20
// @param1 value of TWBR (m328p)
// fclk = 400kHz; TWBR = 2
// @param2 value of Prescaler = 1
TWI_FREQ (2, 1);
}
/**
* @desc TWI MT Start
*
* @param void
*
* @return char
*/
char TWI_MT_Start (void)
{
// null status flag
TWI_TWSR &= ~0xA8;
// START
// -------------------------------------------------------------------------------------
// request for bus
TWI_START();
// wait till flag set
TWI_WAIT_TILL_TWINT_IS_SET();
// test if start or repeated start acknowledged
if ((TWI_STATUS != TWI_START_ACK) && (TWI_STATUS != TWI_REP_START_ACK)) {
// return status
return TWI_STATUS;
}
// success
return SUCCESS;
}
/**
* @desc TWI Send address + write
*
* @param char
*
* @return char
*/
char TWI_MT_Send_SLAW (char address)
{
// SLA+W
// -------------------------------------------------------------------------------------
TWI_TWDR = (address << 1);
// enable
TWI_ENABLE();
// wait till flag set
TWI_WAIT_TILL_TWINT_IS_SET();
// test if SLA with WRITE acknowledged
if (TWI_STATUS != TWI_MT_SLAW_ACK) {
// return status
return TWI_STATUS;
}
// success
return SUCCESS;
}
/**
* @desc TWI Send data
*
* @param char
*
* @return char
*/
char TWI_MT_Send_Data (char data)
{
// DATA
// -------------------------------------------------------------------------------------
TWI_TWDR = data;
// enable
TWI_ENABLE();
// wait till flag set
TWI_WAIT_TILL_TWINT_IS_SET();
// test if data acknowledged
if (TWI_STATUS != TWI_MT_DATA_ACK) {
// return status
return TWI_STATUS;
}
// success
return SUCCESS;
}
/**
* @desc TWI Send address + read
*
* @param char
*
* @return char
*/
char TWI_MR_Send_SLAR (char address)
{
// SLA+R
// -------------------------------------------------------------------------------------
TWI_TWDR = (address << 1) | 0x01;
// enable
TWI_ENABLE();
// wait till flag set
TWI_WAIT_TILL_TWINT_IS_SET();
// test if SLA with READ acknowledged
if (TWI_STATUS != TWI_MR_SLAR_ACK) {
// return status
return TWI_STATUS;
}
// success
return SUCCESS;
}
/**
* @desc TWI stop
*
* @param void
*
* @return void
*/
void TWI_Stop (void)
{
// End TWI
// -------------------------------------------------------------------------------------
// send stop sequence
TWI_STOP ();
// wait for TWINT flag is set
// TWI_WAIT_TILL_TWINT_IS_SET();
}