Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Interfacing HC-06 with PIC

Status
Not open for further replies.

bmuigai

Newbie level 5
Joined
Sep 13, 2012
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Nairobi, Kenya
Activity points
1,345
Hi everyone,
I am trying to make an interface between an android phone and microcontroller using the HC-06 Bluetooth module. I started this morning by trying it out on arduino uno and as there are many tutorials on the internet about the same, I managed in perhaps the first hour to send characters from my phone.

But am a fan of PIC microcontrollers and so I decided to go for the major adventure of the day. As it however turns out about 12 hours later, its turning into a nightmare. I can pair with my phone and initialize the UART communication without problems. when I start typing, I can also tell that there is signal being detected by my PIC. But there seems to be no data transfer except for an error. What could be wrong with my code.

I'm using MikroC and Mplab with PICKIT3 and Blueterm2 in android phone

Code:
 unsigned short counter=0;

void interrupt() {
  if (INTCON.TMR0IF) {
    if (counter >= 20) {
      Soft_UART_Break();
      counter = 0;              // reset counter
    }
    else
      counter++;                // increment counter
    INTCON.TMR0IF = 0;            // Clear Timer0 overflow interrupt flag
    }
}

void main(){
unsigned char error, datain, byte_read=0, i, k=100, j=9, l=200;
    TRISB.F3=0;
    TRISA=0;
    PORTB=0;
    PORTA=0;
    ANSEL=0;
    CMCON=7;
    OSCCON=0B01001100;
     OPTION_REG = 0x04;            // TMR0 prescaler set to 1:32
      TMR0=0;
      INTCON.TMR0IE = 0;              // Enable Timer0 overflow interrupt
      
    for(i=0; i<3; i++){
        PORTA=255;
        Delay_ms(100);
        PORTA=0;
        Delay_ms(100);
        }
    
    error = Soft_UART_Init(&PORTB, 4, 5, 9600, 0);
    Eeprom_Write(0x00, error);
  if (error > 0) {
         Eeprom_Write(0x01, error);
         delay_ms(10);
         /*PORTA=error;  Delay_ms(30);
         PORTA=0;  Delay_ms(30);*/
    while(1);                            // Stop program
  }
  Delay_ms(100);

  while(1) {                              // Endless loop
      INTCON.GIE = 1;               // Global interrupt enable
      INTCON.TMR0IE = 1;              // Enable Timer0 overflow interrupt
    byte_read = Soft_UART_Read(&error);   // Read byte, then test error flag
        INTCON.GIE = 0;                                         // Global interrupt disable
      INTCON.TMR0IE = 0;              // Disable Timer0 overflow interrupt
    if(error==0){     
          Eeprom_Write(j, byte_read);	//save data in Eeprom
          j+=1;  if(j>200) j=9;
          PORTA=3;  Delay_ms(30);		
          Eeprom_Write(0x04, j);		////save number of characters in Eeprom
          PORTA=0;  Delay_ms(30);
          PORTA = byte_read;
          //Eeprom_Write(j, byte_read);
      }
      else Eeprom_Write(0x05, error);	//save error message in Eeprom

  }
}
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
TRISB settings ? Set UART Rx pin as input pin.
 

bmuigai

Newbie level 5
Joined
Sep 13, 2012
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Nairobi, Kenya
Activity points
1,345
I thought
Code:
Soft_UART_Init(&PORTB, 4, 5, 9600, 0);
does that internally. Anyway, I did as you stated but I still get no data. I get the error 255 which is related to
Code:
Soft_UART_Break();
in the timer0 interrupt. MikroC document suggested it as
Code:
Soft_UART_Read(&error);
is a blocking function call. Previously code seemed to hang in the read operation until I added the interrupt.
 

bmuigai

Newbie level 5
Joined
Sep 13, 2012
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Nairobi, Kenya
Activity points
1,345
I have decided to use the hardware UART and I now seem to be getting data sent from Blueterm. However, all the data received is 0xFF no matter what character I transmit. If anyone could point out why this is the case I would really much appreciate..
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top