📄 i2c.lst
字号:
C51 COMPILER V7.50 I2C 08/11/2008 14:05:22 PAGE 1
C51 COMPILER V7.50, 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(.\Output\I2C.lst) OBJECT(.\Output\I2C
-.obj)
line level source
1 /*--------------------------------------------------------------------------
2 I2C.C
3 C file for I2C
4
5 History:
6 07/20/2006 : First Version V0.1 ---HsinChu Office
7
8 Copyright (c) 1998-2006 AverLogic Inc
9 All rights reserved.
10 --------------------------------------------------------------------------*/
11 #include <REG52.H>
12 #include <intrins.h>
13 #include "DATATYPE.H"
14 #include "DEFINE.H"
15 #include "I2C.H"
16
17 // ********************************************************************************
18 // Function: I2CStart;
19 // Description: Sending a start waveform to I2C bus. Start condition is activated when
20 // 1) SCL is set to HIGH 2) SDA Shifts from HIGH TO LOW State.
21 // Input parameters: None.
22 // Return values: None.
23 // ********************************************************************************
24
25 void I2CStart(void)
26 {
27 1 sda=1;
28 1 scl=1;
29 1 I2CWait();
30 1 sda=0;
31 1 I2CWait();
32 1 scl=0;
33 1 }
34
35 // ********************************************************************************
36 // Function: I2CStop;
37 // Description: Sending a stop waveform to I2C bus. Stop condition is activated when
38 // 1) SCL is set to HIGH 2) SDA Shifts from Low to HIGH State.
-
39 // Input parameters: None.
40 // Return values: None.
41 // ********************************************************************************
42
43 void I2CStop(void)
44 {
45 1 sda=0;
46 1 I2CWait();
47 1 scl=1;
48 1 I2CWait();
49 1 sda=1;
50 1 }
51
52 // ********************************************************************************
53 // Function: I2CStop;
C51 COMPILER V7.50 I2C 08/11/2008 14:05:22 PAGE 2
54 // Description: Delay time or waiting for sometime.
55 // Input parameters: None.
56 // Return values: None.
57 // ********************************************************************************
58
59 void I2CWait(void)
60 {
61 1 // _nop_();
62 1 // _nop_();
63 1 }
64
65 // ********************************************************************************
66 // Function: I2CAck;
67 // Description: Return a "Low" to I2C slave as a acknowledgment.
68 // During the acknowledge clock pulse, the master(microcontroller) puts a resistive high level on
69 // SDA line. The peripheral (audio processor) that acknowledges has to pull-down(low) the SDA line
70 // during the acknowledge clock pulse so that the SDA line is in a stable low state during this clock
71 // pulse.
72 // Input parameters: Boolean - bAck.
73 // Return values: None.
74 // ********************************************************************************
75
76 void I2CAck(Bool bAck)
77 {
78 1 sda=bAck;
79 1 scl=1;
80 1 I2CWait();
81 1 scl=0;
82 1 }
83
84
85 // ********************************************************************************
86 // Function: I2CTransmit
87 // Description: SDA will be high if each bit of data is '1'. SDA will be low if each bit of data
88 // is '0'. Send the SDA data first and SCLK is high and then low state. Repeat for 8 times.
89 // Input parameters: Byte ucTxData
90 // Return values: Boolean bAck
91 // ********************************************************************************
92
93 Bool I2CTransmit(Byte ucTxData)
94 {
95 1 Byte ucIndex;
96 1 Bool bAck;
97 1
98 1 for (ucIndex=0;ucIndex<8;ucIndex++)
99 1 {
100 2 if (ucTxData & 0x80)
101 2 sda=1;
102 2 else
103 2 sda=0;
104 2 ucTxData<<=1;
105 2
106 2 // I2CWait(); //jack
107 2
108 2 scl=1;
109 2 I2CWait();
110 2 scl=0;
111 2 // I2CWait(); //jack
112 2 }
113 1 sda=1;
114 1 I2CWait();
115 1 scl=1;
C51 COMPILER V7.50 I2C 08/11/2008 14:05:22 PAGE 3
116 1 I2CWait();
117 1 bAck=sda;
118 1 scl=0;
119 1 // I2CWait();
120 1 return bAck;
121 1 }
122
123 // ********************************************************************************
124 // Function: I2CReceive
125 // Description: SCLK is high. Read the data | with 0x01 if SDA==1 and otherwise 0.
126 // SCLK is low. Repeat for 8 times.
127 // Input parameters:
128 // Return values: ucRxData.
129 // ********************************************************************************
130 Byte I2CReceive(void)
131 {
132 1 Byte ucIndex,ucRxData;
133 1
134 1 for (ucIndex=0;ucIndex<8;ucIndex++)
135 1 {
136 2 scl=1;
137 2 // I2CWait(); //jack
138 2
139 2 ucRxData<<=1;
140 2 if (sda==TRUE)
141 2 ucRxData|=0x01;
142 2
143 2 scl=0;
144 2 // I2CWait(); //jack
145 2 }
146 1 return ucRxData;
147 1 }
148
149 // ********************************************************************************
150 // Function: I2CWrite;
151 // Description: Transmit a byte of data to slave end with assigned ID and sub address.
-
152 // Input parameters: Byte - ucDevice - I2C slave ID of destination chip.
153 // Byte - ucAddress - sub address of destination chip.
154 // Byte - ucSetData - data to be write to the device.
155
156 // Return values: None.
157 // ********************************************************************************
158 void I2CWrite(Byte ucDevice,Byte ucAddress,Byte ucSetData)
159 {
160 1 Byte ucIndex;
161 1 Bool bAck;
162 1
163 1 for(ucIndex=0;ucIndex<5;ucIndex++)
164 1 {
165 2 I2CStart();
166 2 bAck=I2CTransmit(ucDevice);
167 2
168 2 if (bAck==TRUE)
169 2 {
170 3 I2CStop();
171 3 continue;
172 3 }
173 2 bAck=I2CTransmit(ucAddress);
174 2 if (bAck==TRUE)
175 2 {
176 3 I2CStop();
C51 COMPILER V7.50 I2C 08/11/2008 14:05:22 PAGE 4
177 3 continue;
178 3 }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -