-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathX_IIC.cpp
217 lines (163 loc) · 3.25 KB
/
X_IIC.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "X_IIC.h"
#define X_SDA_SET PORTD |= 0x20
#define X_SDA_CLEAR PORTD &= 0xDF
#define X_SCL_SET PORTD |= 0x40
#define X_SCL_CLEAR PORTD &= 0xBF
#define X_SDA_INPUT DDRD &= 0xDF
#define X_SDA_OUTPUT DDRD |= 0x20
#define X_SCL_INPUT DDRD &= 0xBF
#define X_SCL_OUTPUT DDRD |= 0x40
#define X_SDA_READ PIND & 0x20
#define X_PORT_DDR DDRD
#define X_SCK_H {X_SCL_SET; }
#define X_SCK_L {X_SCL_CLEAR; }
#define X_SDA_H {X_SDA_OUTPUT;X_SDA_SET; }
#define X_SDA_L {X_SDA_OUTPUT;X_SDA_CLEAR; }
#define X_READ_SDA X_SDA_READ
#define AS5600_DEFAULT_ADDRESS 0x36
void X_IIC_Init(void)
{
X_SDA_OUTPUT;
X_SCL_OUTPUT;
X_SDA_SET;
X_SCL_SET;
}
void X_IIC_Start(void)
{
X_SDA_H;
X_SCK_H;
delayMicroseconds(5);
X_SDA_L;
delayMicroseconds(5);
X_SCK_L;
delayMicroseconds(2);
}
void X_IIC_Stop(void)
{
X_SCK_L;
X_SDA_L;
delayMicroseconds(5);
X_SCK_H;
delayMicroseconds(5);
X_SDA_H;
delayMicroseconds(5);
}
u8 X_IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
X_SDA_H;delayMicroseconds(2);
X_SCK_H;delayMicroseconds(2);
X_SDA_INPUT;
while(X_READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
X_IIC_Stop();
return 1;
}
}
X_SCK_L;
delayMicroseconds(2);
return 0;
}
void X_IIC_Ack(void)
{
X_SCK_L;
X_SDA_L;
delayMicroseconds(2);
X_SCK_H;
delayMicroseconds(5);
X_SCK_L;
delayMicroseconds(2);
X_SDA_H;
delayMicroseconds(2);
}
void X_IIC_Nack(void)
{
X_SCK_L;
X_SDA_H;
delayMicroseconds(2);
X_SCK_H;
delayMicroseconds(5);
X_SCK_L;
delayMicroseconds(2);
X_SDA_H;
delayMicroseconds(2);
}
void X_IIC_SendByte(u8 Txdata)
{
u8 i;
for(i=0;i<8;i++)
{
X_SCK_L;
if(Txdata&0x80)
X_SDA_H
else
X_SDA_L
delayMicroseconds(5);
X_SCK_H;
delayMicroseconds(5);
X_SCK_L;
Txdata<<=1;
delayMicroseconds(5);
}
delayMicroseconds(2);
X_SDA_H;
delayMicroseconds(2);
}
u8 X_IIC_ReadByte(u8 ack)
{
u8 i,receive=0;
delayMicroseconds(2);
X_SDA_H;
delayMicroseconds(2);
for(i=0;i<8;i++)
{
X_SCK_L;
delayMicroseconds(5);
X_SCK_H;
receive<<=1;
delayMicroseconds(5);
X_SDA_INPUT;
if(X_READ_SDA)
receive|=0x01;
else
receive&=0xfe;
delayMicroseconds(5);
}
if(!ack)
X_IIC_Nack();
else
X_IIC_Ack();
X_SCK_L;
delayMicroseconds(2);
return receive;
}
void X_IIC_Write_OneByte(u8 deviceaddr,u8 writeaddr,u8 writedata)
{
X_IIC_Start();
X_IIC_SendByte(deviceaddr&0xfe);
X_IIC_Wait_Ack();
X_IIC_SendByte(writeaddr);
X_IIC_Wait_Ack();
X_IIC_SendByte(writedata);
X_IIC_Wait_Ack();
X_IIC_Stop();
delay(10);
}
u8 X_IIC_Read_OneByte(u8 deviceaddr,u8 readaddr)
{
u8 temp;
X_IIC_Start();
X_IIC_SendByte(deviceaddr&0xfe);
X_IIC_Wait_Ack();
X_IIC_SendByte(readaddr);
X_IIC_Wait_Ack();
X_IIC_Start();
X_IIC_SendByte(deviceaddr|0x01);
X_IIC_Wait_Ack();
temp=X_IIC_ReadByte(0);
X_IIC_Stop();
return temp;
}