📄 interrupt.lst
字号:
157 3 {
158 4 FAIL = 1; // Indicate failed transfer
159 4 } // and handle at end of ISR
160 3 }
161 2 break;
162 2
163 2 // Master Receiver: byte received
164 2 case SMB_MRDB:
165 2 if ( i < SMB_DATA_LEN ) // Is there any data remaining?
166 2 {
167 3 *pSMB_DATA_IN = SMB0DAT; // Store received byte
168 3 pSMB_DATA_IN++; // Increment data in pointer
169 3 i++; // Increment number of bytes received
170 3 ACK = 1; // Set ACK bit (may be cleared later
171 3 // in the code)
172 3
173 3 }
174 2
175 2 if (i == SMB_DATA_LEN) // This is the last byte
176 2 {
177 3 SMB_BUSY = 0; // Free SMBus interface
178 3 ACK = 0; // Send NACK to indicate last byte
179 3 // of this transfer
C51 COMPILER V7.05 INTERRUPT 04/12/2005 09:13:15 PAGE 4
180 3 STO = 1; // Send STOP to terminate transfer
181 3 }
182 2
183 2 break;
184 2
185 2 default:
186 2 FAIL = 1; // Indicate failed transfer
187 2 // and handle at end of ISR
188 2 break;
189 2 }
190 1
191 1 if (FAIL) // If the transfer failed,
192 1 {
193 2 SMB0CN &= ~0x40; // Reset communication
194 2 SMB0CN |= 0x40;
195 2 SMB_BUSY = 0; // Free SMBus
196 2 }
197 1
198 1 SI=0; // clear interrupt flag
199 1 }
200
201
202
203 //------------------------------------------------------------------------------------
204 // Functions
205 //------------------------------------------------------------------------------------
206
207 //------------------------------------------------------------------------------------
208 // EEPROM_ByteWrite ()
209 //------------------------------------------------------------------------------------
210 //
211 // This function writes the value in <dat> to location <addr> in the EEPROM then
212 // polls the EEPROM until the write is complete.
213 //
214 void EEPROM_ByteWrite( unsigned int addr, unsigned char dat )
215 {
216 1 while (SMB_BUSY); // Wait for SMBus to be free.
217 1 SMB_BUSY = 1; // Claim SMBus (set to busy)
218 1
219 1 // Set SMBus ISR parameters
220 1 TARGET = EEPROM_ADDR; // Set target slave address
221 1 SMB_RW = WRITE; // Mark next transfer as a write
222 1 SMB_SENDWORDADDR = 1; // Send high 8bits Word Address after Slave Address
223 1 SMB_SENDWORDADDR1 = 1; // Send low 8bits Word Address after Slave Address
224 1 SMB_RANDOMREAD = 0; // Do not send a START signal after
225 1 // the word address
226 1 SMB_ACKPOLL = 1; // Enable Acknowledge Polling (The ISR
227 1 // will automatically restart the
228 1 // transfer if the slave does not
229 1 // acknoledge its address.
230 1
231 1 // Specify the Outgoing Data
232 1 WORD_ADDR = addr/256; // Set the target address in the
233 1 // EEPROM's internal memory space
234 1
235 1 WORD_ADDR1 = addr%256; // Set the target address in the
236 1 // EEPROM's internal memory space
237 1
238 1 SMB_SINGLEBYTE_OUT = dat; // store dat (local variable) in a global
239 1 // variable so the ISR can read it after
240 1 // this function exits
241 1
C51 COMPILER V7.05 INTERRUPT 04/12/2005 09:13:15 PAGE 5
242 1 pSMB_DATA_OUT = &SMB_SINGLEBYTE_OUT; // The outgoing data pointer points to
243 1 // the <dat> variable.
244 1
245 1 SMB_DATA_LEN = 1; // Specify to ISR that the next transfer
246 1 // will contain one data byte
247 1
248 1 // Initiate SMBus Transfer
249 1 STA = 1;
250 1
251 1 }
252
253
254
255
256
257 //------------------------------------------------------------------------------------
258 // EEPROM_ByteRead ()
259 //------------------------------------------------------------------------------------
260 //
261 // This function returns a single byte from location <addr> in the EEPROM then
262 // polls the <SMB_BUSY> flag until the read is complete.
263 //
264 unsigned char EEPROM_ByteRead( unsigned int addr)
265 {
266 1 unsigned char retval; // Holds the return value
267 1
268 1 while (SMB_BUSY); // Wait for SMBus to be free.
269 1 SMB_BUSY = 1; // Claim SMBus (set to busy)
270 1
271 1 // Set SMBus ISR parameters
272 1 TARGET = EEPROM_ADDR; // Set target slave address
273 1 SMB_RW = WRITE; // A random read starts as a write
274 1 // then changes to a read after
275 1 // the repeated start is sent. The
276 1 // ISR handles this switchover if
277 1 // the <SMB_RANDOMREAD> bit is set.
278 1 SMB_SENDWORDADDR = 1; // Send high 8bits Word Address after Slave Address
279 1 SMB_SENDWORDADDR1 = 1; // Send low 8bits Word Address after Slave Address
280 1 SMB_RANDOMREAD = 1; // Send a START after the word address
281 1 SMB_ACKPOLL = 1; // Enable Acknowledge Polling
282 1
283 1
284 1 // Specify the Incoming Data
285 1 WORD_ADDR = addr/256; // Set the target address in the
286 1 // EEPROM's internal memory space
287 1
288 1 WORD_ADDR1 = addr%256; // Set the target address in the
289 1 // EEPROM's internal memory space
290 1
291 1 pSMB_DATA_IN = &retval; // The incoming data pointer points to
292 1 // the <retval> variable.
293 1
294 1 SMB_DATA_LEN = 1; // Specify to ISR that the next transfer
295 1 // will contain one data byte
296 1
297 1 // Initiate SMBus Transfer
298 1 STA = 1;
299 1 while(SMB_BUSY); // Wait until data is read
300 1
301 1 return retval;
302 1
303 1 }
C51 COMPILER V7.05 INTERRUPT 04/12/2005 09:13:15 PAGE 6
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 455 ----
CONSTANT SIZE = ---- ----
XDATA SIZE = 8 ----
PDATA SIZE = ---- ----
DATA SIZE = 21 1
IDATA SIZE = ---- ----
BIT SIZE = 7 1
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -