-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTWI_Program.c
177 lines (175 loc) · 3.97 KB
/
TWI_Program.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
171
172
173
174
175
176
177
/*
* TWI_Program.c
*
* Created on: Nov 27, 2021
* Author: gerges
*/
#include"STD_TYPES.h"
#include"BIT_MATH.h"
#include"TWI_Private.h"
#include"TWI_Interface.h"
void TWI_VidInitMaster(u8 Copy_u8Address)
{
/*Enable Acknowledge bit*/
SET_BIT(TWCR,6);
/*Check if the master node will be addressed*/
if(Copy_u8Address!=0)
{
/*Set the Required address in the 7MSB of TWAR*/
TWAR=Copy_u8Address<<1;
}
else
{
/*Do Nothing*/
}
/*Set SCL Frequency To 400kHz,with 16MHz system Frequency*/
/*1- Set TWBR=12*/
TWBR=12;
/*Set Precaler bits(TWPS0,TWPS1)*/
CLR_BIT(TWSR,1);
CLR_BIT(TWSR,0);
/*Enable TWI*/
SET_BIT(TWCR,2);
}
void TWI_VidInitSlave(u8 Copy_u8Address)
{
/*Enable Acknowledge bit*/
SET_BIT(TWCR,6);
/*Set the Required address in the 7MSB of TWAR*/
TWAR=Copy_u8Address<<1;
/*Enable TWI*/
SET_BIT(TWCR,2);
}
TWI_ErrStatus TWI_SendStartCondition(void)
{
TWI_ErrStatus Local_Error=NoError;
/*send Start Condition*/
SET_BIT(TWCR,5);
/*Clear The Interrupt Flag To start The Previous Operation*/
SET_BIT(TWCR,7);
/*Wait Until The operation is finished and the flag is raised */
while(GET_BIT(TWCR,7)==0);
/*Check the operation status*/
if((TWSR&0xF8)!=0X08)
{
Local_Error=StartConditionError;
}
else
{
/*Do nothing*/
}
return Local_Error;
}
TWI_ErrStatus TWI_SendRepeatedStartCondition(void)
{
TWI_ErrStatus Local_Error=NoError;
/*send Start Condition*/
SET_BIT(TWCR,5);
/*Clear The Interrupt Flag To start The Previous Operation*/
SET_BIT(TWCR,7);
/*Wait Until The operation is finished and the flag is raised */
while(GET_BIT(TWCR,7)==0);
/*Check the operation status*/
if((TWSR&0xF8)!=0X10)
{
Local_Error=RepeatedStartError;
}
else
{
/*Do nothing*/
}
return Local_Error;
}
TWI_ErrStatus TWI_SendSlaveAddress_WithWrite(u8 Copy_u8SlaveAddress)
{
TWI_ErrStatus Local_Error=NoError;
/*Send 7bits Slave Address To the Bus*/
TWDR=Copy_u8SlaveAddress<<1;
/*Set the Write Request in the LSB bit in the data Register*/
CLR_BIT(TWDR,0);
/*Clear The start Condition Bit*/
CLR_BIT(TWCR,5);
/*Clear The Interrupt Flag To start The Previous Operation*/
SET_BIT(TWCR,7);
/*Wait Until The operation is finished and the flag is raised */
while(GET_BIT(TWCR,7)==0);
/*Check the operation status*/
if((TWSR&0xF8)!=0X18)
{
Local_Error=SlaveAddressWithWriteError;
}
else
{
/*Do nothing*/
}
return Local_Error;
}
TWI_ErrStatus TWI_SendSlaveAddress_WithRead(u8 Copy_u8SlaveAddress)
{
TWI_ErrStatus Local_Error=NoError;
/*Send 7bits Slave Address To the Bus*/
TWDR=Copy_u8SlaveAddress<<1;
/*Set the Read Request in the LSB bit in the data Register*/
SET_BIT(TWDR,0);
/*Clear The start Condition Bit*/
CLR_BIT(TWCR,5);
/*Clear The Interrupt Flag To start The Previous Operation*/
SET_BIT(TWCR,7);
/*Wait Until The operation is finished and the flag is raised */
while(GET_BIT(TWCR,7)==0);
/*Check the operation status*/
if((TWSR&0xF8)!=0X40)
{
Local_Error=SlaveAddressWithReadError;
}
else
{
/*Do nothing*/
}
return Local_Error;
}
TWI_ErrStatus TWI_MasterSendData(u8 Copy_u8Data)
{
TWI_ErrStatus Local_Error=NoError;
/*Send 8bits Data Byte To the Bus*/
TWDR=Copy_u8Data;
/*Clear The Interrupt Flag To start The Previous Operation*/
SET_BIT(TWCR,7);
/*Wait Until The operation is finished and the flag is raised */
while(GET_BIT(TWCR,7)==0);
/*Check the operation status*/
if((TWSR&0xF8)!=0X28)
{
Local_Error=MasterWriteError;
}
else
{
/*Do nothing*/
}
return Local_Error;
}
TWI_ErrStatus TWI_MasterReadData(u8* Copy_pu8Data)
{
TWI_ErrStatus Local_Error=NoError;
/*Clear The Interrupt Flag To start The Previous Operation*/
SET_BIT(TWCR,7);
/*Wait Until The operation is finished and the flag is raised */
while(GET_BIT(TWCR,7)==0);
/*Check the operation status*/
if((TWSR&0xF8)!=0X50)
{
Local_Error=MasterReadError;
}
else
{
*Copy_pu8Data=TWDR;
}
return Local_Error;
}
TWI_ErrStatus TWI_SendStopCondition(void)
{
/*Send Stop Condition*/
SET_BIT(TWCR,4);
/*Clear The Interrupt Flag To start The Previous Operation*/
SET_BIT(TWCR,7);
}