raushankumar586
Junior Member level 1
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
Here Is my arduino Slave reader program in processing.....
and I am just wring in a while loop from main program of pic18f4520
printToArduino("Something Something");
Help me , Where I am doing wrong...
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...