Severals problems with BT and PIC

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Hi
BT HC06 connected to my pic.
When i enable either RCIE or TXIE the program execution stops.
C:
//
#include    "lcd.c"
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#pragma config FEXTOSC = LP //XT     // External Oscillator mode selection bits (XT (crystal oscillator) above 500kHz, below 4MHz; PFM set to medium power)
#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 = OFF      // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF       // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)

#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF        // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

#pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

#pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)


#define _XTAL_FREQ 8000000
#define FOSC 2000000L

#define back_light RB0
#define led1_on RE0
#define KN1 RC3
#define KN2 RC2
#define KN3 RA7
#define KN4 RA6


#define Skip_ROM             0xCC
#define Convert_T             0x44
#define Read_scratchpad     0xBE

#define Port_18B20             RB5
#define Tx_18B20             TRISB5 = 0
#define Rx_18B20             TRISB5 = 1

#define WAIT1                1000
#define WAIT2                500

#define DS18B20_CONV_TIME    750
#define DS18B20_RESET_PULSE 480
#define DS18B20_WAIT_TIME     60
#define DS18B20_PULLUP_TIME 2

#define ONEWIRE_PRESENT     0
#define ONEWIRE_ABSENT         1

bit modtaget,data_tick,sekund;
int get_value,data_inde;

static void interrupt isr(void)   // Here is interrupt function - the name is unimportant.
{
  
if(TMR1IF)             // Was this a timer overflow?
    {
          TMR1IF=0;        // Clear interrupt flag, ready for next // timer run to 65k, gives 2 sec interrupt
        //TMR1L=0x00;  // overflow every 2 seconds. to prevent to much interrupt.
         //TMR1H=0x80;     // If we set TMR1 to start at 0x8000 (32768), the TMR1 will overflow every 1 second
     sekund=1;  
   }


}



          void setup(void)
{   
           
COSC0=0;
COSC1=0;
COSC2=1; // use second oss.
HFFRQ0=1;
HFFRQ1=1;
HFFRQ2=0; // 8 mhz

TRISA0=1; // NU
TRISA1=1; // NU
TRISA2=1; // NU
TRISA3=1; // NU
TRISA4=1; // input to timer
TRISA5=1; // NU
TRISA6=1; // Button 1
TRISA7=1; // Button 2

TRISB0=0; // Backlight on/off
TRISB1=0; // not used
TRISB2=0; // not used
TRISB3=0; // RS
TRISB4=0; // not used
TRISB5=1;// CHANGE temp reading DS18B20
TRISB6=1; // program
TRISB7=1; // program

TRISC0=1; // Xtal
TRISC1=1; // Xtal.
TRISC2=1; // button 3
TRISC3=1; // button 4
TRISC4=1; // not used
TRISC5=1;  // not used
//TRISC6=0;  //send data
//TRISC7=1; // recieve data

TRISD0=0; // DATA6
TRISD1=0; // DATA7
TRISD2=0; // DATA5
TRISD3=0; // DATA4
TRISD4=0; // NU
TRISD5=0; // NU
TRISD6=0; // NU
TRISD7=0; // ENABLE LCD

TRISE0=0; // LED Out
TRISE1=1;
TRISE2=1;
//TRISE3=1; // allways input RESET

/*IOCAN2=1; // negativ interrupt A2*/
//IOCAP3=1; // positiv interrupt A3 knap 4*/
//IOCAP4=1; // interrupt on pin RA4  positive =  rain income
//IOCCP3=1; // interrupt on pin RC3 positive= knap 1

ANSELA=0b00000000; // set alle digital.
ANSELB=0b00000000;
ANSELC=0b00000000;
ANSELD=0b00000000;
ANSELE=0b0000;

//timer 1 setup
T1CS0=0; // 0110 = SOCS
T1CS1=1;
T1CS2=1;
T1CS3=0;
T1CKPS0=0; // no prescale
T1CKPS1=0;
nT1SYNC=1; // no sync
  
lcd_init();  //lcd's init
lcd_goto(0);
}
          //Function to get a char from Rx.buffer of BT//
char BT_get_char(void) 
{
    if(OERR) // check for over run error
    {
        CREN = 0;
        CREN = 1; //Reset CREN
    }
  
    if(RCIF==1) //if the user has sent a char return the char (ASCII value)
    {
    while(!RCIF);
    return RCREG;
    }
    else //if user has sent no message return 0
        return 0;
}
//End of function/

//Function to broadcast data from RX. buffer//
void broadcast_BT()
{
  TXREG = 13;
  __delay_ms(500);
}
//End of function//

//Function to load the Bluetooth Rx. buffer with one char.//
void BT_load_char(char byte)
{
    TXREG = byte;
    while(!TXIF);
    while(!TRMT);
}
//End of function//

//Function to Load Bluetooth Rx. buffer with string//
void BT_load_string(char* string)
{
    while(*string)
    BT_load_char(*string++);
}
//End of function/


//******Initialize Bluetooth using USART********//
void Initialize_Bluetooth()
{
   //Set the pins of RX and TX//
    TRISC6=1;
    TRISC7=1;
  
  //Set the baud rate using the look up table in datasheet(pg114)//
    BRGH=1;      //Always use high speed baud rate with Bluetooth else it wont work
    SPBRG=12;//129; // baudrate 9615
  
    //Turn on Asyc. Serial Port//
    CREN=1;
[B][SIZE=7]//   SYNC=0; // not possible to use[/SIZE][/B]
    SPEN=1;
  
    //Set 8-bit reception and transmission
    RX9=0;
    TX9=0;

   //Enable transmission and reception//
    TXEN=1;
    CREN=1;
  
    //Enable global and ph. interrupts//
  //  GIE = 1;
   /// PEIE= 1;

    //Enable interrupts for Tx. and Rx.//
  //  RCIE=1;
    //TXIE=1;
}
//___________BT initialized_____________//

      
void main (void)
{
     setup();
       Initialize_Bluetooth(); //lets get our bluetooth ready for action
   //  IOCAF4=0;
    lcd_clear();
   lcd_goto(0x00);
     back_light=1; // turn on light in LCD
    lcd_puts("Line 1 : Hello");
    lcd_goto(0x40);
    lcd_puts("Line 2 : Denmark!");
    lcd_goto(0x14);
    lcd_puts("Line 3 : World");
    lcd_goto(0x54);
   lcd_puts("Line 4 : Goodbye!");
  RE0=1;
  

    T1RD16=0; // turn on timer1 16 bit read write
    TMR1ON=1; // turn on timer1
    T1GE=0; // gate allways count
//   IOCIE=1; // enable IOC
   TMR1IE=1;  // enable interrupr for timer1
   GIE=1; // enable global interrupr
   PEIE=1;  // enable interrupr
   __delay_ms(500); // delay

    RCIF=0;
    RCREG=0;
  
[B][SIZE=7]  // RCIE=1; // interrupt for Recieving enabled , gives program stop[/SIZE][/B]
 
  
     while (1)
     {
      
         if (sekund==1)
         { sekund=0;
         (led1_on=!led1_on);
         }
         if (!KN1)
         {
           lcd_clear();
        
           [B][SIZE=7]TXIE=1; // if enabled, program stops[/SIZE][/B]
        
           BT_load_string("Bluetooth Initialized and Ready");
   broadcast_BT();
   BT_load_string("Press 1 to turn ON LED");
   broadcast_BT();
   BT_load_string("Press 0 to turn OFF LED");
   broadcast_BT();
         }
       
   
         
          get_value=BT_get_char(); //Read the char. received via BT
    //If we receive a '0'//
        if (get_value=='0')
          {
             RE0=0;
             BT_load_string("LED turned OFF");
             broadcast_BT();
          }
     
    //If we receive a '1'// 
        if (get_value=='1')
          {
             RE0=1;
             BT_load_string("LED turned ON");
             broadcast_BT();
          }    
      }
       
       
         }
I think it has something to do with the "SYNC" Problem ??
Datasheet says SYNC, the 16f18877.h files also say SYNC is a valid word,
but Mplab X says Unable to resolve identifier SYNC

BT Modul is connected by a 3.3 to 5v inverter.
Can easy connect phone to BT modul
 

djsfantasi

Joined Apr 11, 2010
9,237
Hi
BT HC06 connected to my pic.
When i enable either RCIE or TXIE the program execution stops.
C:
//
#include    "lcd.c"
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
I think it has something to do with the "SYNC" Problem ??
Datasheet says SYNC, the 16f18877.h files also say SYNC is a valid word,
but Mplab X says Unable to resolve identifier SYNC

BT Modul is connected by a 3.3 to 5v inverter.
Can easy connect phone to BT modul
Don’t you need to include the file 16f18877.h in your program?
 

spinnaker

Joined Oct 29, 2009
7,830
What do you mean "the program execution stops"??? Where is is "stopping" ?


Where is the code for your serial port interrupt?
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Program stops at line 283, if i use TXIE=1;
Get no more interrupt from timer,
Yes i think i need something in interrupt code, but it's a working sample from net, just made for 16F877

as i get it, "they" use
char BT_get_char(void)
to "catch" the interrupt.

.
 

AlbertHall

Joined Jun 4, 2014
12,625
You have an interrupt handler for the timer. It clears the timer interrupt flag and returns to main code until the next timer interrupt.

You do not have an interrupt handler for transmit or receive interrupts. If either of these occur the code will branch to the interrupt handler. It will check whether it is a timer interrupt. As it isn't, the handler will simply exit - interrupt flag will still be set - and the program will immediately branch to the interrupt handler again. It will continue going around that loop for ever and will execute no other code.

Whenever you enable an interrupt you need a handler for that interrupt which will, as a minimum, clear the interrupt flag.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
have made this from an old pic to pic project
C:
static void interrupt isr(void)   // Here is interrupt function - the name is unimportant.
{
  
if(TMR1IF)             // Was this a timer overflow?
    {
          TMR1IF=0;        // Clear interrupt flag, ready for next // timer run to 65k, gives 2 sec interrupt
        //TMR1L=0x00;  // overflow every 2 seconds. to prevent to much interrupt.
         //TMR1H=0x80;     // If we set TMR1 to start at 0x8000 (32768), the TMR1 will overflow every 1 second
     sekund=1;  
   }



  if(PIR3bits.RCIF==1)// data in uart.
    {
        if(RCSTA&0x06)     // more efficient way than following commented method to check for reception error
                        // if(RCSTAbits.FERR==1 || RCSTAbits.OERR==1 )
    {
      RCSTAbits.CREN=0; // Overrun error (can be cleared by clearing bit CREN)
      data_inde=RCREG;  // clear Framing error
      RCSTAbits.CREN=1;
    }
    else
    {
            while(!RCIF)
    {
        get_value = RCREG; // read new data into variable
        data_tick=1;     // new data received. so enable flg
        lcd_puts("Modt");
    }
      }
  }

}
NO way to clear the interrupt, it's an read only bit.
And the "send" if is apposite, meaning that interrupt when nothing to send,
 

spinnaker

Joined Oct 29, 2009
7,830
Program stops at line 283, if i use TXIE=1;
Get no more interrupt from timer,
Yes i think i need something in interrupt code, but it's a working sample from net, just made for 16F877

as i get it, "they" use
char BT_get_char(void)
to "catch" the interrupt.

.

You don't need to enable interrupts for the interrupts flag to be set. You only enable transmit / receive interrupts when you have an interrupt handler, which you have none.

The interrupt flag will still be set when a character arrives in the input buffer.

But are you certain a character is arriving at the Pic? I have had 2 HC05s that do not send characters sent by the PC. Place your scope on the RX pin of your Pic. Send some characters and see if you are getting transitions on the scope.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
Am i missing the meaning.
I mean that by sending to TX pin on my PIC, the BlueTooth should recieve it, and broadcast it.
Recive by RX pin, when connected to BT TX Pin
PIN TX on my PIC is connected to RXD pin on BT.
PIN RX on my PIC is connected to TRD pin on BT:

By connecting a smartphone or PC with BT, and by using of a Bluetooth Terminal i should be able to see data sent to BT and data recieved from BT,
BT Terminal say that i am connected to HC-06.
 

spinnaker

Joined Oct 29, 2009
7,830
I tried the LED idea. It does not work. I think the LED is not turned off long enough to see any flicker or even dimness. But one thing you can check is that the pin from your HC05 is normally high. Transmitting to the pin forces it low. The failurs I have seen both had the TX pin always low.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
708
did a little measurement,
When not connected to other da 5vDC i got the TXD high,3,3V, and the RXD Low 0v.
Tried to connect TXD and RXD, and what i send from phone to BT came return,
So BT module is working,. i think,

I Still think i have something to do with then "SYNC" command i cant use
 

spinnaker

Joined Oct 29, 2009
7,830
If you want to communicate to the HC05, you need to use asynchronous mode not synchronous.


When polling, which is what you are doing, THERE IS NO NEED TO SET RCIE or TXIE!!!!!!!!!!!! How many times to I need to mention that?
 
Top