?? i2c.lst
字號:
C51 COMPILER V7.07 I2C 06/15/2006 18:53:53 PAGE 1
C51 COMPILER V7.07, COMPILATION OF MODULE I2C
OBJECT MODULE PLACED IN .\Output\i2c.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE i2c.c BROWSE DEBUG OBJECTEXTEND PRINT(.\LST\i2c.lst) TABS(3) OBJECT(.\Outpu
-t\i2c.obj)
stmt level source
1 #include <at89x52.H> // SFR definition header file
2
3 #include "SystemEvent.h"
4 #include "Function.h"
5 #include "ExtVar.h"
6 #include <intrins.h>
7 // ********************************************************************************
8 // Function: i2c_start;
9 // Description: Sending a start waveform to I2C bus.
10 // Input parameters: None.
11 // Output parameters: None.
12 // Return values: None.
13 // ********************************************************************************
14 void i2cStart(void){
15 1
16 1 _sda=1;
17 1 _sclk=1;
18 1 _nop_(); _nop_(); _nop_(); _nop_();
19 1 _sda=0;
20 1 _nop_(); _nop_(); _nop_(); _nop_();
21 1 }
22
23 // ********************************************************************************
24 // Function: i2c_stop;
25 // Description: Sending a stop waveform to I2C bus.
26 // Input parameters: None.
27 // Output parameters: None.
28 // Return values: None.
29 // ********************************************************************************
30 void i2cStop(void){
31 1 unsigned int _timeOut;
32 1 _timeOut=0;
33 1 _sda=0;
34 1 _nop_(); _nop_(); _nop_(); _nop_();
35 1 _sclk=1;
36 1
37 1 if((_IDCodeOfI2C==0xb8)||(_IDCodeOfI2C==0xba)){
38 2 while((_sclk==0)&&(_timeOut<0x4f00))
39 2 {
40 3 _nop_();
41 3 _timeOut++;
42 3 }
43 2 }
44 1 _sda=1;
45 1 _nop_(); _nop_(); _nop_();_nop_();
46 1 }
47
48 // ********************************************************************************
49 // Function: i2cTransmit;
50 // Description: Transmit a byte to the I2C slaver.
51 // Input parameters: unsigned char valueX: value to transmit.
52 // Output parameters: unsigned char i: i=0; Ack from slaver was receved
53 // i=1; Ack was not receved
54 // Return values: None.
C51 COMPILER V7.07 I2C 06/15/2006 18:53:53 PAGE 2
55 // ********************************************************************************
56 unsigned char i2cTransmit( unsigned char valueX )
57 {
58 1 unsigned char i;
59 1 unsigned int _Timeout;
60 1 unsigned char value=valueX;
61 1 _sda=0;
62 1 _sclk=0;
63 1 _nop_(); _nop_(); _nop_(); _nop_();
64 1 for (i=0; i<8; i++){
65 2 _sclk=0;
66 2 _nop_(); _nop_(); _nop_(); _nop_();
67 2 if (value & 0x80) _sda=1;
68 2 else _sda=0;
69 2 _nop_(); _nop_(); _nop_(); _nop_();
70 2 _sclk=1;
71 2 _nop_(); _nop_(); _nop_(); _nop_();
72 2 value <<= 1;
73 2 }
74 1 _sclk=0;
75 1 _nop_(); _nop_(); _nop_(); _nop_();
76 1 _sda=1;
77 1 _nop_(); _nop_(); _nop_(); _nop_();
78 1 _Timeout=0;
79 1 _sclk=1;
80 1 while((_sda==1)&&(_Timeout<0x2)){
81 2 _Timeout++;
82 2 }
83 1 i=(unsigned char)_sda;
84 1 _nop_(); _nop_(); _nop_(); _nop_();
85 1 _sclk=0;
86 1 _nop_(); _nop_(); _nop_(); _nop_();
87 1 return(i);
88 1 }
89
90 // ********************************************************************************
91 // Function: i2cLocate;
92 // Description: Transmit ID and sub addr to the I2C slaver.
93 // Input parameters: unsigned char slave_addr: I2C device slave ID.
94 // unsigned char sub_addr: sub address to access of the I2C slaver.
95 // Output parameters: unsigned char i: i=1; Ack from slave was receved
96 // i=0; Ack was not receved
97 // Return values: None.
98 // ********************************************************************************
99 unsigned char i2cLocate( unsigned char slave_addr, unsigned char sub_addr )
100 {
101 1 i2cStart();
102 1 if(i2cTransmit(slave_addr)) { i2cStop(); return(FALSE); }
103 1 else { if(i2cTransmit(sub_addr)) { i2cStop(); return(FALSE);}}
104 1 return(TRUE);
105 1 }
106
107
108 // ********************************************************************************
109 // Function: i2cNotAck;
110 // Description: Return a "Hight" to I2C slaver .
111 // Input parameters: None.
112 // Output parameters: None.
113 // Return values: None.
114 // ********************************************************************************
115 void i2cNotAck( void )
116 {
C51 COMPILER V7.07 I2C 06/15/2006 18:53:53 PAGE 3
117 1 _sclk=0;
118 1 _sda=1;
119 1 _sclk=1;
120 1 _sclk=0;
121 1 }
122
123
124 // ********************************************************************************
125 // Function: i2cReceive;
126 // Description: Return 1 byte of data which received from I2C bus.
127 // Input parameters: None.
128 // Output parameters: unsigned char value: data been received from I2C bus.
129 // Return values: None.
130 // ********************************************************************************
131 unsigned char i2cReceive( void )
132 {
133 1 /*Read ack.*/
134 1 unsigned char i;
135 1 unsigned char value;
136 1 _sclk=0;
137 1 /*Set value bit by bit...MSB first*/
138 1 value = 0;
139 1 for (i=0; i<8; i++)
140 1 {
141 2 value <<= 1;
142 2 _sclk=0;
143 2 if(_sda==1) value|=0x01;
144 2 _sclk=1;
145 2 }
146 1 _sclk=0;
147 1 return(value);
148 1 }
149
150 // ********************************************************************************
151 // Function: i2c_write;
152 // Description: Transmit 1 byte of data to slave end with assigned ID and sub address.
-
153 // Input parameters: unsigned char slave_addr: I2C slave ID of destination chip.
154 // unsigned char sub_addr: sub address of destination chip.
155 // unsigned char _Count: count of data to write
156 // unsigned char *_DataX: pointer to the data stream will be tramsitted.
157 // Output parameters: None.
158 // Return values: None.
159 // ********************************************************************************
160 void i2c_write( unsigned char slave_addr, unsigned char sub_addr,unsigned char _Count,unsigned char *_Data
-X)
161 {
162 1 unsigned int _ix;
163 1 i2cStop();
164 1 if(((slave_addr&0xf0)!=0x20)&&(slave_addr!=0x78)&&((slave_addr&0xf0)!=0x60)){
165 2 if (i2cLocate(slave_addr, sub_addr )){
166 3 for(_ix=0;_ix<(unsigned int)_Count;_ix++)
167 3 { i2cTransmit(*(_DataX+(unsigned int)(_ix))); }
168 3 }
169 2 i2cStop();
170 2 }
171 1 else {
172 2 for(_ix=0;_ix<_Count;_ix++) {
173 3 if (i2cLocate(slave_addr, sub_addr+_ix)) i2cTransmit(*(_DataX+_ix));
174 3 i2cStop();
175 3 }
176 2 }
C51 COMPILER V7.07 I2C 06/15/2006 18:53:53 PAGE 4
177 1 }
178
179 // ********************************************************************************
180 // Function: i2cAck;
181 // Description: Return a "Low" to I2C slaver as a acknowledgment.
182 // Input parameters: None.
183 // Output parameters: None.
184 // Return values: None.
185 // ********************************************************************************
186 void i2cAck( void )
187 { //receive state, ack set by master
188 1 _sclk=0;
189 1 _sda=0;
190 1 _sclk=1;
191 1 _sclk=0;
192 1 _sda=1;
193 1 }
194
195 // ********************************************************************************
196 // Function: i2c_read;
197 // Description: Read data stream from I2C slaver.
198 // Input parameters: unsigned char slave_addr: I2C slave ID of destination chip.
199 // unsigned char sub_addr: sub address of destination chip.
200 // unsigned char _Count: count of data to write
201 // unsigned char *_DataX: pointer to the data stream will be read.
202 // Output parameters: None.
203 // Return values: None.
204 // ********************************************************************************
205 void i2c_read( unsigned char slave_addr, unsigned char sub_addr,unsigned char _Count,unsigned char *_Data
-X )
206 {
207 1 unsigned char _ix;
208 1 if(((slave_addr&0xf0)!=0x20)&&(slave_addr!=0x78)&&((slave_addr&0xf0)!=0x60)){
209 2 if (i2cLocate(slave_addr, sub_addr)) {
210 3 i2cStart();
211 3 if(i2cTransmit((unsigned char)(slave_addr | 0x0001))){
212 4 i2cStop();
213 4 return;
214 4 }
215 3 for(_ix=0;_ix<_Count;_ix++) {
216 4 *(_DataX+(unsigned int)_ix) = i2cReceive();
217 4 if(_ix==(_Count-1)) i2cNotAck();
218 4 else i2cAck();
219 4 }
220 3 i2cStop();
221 3 }
222 2 }
223 1 else {
224 2 for(_ix=0;_ix<_Count;_ix++) {
225 3 if (i2cLocate(slave_addr,sub_addr+_ix)){
226 4 i2cStart();
227 4 if(i2cTransmit(slave_addr|0x0001)){
228 5 i2cStop();
229 5 return;
230 5 }
231 4 *(_DataX+(unsigned int)_ix) = i2cReceive();
232 4 i2cNotAck(); i2cStop();
233 4 }
234 3 }
235 2 }
236 1 }
237
C51 COMPILER V7.07 I2C 06/15/2006 18:53:53 PAGE 5
238
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 539 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = ---- 15
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 + -