Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Serial Interface on pic18f2550
26-08-2014, 09:39 AM,
RE: Serial Interface on pic18f2550
Most of time the failure of serial io when USB is used with PIC18F2550 is the wrong baud rate setup using crystal speed instead of 48Mhz core clock. From your CONFIG1 = 0E22 (0000-1110 0010-0010), the System_getCpuFrequency() will show 48MHz correctly and serial.c should set correct baud rate.

Try add a little delay after reset or boot to wait for the clock frequency to stabilize, and a little delay after serial init before starting to send output.

The circuit you linked is just voltage level shifter between Arduino (or PIC18F2550) 5V and Raspberry Pi 3.3V. If you are only sending output from PIC to Raspberry Pi, you can use a resister voltage divider instead of using a transistor to cut down 5V to 3.3V. Output from RPi 3.3V is not high enough to drive PIC input 5V (4.25V min).

You look like having 2 potential issues - PIC serial io, and interface to RPi. My suggestion is to isolate the problem and solve one at each time. Connect PC instead of RPi as terminal to settle the PIC serial io issue.

26-08-2014, 10:36 AM, (This post was last modified: 26-08-2014, 10:43 AM by Xumpino.)
RE: Serial Interface on pic18f2550
Thanks DJ,

Yesterday I desoldered my pic from my development board and placed a socket on it. Now it's much easier for me to reprogram the bootloader. I've seen that for version 4.14 pinguino has a build for a 12Mhz crystal.

I'm going to try to use this bootloader. Then I will try to add some delay after boot and after initialization of the baud rate.

Connecting it straight to my computer is also a possibility although I don't have a serial port anymore and windows was giving me a hard time configuring those USB to serial modules... I still have my max232 adapter so it could work but I believe this could give more problems then actually resolve some Wink



PS: I always believed the PIC18F2550 used 3.3V on uart. If this is 5V it would explain a lot of problems...
26-08-2014, 10:58 AM,
RE: Serial Interface on pic18f2550
PIC18F2550 runs on 4.2 - 5.0V supply. PIC18LF2550 can run with 2V supply at 4MHz, 3V at 16MHz, 4V at 25MHz besides 4.2 - 5V at 48MHz.

26-08-2014, 11:44 AM, (This post was last modified: 26-08-2014, 11:44 AM by Xumpino.)
RE: Serial Interface on pic18f2550
Ok, but isn't this 4.2 - 5.0V the power supply of the pic. I've connected power to the RPi pin 2 (5V):

I always believed TTL could operate between 2.2 and 5V?

Yesterday I also tried to send a larger string like 'Hello World'.
The strange thing was that the first 3 characters where gibberish and afterwards I continuously received Hello World.

So I think the problem lies more in initialization. When I'm back home I'm going to experiment with some delays.
I really liked that idea.
26-08-2014, 10:44 PM, (This post was last modified: 26-08-2014, 10:57 PM by Xumpino.)
RE: Serial Interface on pic18f2550
Ok, I'm getting a little tired.
I have put bootloader 4.13 on my pic, tested my connection with a pc and tried numerous baud rates.
I even tried a voltage switch without any luck...

Nothing worked. I can receive data. Receiving as in "ello World" instead of "Hello World". Sometimes it gives me "Hello World" but this is rarely.

Sending data to the pinguino is unfortunately impossible...
I really need to send data cause I'm intending to use a GSM module with AT commands over UART.

I don't have an oscillator but imho it can only be 2 possible things.
- I use LVP. The bootloader is programmed with this. Can this be a problem?
- I use 12Mhz crystal. I can't seem to make bootloader 4.14 which have a custom build for 12Mhz to work.

I'm really out of options here. Any help is greatly appreciated. I'm also working on a wiki to program the pic with an ART2003 but if LVP is not possible then I think my wiki is kinda useless...

EDIT: Odd thing is that if I don't add a delay before Serial begins I recieve gibberish to. Adding a delay after does not work...
27-08-2014, 11:15 AM, (This post was last modified: 27-08-2014, 03:47 PM by Xumpino.)
RE: Serial Interface on pic18f2550
Ok, I've found a new interesting path:

Apparently if you use 2 off the same Crystel Load Capacitors their value should be 2(Clock - Cstray).

For the pinguino framework this means. 22pf = 2(20 - 9).
So the Cstray = 9 on 20Mhz. I'm not quit sure if this is normal cause some people say it should be between 2 and 5 but I suppose it depends on your crystal...

Now in my design I use 33pf caps and a clock of 12Mhz.
This means 33pf = 2(12 - (-4.5))

So I have a Cstray of -4.5. Woops that doesn't seem right. I assume this can affect the UART port.

When I'm home I'm going to replace the caps with 15pf. This should give 15pf = 2(12 - 4.5).
So then I would have a Cstray of 4.5 on my 12Mhz crystal.

Can anyone elaborate on this. Why wouldn't this affect the USB interface???



EDIT: Ok other people are telling me that 33pf is no problem at all. I see following in the code of sources.c
// this program is intended for an oscillator frequency of 20 Mhz
I'm going to buy myself some 20Mhz clocks. Nothing to do about it. Alternatively I'm going to experiment with mplab to get this working for 12Mhz. It would be nice if someone could confirm to me that this is the problem before I buy them...
27-08-2014, 08:05 PM,
RE: Serial Interface on pic18f2550
Ok I'm now 100% sure that it's not my hardware. I created an MPLAB project with following code.

* File:   main.c
* Author: Nico
* Created on 27 augustus 2014, 19:33

#include <p18f2550.h>
#include <delays.h>
#include <portb.h>
#include <usart.h>
#include <stdio.h>
#include <stdlib.h>

/*  PIC Configuratings */
#pragma config PLLDIV = 3
#pragma config FOSC = HS
#pragma config WDT = OFF
#pragma config PBADEN = OFF
#pragma config LVP = ON
#pragma config PWRT = ON
#pragma config DEBUG= OFF

/* UART Global variables */
char message;                       //  Used for receiving commands from the computer

void main(void)
    //  Set all of PORTC as outputs. TX and RX must be set as outputs first

    //  Configure UART

I calculated the BRGH with following link:

It came out 77 for baudrate 9600 on 12Mhz crystal.
I uploaded the program with my ART2003 and everything works!!!!

So the raspberry pi can handle the voltage, the crystal can handle 33pf everything is perfectly fine.
I think the problem lies with the serial.c class which is programmed for 20Mhz I believe...

I'm going to investigate if I can refactor this. Anyway.

Thanks for the help,


28-08-2014, 06:11 AM,
RE: Serial Interface on pic18f2550
When you use HS, the CPUDIV default 00 gives 12MHz clock. With BRGH_HIGH and SPBRG 77 to get 9600 (9615) baud, you are using 8bit BRG mode. It is all correct when PLL is not used for USB.

When you use USB PLL, the core clock will be 48MHz instead of 12Mhz and the SPBRG will have to be different. To confirm this, set the config as used in the bootloader.

#pragma config FOSC = HSPLL_HS
#pragma config PLLDIV = 3
#pragma config CPUDIV = OSC1_PLL2  
#pragma config USBDIV = 2

Then with the SPBRG is calculated to 311 (48000000/9600/16-1) and it is bigger than 8bit SPBRG can hold. In 8bit BRG mode, SPBRGH will be ignored and the actual SPBRG used for baud rate generation is 8bit 55 and it will generate 53571 baud instead of 9600.

So you have to use 16bit mode and SPBRGH:SPBRG is calculated to 1249 (48000000/9600/4-1). Since the default is 8bit mode, you need to add the following line to force it to 16bit mode.

BAUDCONbits.BRG16 = 1;

Don't forget to add some delay after this.

28-08-2014, 08:57 AM, (This post was last modified: 28-08-2014, 09:08 AM by Xumpino.)
RE: Serial Interface on pic18f2550
I see, well for my current application I don't really need USB.
But if I abandon Pinguino I'm stuck with uploading my program every time trough my Art2003. Also my license is only valid for 60 days :p

So back to Pinguino.
I see that Pinguino is also configured with BAUDCONbits.BRG16 = 1;
My guess is that the problem does not lie in the booltloader, connections, crystals, caps,...

I think the problem lies in the PinguinoIDE tool.
For some reason it is not compatible with UART over crystals different then 20Mhz. In fact this is stated in the code:
// this program is intended for an oscillator frequency of 20 Mhz

So anyone know how to fix this or is it meant to be this way? This would mean that pinguino has to have a 20Mhz crystal!


29-08-2014, 08:16 AM,
RE: Serial Interface on pic18f2550
Ok, I've setup USB PLL. Core clock is now running at 48Mhz speed. Everything works te same as when I don't use USB PLL.

UART is working perfectly. Now I've searched in the pinguino source code and found that in main.c the variable CRYSTAL is hardcoded to 20.

I think the bootloader can handle other frequencies but at some stage the PinguinoIDE tool can't.
I don't really understand why because normally if the core clock is always 48Mhz then the spbrg is also always 1249. So it shouldn't be hard to calculate all the values or am I missing something?

But hey, in the weekend I have some time to go to my local electronics store and by myself a 20Mhz crystal. I'll report my tests over so anyone else facing the same problem isn't looking at this for days like me.


Forum Jump:

Users browsing this thread: 1 Guest(s)