Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

I2c communication with pic18f4520 and arduino mega 2560 is being delayed

Status
Not open for further replies.

raushankumar586

Junior Member level 1
Joined
Jun 26, 2017
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
191
Hello Electronics Enthusiasts ,

I am establishing I2C connection between pic18f4520 and arduino mega module. It is working fine but I checked with oscilloscope that it is delaying so much around 500ms for each bunch of instruction. I am not getting where I am doing wrong....
Here is my I2C implementation

Code:
/* 
 * File:  i2cSetup.c
 * Author: Lenovo
 * Created on 23 June, 2017, 6:12 PM
 */
#include "ConfigurationBits.h"


void I2C_Master_Init(const unsigned long c) {
    SSPCON1 = 0x28;
    SSPCON2 = 0;
    SSPADD = (_XTAL_FREQ / (4 * c)) - 1; // FOSC/(4 * (SSPADD + 1))
    SSPSTAT = 0x80;
    TRISCbits.RC3 = 1; //Setting as input as given in datasheet
    TRISCbits.RC4 = 1; //Setting as input as given in datasheet
}

void I2C_Master_Wait() {
    while ((SSPSTAT & 0x04) || (SSPCON2 & 0x1F));
}

void I2C_Master_Start() {
    I2C_Master_Wait();
    SSPCON2bits.SEN = 1;
}

void I2C_Master_Stop() {
    I2C_Master_Wait();
    SSPCON2bits.PEN = 1;
}

void I2C_Master_WriteC(unsigned char d) {
    I2C_Master_Wait();
    SSPBUF = d;
}

void I2C_Master_WriteS(unsigned char *e) {
    while (*e) {
        I2C_Master_WriteC(*e); // send character pointed to by s
        e++; // increase pointer location to the next character
    }
}

void printToArduino(unsigned char *printOutput) {
    I2C_Master_Start(); //Start condition
    I2C_Master_WriteC(0x12); //7 bit address + Write (000001-0)(SELECT ADDRESS TO COMMUNICATE )
    I2C_Master_WriteS(printOutput); //Write data
    I2C_Master_Stop(); //Stop condition
    __delay_ms(200);
}

void printToArduinoChar(unsigned char printOutput) {
    I2C_Master_Start(); //Start condition
    I2C_Master_WriteC(0x12); //7 bit address + Write (000001-0)(SELECT ADDRESS TO COMMUNICATE )
    I2C_Master_WriteC(printOutput); //Write data
    I2C_Master_Stop(); //Stop condition
    __delay_ms(200);
}


Here Is my arduino Slave reader program in processing.....
Code:
#include <Wire.h>

void setup() {
  Wire.begin(9);                // join i2c bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
  while (!Serial);
  Serial.println("Setup Complete!");
}
void loop() {       
  delay(100);
}
void receiveEvent(int howMany) {
  char c;
 
  while (Wire.available() > 0) { // loop through all but the last
     c = Wire.read(); // receive byte as a character
     if(c=='\n')
     Serial.println();
      else
    Serial.print(c);
  }
  
}

and I am just wring in a while loop from main program of pic18f4520
printToArduino("Something Something");

Help me , Where I am doing wrong...
 

You get out what you put in...
What's the particular purpose of __delay_ms(200) ?
 

You get out what you put in...
What's the particular purpose of __delay_ms(200) ?

Even I tried with removing those 200ms delay , believe me then also same delay between two response , nothing changed
 

The Serial.println() function within receiveEvent is likely delaying the release of the I2C bus to the Master.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top