📄 p87c591_i2c_test.lst
字号:
C51 COMPILER V6.02 P87C591_I2C_TEST 08/25/2000 12:59:44 PAGE 1
C51 COMPILER V6.02, COMPILATION OF MODULE P87C591_I2C_TEST
OBJECT MODULE PLACED IN .\P87C591_I2C_Test.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE .\P87C591_I2C_Test.c ROM(SMALL) DEBUG OBJECTEXTEND
stmt level source
1 //------------------------------------------------------------------------------
2 // Keil Software, Inc.
3 //
4 // Project: I2C EXAMPLE PROGRAM (BIT BANGED) FOR P87C591 MCU
5 //
6 // Filename: P87C591_I2C_Test.c
7 // Version: 1.0.0
8 // Description: This file contains example code to print over the serial
9 // port of the P87C591 MCU and Evaultion Board (EVAL-P87C591QS).
10 // This example will test the I2C function of the P87C591.
11 // This will be done by writing and reading from
12 // a serial A/D & D/A (P8591).
13 //
14 // This example will bit bang two general I/O ports for I2C
15 //
16 // The example work with communicate at: 9600, 8, N, 1.
17 //
18 // This code was tested using the mon-51 connected through COM-1
19 // of the eval board.
20 // CPU Frequency: 33.00 MHz
21 //
22 // Copyright 2000 - Keil Software, Inc.
23 // All rights reserved.
24 //------------------------------------------------------------------------------
25
26 //------------------------------------------------------------------------------
27 // Header files
28 //------------------------------------------------------------------------------
29 #include "REG591.H" // Header file for the P87C591 MCU
30 #include <STDIO.H> // Standard I/O header file
31
32 sbit P1_6 = 0x96; // Define the individual bit
33 sbit P1_7 = 0x97; // Define the individual bit
34
35 //------------------------------------------------------------------------------
36 // Value Definitions
37 //------------------------------------------------------------------------------
38 #define TRUE 0x01 // Value representing TRUE
39 #define FALSE 0x00 // Value representing FALSE
40 #define ON 0x01 // Value representing ON
41 #define OFF 0x00 // Value representing OFF
42 #define HIGH 0x01 // Value representing ON
43 #define LOW 0x00 // Value representing OFF
44
45 #define DELAY_WAIT 5000 // Value for delay time
46
47 #define I2C_ERROR 0x00
48 #define I2C_OK 0x01
49 #define I2C_BUSY 0x02
50
51
52 //------------------------------------------------------------------------------
53 // I2C Status Message Codes (Master Codes)
54 //------------------------------------------------------------------------------
55 #define START_TXD 0x08 // Start condition for bus transmited
C51 COMPILER V6.02 P87C591_I2C_TEST 08/25/2000 12:59:44 PAGE 2
56 #define REPEAT_START_TXD 0x10 // Repeated Start condition for bus transmited
57 #define ADDRESS_TXD_ACK 0x18 // Address plus write sent, ack recieved
58 #define ADDRESS_TXD_NOACK 0x20 // Address plus write sent, NO ack recieved!
59 #define DATA_TXD_ACK 0x28 // Data sent, ack recieved
60 #define DATA_TXD_NOACK 0x30 // Data sent, NO ack recieved!
61 #define ARB_LOST 0x38 // I2C Master Arbitration Lost
62
63 #define ADDRESS_RXD_ACK 0x40 // Address plus read sent, ack recieved
64 #define ADDRESS_RXD_NOACK 0x48 // Address plus read sent, NO ack recieved!
65 #define DATA_RXD_ACK 0x50 // Data received, ack sent
66 #define DATA_RXD_NOACK 0x58 // Data received, NO ack sent!
67
68
69 //------------------------------------------------------------------------------
70 // I2C Peripheral Function Prototypes
71 //------------------------------------------------------------------------------
72 // Reads the ADC value for a given channel
73 unsigned char read_adc_channel(unsigned char channel_number);
74 void write_dac (unsigned char voltage_out); // Writes a byte to the DAC
75
76 //------------------------------------------------------------------------------
77 // I2C Functions
78 //------------------------------------------------------------------------------
79 void i2c_start (void); // Starts a transfer
80
81 //------------------------------------------------------------------------------
82 // Support Function Prototypes
83 //------------------------------------------------------------------------------
84 void initialize_system (void); // Initializes MCU (RS-232)
85 void delay_time (unsigned int time_end); // To pause execution for pre-determined time
86
87 //------------------------------------------------------------------------------
88 // Globar Variables
89 //------------------------------------------------------------------------------
90 unsigned char data_buffer[4], number_of_bytes, device_address, data_in, transfer_status;
91 bit i2c_read_write;
92
93 //------------------------------------------------------------------------------
94 // MAIN FUNCTION
95 //------------------------------------------------------------------------------
96 void main (void)
97 {
98 1 unsigned char voltage_out;
99 1
100 1 initialize_system();
101 1
102 1 printf("\n\rKeil Software, Inc.\n\r"); // Display starup message
103 1 printf("P87C591 MCU I睠 Example Test Program\n\r");
104 1 printf("Version 1.0.0\n\r");
105 1 printf("Copyright 2000 - Keil Software, Inc.\n\r");
106 1 printf("All rights reserved.\n\n\r");
107 1 printf("P8591 Test Program....Reading from ADC channel 0, Writing to DAC!\n\r");
108 1
109 1 while (TRUE)
110 1 {
111 2 for (voltage_out = 0; voltage_out < 0xFF; voltage_out++)
112 2 {
113 3 write_dac(voltage_out); // Write voltage value to DAC
114 3 delay_time(DELAY_WAIT);
115 3 // Read voltage (ADC 0) and display results
116 3 printf("DAC output: %3bu ADC Channel 0: %3bu\n\r", voltage_out, read_adc_channel(0x00));
117 3 }
C51 COMPILER V6.02 P87C591_I2C_TEST 08/25/2000 12:59:44 PAGE 3
118 2 }
119 1 }
120
121 //------------------------------------------------------------------------------
122 // I2C Peripheral Function Prototypes
123 //------------------------------------------------------------------------------
124
125 //------------------------------------------------------------------------------
126 // Procedure: write_dac
127 // Inputs: voltage_out
128 // Outputs: none
129 // Description: Writes a byte to the DAC
130 //------------------------------------------------------------------------------
131 void write_dac (unsigned char voltage_out)
132 {
133 1 device_address = 0x90;
134 1 i2c_read_write = 0;
135 1 number_of_bytes = 2;
136 1 data_buffer[0] = 0x40;
137 1 data_buffer[1] = voltage_out;
138 1 i2c_start(); // Start the I2C Transfer
139 1 }
140
141 //------------------------------------------------------------------------------
142 // Procedure: read_adc_channel
143 // Inputs: channel
144 // Outputs: none
145 // Description: Reads the ADC value for a given channel
146 //------------------------------------------------------------------------------
147 unsigned char read_adc_channel(unsigned char channel_number)
148 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -