problem interfacing bluetooth with pic

Status
Not open for further replies.

fuwty

New Member
hi everyone ,
my project is simple :
I'am using 16f877A PIC microcontroller , hc05 bluetooth module
if I send a letter(ex: 'a') via terminal on my mobile phone it turns on all port b legs
but its not working
i wrote a code to echo what has been received and its working , it receives the information from my mobile correctly
here is my code :
C:
=======================

unsigned char uart_rd ;
char* data_r = "data received: ";
main()
{
    trisb =0 ;
    portb =0 ;
  
    UART1_init(9600);
    delay_ms(200) ;

    while(1)
    {
        if ( UART1_Data_Ready() )
        {

            uart_rd = UART1_Read() ;

            UART1_Write_Text(data_r);
            UART1_Write(uart_rd)   ;
        }
        if ( uart_rd == 'a')
              portb=0b11111111 ;
            else if ( uart_rd == 'b' )
              portb =0b00000000 ;
            UART1_Write(x);
    }
}
======================
wrote on mikroC pro for pic

a picture of the terminal on my mobile phone: https://i.imgur.com/ApjTfFa.png

ps: everything works fine on protus
but not in real life :/
 
Last edited by a moderator:
Could you post a block diagram showing how each part relates to the rest?
 
Normally When Proteus works but real devices don't the OSC is set wrong.... Its the only thing Proteus doesn't simulate..
 
Hi Ian,
I think this test is using real hardware. My feeling is that the hardware is working correctly as the character sent is sent back with "data received" appended to it. It looks like the code that tests for an "a" or a "b" and sets or clears all the bits in portB is not working. If it was me I would modify the program so that its sets bits in port B without any test. As port B is cleared at the start of the program any received character should cause port B to change. My understanding of "C" programming is VERY LIMITED so I do not know if the syntax is correct in the last part of the program. I THINK I can follow the code except for the last line "UART1_Write(x);" I do not know waht x is.

Les.
 
I THINK I can follow the code except for the last line "UART1_Write(x);" I do not know waht x is.
Same here!! MikroC is peculiar... A) it allows non case sensitivity (does my nut in ) and B) doesn't really tell you a compile has failed... You need to monitor the output dialog... Using "X" like that should throw an exception and fail a compile!!
 
I forgot to remove this , I was debugging my code but forgot it , any way the problem existed even before i wrote that line xD
================= i tried to use ASCII value like below but in vain =============
unsigned char uart_rd ;
main()
{
trisb =0 ;
portb =0 ;

UART1_init(9600);
delay_ms(200) ;

while(1)
{
if ( UART1_Data_Ready() )
{
uart_rd = UART1_Read() ;
UART1_Write_Text("data received: ");
UART1_Write(uart_rd) ;
}
if ( uart_rd == 97)
portb=0b11111111 ;
else if ( uart_rd == 98 )
portb =0b00000000 ;

}
}
======================== ( sorry for writing code like this , the code insert button not working :/ =================
here is a block diagram of this project (protus simulation /)
 
i tried to display the recieved data on my lcd but i get 0
=================code ================
sbit LCD_RS at Rb2_bit; sbit LCD_EN at Rb3_bit;
sbit LCD_D4 at Rb4_bit; sbit LCD_D5 at Rb5_bit;
sbit LCD_D6 at Rb6_bit; sbit LCD_D7 at Rb7_bit;
sbit LCD_RS_Direction at TRISb2_bit; sbit LCD_EN_Direction at TRISb3_bit;
sbit LCD_D4_Direction at TRISb4_bit; sbit LCD_D5_Direction at TRISb5_bit;
sbit LCD_D6_Direction at TRISb6_bit; sbit LCD_D7_Direction at TRISb7_bit;

char txt[7];
unsigned int uart_rd ;
main()
{ lcd_init(); lcd_cmd(_lcd_cursor_off);

UART1_init(9600);
delay_ms(200) ;
lcd_out(2,1,"All");
while(1)
{
if ( UART1_Data_Ready() )
{
uart_rd = UART1_Read() ;
UART1_Write_Text("data received: ");
UART1_Write(uart_rd) ;
IntToStr(uart_rd, txt);
lcd_out(1,1,txt);

}


}
}
======================================
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…