USART protocol with pic18f4580

hello

I have a question about the use of the USART protocol with PIC microcontrollers (I use pic18f4580).
I realized a project using the arduino uno board which consisted of simply lighting an LED with the HC-05 bluetooth module. After doing this I try to do the same thing but using the pic18f4580.
I managed to make all the connections and I managed to realize the code allowing to simply flash a LED (by using MPLAB V8.9). But I do not know how to use the USART serial connection to establish a connection between my microcontroller and bluetooth module HC-05. I would like to know if there are predefined functions allowing me to do all this.
I just remember that I want to light a led with the HC-05 module like with the arduino card uno.

Thanks you.

Can you program a PIC18f4580 as an Arduino ?

I know how to program a pic18f4580(I just have basic knowledge) but I don't how to use the serial communication. On arduino, it is very easzy to use it because we have predefined functions. Can you help me?

takami:
I know how to program a pic18f4580(I just have basic knowledge) but I don't how to use the serial communication. On arduino, it is very easzy to use it because we have predefined functions. Can you help me?

Dont you think it might be a question best asked in a forum that actually supports the processor you want help with ?

The problem is that I don't know an appropriate forum for this question. I have tried to search but I don't find.
Do you know some forum that can help me?

Searching google for "pic microcontroller forum" might be a good start - that gets lots of results which look like active forums...

I'd also try searching for things like "pic microcontroller UART tutorial".

You're just not going to find people who know the answer to PIC-specific questions here, as you can't use the PIC with Arduino, and it's unusual for people who hang out in the Arduino forum to also work with PIC.

Also - UART and USART are different. UART is async only, USART can also be used in syncronous mode. Async mode is what is normally used, it's what you get with the Serial class in Arduino; it uses two wires plus ground, and both sides have to know the baud rate. A USART also supports synchronous mode, an uncommonly used variant on UART which uses an additional wire as a clock signal, eliminating the possibility of baud rate mismatch (ex, from using inaccurate internal oscillators), at the cost of an extra wire, and the need for one device to be the master which generates the clock; it's more like SPI than UART in that way. Synchronous mode is very rarely used. I don't think I have ever seen someone on these forums say they used it!

Hello Takami,
I’ve been using PICs for years, but as others have said; this is not a PIC forum.

There is nothing like:

serial.Begin(9600);

For a PIC, you have to write your own. You need to read the section of the 18F4580 data sheet ‘Enhanced universal synchronous asynchronous receiver transmitter (EUSART)’ and work out what values need to be put into what registers to get what you want. I’ve not used that particular PIC but the EUSARTs are similar if not the same in the 18F series. I’ve posted below sample code from one of my projects that configures one of the serial ports in a 18F26K22. Note that this a sample, it won’t be right for you, but it should get you started.

void initialise_serialport_common() {
    BAUDCON1 = 0x00;
    SPBRGH1 = 0;                  //For rates below
    //SPBRG1 = 25;                  //9600 baud with 16MHz clock
    //SPBRG1 = 103;                 //9600 baud with 64MHz clock
    SPBRG1 = 12;                  //19200 baud with 16MHz clock
    
    //SPBRGH1 = 1;                    //For rates below
    //SPBRG1 = 16;                    //57600 baud with 16MHz clock
    
    RCSTA1bits.SPEN = 1;
}

void initialise_serialport_TX() {
    TXSTA1bits.TXEN = 1;
    //PIE1bits.TX1IE = 1;             //TEST
    IPR1bits.TX1IP = 0;             //TEST
    }

void initialise_serialport_RX() {
    unsigned char x;
    RCSTA1bits.CREN = 1;
    PIE1bits.RC1IE = 1;
    IPR1bits.RCIP = 0;
    x = RCREG1;                     //Remove any spurious unread data from receive buffer
    x = RCREG1;
    x = RCREG1;
}

You call each function once in setup in the order they are shown.