📄 i2c2.lst
字号:
C51 COMPILER V7.07 I2C2 11/08/2007 10:23:05 PAGE 1
C51 COMPILER V7.07, COMPILATION OF MODULE I2C2
OBJECT MODULE PLACED IN I2c2.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE I2c2.c OPTIMIZE(2,SPEED) DEFINE(monitor51) DEBUG OBJECTEXTEND
stmt level source
1 #include "reg51.h"
2 #include "intrins.h"
3
4 #define uchar unsigned char
5 #define uint unsigned int
6 sbit SDA=P2^6;
7 sbit SCL=P2^5;
8
9 //函数声明
10 uchar i2c_read(uchar);
11 void i2c_write(uchar,uchar);
12
13 void i2c_send8bit(uchar);
14 uchar i2c_receive8bit(void);
15 void i2c_start(void);
16 void i2c_stop(void);
17 bit i2c_ack(void);
18 void delay(uint i);
19 //=======================================================
20 void main(void)
21 {
22 1 uchar dd;
23 1
24 1 i2c_write(0x00,0x55);
25 1 _nop_();
26 1
27 1 dd=i2c_read(0x00);
28 1 for(;;)
29 1 {}
30 1 }
31
32 /*=======================================================
33 i2c_write(地址,数据),写一个字节
34 =======================================================*/
35 void i2c_write(uchar Address,uchar Data)
36 {
37 1 do{
38 2 i2c_start();
39 2 i2c_send8bit(0xA0);
40 2 }while(i2c_ack());
41 1 i2c_send8bit(Address);
42 1 i2c_ack();
43 1 i2c_send8bit(Data);
44 1 i2c_ack();
45 1 i2c_stop();
46 1 return;
47 1 }
48 /*=======================================================
49 i2c_read(地址,数据),写一个字节
50 =======================================================*/
51 uchar i2c_read(uchar Address)
52 {
53 1 uchar c;
54 1 do{
55 2 i2c_start();
C51 COMPILER V7.07 I2C2 11/08/2007 10:23:05 PAGE 2
56 2 i2c_send8bit(0xA0);
57 2 }while(i2c_ack()); //=1,表示无确认,再次发送
58 1 i2c_send8bit(Address);
59 1 i2c_ack();
60 1
61 1 do{
62 2 i2c_start();
63 2 i2c_send8bit(0xA1);
64 2 }while(i2c_ack());
65 1 c=i2c_receive8bit();
66 1 i2c_ack();
67 1 i2c_stop();
68 1 return(c);
69 1 }
70 //=======================================================
71 //发送开始信号
72 void i2c_start(void)
73 {
74 1 SDA = 1;
75 1 _nop_();
76 1 _nop_();
77 1 _nop_();
78 1 SCL = 1;
79 1 _nop_();
80 1 _nop_();
81 1 _nop_();
82 1 SDA = 0;
83 1 _nop_();
84 1 _nop_();
85 1 _nop_();
86 1 SCL = 0;
87 1 return;
88 1 }
89 //发送结束信号
90 void i2c_stop(void)
91 {
92 1 SDA = 0;
93 1 _nop_();
94 1 _nop_();
95 1 _nop_();
96 1 SCL = 1;
97 1 _nop_();
98 1 _nop_();
99 1 _nop_();
100 1 SDA = 1;
101 1 return;
102 1 }
103 //发送接收确认信号
104 bit i2c_ack(void)
105 {
106 1 bit ack;
107 1 SDA = 1;
108 1 _nop_();
109 1 _nop_();
110 1 _nop_();
111 1 SCL = 1;
112 1 _nop_();
113 1 _nop_();
114 1 _nop_();
115 1 if(SDA==1)
116 1 ack = 1;
117 1 else
C51 COMPILER V7.07 I2C2 11/08/2007 10:23:05 PAGE 3
118 1 ack = 0;
119 1 SCL = 0;
120 1 _nop_();
121 1 _nop_();
122 1 _nop_();
123 1 return (ack);
124 1 }
125
126 //送八位数据
127 void i2c_send8bit(uchar b)
128 {
129 1 uchar a;
130 1 for(a=0;a<8;a++)
131 1 {
132 2 if(b<<1)
133 2 {SDA = 1;
134 3 _nop_();
135 3 _nop_();
136 3 _nop_();}
137 2 else
138 2 {SDA = 0;
139 3 _nop_();
140 3 _nop_();
141 3 _nop_();}
142 2 SCL = 1;
143 2 _nop_();
144 2 _nop_();
145 2 _nop_();
146 2 SCL = 0;
147 2 _nop_();
148 2 _nop_();
149 2 _nop_();
150 2 }
151 1 return;
152 1 }
153 //接收八位数据
154 uchar i2c_receive8bit(void)
155 {
156 1 uchar a;
157 1 uchar b=0;
158 1 for(a=0;a<8;a++)
159 1 {
160 2 SCL = 1;
161 2 _nop_();
162 2 _nop_();
163 2 _nop_();
164 2 b=b<<1;
165 2 if (SDA==1)
166 2 b=b|0x01; //按位或
167 2 SCL = 0;
168 2 _nop_();
169 2 _nop_();
170 2 _nop_();
171 2 }
172 1 return (b);
173 1 }
174 //=======================================================
175 void delay(uint i) //
176 {
177 1 while(--i);
178 1 }
C51 COMPILER V7.07 I2C2 11/08/2007 10:23:05 PAGE 4
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 273 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 11
IDATA SIZE = ---- ----
BIT SIZE = ---- 1
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -