📄 i2c.lst
字号:
C51 COMPILER V6.23a I2C 12/13/2002 12:45:25 PAGE 1
C51 COMPILER V6.23a, COMPILATION OF MODULE I2C
OBJECT MODULE PLACED IN I2c.OBJ
COMPILER INVOKED BY: D:\KEIL\C51\BIN\C51.EXE I2c.c BROWSE DEBUG OBJECTEXTEND
stmt level source
1 #include "reg51.h"
2 #include "intrins.h"
3
4 #define salve_addr 0xa0
5 #define read 0x01
6 #define write 0x00;
7
8 void I2c_Start();
9 void I2c_Stop();
10 void I2c_Wait();
11 void Ack(bit );
12 bit Send_Byte(unsigned char);
13 bit Read_Byte(unsigned char *);
14
15 extern unsigned char error;
16 extern unsigned char data intr[6];
17 sbit Sda = P1^6;
18 sbit Scl = P1^7;
19
20 bit I2c_Read(unsigned char address,unsigned char count)
21 {
22 1 unsigned char mode,i;
23 1
24 1 mode = salve_addr | write;
25 1 I2c_Start(); error++;
26 1 if (Send_Byte(mode) == 0)
27 1 { error++;
28 2 if (Send_Byte(address) == 0)
29 2 { error++;
30 3 I2c_Start();
31 3 mode = salve_addr | read;
32 3 if (Send_Byte(mode) == 0)
33 3 { error++;
34 4 for (i = 0;i < count - 1;i++)
35 4 { error++;
36 5 Read_Byte(&intr[i]);
37 5 Ack(0);
38 5 }
39 4 Read_Byte(&intr[i]);
40 4 Ack(1);
41 4 if (i == count - 1)
42 4 {
43 5 I2c_Stop();
44 5 return 0;
45 5 }
46 4 }
47 3 }
48 2 }
49 1 I2c_Stop();
50 1 return 1; // error
51 1 }
52
53 bit I2c_Write(unsigned char address,unsigned char Write_Data)
54 {
55 1 unsigned char mode;
C51 COMPILER V6.23a I2C 12/13/2002 12:45:25 PAGE 2
56 1
57 1 mode = salve_addr;
58 1 mode = mode | write;
59 1 I2c_Start();
60 1 if (Send_Byte(mode) == 0)
61 1 {
62 2 if (Send_Byte(address) == 0)
63 2 if (Send_Byte(Write_Data) == 0)
64 2 {
65 3 I2c_Stop();
66 3 return 0;
67 3 }
68 2 }
69 1 I2c_Stop();
70 1 return 1;
71 1 }
72
73 void I2c_Start()
74 {
75 1 Sda = 1;
76 1 Scl = 1;
77 1 I2c_Wait();
78 1 Sda = 0;
79 1 I2c_Wait();
80 1 Scl = 0;
81 1 }
82
83 void I2c_Stop()
84 {
85 1 Sda = 0;
86 1 I2c_Wait();
87 1 Scl = 1;
88 1 I2c_Wait();
89 1 Sda = 1;
90 1 }
91
92 void I2c_Wait()
93 {
94 1 _nop_();
95 1 _nop_();
96 1 }
97
98 bit Send_Byte(unsigned char Send_Data)
99 {
100 1 unsigned char i;
101 1 bit b;
102 1
103 1 for (i = 0;i < 8;i++)
104 1 {
105 2 if (Send_Data & 0x80)
106 2 Sda = 1;
107 2 else
108 2 Sda = 0;
109 2 Send_Data = Send_Data << 1;
110 2 Scl = 1;
111 2 I2c_Wait();
112 2 Scl = 0;
113 2 I2c_Wait();
114 2 }
115 1 Sda = 1;
116 1 I2c_Wait();
117 1 Scl = 1;
C51 COMPILER V6.23a I2C 12/13/2002 12:45:25 PAGE 3
118 1 I2c_Wait();
119 1 b = Sda;
120 1 Scl = 0;
121 1 I2c_Wait();
122 1 return b;
123 1 }
124
125 bit Read_Byte(unsigned char *Read_Data)
126 {
127 1 unsigned char i,temp = 0;
128 1
129 1 for(i = 0;i < 8;i++)
130 1 {
131 2 Scl = 1;
132 2 I2c_Wait();
133 2 temp <<= 1;
134 2 if (Sda == 1)
135 2 temp = temp | 0x01;
136 2 Scl = 0;
137 2 I2c_Wait();
138 2 }
139 1 *Read_Data = temp;
140 1 return 0;
141 1 }
142
143 void Ack(bit b)
144 {
145 1 Sda = b;
146 1 Scl = 1;
147 1 I2c_Wait();
148 1 Scl = 0;
149 1 Sda = 1;
150 1 }
151
152 bit Read_Test(unsigned char address)
153 {
154 1 bit b;
155 1 I2c_Start();
156 1 b = Send_Byte(0xa0);
157 1 b |= Send_Byte(address);
158 1 I2c_Start();
159 1 b |= Send_Byte(0xa1);
160 1 Read_Byte(&intr[0]);
161 1 Ack(0);
162 1 Read_Byte(&intr[1]);
163 1 Ack(0);
164 1 Read_Byte(&intr[2]);
165 1 Ack(1);
166 1 I2c_Stop();
167 1 return b;
168 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 353 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 8
IDATA SIZE = ---- ----
BIT SIZE = ---- 3
END OF MODULE INFORMATION.
C51 COMPILER V6.23a I2C 12/13/2002 12:45:25 PAGE 4
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -