-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi2c总线at24c02E2PROM读写.c
138 lines (116 loc) · 1.63 KB
/
i2c总线at24c02E2PROM读写.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
/*******************************************************************/
/* AT24c02 */
/* E2PROM */
/* I2C总线 */
/*******************************************************************/
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit scl=P2^1;
sbit sda=P2^0;
void delay1(uint z) //z毫秒延时
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void delay()
{
;;
}
void start() //开始信号
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop() //停止信号
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void ack() //应答信号
{
uchar i=255;
scl=1;
while(sda==1&&i>0)i--;
sda=1;
scl=0;
}
void write_byte(uchar dat) //写一个字节
{
uchar i,aa;
scl=0;
delay();
aa=dat;
for(i=0;i<8;i++)
{
dat=dat<<1;
sda=CY;
scl=1;
delay();
scl=0;
delay();
sda=1;
delay();
}
}
uchar read_byte() //读一个字节
{
uchar i,x,y;
scl=0;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
x=sda;
y=y<<1|x;
scl=0;
delay();
sda=1;
delay();
}
return y;
}
void randon_write(uchar add,uchar dat) //指定地址写
{
start();
write_byte(0xa0);
ack();
write_byte(add);
ack();
write_byte(dat);
ack();
stop();
delay1(100);
}
uchar randon_read(uchar add) //指定地址读
{
uchar aa;
start();
write_byte(0xa0);
ack();
write_byte(add);
ack();
start();
write_byte(0xa1);
ack();
aa=read_byte();
stop();
delay1(100);
return aa;
}
void main()
{
randon_write(5,0x01);
P1=randon_read(5);
while(1);
}