📄 p8574.lst
字号:
C51 COMPILER V7.50 P8574 12/22/2008 17:00:34 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE P8574
OBJECT MODULE PLACED IN P8574.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE P8574.c BROWSE DEBUG OBJECTEXTEND
line level source
1 #include <intrins.h>
2 #include <reg52.h>
3
4 sbit SCL=P3^7;
5 sbit SDA=P3^6;
6
7 void NOP(void);
8 void I2cStartBit(void);
9 void I2cStopBit(void);
10 void I2cSendAck(void);
11 void I2cSendNAck(void);
12 void I2cTestAck(void);
13 void I2cWriteByte(unsigned char OneByte);
14 unsigned char I2cReadByte(void);
15 void I2cWriteStringA(unsigned char slaw,unsigned char *Buffer,unsigned char Index);
16 void I2cWriteString(unsigned char slaw,unsigned char *Buffer,unsigned char Length);
17 void I2cReadString(unsigned char slar,unsigned char *Buffer,unsigned char Length);
18
19 void NOP(void)
20 {
21 1 _nop_();
22 1 _nop_();
23 1 _nop_();
24 1 _nop_();
25 1 _nop_();
26 1 _nop_();
27 1 _nop_();
28 1 _nop_();
29 1 _nop_();
30 1 _nop_();
31 1 }
32
33 void I2cStartBit(void) //start the i2c bus
34 {
35 1 SDA=1;
36 1 SCL=1;
37 1 NOP();
38 1 SDA=0;
39 1 NOP();
40 1 SCL=0;
41 1 }
42
43 void I2cStopBit(void) //stop the i2c bus
44 {
45 1 SDA=0;
46 1 SCL=1;
47 1 NOP();
48 1 SDA=1;
49 1 NOP();
50 1 SCL=0;
51 1 }
52
53 void I2cSendAck(void) //send the answer bit
54 {
55 1 SDA=0;
C51 COMPILER V7.50 P8574 12/22/2008 17:00:34 PAGE 2
56 1 SCL=1;
57 1 NOP();
58 1 SCL=0;
59 1 SDA=1;
60 1 }
61
62 void I2cSendNAck(void) //send the not answer bit
63 {
64 1 SDA=1;
65 1 SCL=1;
66 1 NOP();
67 1 SCL=0;
68 1 SDA=0;
69 1 }
70
71 void I2cTestAck(void) //test the answer bit
72 {
73 1 SDA=1;
74 1 SCL=1;
75 1 F0=0;
76 1 if(SDA==1)
77 1 F0=1;
78 1 SCL=0;
79 1 NOP();
80 1 }
81
82 void I2cWriteByte(unsigned char OneByte) //send one byte to i2c bus
83 {
84 1 unsigned char i;
85 1 for(i=0;i<8;i++)
86 1 {
87 2 if((OneByte&0x80)>0)
88 2 {
89 3 SDA=1;
90 3 SCL=1;
91 3 NOP();
92 3 NOP();
93 3 NOP();
94 3 NOP();
95 3 SCL=0;
96 3 SDA=0;
97 3 }
98 2 else
99 2 {
100 3 SDA=0;
101 3 SCL=1;
102 3 NOP();
103 3 NOP();
104 3 NOP();
105 3 NOP();
106 3 SCL=0;
107 3 }
108 2 OneByte=OneByte<<1;
109 2 }
110 1 }
111
112 unsigned char I2cReadByte(void) //read one byte from i2c bus
113 {
114 1 unsigned char nn=0xff,mm=0x80,uu=0x7f;
115 1 unsigned char j;
116 1
117 1 for(j=0;j<8;j++)
C51 COMPILER V7.50 P8574 12/22/2008 17:00:34 PAGE 3
118 1 {
119 2 SDA=1;
120 2 SCL=1;
121 2 if(SDA==0)
122 2 nn=(nn&uu);
123 2 else
124 2 nn=(nn|mm);
125 2 nn=_crol_(nn,1);
126 2 SCL=0;
127 2 }
128 1
129 1 return(nn);
130 1 }
131
132 void I2cWriteStringA(unsigned char slaw,unsigned char *Buffer,unsigned char Index)
133 {
134 1 do
135 1 {
136 2 I2cStartBit();
137 2 I2cWriteByte(slaw);
138 2 I2cTestAck();
139 2 }
140 1 while(F0==1);
141 1 I2cWriteByte(Buffer[Index]);
142 1 I2cTestAck();
143 1 }
144
145 void I2cWriteString(unsigned char slaw,unsigned char *Buffer,unsigned char Length) //send n bytes to i2c
-bus
146 {
147 1 unsigned char k;
148 1 do
149 1 {
150 2 I2cStartBit();
151 2 I2cWriteByte(slaw);
152 2 I2cTestAck();
153 2 }
154 1 while(F0==1);
155 1 for(k=0;k<Length;k++)
156 1 {
157 2 I2cWriteByte(Buffer[k]); //////////////??????????
158 2 I2cTestAck();
159 2 while(F0==1)
160 2 I2cWriteStringA(slaw,Buffer,k);
161 2 }
162 1 I2cStopBit();
163 1 }
164
165 void I2cReadString(unsigned char slar,unsigned char *Buffer,unsigned char Length) //read n bytes from i2c
- bus
166 {
167 1 unsigned char OneByte;
168 1 unsigned char i;
169 1 do
170 1 {
171 2 I2cStartBit();
172 2 I2cWriteByte(slar);
173 2 I2cTestAck();
174 2 }
175 1 while(F0==1);
176 1 for(i=0;i<Length-1;i++)
177 1 {
C51 COMPILER V7.50 P8574 12/22/2008 17:00:34 PAGE 4
178 2 OneByte=I2cReadByte();
179 2 Buffer[i]=OneByte;
180 2 I2cSendAck();
181 2 }
182 1 OneByte=I2cReadByte();
183 1 Buffer[i]=OneByte;
184 1 I2cSendNAck();
185 1 I2cStopBit();
186 1 }
187
188 void main(void)
189 {
190 1 //unsigned char temp3;
191 1 unsigned char temp[2];
192 1 temp[0]=0x55;
193 1 temp[1]=0x55;
194 1 //PCF8574_Init();
195 1 while(1)
196 1 {
197 2 //temp3=IC_Read_Data();
198 2 I2cWriteString(0x40,temp,2);
199 2 //Delay();
200 2 }
201 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 375 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 19
IDATA SIZE = ---- ----
BIT SIZE = ---- ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -