mikroC
making it simple...
Library Example
mikroC - C Compiler for Microchip PIC microcontrollers
This code demonstrates use of I²C Library functions. PIC MCU is connected (SCL, SDA pins ) to 24c02 EEPROM. Program sends data to EEPROM (data is written at address 2). Then, we read data via I2C from EEPROM and send its value to PORTD, to check if the cycle was successful (see the figure below how to interface 24c02 to PIC).
void main(){ PORTB = 0; TRISB = 0;
I2C_Init(100000); |
|
|
|
|
|
I2C_Start(); | // Issue I2C | start | signal |
| |
I2C_Wr(0xA2); | // Send byte | via I2C | (command to 24cO2) | ||
I2C_Wr(2); | // Send byte | (address of EEPROM location) | |||
I2C_Wr(0xF0); | // Send data | (data | to be written) | ||
I2C_Stop(); |
|
|
|
|
|
Delay_ms(100); |
|
|
|
|
|
I2C_Start(); | // Issue I2C | start | signal |
| |
I2C_Wr(0xA2); | // Send byte | via I2C | (device address + W) | ||
I2C_Wr(2); | // Send byte | (data | address) |
| |
I2C_Repeated_Start(); | // Issue I2C | signal repeated | start | ||
I2C_Wr(0xA3); | // Send byte | (device address | + R) | ||
PORTB = I2C_Rd(0u); | // Read the data (NO acknowledge) | ||||
I2C_Stop(); |
|
|
|
|
|
}
|
| page |
|
MikroElektronika: Development tools - Books - Compilers | 191 | ||
|
|