Mikroelektronika MIKROE-724 データシート

ページ / 726
370
mikoBasic PRO for dsPIC30/33 and PIC24
MikroElektronika
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 dsPIC30/33 and PIC24.
Copy Code To Clipboard 
program I2C_Simple
main:
  ADPCFG = 0xFFFF        ‘ initialize AN pins as digital
  LATB = 0
  TRISB = 0              ‘ Configure PORTB as output
  I2C1_Init(100000)      ‘ initialize I2C communication
  I2C1_Start()           ‘ issue I2C start signal
  I2C1_Write(0xA2)       ‘ send byte via I2C  (device address + W)
  I2C1_Write(2)          ‘ send byte (address of EEPROM location)
  I2C1_Write(0xAA)       ‘ send data (data to be written)
  I2C1_Stop()            ‘ issue I2C stop signal
  Delay_100ms()
  I2C1_Start()           ‘ issue I2C start signal
  I2C1_Write(0xA2)       ‘ send byte via I2C  (device address + W)
  I2C1_Write(2)          ‘ send byte (data address)
  I2C1_Restart()         ‘ issue I2C signal repeated start
  I2C1_Write(0xA3)       ‘ send byte (device address + R)
  PORTB = I2C1_Read(1)   ‘ Read the data (NO acknowledge)
  I2C1_Stop()            ‘ issue I2C stop signal
end.
HW Connection
Interfacing 24c02 to dsPIC30/33 and PIC24 via I²C