Serial communication RS-485 between Pic16F690/PIC16F18877

Serial communication RS-485 between Pic16F690/PIC16F18877

Postby ciclingman » Mon Jan 19, 2026 9:22 pm

Good evening everyone. I'm trying to get a PIC16F690 to communicate with PIC16F18877. I think I have made all the necessary settings, but my doubt is the clock; on the Pic17F690 I have an internal 8MHz oscillator and on the Pic16F18877; I set values based on each of the clocks, following 9600. Can anyone say where I'm wrong?
ciclingman
 
Posts: 18
Joined: Sun Mar 17, 2024 9:11 pm

Re: Serial communication RS-485 between Pic16F690/PIC16F1887

Postby AussieSusan » Tue Jan 20, 2026 3:02 am

We really can't help much without seeing the full code (preferably of a short app that exhibits the problem). Please include the config settings.
AussieSusan
Verified identity
 
Posts: 183
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Serial communication RS-485 between Pic16F690/PIC16F1887

Postby ciclingman » Tue Jan 20, 2026 8:46 pm

Setting PIC16F18877

#define _XTAL_FREQ 32000000

#include <xc.h>
#include <stdint.h>
#include <stdbool.h>
#include <pic.h>

#define Enable PORTCbits.RC5 //Clock for comunication

// PORT C
TRISC = 0b10001100; //RC7 input (RX) RC6 output (TX)

//Set interrupt and timer at 1 ms
INTCONbits.GIE = 1;
PIE0bits.TMR0IE = 1;
T0CON0 = 0x87;

// Enable TMR0 interrupt.
PIE0bits.TMR0IE = 1;

// T0OUTPS 1:1; T0EN enabled; T016BIT 8-bit;
T0CON0 = 0x80;

RCIE = 1; //Interrupt abilited ricection

TX1STAbits.SYNC = 0;
RC1STAbits.SPEN = 1;
RC1STAbits.CREN = 1; //Settaggi per ricezione
BRGH = 1; BRG16 = 0; SPBRG = 191;





//Setting PIC16F690

#define _XTAL_FREQ 8000000

#define Enable RB6 //Clock for comunication

ANSEL = 0x00;
ANSELH= 0X00;

// PORT B
TRISB = 0b00111111; //RB5 input (RX) RB7 output (TX)

OPTION_REG = 0b10000010; //Prescaler 1:8 e disabilitazione Pull-up
INTCON = 0b10100000;
OSCCON = 0b01110000; //8 MHz
TMR0 = 6; //TIMER0 a 1 ms
RCIE = 1; //Interrupt abilited ricection
SYNC = 0; CREN = 1; SPEN = 1;
BRGH = 1; BRG16 = 0; SPBRG = 17;
ciclingman
 
Posts: 18
Joined: Sun Mar 17, 2024 9:11 pm

Re: Serial communication RS-485 between Pic16F690/PIC16F1887

Postby ciclingman » Tue Jan 20, 2026 8:48 pm

I would also like to send the hardware schematic but it is not possible. However, the two tranceivers are connected to the two PICs as per the datasheet.
ciclingman
 
Posts: 18
Joined: Sun Mar 17, 2024 9:11 pm

Re: Serial communication RS-485 between Pic16F690/PIC16F1887

Postby AussieSusan » Thu Jan 22, 2026 3:10 am

There is a reason I asked for the config register settings.
For the PIC16F690, your setting of OSCCON to 0x01110000 has the SCS bit set to 0 which says that the clock source is set by FOSC but you don't show that setting. Assuming that you really do have an 8MHz clock, then using Table 12-1 of the data sheet, I get a baud rate of 27,778. Looking at the 3rd part of Table 12-3 which matches your values for SYNC, BRGH and BRG16, then for 8MHz, the SPBRG value should be 51. In general, the Microchip UARTs need to be initialised strictly in the order they describe in the data sheet - in this case sections 12.3.1 and 12.3.2
For the PIC16F18877, you don't show anything about the clock speed but given your (slightly ambiguous) statement in the original post, I assume that you have an 8MHz clock there as well. If so then your baud rate calculation is way off. Even taking the Example 33-1 and using halving their Fosc, you should have a SPBRG value closer to 12. As above, the initialisation sections are 33.1.1 and 33.1.2.8.
Your example includes a number of lines that set the timers - these are totally irrelevant for the UARTs.
At this stage I would also NOT play with interrupts - get the basic functionality working first with blocking code and then add in the complexities of interrupts later on (if needed at all).
(By the way - at this stage you don't need to define _XTAL_FREQ as you are not using any compiler-defined delay functions.)
AussieSusan
Verified identity
 
Posts: 183
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Serial communication RS-485 between Pic16F690/PIC16F1887

Postby ciclingman » Thu Jan 22, 2026 12:34 pm

In PIC16F18877 I set the internal clock to 32MHz and I think that's right, because the firmware works perfectly, except for communication. In PIC16F690 I set the internal clock to 8 MHz and I'm sure it works fine for the rest of the software works fine. You say that the problem of communication malfunction is due to the wrong set clocks? If so, could you please write the right setting for the two clocks? However I can deduce that it is possible to communicate with two PICs with different clocks from each other
ciclingman
 
Posts: 18
Joined: Sun Mar 17, 2024 9:11 pm

Re: Serial communication RS-485 between Pic16F690/PIC16F1887

Postby ciclingman » Thu Jan 22, 2026 8:45 pm

Oscillator PIC16F18877

#pragma config CLKOUTEN = OFF // Clock Out Enable bit->CLKOUT function is disabled; i/o or oscillator function on OSC2
#pragma config CSWEN = ON // Clock Switch Enable bit->Writing to NOSC and NDIV is allowed
#define _XTAL_FREQ 32000000

// NOSC HFINTOSC; NDIV 4;
OSCCON1 = 0x62;
// CSWHOLD may proceed; SOSCPWR Low power;
OSCCON3 = 0x00;
// MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
OSCEN = 0x00;
// HFFRQ 4_MHz;
OSCFRQ = 0x02;
// HFTUN 0;
OSCTUNE = 0x00;
ciclingman
 
Posts: 18
Joined: Sun Mar 17, 2024 9:11 pm

Re: Serial communication RS-485 between Pic16F690/PIC16F1887

Postby AussieSusan » Fri Jan 23, 2026 2:47 am

According to that bit of code, you are using the HFINTOSC but (even in your comment) you set OSCFRQ to 2 which sets the HFINTOSC to 4MHz.
You then divide that by 4 (NDIV/CDIV) so the frequency is 1MHz.
Therefore I don't think you have "...set the internal clock to 32MHz...".
You can also save all this code by setting the RSTOSC config work to HFINT1 (check this as I'm only going by the key word used in the datasheet) which will set OSCCON1 to 0x62 for you. Ditto the OSCFRQ register. (See the descriptions and nodes for these 2 registers).
Also writing 0 to the OSCCON3 and OSCEN registers is not necessary (given only the code you have shown) as these are the POR values.
AussieSusan
Verified identity
 
Posts: 183
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist

Re: Serial communication RS-485 between Pic16F690/PIC16F1887

Postby ciclingman » Fri Jan 23, 2026 12:34 pm

I used a PIC16F18877 for my project because I need a lot of income and expenses, but I don't know it at all. I thought I had set the oscillator to 32 MHz; it's strange that the timer0 works fine despite the settings for the 32MHz oscillator. Thank you Susan for the technical support and I kindly ask if you can help me set the internal oscillator to 32 MHz.
ciclingman
 
Posts: 18
Joined: Sun Mar 17, 2024 9:11 pm

Re: Serial communication RS-485 between Pic16F690/PIC16F1887

Postby ciclingman » Fri Jan 23, 2026 6:26 pm

From the test I did now communicate, thanks to you. I did nothing but eliminate:

/*NOSC HFINTOSC; NDIV 4;
OSCCON1 = 0x62;
// CSWHOLD may proceed; SOSCPWR Low power;
OSCCON3 = 0x00;
// MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
OSCEN = 0x00;
// HFFRQ 4_MHz;
OSCFRQ = 0x02;
// HFTUN 0;
OSCTUNE = 0x00;*/

and leave the lines:
#pragma config FEXTOSC = OFF // External Oscillator mode selection bits->Oscillator not enabled
#pragma config RSTOSC = HFINT32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)
#pragma config CLKOUTEN = OFF // Clock Out Enable bit->CLKOUT function is disabled; i/o or oscillator function on OSC2
#pragma config CSWEN = ON // Clock Switch Enable bit->Writing to NOSC and NDIV is allowed
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit->FSCM timer enabled
#define _XTAL_FREQ 32000000
ciclingman
 
Posts: 18
Joined: Sun Mar 17, 2024 9:11 pm

Next

Return to SCI/USART/EUSART

Who is online

Users browsing this forum: No registered users and 1 guest