Mikroelektronika MIKROE-738 Datenbogen

Seite von 682
342
mikoC PRO for PIC32
MikroElektronika
I2Cx_Stop
Prototype
void I2Cx_Stop();
Description Issues STOP signal.
Parameters None.
Returns
Nothing.
Requires
MCU with at least one I²C module.
Used I²C module must be initialized before using this function. See I2Cx_Init routine.
Example
// Issue STOP signal
I2C1_Stop();
Notes
- I²C library routines require you to specify the module you want to use. To select the desired I²C 
module, simply change the letter 
x in the routine prototype for a number from 1 to 5.
- Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet 
before utilizing this library.
Library Example
This code demonstrates working with the I²C library. Program sends data to EEPROM (data is written at the address 
2). After that, program reads data from the same EEPROM address and displays it on PORTB for visual check. See the 
figure below how to interface the 24C02 to PIC32.
Copy Code To Clipboard
void EEPROM_24C02_Init() {
  I2C2_Init(
100000
);
}
//--------------- Writes data to 24C02 EEPROM - signle location
void EEPROM_24C02_WrSingle(unsigned short wAddr, unsigned short wData) {
  I2C2_Start(); 
             // issue I2C start signal
  
I2C2_Write(0xA0); 
         // send byte via I2C  (command to 24cO2)
  
I2C2_Write(wAddr);
         // send byte (address of EEPROM location)
  
I2C2_Write(wData);
         // send data (data to be written)
  
I2C2_Stop();
}
//--------------- Reads data from 24C02 EEPROM - single location (random)
unsigned short EEPROM_24C02_RdSingle(unsigned short rAddr) {
  unsigned short reslt;
  I2C2_Start(); 
         
   
 // issue I2C start signal
  I2C2_Write(0xA0);        
  // send byte via I2C  (device address + W)
  I2C2_Write(rAddr);         
// send byte (data address)
  I2C2_Restart();            
// issue I2C signal repeated start
  I2C2_Write(0xA1);         
 // send byte (device address + R)
  reslt = I2C2_Read(1);      
// Read the data (NO acknowledge)
  I2C2_Stop();
  return reslt;
}