📄 i2c.lst
字号:
C51 COMPILER V7.50 I2C 02/16/2009 09:18:45 PAGE 1
C51 COMPILER V7.50, COMPILATION OF MODULE I2C
OBJECT MODULE PLACED IN D:\VW\INTEGR~1\MAIN\I2C.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE D:\VW\INTEGR~1\MAIN\I2C.C DB SB OE
line level source
1 /*
2 +---------------------------------------------------+
3 | Project: TR2007 with UPSD3234A |
4 | File: I2C.c |
5 | Data: 2007-01-18 |
6 | Version: V 1.0 |
7 | Initial Editor: Li Jifeng |
8 +---------------------------------------------------+
9 | following environments are supported |
10 | Processor: UPSD3234A |
11 | Compiler: Keil uVision2 V7.50 Full |
12 | hardware: PH-2004 |
13 +---------------------------------------------------+
14 | Copyright (C) PH Ltd, 2006 |
15 | All Rights reserved |
16 +---------------------------------------------------+
17 | 功能简介: the I2C function |
18 +---------------------------------------------------+
19 */
20
21 #include "system.h"
22
23 /***********************************************************************************
24 函数名称:Delay_1_uS()
25 功能描述:延时子程序
26 参 数:无
27 ***********************************************************************************/
28 void Delay_1_uS(void)
29 {unsigned char data i=2;
30 1 while(i--);
31 1 }
32 /***********************************************************************************
33 函数名称:Delay_N_mS()
34 功能描述:延时子程序
35 参 数:n_milisecond
36 ***********************************************************************************/
37 void Delay_N_mS(unsigned int data n)
38 {unsigned char data i;
39 1 while(n--)
40 1 {i=10;
41 2 while(i--);
42 2 }
43 1 }
44 /***********************************************************************************
45 函数名称:I2C_Start()
46 功能描述:I2C起始信号子程序
47 参 数:无
48 ***********************************************************************************/
49 bit I2C_Start(void)
50 {Delay_1_uS();
51 1 I2C_SDA=1;
52 1 Delay_1_uS();
53 1 I2C_SCK=1;
54 1 Delay_1_uS();
55 1 if(I2C_SDA==0) return 0;
C51 COMPILER V7.50 I2C 02/16/2009 09:18:45 PAGE 2
56 1 if(I2C_SCK==0) return 0;
57 1 I2C_SDA=0;
58 1 Delay_1_uS();
59 1 I2C_SCK=0;
60 1 Delay_1_uS();
61 1 return 1;
62 1 }
63 /***********************************************************************************
64 函数名称:I2C_Stop()
65 功能描述:I2C停止信号子程序
66 参 数:无
67 ***********************************************************************************/
68 void I2C_Stop(void)
69 {Delay_1_uS();
70 1 I2C_SDA=0;
71 1 Delay_1_uS();
72 1 I2C_SCK=1;
73 1 Delay_1_uS();
74 1 I2C_SDA=1;
75 1 Delay_1_uS();
76 1 }
77 /***********************************************************************************
78 函数名称:I2C_Ack()
79 功能描述:I2C应答子程序
80 参 数:无
81 ***********************************************************************************/
82 void I2C_Ack(void)
83 {Delay_1_uS();
84 1 I2C_SDA=0;
85 1 Delay_1_uS();
86 1 I2C_SCK=1;
87 1 Delay_1_uS();
88 1 I2C_SCK=0;
89 1 Delay_1_uS();
90 1 }
91 /***********************************************************************************
92 函数名称:I2C_Nack()
93 功能描述:I2C非应答子程序
94 参 数:无
95 ***********************************************************************************/
96 void I2C_Nack(void)
97 {Delay_1_uS();
98 1 I2C_SDA=1;
99 1 Delay_1_uS();
100 1 I2C_SCK=1;
101 1 Delay_1_uS();
102 1 I2C_SCK=0;
103 1 Delay_1_uS();
104 1 }
105 /***********************************************************************************
106 函数名称:I2C_Send_Byte()
107 功能描述:I2C发送字节子程序
108 参 数:无
109 ***********************************************************************************/
110 bit I2C_Send_Byte(unsigned char data d)
111 {unsigned char data i=8;
112 1 bit bit_ack;
113 1 while(i--)
114 1 {Delay_1_uS();
115 2 if(d&0x80) I2C_SDA=1;
116 2 else I2C_SDA=0;
117 2 Delay_1_uS();
C51 COMPILER V7.50 I2C 02/16/2009 09:18:45 PAGE 3
118 2 I2C_SCK=1;
119 2 Delay_1_uS();
120 2 I2C_SCK=0;
121 2 d=d<<1;
122 2 }
123 1 Delay_1_uS();
124 1 I2C_SDA=1;
125 1 Delay_1_uS();
126 1 I2C_SCK=1;
127 1 Delay_1_uS();
128 1 bit_ack=I2C_SDA;
129 1 I2C_SCK=0;
130 1 Delay_1_uS();
131 1 return bit_ack;
132 1 }
133 /***********************************************************************************
134 函数名称:I2C_Receive_Byte()
135 功能描述:I2C接收字节子程序
136 参 数:无
137 ***********************************************************************************/
138 unsigned char I2C_Receive_Byte(void)
139 {unsigned char data i=8, d;
140 1 Delay_1_uS();
141 1 I2C_SDA=1;
142 1 while(i--)
143 1 {d=d<<1;
144 2 Delay_1_uS();
145 2 I2C_SCK=1;
146 2 Delay_1_uS();
147 2 if(I2C_SDA) d++;
148 2 I2C_SCK=0;
149 2 }
150 1 return d;
151 1 }
152 /***********************************************************************************
153 函数名称:AT24C128_write()
154 功能描述:24C128写数据子程序
155 参 数:AT24C128_address:24C128地址
156 dat_address:24C128数据地址
157 *dat:数据指针
158 j:数据长度
159 ***********************************************************************************/
160 bit AT24C128_write(unsigned int data dat_address,unsigned char *dat,unsigned char data n)
161 {
162 1 if(I2C_Start())
163 1 {if(!I2C_Send_Byte(0xa0))
164 2 {if(!I2C_Send_Byte(dat_address/256))
165 3 {if(!I2C_Send_Byte(dat_address%256))
166 4 {while(n--)
167 5 {if(!I2C_Send_Byte(*dat))
168 6 dat++;
169 6 else return 0;
170 6 }
171 5 I2C_Stop();
172 5 Delay_N_mS(50);
173 5 return 1;
174 5 }
175 4 else return 0;
176 4 }
177 3 else return 0;
178 3 }
179 2 else return 0;
C51 COMPILER V7.50 I2C 02/16/2009 09:18:45 PAGE 4
180 2 }
181 1 else return 0;
182 1 }
183 /***********************************************************************************
184 函数名称:AT24C128_read()
185 功能描述:24C128读数据子程序
186 参 数:AT24C128_address:24C128地址
187 dat_address:24C128数据地址
188 *dat:数据指针
189 j:数据长度
190 ***********************************************************************************/
191 bit AT24C128_read(unsigned int data dat_address,unsigned char *dat,unsigned int data n)
192 {
193 1 if(I2C_Start())
194 1 {if(!I2C_Send_Byte(0xa0))
195 2 {if(!I2C_Send_Byte(dat_address/256))
196 3 {if(!I2C_Send_Byte(dat_address%256))
197 4 {if(I2C_Start())
198 5 {if(!I2C_Send_Byte(0xa1))
199 6 {while(--n)
200 7 {*dat=I2C_Receive_Byte();
201 8 dat++;
202 8 I2C_Ack();
203 8 }
204 7 *dat=I2C_Receive_Byte();
205 7 I2C_Nack();
206 7 I2C_Stop();
207 7 Delay_N_mS(20);
208 7 return 1;
209 7 }
210 6 else return 0;
211 6 }
212 5 else return 0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -