Electronic – Is it possible to enable both UART and PLL at the same time on PIC18F

picplluart

I'm doing an application where I'm turning on some ws2812 LED with my pic microcontroller and I want it to talk with my smartphone via bluetooth. Now, I got the code for the LEDs from

http://mjhdesigns.com/?p=312

and I can light up the LEDs with the colors I want (via hardcoding a RGB value) and separately, I can send a message on my phone (via bluetooth terminal) and my microcontroller does receive the full message (inspected by debugger).

However, the problem is when I put the two together. The LED code I got require PLL to be enabled, but I am finding out that when I put the line to enable the PLL

OSCTUNEbits.PLLEN=1;

the microcontroller's receive message gets garbled.

I already tried turning the PLL on and off after the microcontroller receives the message ie

//Receives the message from smart phone      
for(i=0;i<10;i++){
            while(!RCIF);
            receiveArray[i]=RCREG;
 }
 //Turns on lights
 OSCTUNEbits.PLLEN=1;
 OSCCONbits.IRCF = 0b110; 
 SetAllRGB(0,0,100);
 writePinMain(0);
 DelayMs(5000);    
 OSCTUNEbits.PLLEN=0;
 OSCCONbits.IRCF = 0b000; 

However, this results in the LED colors being inaccurate, which leads me to believe the frequency is off or the PLL is screwed up.

I should also note that the code runs on a XTAL_FREQ of 8MHz configuration bits haven't changed at all when I swapped between LED lighting and UART code (comment one or the other out), so I do not think configuration bits are a problem, but other then that, I'm out of ideas on what could cause this.

Is there something I'm just not seeing?

Best Answer

As already pointed out by the comments, you have to adjust your baud rate settings for the UART to account for the change in frequency by a factor of 4. I usually use something like

// baudrate = ~115,2k
if (PLLEN) {
    SPBRGH1 = 0x00;
    SPBRG1 = 0x89;
} else {
    SPBRGH1 = 0x00;
    SPBRG1 = 0x22;
}

in my UART initialization to have it always work (of course you have to configure PLLEN first).