📄 p87c591_i2c_test.lst
字号:
149 1 device_address = 0x90;
150 1 i2c_read_write = 0;
151 1 number_of_bytes = 1;
152 1 data_buffer[0] = 0x40 | channel_number;
153 1 i2c_start(); // Start the I2C Transfer
154 1
155 1 i2c_read_write = 1;
156 1 i2c_start(); // Start the I2C Transfer
157 1
158 1 return data_in;
159 1 }
160
161 //------------------------------------------------------------------------------
162 // I2C Functions
163 //------------------------------------------------------------------------------
164
165 //------------------------------------------------------------------------------
166 // Routine: i2c_start
167 // Inputs: none
168 // Outputs: none
169 // Purpose: Starts I2C Transfer
170 //------------------------------------------------------------------------------
171 void i2c_start (void)
172 {
173 1 S1CON = 0x43; // Enable I2C, 75 KHz, Int.
174 1 STA = 1; // Send start signal
175 1 transfer_status = I2C_BUSY;
176 1 while (transfer_status == I2C_BUSY); // Wait unitl finished
177 1 }
178
179 //------------------------------------------------------------------------------
C51 COMPILER V6.02 P87C591_I2C_TEST 08/25/2000 12:59:44 PAGE 4
180 // SUPPORT FUNCTIONS
181 //------------------------------------------------------------------------------
182
183 //------------------------------------------------------------------------------
184 // Procedure: initialize_system
185 // Inputs: none
186 // Outputs: none
187 // Description: Initializes embedded system MCU LPC764
188 //------------------------------------------------------------------------------
189 void initialize_system (void)
190 {
191 1 // Initialize the serial port (9600, 8, N, 1)
192 1 PCON &= 0x7F; // Set bit 7 of the PCON register (SMOD = 0)
193 1 S0PSH = 0x00; // Set the baud rate to be generated from Timer #1
194 1 S0CON = 0x50; // 0101,0000 (Mode 1 and RxD enable)
195 1 TMOD |= 0x20; // Timer #1 in autoreload 8 bit mode
196 1 TH1 = 0xF3; // Baud rate is determined by
197 1 // Timer #1 overflow rate (T1H = 256 - (Fosc / (192 * BR)))
198 1 TR1 = 1; // Turn on Timer 1
199 1 TI = 1; // Set UART to send first char
200 1
201 1 // Initialize I2C Interface (75 kHZ operation)
202 1 P1_6 = 1;
203 1 P1_7 = 1;
204 1 P1M1 |= 0xC0;
205 1 P1M2 |= 0xC0;
206 1
207 1 S1CON = 0x43; // Configure I2C
208 1 ES1 = 1; // Enable I2C Int.
209 1 EA = 1; // Int. Enable Global
210 1 }
211
212 ////////////////////////////////////////////////////////////////////////////////
213 // Routine: delay_time
214 // Inputs: counter value to stop delaying
215 // Outputs: none
216 // Purpose: To pause execution for pre-determined time
217 ////////////////////////////////////////////////////////////////////////////////
218 void delay_time (unsigned int time_end)
219 {
220 1 unsigned int index;
221 1 for (index = 0; index < time_end; index++);
222 1 }
223
224 ////////////////////////////////////////////////////////////////////////////////
225 // Routine: i2c_isr
226 // Inputs: none
227 // Outputs: none
228 // Purpose: Services the I2C Interrupt based upon status value and mode
229 ////////////////////////////////////////////////////////////////////////////////
230 void i2c_isr(void) interrupt 5 using 1
231 {
232 1 static buffer_index;
233 1
234 1 switch(S1STA)
235 1 {
236 2 case START_TXD: // Start condition for bus transmited, send the address
237 2 STA = 0; // Clear start bit
238 2 if (i2c_read_write) // If Read Mode, Send device addres and read signal
239 2 S1DAT = device_address | 0x01;
240 2 else // Send device addres and write signal
241 2 S1DAT = device_address & 0xFE;
C51 COMPILER V6.02 P87C591_I2C_TEST 08/25/2000 12:59:44 PAGE 5
242 2 break;
243 2 case REPEAT_START_TXD: // Repeated Start condition for bus transmited
244 2 STA = 0; // Clear start bit
245 2 break;
246 2 case ADDRESS_TXD_ACK: // Address plus write sent, ack recieved, send the
247 2 buffer_index = 0; // Send first byte
248 2 S1DAT = data_buffer[buffer_index];
249 2 break;
250 2 case ADDRESS_TXD_NOACK: // Address plus write sent, NO ack recieved! Stop the transfer
251 2 STO = 1; // Stop the transfer - ERROR
252 2 transfer_status = I2C_ERROR;
253 2 break;
254 2 case DATA_TXD_ACK: // Data Bytes sent, ack recieved, send the next byte (if any)
255 2 buffer_index++;
256 2 if (number_of_bytes > buffer_index)
257 2 { // Send next byte in series
258 3 S1DAT = data_buffer[buffer_index];
259 3 }
260 2 else
261 2 {
262 3 STO = 1; // Else stop the transfer - OK!
263 3 transfer_status = I2C_OK;
264 3 }
265 2 break;
266 2 case DATA_TXD_NOACK: // Data Bytes sent, NO ack recieved! Stop the transfer
267 2 STO = 1; // Stop the transfer - ERROR
268 2 transfer_status = I2C_ERROR;
269 2 break;
270 2 case ADDRESS_RXD_ACK: // Address plus read sent, ack recieved
271 2 break;
272 2 case ADDRESS_RXD_NOACK: // Address plus read sent, NO ack recieved!
273 2 STO = 1; // Stop the transfer - ERROR
274 2 transfer_status = I2C_ERROR;
275 2 break;
276 2 case DATA_RXD_NOACK: // Data Byte received, NO ack sent!
277 2 data_in = S1DAT; // Read the byte
278 2 STO = 1; // Stop the transfer - OK!
279 2 transfer_status = I2C_OK;
280 2 break;
281 2 default: // ERROR OUT, UNKOWN STATE
282 2 STO = 1;
283 2 ES1 = 0;
284 2 transfer_status = I2C_ERROR;
285 2 break;
286 2 }
287 1 SI = 0;
288 1 }
289
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 379 ----
CONSTANT SIZE = 253 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 10 1
IDATA SIZE = ---- ----
BIT SIZE = 1 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -