Good day! I am trying to make an "i2c" interface, but I cant seem to get it to work. I read the datasheet, but Its not working.
Compiler: CCS PIC C v5.061
Simulator: Proteus 8.5
Source code
Schematic
Moderators note : made code and schematic directly viewable
Compiler: CCS PIC C v5.061
Simulator: Proteus 8.5
Source code
C:
/*
Description:
Second attempt to make an i2c program, examples and others are taken from the CCS forums, file for reference:
i2c-PCM-programmer-examples-CCS __ View topic - sensor networks.pdf
Original version from PCM programmer, the rest are modified versions
*/
/*
Description:
Second attempt to make an i2c program, examples and others are taken from the CCS forums, file for reference:
i2c-PCM-programmer-examples-CCS __ View topic - sensor networks.pdf
*/
#include <16F690.h>
#define GP0 PIN_C0
#define GP1 PIN_C1
#define GP2 PIN_C2
#define GP3 PIN_C3
#define GP4 PIN_C4
#define GP5 PIN_C5
#define GP6 PIN_C6
#define GP7 PIN_C7
#fuses INTRC_IO, NOWDT, NOPROTECT, BROWNOUT /* Ext. oscilator, no watchdog, no code protect, no brownout (until voltage
rises on power up the PIC is held in reset = inactive) reset */
#use delay(clock=4M) /* Specify clock frequency, to use "delay_()" function */
#use rs232 (baud=9600, xmit=PIN_B5, rcv=PIN_B7, parity=N, bits=8, errors)
#use i2c (master, sda=PIN_C5, scl=PIN_C4) /* Master mode, SDA pin = RB4, SCL pin = RB6, force hardware i2c functions */
#define SLAVE1_WRT_CON_BYTE 0b10100000 /* This byte should contain the control byte from the
datasheet with control code, AN0, AN1, AN2 pins, READ or WRITE bit */
/* bit<0> = 0 for write and 1 for read
bits<1:3> = the values of the AN0:2 pins
bits<4:7> = control code of "1010" for EEPROM24AA128 */
/* !!! Dont forget that these must be put as the most significant bits first,
A0, A1, A2 become A2, A1, A0, making:
10100000 = control 4 bits/A2, A1, A0,/Read or write */
/* HEX = 0xA0 */
#define SLAVE1_READ_CON_BYTE 0b10100001 /* This byte should contain the control byte from the
datasheet with control code, AN0, AN1, AN2 pins, READ or WRITE bit */
/* bit<0> = 0 for write and 1 for read
bits<1:3> = the values of the AN0:2 pins
bits<4:7> = control code of "1010" for EEPROM24AA128 */
/* !!! Dont forget that these must be put as the most significant bits first,
A0, A1, A2 become A2, A1, A0, making:
10100001 = control 4 bits/A2, A1, A0,/Read or write */
/* HEX = A1 */
#define SLAVE1_WRITE_ADDRESS_2_BYTES 0x00A2
#define SLAVE1_READ_ADDRESS_2_BYTES 0x00A3
//====================================
void main()
{
int8 data;
while(1)
{
printf ("Startin i2c module");
/* to write data */
i2c_start();
i2c_write(SLAVE1_WRT_CON_BYTE); /* 1. Following the start condition, the next byte is control code (1010)\chip select (A2, A1, A0), R/W bit (logic low). This indicates to the addressed slave that the address high byte will follow after it has generated an Acknowledge bit */
delay_ms (10); /* delay 10ms for the acknowledge bit to be sent, because we dont record it with the microcontroller */
i2c_write (0x80); /* 2. The next byte is the high order byte of the word address and will be written into the Address Pointer of 24AA128 */
i2c_write (0x00); /* 3. The next byte is the least significant address byte */
i2c_write (0x01); /* 4. After receiving another acknowledge signal from the 24AA128, the master will transmit tha data word to be written into the addressed memory location */
/* After a byte write command, the internal address counter will point
/* to the address location following the one that was just written */
i2c_write (0x02);
delay_ms (10);
i2c_stop (); /* 5. After another acknowledge bit, the master sends the stop condition */
delay_ms (10);
/* to read data subsequently */
i2c_start();
i2c_write(SLAVE1_WRT_CON_BYTE); /* 1. Following the start condition, the next byte is control code (1010)\chip select (A2, A1, A0), R/W bit (logic low). This indicates to the addressed slave that the address high byte will follow after it has generated an Acknowledge bit */
delay_ms (10); /* delay 10ms for the acknowledge bit to be sent, because we dont record it with the microcontroller */
i2c_write (0x80); /* 2. The next byte is the high order byte of the word address and will be written into the Address Pointer of 24AA128 */
i2c_write (0x00); /* 3. The next byte is the least significant address byte */
i2c_write (SLAVE1_READ_CON_BYTE);
delay_ms (10);
data = i2c_read(0);
delay_ms (10);
data = i2c_read (0);
//i2c_stop (); /* 5. After another acknowledge bit, the master sends the stop condition */
//delay_ms (10);
printf("read %X \n\r", data);
delay_ms(1000);
}
}
Schematic
Moderators note : made code and schematic directly viewable
Last edited by a moderator: