i2c_master_reverse_string.c - Master in pair of files that shows a PIC24 I2C master talking to a PIC24 I2C slaveΒΆ

I2C Example: Demonstrates a PIC24 CPU acting as an I2C slave. The PIC24 slave responds to both write and read transactions.

 
#include "pic24_all.h"

#define SLAVE_I2C_ADDR (0x60)

int16_t getStringLength(char* psz_1) {
  uint16_t u16_len;
  u16_len = 0;
  while (*psz_1) {
    psz_1++;
    u16_len++;
  }
  u16_len++;  //contains length of string including null
  return u16_len;
}

#define BUFSIZE 64
char sz_1[BUFSIZE];

int main (void) {
  uint16_t u16_len;
  configBasic(HELLO_MSG);
  configI2C1(400);            //configure I2C for 400 KHz
  while (1) {
    inStringEcho(sz_1,BUFSIZE);       //get a string from the console
    if (*sz_1 == 0) continue;         //don't send empty string
    u16_len = getStringLength(sz_1);  //determine string length
    writeNI2C1(SLAVE_I2C_ADDR,(uint8_t *)sz_1,u16_len);   //send the string
    readNI2C1(SLAVE_I2C_ADDR, (uint8_t *) sz_1,u16_len) ;  //read the reversed string
    outString(sz_1);
    outString("\n");
  }
}