📄 f33x_smbus_eeprom.lst
字号:
C51 COMPILER V8.09 F33X_SMBUS_EEPROM 12/28/2008 10:19:32 PAGE 1
C51 COMPILER V8.09, COMPILATION OF MODULE F33X_SMBUS_EEPROM
OBJECT MODULE PLACED IN F33x_SMBus_EEPROM.OBJ
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE F33x_SMBus_EEPROM.c BROWSE DEBUG OBJECTEXTEND
line level source
1 //-----------------------------------------------------------------------------
2 // F33x_SMBus_EEPROM.c
3 //-----------------------------------------------------------------------------
4 // Copyright 2006 Silicon Laboratories, Inc.
5 // http://www.silabs.com
6 //
7 // Program Description:
8 //
9 // This example demonstrates how the C8051F33x SMBus interface can communicate
10 // with a 256 byte I2C Serial EEPROM (Microchip 24LC02B).
11 // - Interrupt-driven SMBus implementation
12 // - Only master states defined (no slave or arbitration)
13 // - Timer1 used as SMBus clock source
14 // - Timer2 used by SMBus for SCL low timeout detection
15 // - SCL frequency defined by <SMB_FREQUENCY> constant
16 // - Pinout:
17 // P0.0 -> SDA (SMBus)
18 // P0.1 -> SCL (SMBus)
19 //
20 // P1.3 -> LED
21 //
22 // P2.0 -> C2D (debug interface)
23 //
24 // all other port pins unused
25 //
26 // How To Test:
27 //
28 // 1) Verify that J6 is not populated.
29 // 2) Download code to a 'F33x device that is connected to a 24LC02B serial
30 // EEPROM (see the EEPROM datasheet for the pinout information).
31 // 3) Run the code:
32 // a) the test will indicate proper communication with the EEPROM by
33 // turning on the LED at the end the end of the test
34 // b) the test can also be verified by running to the if statements
35 // in main and checking the sent and received values by adding
36 // the variables to the Watch Window
37 //
38 // FID: 33X000014
39 // Target: C8051F33x
40 // Tool chain: Keil C51 7.50 / Keil EVAL C51
41 // Command Line: None
42 //
43 // Release 1.0
44 // -Initial Revision (TP)
45 // -30 MAR 2006
46 //
47
48 //-----------------------------------------------------------------------------
49 // Includes and Device-Specific Parameters
50 //-----------------------------------------------------------------------------
51
52 #include <C8051F330.h>
53
54 //-----------------------------------------------------------------------------
55 // Global CONSTANTS
C51 COMPILER V8.09 F33X_SMBUS_EEPROM 12/28/2008 10:19:32 PAGE 2
56 //-----------------------------------------------------------------------------
57
58
59 #define SYSCLK 24500000 // System clock frequency in Hz
60
61 #define SMB_FREQUENCY 50000 // Target SCL clock rate
62 // This example supports between 10kHz
63 // and 100kHz
64
65 #define WRITE 0x00 // SMBus WRITE command
66 #define READ 0x01 // SMBus READ command
67
68 // Device addresses (7 bits, lsb is a don't care)
69 #define EEPROM_ADDR 0xA0 // Device address for slave target
70 // Note: This address is specified
71 // in the Microchip 24LC02B
72 // datasheet.
73 // SMBus Buffer Size
74 #define SMB_BUFF_SIZE 0x08 // Defines the maximum number of bytes
75 // that can be sent or received in a
76 // single transfer
77
78 // Status vector - top 4 bits only
79 #define SMB_MTSTA 0xE0 // (MT) start transmitted
80 #define SMB_MTDB 0xC0 // (MT) data byte transmitted
81 #define SMB_MRDB 0x80 // (MR) data byte received
82 // End status vector definition
83
84 //-----------------------------------------------------------------------------
85 // Global VARIABLES
86 //-----------------------------------------------------------------------------
87 unsigned char* pSMB_DATA_IN; // Global pointer for SMBus data
88 // All receive data is written here
89
90 unsigned char SMB_SINGLEBYTE_OUT; // Global holder for single byte writes.
91
92 unsigned char* pSMB_DATA_OUT; // Global pointer for SMBus data.
93 // All transmit data is read from here
94
95 unsigned char SMB_DATA_LEN; // Global holder for number of bytes
96 // to send or receive in the current
97 // SMBus transfer.
98
99 unsigned char WORD_ADDR; // Global holder for the EEPROM word
100 // address that will be accessed in
101 // the next transfer
102
103 unsigned char TARGET; // Target SMBus slave address
104
105 bit SMB_BUSY = 0; // Software flag to indicate when the
106 // EEPROM_ByteRead() or
107 // EEPROM_ByteWrite()
108 // functions have claimed the SMBus
109
110 bit SMB_RW; // Software flag to indicate the
111 // direction of the current transfer
112
113 bit SMB_SENDWORDADDR; // When set, this flag causes the ISR
114 // to send the 8-bit <WORD_ADDR>
115 // after sending the slave address.
116
117
C51 COMPILER V8.09 F33X_SMBUS_EEPROM 12/28/2008 10:19:32 PAGE 3
118 bit SMB_RANDOMREAD; // When set, this flag causes the ISR
119 // to send a START signal after sending
120 // the word address.
121 // For the 24LC02B EEPROM, a random read
122 // (a read from a particular address in
123 // memory) starts as a write then
124 // changes to a read after the repeated
125 // start is sent. The ISR handles this
126 // switchover if the <SMB_RANDOMREAD>
127 // bit is set.
128
129 bit SMB_ACKPOLL; // When set, this flag causes the ISR
130 // to send a repeated START until the
131 // slave has acknowledged its address
132
133 // 16-bit SFR declarations
134 sfr16 TMR3RL = 0x92; // Timer3 reload registers
135 sfr16 TMR3 = 0x94; // Timer3 counter registers
136
137 sbit LED = P1^3; // LED on port P1.3
138
139 sbit SDA = P0^0; // SMBus on P0.0
140 sbit SCL = P0^1; // and P0.1
141
142 //-----------------------------------------------------------------------------
143 // Function PROTOTYPES
144 //-----------------------------------------------------------------------------
145
146 void SMBus_Init(void);
147 void Timer1_Init(void);
148 void Timer3_Init(void);
149 void Port_Init(void);
150
151 void SMBus_ISR(void);
152 void Timer3_ISR(void);
153
154 void EEPROM_ByteWrite(unsigned char addr, unsigned char dat);
155 void EEPROM_WriteArray(unsigned char dest_addr, unsigned char* src_addr,
156 unsigned char len);
157 unsigned char EEPROM_ByteRead(unsigned char addr);
158 void EEPROM_ReadArray(unsigned char* dest_addr, unsigned char src_addr,
159 unsigned char len);
160
161 //-----------------------------------------------------------------------------
162 // MAIN Routine
163 //-----------------------------------------------------------------------------
164 //
165 // Main routine performs all configuration tasks, then loops forever sending
166 // and receiving SMBus data to the slave EEPROM.
167
168 void main (void)
169 {
170 1 char in_buff[8] = {0}; // Incoming data buffer
171 1 char out_buff[8] = "ABCDEFG"; // Outgoing data buffer
172 1
173 1 unsigned char temp_char; // Temporary variable
174 1 bit error_flag = 0; // Flag for checking EEPROM contents
175 1 unsigned char i; // Temporary counter variable
176 1
177 1
178 1 PCA0MD &= ~0x40; // WDTE = 0 (disable watchdog timer)
179 1
C51 COMPILER V8.09 F33X_SMBUS_EEPROM 12/28/2008 10:19:32 PAGE 4
180 1
181 1 // Set internal oscillator to highest
182 1 // setting of 24500000 (or 12000000 for 'F320)
183 1 OSCICN |= 0x03;
184 1
185 1 // If slave is holding SDA low because of an improper SMBus reset or error
186 1 while(!SDA)
187 1 {
188 2 // Provide clock pulses to allow the slave to advance out
189 2 // of its current state. This will allow it to release SDA.
190 2 XBR1 = 0x40; // Enable Crossbar
191 2 SCL = 0; // Drive the clock low
192 2 for(i = 0; i < 255; i++); // Hold the clock low
193 2 SCL = 1; // Release the clock
194 2 while(!SCL); // Wait for open-drain
195 2 // clock output to rise
196 2 for(i = 0; i < 10; i++); // Hold the clock high
197 2 XBR1 = 0x00; // Disable Crossbar
198 2 }
199 1
200 1 Port_Init (); // Initialize Crossbar and GPIO
201 1
202 1 LED = 0; // Turn off the LED before the test
203 1 // starts
204 1
205 1 Timer1_Init (); // Configure Timer1 for use as SMBus
206 1 // clock source
207 1
208 1 Timer3_Init (); // Configure Timer3 for use with SMBus
209 1 // low timeout detect
210 1
211 1 SMBus_Init (); // Configure and enable SMBus
212 1
213 1
214 1 EIE1 |= 0x01; // Enable the SMBus interrupt
215 1
216 1 EA = 1; // Global interrupt enable
217 1
218 1
219 1
220 1 // Read and write some bytes to the EEPROM and check for proper
221 1 // communication
222 1
223 1 // Write the value 0xAA to location 0x25 in the EEPROM
224 1 EEPROM_ByteWrite(0x25, 0xAA);
225 1
226 1 // Read the value at location 0x25 in the EEPROM
227 1 temp_char = EEPROM_ByteRead(0x25);
228 1
229 1 // Check that the data was read properly
230 1 if (temp_char != 0xAA)
231 1 {
232 2 error_flag = 1;
233 2 }
234 1
235 1 // Write the value 0xBB to location 0x25 in the EEPROM
236 1 EEPROM_ByteWrite(0x25, 0xBB);
237 1
238 1 // Write the value 0xCC to location 0x38 in the EEPROM
239 1 EEPROM_ByteWrite(0x38, 0xCC);
240 1
241 1 // Read the value at location 0x25 in the EEPROM
C51 COMPILER V8.09 F33X_SMBUS_EEPROM 12/28/2008 10:19:32 PAGE 5
242 1 temp_char = EEPROM_ByteRead(0x25);
243 1
244 1 // Check that the data was read properly
245 1 if (temp_char != 0xBB)
246 1 {
247 2 error_flag = 1;
248 2 }
249 1
250 1 // Read the value at location 0x38 in the EEPROM
251 1 temp_char = EEPROM_ByteRead(0x38);
252 1
253 1 // Check that the data was read properly
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -