1
\$\begingroup\$

I'm currently working on a project that allows you control LEDs with bluetooth. I'm using an HC-05 bluetooth module. I got some code from the DeepBlue.com site where they explained the use of bluetooth modules with pic microcontrollers. I got the code working with pic16F628A, but I want to use an pic18 version. So I used an pic18F14k22, since I had that laying around, but there the code doesn't work. Of course I made some changes to the pin numbers(RX & TX are different, as well as the LED pins). I can see the HC 05 connecting with my phone(there's a delay in the blinking of the red light). But it doesn't send out any signal.

And yes I made sure the pins are connected correctly. Don't know if it's just the pic or the code. Hope anyone can help me.

Here's my code:

#include <xc.h>
#include "config.h"
#include <stdint.h>
#define _XTAL_FREQ 4000000
//--------------------------------
#define LED_ON 49
#define LED_OFF 50

// Functions Declarations
void UART_RX_Init(void);

// Globals
uint8_t UART_Buffer = 0;

// Main Routine
void main(void)
{
UART_RX_Init(); // Initialize The UART in Master Mode @ 9600bps 
TRISB4 = 0; 
TRISB6 = 0; 
RB6 = 1; // Initially OFF
RB4 = 0; // Initially OFF

while(1)
{


}
return;
}

// Functions Definitions
void UART_RX_Init()
{
BRGH = 1; // Set For High-Speed Baud Rate
SPBRG = 25; // Set The Baud Rate To Be 9600 bps

// Enable The Ascynchronous Serial Port
SYNC = 0;
SPEN = 1;

// Set The RX-TX Pins to be in UART mode (not io)
TRISB5 = 1; // As stated in the datasheet
TRISB7 = 1; // As stated in the datasheet

//--[ Enable UART Receiving Interrupts ]--
RCIE = 1; // UART Receving Interrupt Enable Bit
PEIE = 1; // Peripherals Interrupt Enable Bit
GIE = 1; // Global Interrupt Enable Bit
CREN = 1; // Enable Data Continous Reception
}

void __interrupt() my_isr (void)
{
  if (RCIF == 1)
  {
    RB4 = 1;
    UART_Buffer = RCREG; // Read The Received Data Buffer
    if(UART_Buffer == LED_ON)
    {
      RB6 = 1;
      RB4 = 0;
    }
    if(UART_Buffer == LED_OFF)
    {
      RB6 = 0;
      RB4 = 1;
    }
    RCIF = 0;
  }
}



// PIC18F14K22 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1H
#pragma config FOSC = IRC       // Oscillator Selection bits 
#pragma config PLLEN = OFF      // 4 X PLL Enable bit 
#pragma config PCLKEN = OFF     // Primary Clock Enable bit
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable 
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit 

// CONFIG2L
#pragma config PWRTEN = ON      // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bits 
#pragma config BORV = 19        // Brown Out Reset Voltage bits 

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer Enable bit 
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits 

// CONFIG3H
#pragma config HFOFST = OFF     // HFINTOSC Fast Start-up bit 
#pragma config MCLRE = ON       // MCLR Pin Enable bit 

// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit 
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit 
#pragma config BBSIZ = OFF      // Boot Block Size Select bit 
#pragma config XINST = OFF      // Extended Instruction Set Enable bit 

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit 
#pragma config CP1 = OFF        // Code Protection bit 

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit 
#pragma config CPD = OFF        // Data EEPROM Code Protection bit 

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit 
#pragma config WRT1 = OFF       // Write Protection bit

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit
#pragma config WRTB = OFF       // Boot Block Write Protection bit 
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit
#pragma config EBTR1 = OFF      // Table Read Protection bit 

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit

#include <xc.h>
\$\endgroup\$
8
  • 1
    \$\begingroup\$ The code is an unreadable mess, please try to re-format it. \$\endgroup\$
    – Lundin
    Commented Sep 28, 2020 at 13:11
  • \$\begingroup\$ There you go Lundin, edited especially for you :) \$\endgroup\$
    – user263952
    Commented Sep 28, 2020 at 13:43
  • \$\begingroup\$ I unsloppified some of it, it's still a mess but almost readable now. \$\endgroup\$
    – Lundin
    Commented Sep 28, 2020 at 13:47
  • \$\begingroup\$ that's .... great .... I guess, BUT can you help me with the problem \$\endgroup\$
    – user263952
    Commented Sep 28, 2020 at 13:50
  • \$\begingroup\$ Not used to PIC myself, but I'd suspect clock setup before anything else. Also setting UART Tx as input doesn't seem right. And you should call UART_RX_Init() after setting data direction and initial state of the LED pins. \$\endgroup\$
    – Lundin
    Commented Sep 28, 2020 at 13:53

1 Answer 1

0
\$\begingroup\$

I see three issues in your code:

  1. Since you're using the internal oscillator with PLL turned off, the frequency of the clock will be defined by the IRCF bits of OSCCON. Your code doesn't set the value of IRCF, so it will run with the default value of 1MHz. That conflicts with your value of _XTAL_FREQ.

  2. The value of SPBRG you set will result in a baud rate of 2400, not 9600. enter image description here

  3. Pins B4 and B5 default to analog input functionality. You have to explicitly disable analog in order to use digital features. Do this with the ANSELH register. enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ Ok thanks, I got it working. changed SPBRG to 103 and added IRFC2 = 101 to the code \$\endgroup\$
    – user263952
    Commented Sep 30, 2020 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.