send value from pic to Arduino

i want to send number from Pic16F877A By MikroC To Arduino but i have problem , any one Know where is the problem ???

Code Arduino

#include <SoftwareSerial.h>
#define rxPin 2
#define txPin 3
#define ledPin 12
SoftwareSerial mySerial = SoftwareSerial(rxPin, txPin);

void setup()
{
pinMode(rxPin, INPUT);
pinMode(txPin, OUTPUT);
pinMode(ledPin, OUTPUT);
mySerial.begin(9600);
}

void loop()
{ 

char c= mySerial.read();

if (c ==2)

{digitalWrite(ledPin,HIGH);} 

}

Code of MikroC

 char   x =2;
void main() 
{
uart1_init(9600);
delay_ms(1000);
trisc=0;
        while(1)       
       {
              uart1_write(x);
          }  
       }

any one know?

Got any pictures of the setup? Do you have any test equipment to verify if the PIC is actually sending anything? You might use "real" chars to send and receive such as '2', which is not the same as 2. Binary data will only add to the confusion should you decide to try and print the input to the Arduino.

Which Arduino do you have? The mega doesn't do pin change interrupts on all the pins like an Uno, so that could be it. Maybe a dumb question, but have you verified that the LED is working and is connected properly?

while(1)       
       {
        uart1_write(x);
       }

This will send bytes one after another with no delay, unless you jag it and start reading on a byte boundary the poor Arduino will never get in sync with the PIC.

Try moving the delay inside the while loop.


Rob

i use Arduino uno , and pic16f877a with 4 MHZ crystal , do u think crystal may cause this problem ?????

Why not send ASCII characters and use the Serial interface on the Arduino to see what it is actually receiving?

i use serial interface to power arduino with usb

So? Use it to also debug what are receiving.

do u think crystal 4MHZ May cause this error ?
i use baud rate 9200 , may it need to speed up ?

Try moving the delay inside the while loop.

Did you?


Rob

yes , i do and still not working :((

i change code but still not working :frowning:
arduino

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup()
{
mySerial.begin(9600);
pinMode(13, OUTPUT);
}

void loop()
{ 

char c= mySerial.read();

if (c ==2)

{
digitalWrite(13,HIGH);
} 

}

for MikroC i use 16f877a with 4 MHZ crystal

char x=2;
void main() {
delay_ms(500);
trisc=0;
uart1_init(9600);

while (1){
uart1_write(x);
delay_ms(1000) ;
}

}

This may be obvious but did you try swapping your Tx/Rx pins between them?

of course

If you'd just make your numbers have single quotes like '2', then you'd be able to examine the data with the serial monitor. As you have it, you are sending a binary 2, not the ASCII character 2. Or at least add the Serial.print to dump the received data in hex like this:

void loop()  { 

  char c= mySerial.read();

  // if a character comes in, print it in hex
  if(c != 255)  {
    Serial.print(c, HEX);
  }

  if (c ==2)  {
    digitalWrite(13,HIGH);
  } 

}

I change the program in Mikroc
to write '2'

the serial monitor in arduino write FFFFFFFFFFFFFFFFFFFFFFFFFFF

i connect arduino without any connection , just plug in to computer , serial monitor print
"FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup()
{
mySerial.begin(9600);
Serial.begin(9600);
pinMode(13, OUTPUT);
}

void loop()
{ 

char c= mySerial.read();


    Serial.print(c, HEX);
 
}

Because you are printing the return value of mySerialRead() without checking to see if there's anything to read.

As you have nothing connected there are no characters so the read() func returns -1 (IIRC) which is 0xFF, hence that's what you are seeing on the monitor.


Rob

That's why I told you to use an if statement to ignore the -1 returns when there is no serial data available.

Then i should use this code ..is this do u mean ?

#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3); // RX, TX

void setup()
{
mySerial.begin(9600);
Serial.begin(9600);
pinMode(13, OUTPUT);
}

void loop()
{ 

 if (mySerial.available())
    Serial.write(mySerial.read(),HEX);

  //  Serial.print(c, HEX);
 
}

Posted on: Today at 01:11:33 AMPosted by: Mohamed_Aouf