|
|
|
|
C51 COMPILER V9.56.0.0 I2C 04/08/2022 02:58:55 PAGE 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C51 COMPILER V9.56.0.0, COMPILATION OF MODULE I2C
|
|
|
|
|
OBJECT MODULE PLACED IN i2c.OBJ
|
|
|
|
|
COMPILER INVOKED BY: D:\Keil_v5\C51\BIN\C51.EXE i2c.c OPTIMIZE(8,SPEED) BROWSE DEBUG OBJECTEXTEND TABS(2)
|
|
|
|
|
|
|
|
|
|
line level source
|
|
|
|
|
|
|
|
|
|
1 #include"i2c.h"
|
|
|
|
|
2
|
|
|
|
|
3 /*******************************************************************************
|
|
|
|
|
4 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : Delay10us()
|
|
|
|
|
5 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>ʱ10us
|
|
|
|
|
6 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>
|
|
|
|
|
7 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>
|
|
|
|
|
8 *******************************************************************************/
|
|
|
|
|
9
|
|
|
|
|
10 void Delay10us()
|
|
|
|
|
11 {
|
|
|
|
|
12 1 unsigned char a,b;
|
|
|
|
|
13 1 for(b=1;b>0;b--)
|
|
|
|
|
14 1 for(a=2;a>0;a--);
|
|
|
|
|
15 1
|
|
|
|
|
16 1 }
|
|
|
|
|
17 /*******************************************************************************
|
|
|
|
|
18 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : I2cStart()
|
|
|
|
|
19 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>ʼ<EFBFBD>źţ<EFBFBD><EFBFBD><EFBFBD>SCLʱ<EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD><EFBFBD>ڸߵ<EFBFBD>ƽ<EFBFBD>ڼ<EFBFBD>SDA<EFBFBD>źŲ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>½<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
20 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>
|
|
|
|
|
21 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>
|
|
|
|
|
22 * <EFBFBD><EFBFBD>ע : <EFBFBD><EFBFBD>ʼ֮<EFBFBD><EFBFBD>SDA<EFBFBD><EFBFBD>SCL<EFBFBD><EFBFBD>Ϊ0
|
|
|
|
|
23 *******************************************************************************/
|
|
|
|
|
24
|
|
|
|
|
25 void I2cStart()
|
|
|
|
|
26 {
|
|
|
|
|
27 1 SDA=1;
|
|
|
|
|
28 1 Delay10us();
|
|
|
|
|
29 1 SCL=1;
|
|
|
|
|
30 1 Delay10us();//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>SDA<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>>4.7us
|
|
|
|
|
31 1 SDA=0;
|
|
|
|
|
32 1 Delay10us();//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>>4us
|
|
|
|
|
33 1 SCL=0;
|
|
|
|
|
34 1 Delay10us();
|
|
|
|
|
35 1 }
|
|
|
|
|
36 /*******************************************************************************
|
|
|
|
|
37 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : I2cStop()
|
|
|
|
|
38 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>ֹ<EFBFBD>źţ<EFBFBD><EFBFBD><EFBFBD>SCLʱ<EFBFBD><EFBFBD><EFBFBD>źŸߵ<EFBFBD>ƽ<EFBFBD>ڼ<EFBFBD>SDA<EFBFBD>źŲ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
39 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>
|
|
|
|
|
40 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>
|
|
|
|
|
41 * <EFBFBD><EFBFBD>ע : <EFBFBD><EFBFBD><EFBFBD><EFBFBD>֮<EFBFBD><EFBFBD>SDA<EFBFBD><EFBFBD>SCL<EFBFBD><EFBFBD>Ϊ1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD>߿<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
42 *******************************************************************************/
|
|
|
|
|
43
|
|
|
|
|
44 void I2cStop()
|
|
|
|
|
45 {
|
|
|
|
|
46 1 SDA=0;
|
|
|
|
|
47 1 Delay10us();
|
|
|
|
|
48 1 SCL=1;
|
|
|
|
|
49 1 Delay10us();//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4.7us
|
|
|
|
|
50 1 SDA=1;
|
|
|
|
|
51 1 Delay10us();
|
|
|
|
|
52 1 }
|
|
|
|
|
53 /*******************************************************************************
|
|
|
|
|
54 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : I2cSendByte(unsigned char dat)
|
|
|
|
|
55 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : ͨ<EFBFBD><EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽڡ<EFBFBD><EFBFBD><EFBFBD>SCLʱ<EFBFBD><EFBFBD><EFBFBD>źŸߵ<EFBFBD>ƽ<EFBFBD>ڼ䣬<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>SDA<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȶ<EFBFBD>
|
|
|
|
|
C51 COMPILER V9.56.0.0 I2C 04/08/2022 02:58:55 PAGE 2
|
|
|
|
|
|
|
|
|
|
56 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : num
|
|
|
|
|
57 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : 0<EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܷ<EFBFBD><EFBFBD><EFBFBD>0
|
|
|
|
|
58 * <EFBFBD><EFBFBD>ע : <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>SCL=0,SDA=1
|
|
|
|
|
59 *******************************************************************************/
|
|
|
|
|
60
|
|
|
|
|
61 unsigned char I2cSendByte(unsigned char dat)
|
|
|
|
|
62 {
|
|
|
|
|
63 1 unsigned char a=0,b=0;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>255<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ1us<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ255us<EFBFBD><EFBFBD>
|
|
|
|
|
64 1 for(a=0;a<8;a++)//Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>8λ<EFBFBD><EFBFBD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ӧ<EFBFBD>𣬱<EFBFBD>ʾ<EFBFBD><EFBFBD><EFBFBD>ս<EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
81 2 {
|
|
|
|
|
82 3 SCL=0;
|
|
|
|
|
83 3 Delay10us();
|
|
|
|
|
84 3 return 0;
|
|
|
|
|
85 3 }
|
|
|
|
|
86 2 }
|
|
|
|
|
87 1 SCL=0;
|
|
|
|
|
88 1 Delay10us();
|
|
|
|
|
89 1 return 1;
|
|
|
|
|
90 1 }
|
|
|
|
|
91 /*******************************************************************************
|
|
|
|
|
92 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : I2cReadByte()
|
|
|
|
|
93 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : ʹ<EFBFBD><EFBFBD>I2c<EFBFBD><EFBFBD>ȡһ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
|
|
|
|
94 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>
|
|
|
|
|
95 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : dat
|
|
|
|
|
96 * <EFBFBD><EFBFBD>ע : <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>SCL=0,SDA=1.
|
|
|
|
|
97 *******************************************************************************/
|
|
|
|
|
98
|
|
|
|
|
99 unsigned char I2cReadByte()
|
|
|
|
|
100 {
|
|
|
|
|
101 1 unsigned char a=0,dat=0;
|
|
|
|
|
102 1 SDA=1; //<EFBFBD><EFBFBD>ʼ<EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>֮<EFBFBD><EFBFBD>SCL<EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
|
|
|
|
|
103 1 Delay10us();
|
|
|
|
|
104 1 for(a=0;a<8;a++)//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>8<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
|
|
|
|
|
105 1 {
|
|
|
|
|
106 2 SCL=1;
|
|
|
|
|
107 2 Delay10us();
|
|
|
|
|
108 2 dat<<=1;
|
|
|
|
|
109 2 dat|=SDA;
|
|
|
|
|
110 2 Delay10us();
|
|
|
|
|
111 2 SCL=0;
|
|
|
|
|
112 2 Delay10us();
|
|
|
|
|
113 2 }
|
|
|
|
|
114 1 return dat;
|
|
|
|
|
115 1 }
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
|
|
C51 COMPILER V9.56.0.0 I2C 04/08/2022 02:58:55 PAGE 3
|
|
|
|
|
|
|
|
|
|
118 /*******************************************************************************
|
|
|
|
|
119 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : void At24c02Write(unsigned char addr,unsigned char dat)
|
|
|
|
|
120 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>24c02<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַд<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
121 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>
|
|
|
|
|
122 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>
|
|
|
|
|
123 *******************************************************************************/
|
|
|
|
|
124
|
|
|
|
|
125 void At24c02Write(unsigned char addr,unsigned char dat)
|
|
|
|
|
126 {
|
|
|
|
|
127 1 I2cStart();
|
|
|
|
|
128 1 I2cSendByte(0xa0);//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
129 1 I2cSendByte(addr);//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫд<EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
130 1 I2cSendByte(dat); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
131 1 I2cStop();
|
|
|
|
|
132 1 }
|
|
|
|
|
133 /*******************************************************************************
|
|
|
|
|
134 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : unsigned char At24c02Read(unsigned char addr)
|
|
|
|
|
135 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>ȡ24c02<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
136 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>
|
|
|
|
|
137 * <EFBFBD><EFBFBD><EFBFBD><EFBFBD> : <EFBFBD><EFBFBD>
|
|
|
|
|
138 *******************************************************************************/
|
|
|
|
|
139
|
|
|
|
|
140 unsigned char At24c02Read(unsigned char addr)
|
|
|
|
|
141 {
|
|
|
|
|
142 1 unsigned char num;
|
|
|
|
|
143 1 I2cStart();
|
|
|
|
|
144 1 I2cSendByte(0xa0); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
145 1 I2cSendByte(addr); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>ȡ<EFBFBD>ĵ<EFBFBD>ַ
|
|
|
|
|
146 1 I2cStart();
|
|
|
|
|
147 1 I2cSendByte(0xa1); //<EFBFBD><EFBFBD><EFBFBD>Ͷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
148 1 num=I2cReadByte(); //<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
149 1 I2cStop();
|
|
|
|
|
150 1 return num;
|
|
|
|
|
151 1 }
|
|
|
|
|
152
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MODULE INFORMATION: STATIC OVERLAYABLE
|
|
|
|
|
CODE SIZE = 201 ----
|
|
|
|
|
CONSTANT SIZE = ---- ----
|
|
|
|
|
XDATA SIZE = ---- ----
|
|
|
|
|
PDATA SIZE = ---- ----
|
|
|
|
|
DATA SIZE = ---- ----
|
|
|
|
|
IDATA SIZE = ---- ----
|
|
|
|
|
BIT SIZE = ---- ----
|
|
|
|
|
END OF MODULE INFORMATION.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
|