Problem whit usart communication between PIC18 and PIC16

General discussion on mikroC.
Post Reply
Author
Message
Jimmie
Posts: 45
Joined: 18 Apr 2008 12:12

Problem whit usart communication between PIC18 and PIC16

#1 Post by Jimmie » 23 Jan 2009 19:10

Hi a have a problem sending data between a PIC16F690 and a PIC18F4550 with usart. The PIC16F690 understand what PIC18F4550 says but PIC18F4550 does not understand what the PIC16F690 says.
I have no problem sending data between two PIC16 processors

I made a program sending the number five and looked at the signal with a oscilloscope and the different processors send different signals. Why is it like this and how do I make the two processors talk the same langue?


[img][img]http://img300.imageshack.us/img300/4561/image2rc5.th.png[/img][/img]
the blue signal come from PIC18F4550 and the red from PIC16F690

PIC16F690 code

Code: Select all

void main() {


  osccon = 117;

  TRISc     =   0;         
  portc       = 0;          
  ANSEL  = 0;                         // Configure AN pins as digital I/O
  ANSELH = 0;
  portb       = 0;
  TRISb     =   0;

 
Usart_Init(9600);

  while(1){

         USART_Write(5);
}
}
PIC18F4550 code

Code: Select all

void main() {

CMCON = 7;

TRISD = 0b00000000;
PORTD = 0b00000000;
TRISb = 0b00000000;
PORTb = 0b11111111;
Delay_ms(2000);
PORTb = 0b00000000;
Usart_Init(9600);


while(1) {

        USART_Write(5);
}
}
/Jimmie

sadavis80
Posts: 114
Joined: 15 Nov 2008 19:27

#2 Post by sadavis80 » 24 Jan 2009 16:03

Not having either processor, I might guess that the default STOP BITS or PARITY might be different between the 2. You have not initialized them in either case.
Steve

Jimmie
Posts: 45
Joined: 18 Apr 2008 12:12

#3 Post by Jimmie » 25 Jan 2009 02:18

Aha, thanks for the answer. How do I initialize the STOP BITS or PARITY. I thought that the usart library did that automatic.

Sobrietytest
Posts: 619
Joined: 05 Jul 2008 06:05
Location: Thailand

#4 Post by Sobrietytest » 25 Jan 2009 02:49

From the help files:

Usart_Init
Prototype void Usart_Init(const unsigned long baud_rate);

Returns Nothing.

Description Initializes hardware USART module with the desired baud rate. Refer to the device data sheet for baud rates allowed for specific Fosc. If you specify the unsupported baud rate, compiler will report an error.

Requires You need PIC MCU with hardware USART.

Usart_Init needs to be called before using other functions from USART Library.

Example This will initialize hardware USART and establish the communication at 2400 bps:

Usart_Init(2400);
In MikroC, the Usart_Init() function will set the port at 8-bit, no parity, 1 stop bit. You could also try this:

Usart_Write('5')

The Usart communicates in ASCII characters, not integers, so if you send (5) it will actually send a control byte called <ENQ>. If you want to send the character 5 then you need to put it in single quotes ('5') or in hex format (0x35), decimal (53) or binary (0b00110101). If you click on the A icon on the right of your compiler, you can see all the ASCII character set and their decimal, hexadecimal and binary equivalents.

Jimmie
Posts: 45
Joined: 18 Apr 2008 12:12

#5 Post by Jimmie » 30 Jan 2009 21:21

Hi thanks for the tip. But the two different signals still does not look the same and they still have problem understanding each other. This is how the signal looks if I send: “Usart_Write('5')”.


[img][img]http://img160.imageshack.us/img160/7747/image3ej7.th.png[/img][/img]
the blue signal come from PIC18F4550 and the red from PIC16F690

You can clearly see the control byte called <ENQ> that Sobrietytest talked about.

But where does that long byte come from that pic18 sends out , that pic16 doesn’t send?
How do I get the two different pic to understand each other?

mister_e
Posts: 11
Joined: 29 Jan 2009 19:31
Location: Montréal,Québec,Canada

#6 Post by mister_e » 30 Jan 2009 22:08

Are you using any internal osc or both use external OSC?

It is always better to use crystal/ceramic osc for reliable serial communication.

Not sure how MikroC work, but if using an external OSC, I would check the SPBRG, TXSTA settings to make sure they're set to achieve a minimum err%
Regards

Steve

drdoug
Posts: 1074
Joined: 16 Aug 2007 03:49
Location: St. Louis, MO

#7 Post by drdoug » 30 Jan 2009 22:11

Have you tried sending the signals to a PC(through a max232 or similar)?

If so, 1)does the computer read the P18 and P16? 2)What does the signal coming from the computer look like? 3)Can the two pics receive the computer signal?

Jimmie
Posts: 45
Joined: 18 Apr 2008 12:12

#8 Post by Jimmie » 31 Jan 2009 16:25

I am using external 20MHz crystal on Pic18 and the internal 8MHz crystal on pic16. But I don’t think the oscillator is the problem. I tried to send a 10ms pulse whit the delay library whit both the processors and the pulse was exact equal from both the processors.

I tried sending the number hex 5 from the computer, pic16 and pic18 and the computer signal looks exactly like the signal from pic16. (I used the usart tool in my PICkit2 to send and receive information) See picture

[img][img]http://img84.imageshack.us/img84/6417/pic16sending5hexpcsendipi7.th.png[/img][/img]
red is from computer Blue is from Pic16

[img][img]http://img84.imageshack.us/img84/6743/usarttr5.th.png[/img][/img]PICkit2 usart tool

The computer receive hex number 5 when connected to pic16 but connected to pic18 computer receives the hex number 56 instead of the number hex 5.

So my conclusion is that there is something wrong whit how Pic18 sends the information over usart. There must be some configuration that I have mist. Someone else should also have had this problem or am I the first one sending information whit usart between PIC18 and PIC16?

arcom
Posts: 271
Joined: 07 Dec 2007 17:00
Location: Rijeka/Croatia
Contact:

#9 Post by arcom » 31 Jan 2009 20:01

Have you tried the code in another PIC?
Maybe the one you have is damaged or malfunctioning.
Check out the ERRATA documents for your PIC.

I've successfully sent data out through EUSART on a PIC18F4455 so it's working OK, at least in my case.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#10 Post by Dany » 31 Jan 2009 20:33

Hi, the P16 and P18 usart libraries are not the same, the .mcl files are different. Perhaps this is the reason (in combination with different hardware perhaps...).
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

mister_e
Posts: 11
Joined: 29 Jan 2009 19:31
Location: Montréal,Québec,Canada

#11 Post by mister_e » 31 Jan 2009 20:34

Can you post the .HEX file for your PIC18, or the Config Fuses setting?

The 4550 have a huge set of OSC/PLL setting which are easy to mess up. Even with a 4MHz crystal, you can achieve a processor speed of 48MHz :shock:
Regards

Steve

Jimmie
Posts: 45
Joined: 18 Apr 2008 12:12

#12 Post by Jimmie » 24 Apr 2009 20:05

Hi again. It was some time since I got tired of this problem and gave up my project. But now I really need to know what is wrong whit this program. Still the problem is that PIC18f4550 send a weird usart signal as you can see in the pictures from the oscilloscope.

Now I have tied to enable and disable the 9th usart bit in pic18. If I enable the ninth bit the first pulse(from left) gets even longer.

If u want u can download my program files here: http://www.speedyshare.com/134737887.html

Does anyone have any idea why this is happening?
I really beg for help
:(

Mince-n-Tatties
Posts: 2780
Joined: 25 Dec 2008 15:22
Location: Scotland

#13 Post by Mince-n-Tatties » 25 Apr 2009 11:54

have a read here... may help

http://www.mikroe.com/forum/viewtopic.php?t=10646

note: the advice about what to set the clock to in the IDE.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

Re:

#14 Post by Dany » 11 Jan 2011 21:20

Jimmie wrote:If u want u can download my program files here: http://www.speedyshare.com/134737887.html :(
Hi Jimmie, did you remove the project on the URL you mention? I get the message "File not found. It has been either deleted, or it never existed at all." I am curious to see your whole project, including the config files.

Thanks in advance.

In can not promise anything of course. :?

There is one thing I have learned in my own projects (I do not know if applicable in yours though): after Uart_Init sometimes I have to wait for a few 100 ms to make the uart function properly. See http://www.rosseeld.be/DRO/PIC/Pitfalls.htm, point 1. :o
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

shakak
Posts: 2
Joined: 05 Mar 2011 22:59

Re: Problem whit usart communication between PIC18 and PIC16

#15 Post by shakak » 05 Mar 2011 23:03

hello!!

i had a problem with the pic18f4550 too, using the serial comunication with xbee.

what you have to do is configure the pic to 48 MHz for the CPU speed, see in the datasheet cpudiv.

thats work for me, i hope it help you too!!

Post Reply

Return to “mikroC General”