Communicating with PIC

I want to serially send an ASCII value from a PIC18F4550 to an Arduino Duemilanove. This is the code I burned to the PIC…

#include <P18cxxx.h>
#include <stdio.h>
#include “delays.h”
#include “usart.h”
#include “i2c.h”
#include “adc.h”
//#include
#pragma udata

char gettemp(void);

void main()
{
int result;
char high;
char d, usartcnt=0, usartin;

OpenUSART(USART_TX_INT_ON & USART_RX_INT_ON & USART_ASYNCH_MODE & USART_EIGHT_BIT & USART_BRGH_HIGH, 51);

TRISA = 0b11011111;
TRISB = 0b11111111;

while(1)
{

WriteUSART(‘7’);
Delay10KTCYx(1000);

while (BusyUSART());
}
}

and this is the Arduino sketch…

int incomingByte=0 ; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}

I have the TX pin of the PIC (Pin 25) connected to the Rx pin of the Arduino. The only thing I receive on the serial monitor is -1 or 255 which I suspect is just noise. Any ideas???

Are you using the same baudrate on both chips?

I think so, the value of 51 in the OpenUSART statement sets the baud rate at 9600 (based on a 8MHz clock). I have no oscilloscope to measure clock speed, but I think the PIC is 8MHz by default.

Have you verified that the PIC works? With a terminal or something?

The code looks ok by me.

I thought I read somewhere that the signal needs to be inverted or something? I could be thinking of something else… some UART uses inverted and non-inverted correct? If I’m right you could use an Inverter, fairly common chips… I believe the 7404 would do it?

And isn’t there some code for the PIC to choose inverted or non-inverted? I’ve only over-heard a couple people chatting about it, so don’t know any of the specifics, but try inverting or non-inverting to see if that makes a difference?

I thought I read somewhere that the signal needs to be inverted or something? I could be thinking of something else.. some UART uses inverted and non-inverted correct? If I'm right you could use an Inverter, fairly common chips.. I believe the 7404 would do it?

I remember reading about that also, something about TTL and logic levels. I will research more... :)

Have you verified that the PIC works? With a terminal or something?

No, parts are in short supply. I would like to try another PIC, but only have the one; also, I don't have a RS232 cable to try using Hyper Terminal so I am stuck using the Arduino and the parts I have until I receive a parts order (this is the military, so that could be awhile) :(

The picaxe (a bootloaded PIC) uses a very simple RS232 circuit for programming (2 resisters) whereas the Arduino requires inverters to work. the picaxe programming language allows for the serial to be inverted in software and I have to switch it the opposite way to default to make it communicate with my arduino. How a raw PIC compares with a picaxe, I don't know.

do you have a common ground between the two chips?

The only thing I receive on the serial monitor is -1 or 255 which I suspect is just noise. Any ideas???

It is not just noise it is a clue. It means you are receiving all logic 1's after an initial start pulse. This would imply:- 1) The signal is the right way up. 2) Something is being sent. However it could be that there is a big missmatch between the transmit and receive baud rate.

do you have a common ground between the two chips?

I did not, am trying that now. Thanks for the replies.

This is just a small part of the big picture. Once I confirm data being sent from the PIC, I am connecting the PIC to a ZIgbee module and will try to send the data wirelessly to an Arduino with the Zigbee shield.

However it could be that there is a big missmatch between the transmit and receive baud rate

That may be so. Without a scope I cannot measure the clock speed of the PIC, but from the spec sheet I think it is 8MHz and based the Baud Rate setting on that value.

The common grounds made progress. The Serial Monitor displays values that change when I reprogram the PIC with new values. The problem is that I am not getting what I sent, for example I receive a sequence like....0, -128, -128, 0, 0, .... I am pretty sure that my problem is the Baud Rate mismatch. Does anyone know what the default clock speed of the PIC18F4550 is, or for that matter, the proper syntax to set the clock speed?

Does anyone know what the default clock speed of the PIC18F4550 is

There isn't a default speed as such, you have to set up registers to set clock source and divide ratios and pre divide ratios. These registers do have a 'default' power up value which would set the speed but that isn't any sort of standard baud rate you can use. You will have to look at the data sheet for this.

This is the code I burned to the PIC

Well not quite ... it is the code you sent to the compiler to produce hex bytes that you burnt into the PIC. However the line OpenUSART( sets up the data format of the UART but where do you define the baud rate? Is it the 51 on the end? The C compiler is doing the setting of the registers for you but it will depend on the clock speed being the same as the compiler / environment writers assumed.

Interestingly (but irrelevant to your problem) why are you getting -128 when you got 255 before, this implies something has changed with the software.

However the line OpenUSART(
sets up the data format of the UART but where do you define the baud rate? Is it the 51 on the end

The 51 is the USART Baud Rate Register. I googled a Baud Rate calculator and the value of 51 is used for an 8MHz clock.

why are you getting -128 when you got 255 before

I made so many changes, I forget. But I did try changing the data being sent from the PIC from text to numerical and also changed the incoming byte to DEC, HEX, and then unspecified. This may have caused this.
By the way, I know now that there is data being sent because when I change the delay time of data being sent it changes the rate of data received.
I appreciate all the help.

Hiya,

I got a similar problem talking to a PIC, using uart rx/tx and ground cross connected. Got an SerialX.avaible() return of -127 initally now get 0.

The PIC is the front end of a compass sensor, with sealed code and no other interface apart from an I2C bus so that's avenue is closed to me at the moment.

Using a seeeduino mega on Hardwareserial. Ports on the Mega loopback on each other I checked the incoming datastream with the loops setup and have the correct ansii/binary/hex sequences as you have.

I did some reading and the PIC is very programmable in terms of inverting non inverting output levels etc. I'm not sure about TTL voltage levels etc but as the sensor module is 5V or 3.3V that's my next area of interest.

From your post I understood that you were using RS 232 could you connect directly to the tx/rx lines and simplify things for now? I do not know how many serial ports you have on the Ard Dom, I put a LCd on the Mega to try the Serial Port with no differnce as well!

Dave

I assume that the Arduino library sets 9600,N, 1 start/stop when it is setup nothing silly like 9600, N, 1 and missing out the other non stated stop/start bit...LOL serial comms...I do n't have a scope etc.

and the value of 51 is used for an 8MHz clock.

Was this in decimal or hex? I am sure you have a baud rate problem.

From your post I understood that you were using RS 232 could you connect directly to the tx/rx lines and simplify things for now?

No that is exactly what you can't do. Yo need a voltage converter and logic inverter to convert TTL to RS232.

but as the sensor module is 5V or 3.3V

With these voltages the RS232 converter chips generate the +12 / - 12V the RS232 uses.

I assume that the Arduino library sets 9600,N, 1 start/stop when it is setup

No it does nothing if you don't initialise it with the serialbegin() call.

Was this in decimal or hex? I am sure you have a baud rate problem

This is a decimal number. I too think I have a baud rate problem. I am going to read more about the PIC and try to figure out the syntax to set the clock speed.

Got a 12MHz crystal, set baud rate correctly, thing works perfect. Thanks all, for the help.

Thanks!

Dave