Continue to Site

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.

Sending data via the USART port in pic18f4550 to a bluetooth module

Status
Not open for further replies.

hmrox

Junior Member level 1
Joined
Nov 27, 2011
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,508
As a part of my project, I'm supposed to send the readings I receive from sensors to a mobile phone. I'm using the BlueTerm app in an Android phone to connect to a lowcost Bluetooth adapter which is attached to the USART port of the pic18f4550.To confirm the functionality of the arrangement I wrote a code which sends back whatever text or number sent from the BlueTerm app. Furthermore, I held the PIC in reset when sending the input from the phone and the echoing stopped.

Now the issue is, I'm unable to send data to the phone directly from the code. Should I convert the data to a certain variable type or send it directly as integer type ? Any help is appreciated..
 

what is the V+ and V- for the amplifier
 

ah - sorry - wrong window .. i was replying another thread when reading yours
.. cool .. u already have the communication tested working ..
from what i read in your post - the echo worked .. and stopped when pic was held in reset - thus confirming that your code was doing the echoing

and now when u want to transfer from pic to bluetooth(phone) - it stopped working ?
should be some minor bug in code
i would suggest to use the same routine you use to receive character ( and echo it ) .. since it was tested before
but instead of echo send the new data you want ..

the variable type depends on the way your compiler handles it ..
if your data is 8 - bits long (assuming that) you could also define it as int8 . or byte .. depends on what your compiler supports
 

ah - sorry - wrong window .. i was replying another thread when reading yours
.. cool .. u already have the communication tested working ..
from what i read in your post - the echo worked .. and stopped when pic was held in reset - thus confirming that your code was doing the echoing

and now when u want to transfer from pic to bluetooth(phone) - it stopped working ?
should be some minor bug in code
i would suggest to use the same routine you use to receive character ( and echo it ) .. since it was tested before
but instead of echo send the new data you want ..

the variable type depends on the way your compiler handles it ..
if your data is 8 - bits long (assuming that) you could also define it as int8 . or byte .. depends on what your compiler supports


I'm using the C18 compiler.. In the previous code, I used the variable type *char* to hold my received data. I tried using the same routine. i.e storing an alphabet in a variable with *char* type and transmitting it inside a while() loop. But instead of that alphabet, I was receiving some other on my phone. Stopping the code and running it back again gave me '{' .. I tried transmitting numbers, storing them as *char* type, but what was displayed on the phone after transmission were some random symbols.
 

the data type may not be a problem .. but u can try unsigned char to be sure you are using 8 bits of data
you first need to get it working in the echo mode ..
i doubt that you are not using the same baud on both ends .. that's when USART communication causes errors like you mentioned
can u cross check this and see if it works
 

hello

I don't know what is your bluetooth device.
I am using bluetooth-click device
in the technical document i saw, something interessting to wake up the device by receiving a BREAK

Trigger Master Mode (SM,2)
In this mode, the device will automatically connect to the pre configured remote slave address when
a character (or characters) are received on the local UART. Connection will remain open until a
configurable idle timer (1 to 255 seconds) expires with no data being received, or a configurable
BREAK character is seen.


but in this case PIC side is MASTER !
 

hello

I don't know what is your bluetooth device.
I am using bluetooth-click device
in the technical document i saw, something interessting to wake up the device by receiving a BREAK

Trigger Master Mode (SM,2)
In this mode, the device will automatically connect to the pre configured remote slave address when
a character (or characters) are received on the local UART. Connection will remain open until a
configurable idle timer (1 to 255 seconds) expires with no data being received, or a configurable
BREAK character is seen.


but in this case PIC side is MASTER !


I'm using a low cost bluetooth transceiver module. It can be seen here **broken link removed**

I understand what you are saying. In my first arrangement, the phone was the Master. But now, since I'm sending data from the PIC, it's going to be the Master. But can you explain a bit more on how the Master mode is triggered ? I didnt quite get it from what you said.

---------- Post added at 20:47 ---------- Previous post was at 20:33 ----------

the data type may not be a problem .. but u can try unsigned char to be sure you are using 8 bits of data
you first need to get it working in the echo mode ..
i doubt that you are not using the same baud on both ends .. that's when USART communication causes errors like you mentioned
can u cross check this and see if it works

I tried using unsigned char, to send a single character 'f', but on receiving the phone was displaying 'u'. The USART port in pic18f4550 is an Enhanced type; meaning that it automatically adjusts the baud rate. Atleast that's what the datasheet mentioned. Nevertheless I assigned a baud rate in my code. The following is my code for echoing from my phone.

Code:
// Program to depict the configuration of EUSART in PIC18F4550
// This code receives and then transmits the same data back to the phone .. // ..

#include "p18f4550.h" // Always include the header file
#include "delays.h" 
 #include "string.h"
 #include "usart.h"



// Configuration bits
/* _CPUDIV_OSC1_PLL2_1L,  // Divide clock by 2
   _FOSC_HS_1H,           // Select High Speed (HS) oscillator
   _WDT_OFF_2H,           // Watchdog Timer off
   MCLRE_ON_3H            // Master Clear on
*/
#pragma config FOSC = HSPLL_HS    // Using 20 MHz crystal with PLL
#pragma config PLLDIV = 5 		// Divide by 5 to provide the 96 MHz PLL with 4 MHz input
#pragma config CPUDIV = OSC1_PLL2 // Divide 96 MHz PLL output by 2 to get 48 MHz system clock
#pragma config USBDIV = 2 		// USB clock comes from 96 MHz PLL output / 2
#pragma config FCMEN = OFF // Disable Fail-Safe Clock Monitor
#pragma config IESO = OFF  // Disable Oscillator Switchover mode
#pragma config PWRT = OFF  // Disable Power-up timer
#pragma config BOR = OFF   // Disable Brown-out reset
#pragma config VREGEN = ON // Use internal USB 3.3V voltage regulator
#pragma config WDT = OFF   // Disable Watchdog timer
#pragma config MCLRE = ON  // Enable MCLR Enable
#pragma config LVP = OFF   // Disable low voltage ICSP
#pragma config ICPRT = OFF // Disable dedicated programming port (44-pin devices)
#pragma config CP0 = OFF   // Disable code protection

void tx_data(unsigned char);

unsigned char rx_data(void);
unsigned char serial_data;
unsigned int i=0;

#define FREQ 20000000    // Frequency = 20MHz
#define baud 9600
#define spbrg_value (((FREQ/64)/baud)-1)    // Refer to the formula for Baud rate calculation in Description tab

void main()
{
   SPBRG=spbrg_value;                                // Fill the SPBRG register to set the Baud Rate
    RCSTAbits.SPEN=1;                                     // To activate Serial port (TX and RX pins)
    TXSTAbits.TXEN=1;                                     // To enable transmission
    RCSTAbits.CREN=1;                                     // To enable continuous reception
    while(1)
    {
        serial_data=rx_data();    // Receive data from PC
        tx_data(serial_data);        // Transmit the same data back to PC
    }


}


void tx_data(unsigned char data1)
{
    TXREG=data1;                                     // Store data in Transmit register
    while(PIR1bits.TXIF==0);                             // Wait until TXIF gets low
}


unsigned char rx_data(void)
{
    while(PIR1bits.RCIF==0);                            // Wait until RCIF gets low
    return RCREG;                                   // Retrieve data from reception register
}


and this is the change I made to transmit directly from the code..


 while(1)
    {
       [COLOR="#FF0000"] //serial_data=rx_data();  [/COLOR]  // Receive data from PC
        tx_data();        // Transmit the same data back to PC
Delay1KTCYx(100);
    }


}


void tx_data(void)
{
    [COLOR="#FF0000"]TXREG='f';         [/COLOR]                            // Store data in Transmit register
    while(PIR1bits.TXIF==0);                             // Wait until TXIF gets low
}


unsigned char rx_data(void)
{
    while(PIR1bits.RCIF==0);                            // Wait until RCIF gets low
    return RCREG;                                   // Retrieve data from reception register
}

Can you please tell me whether I'm headed in the right direction, or what changes to make ?
 
Last edited by a moderator:

hello

meaning that it automatically adjusts the baud rate.
Atleast that's what the datasheet mentioned.
Nevertheless I assigned a baud rate in my code

but in your code you have

#define FREQ 20000000 // Frequency = 20MHz
#define baud 9600
#define spbrg_value (((FREQ/64)/baud)-1) // Refer to the formula for Baud rate calculation in Description tab


My bluetooth device had a default Uart speed of 115200 bds or 9600bds by using extra input.

I configured my PIC UART at 38400 bds AND ALSO my Bluetooth device at the same speed..
so i can test it with PC terminal with same speed on virtual COMx port.

did you successfull get a dialogue between your BT device (PIC side) and PC terminal with virtual port on bluetooth
its more easy to have this first test before to do trials with a phone ....

this is my init sequence for my device..
do you have specsheet data for your device ? to see details for init.

Code:
void  Init_BT_direct()
{
    OUT_RS232
    dummy=PutStrR_RS("$$$");    // Enter command mode
    Put_RS(13);
    OUT_LCD
    LCD_Erase_Ligne(1); LCD_PutRomString(" 1 init mode CMD");
    LCD_Erase_Ligne(2); LCD_PutRomString("$$$             ");
    Tempo(100000);
     OUT_RS232
    dummy=PutStrR_RS("+");                  // Security pin code (mikroe)
    Put_RS(13);
    OUT_LCD                          // CR
    LCD_Erase_Ligne(1);LCD_PutRomString("active Echo     ");
    LCD_Erase_Ligne(2);
    Tempo(100000);
    OUT_RS232
    dummy=PutStrR_RS("SN,MyBT-DB49"); // Name of device
    Put_RS(13);
     OUT_LCD
    LCD_Erase_Ligne(1); LCD_PutRomString(" 2  Name ");
    LCD_Erase_Ligne(2); LCD_PutRomString("SN,MyBT-DB49    ");
    Tempo(100000);
    dummy=PutStrR_RS("SO,CONN");   // Extended status string
    Put_RS(13);
    LCD_Erase_Ligne(1);LCD_PutRomString(" 3 ");
    LCD_Erase_Ligne(2); LCD_PutRomString("SO,CONN         ");
    Tempo(100000);
    OUT_RS232
    dummy=PutStrR_RS("SM,0");
  // Set mode (0 = slave, 1 = master, 2 = trigger, 3 =auto, 4 = DTR, 5 = ANY)
    Put_RS(13);
     OUT_LCD
    LCD_Erase_Ligne(1);LCD_PutRomString(" 4 mode Slave   ");
    LCD_Erase_Ligne(2);LCD_PutRomString("SM,0            ");
    Tempo(100000);
    OUT_RS232
    dummy=PutStrR_RS("SA,1");    // Authentication (1 to enable, 0 to disable)
    Put_RS(13);
     OUT_LCD
    LCD_Erase_Ligne(1);LCD_PutRomString(" 5 Authentifie .");
    LCD_Erase_Ligne(2);LCD_PutRomString("SA,1            ");
    Tempo(100000);
    OUT_RS232
    dummy=PutStrR_RS("SP,1234");  // Security pin code (mikroe)
    Put_RS(13);
     OUT_LCD
    LCD_Erase_Ligne(1);LCD_PutRomString(" 6 Security Pin.");
    LCD_Erase_Ligne(2);LCD_PutRomString("SP,1234         ");
    Tempo(100000);
    OUT_RS232
    dummy=PutStrR_RS("SU,38.4");  // Speed uart defined with 4 caracteres
   // dummy=PutStrR_RS("SU,9600");  // Speed uart
    Put_RS(13);
    OUT_LCD
    LCD_Erase_Ligne(1);LCD_PutRomString("7 New Uart Speed");
    LCD_Erase_Ligne(2);LCD_PutRomString("SU,38.4         ");
    Tempo(100000);
    OUT_RS232
    dummy=PutStrR_RS("---");                  // Security pin code (mikroe)
    Put_RS(13);
     OUT_LCD                          // CR
    LCD_Erase_Ligne(1);LCD_PutRomString(" 8/8 mode Cde   ");
    LCD_Erase_Ligne(2);LCD_PutRomString("--- fin init    ");
    Tempo(100000);
     LCD_Erase_Ligne(2);
    LCD_Erase_Ligne(1);
}

At this init, i set my speed to 38400 bauds !
and i am using SLAVE mode in my application..
 

hello



but in your code you have

#define FREQ 20000000 // Frequency = 20MHz
#define baud 9600
#define spbrg_value (((FREQ/64)/baud)-1) // Refer to the formula for Baud rate calculation in Description tab


My bluetooth device had a default Uart speed of 115200 bds or 9600bds by using extra input.

I configured my PIC UART at 38400 bds AND ALSO my Bluetooth device at the same speed..
so i can test it with PC terminal with same speed on virtual COMx port.

did you successfull get a dialogue between your BT device (PIC side) and PC terminal with virtual port on bluetooth
its more easy to have this first test before to do trials with a phone ....

this is my init sequence for my device..
do you have specsheet data for your device ? to see details for init.

Code:
void  Init_BT_direct()
{
    OUT_RS232
    dummy=PutStrR_RS("$$$");    // Enter command mode
    Put_RS(13);
    OUT_LCD
    LCD_Erase_Ligne(1); LCD_PutRomString(" 1 init mode CMD");
    LCD_Erase_Ligne(2); LCD_PutRomString("$$$             ");
    Tempo(100000);
     OUT_RS232
    dummy=PutStrR_RS("+");                  // Security pin code (mikroe)
    Put_RS(13);
    OUT_LCD                          // CR
    LCD_Erase_Ligne(1);LCD_PutRomString("active Echo     ");
    LCD_Erase_Ligne(2);
    Tempo(100000);
    OUT_RS232
    dummy=PutStrR_RS("SN,MyBT-DB49"); // Name of device
    Put_RS(13);
     OUT_LCD
    LCD_Erase_Ligne(1); LCD_PutRomString(" 2  Name ");
    LCD_Erase_Ligne(2); LCD_PutRomString("SN,MyBT-DB49    ");
    Tempo(100000);
    dummy=PutStrR_RS("SO,CONN");   // Extended status string
    Put_RS(13);
    LCD_Erase_Ligne(1);LCD_PutRomString(" 3 ");
    LCD_Erase_Ligne(2); LCD_PutRomString("SO,CONN         ");
    Tempo(100000);
    OUT_RS232
    dummy=PutStrR_RS("SM,0");
  // Set mode (0 = slave, 1 = master, 2 = trigger, 3 =auto, 4 = DTR, 5 = ANY)
    Put_RS(13);
     OUT_LCD
    LCD_Erase_Ligne(1);LCD_PutRomString(" 4 mode Slave   ");
    LCD_Erase_Ligne(2);LCD_PutRomString("SM,0            ");
    Tempo(100000);
    OUT_RS232
    dummy=PutStrR_RS("SA,1");    // Authentication (1 to enable, 0 to disable)
    Put_RS(13);
     OUT_LCD
    LCD_Erase_Ligne(1);LCD_PutRomString(" 5 Authentifie .");
    LCD_Erase_Ligne(2);LCD_PutRomString("SA,1            ");
    Tempo(100000);
    OUT_RS232
    dummy=PutStrR_RS("SP,1234");  // Security pin code (mikroe)
    Put_RS(13);
     OUT_LCD
    LCD_Erase_Ligne(1);LCD_PutRomString(" 6 Security Pin.");
    LCD_Erase_Ligne(2);LCD_PutRomString("SP,1234         ");
    Tempo(100000);
    OUT_RS232
    dummy=PutStrR_RS("SU,38.4");  // Speed uart defined with 4 caracteres
   // dummy=PutStrR_RS("SU,9600");  // Speed uart
    Put_RS(13);
    OUT_LCD
    LCD_Erase_Ligne(1);LCD_PutRomString("7 New Uart Speed");
    LCD_Erase_Ligne(2);LCD_PutRomString("SU,38.4         ");
    Tempo(100000);
    OUT_RS232
    dummy=PutStrR_RS("---");                  // Security pin code (mikroe)
    Put_RS(13);
     OUT_LCD                          // CR
    LCD_Erase_Ligne(1);LCD_PutRomString(" 8/8 mode Cde   ");
    LCD_Erase_Ligne(2);LCD_PutRomString("--- fin init    ");
    Tempo(100000);
     LCD_Erase_Ligne(2);
    LCD_Erase_Ligne(1);
}

At this init, i set my speed to 38400 bauds !
and i am using SLAVE mode in my application..


Slave mode ? But what if your PIC is sending the data. Doesn't that make it the MAster ? There's no spec sheet available for this bluetooth adapter. It's from ebay and costs like $10 or less. But it is said in the ebay site, that the default baud rate is 9600 for this. By the way, I dont understand how to test it with a PC terminal. Do I need any extra equipment, or will a bluetooth capable laptop suffice ?
 

"I tried using unsigned char, to send a single character 'f', but on receiving the phone was displaying 'u'"

see if the UART uses NRZI encoding to send/receive data
 

Hi,

Try sending the ASCII number from your pic; for example, to receive an "A' on your Blueterm, send 65 (decimal) from your pic to the BT module.

Let us know if that works for you.

Regards,

Anand Dhuru
 

hello

By the way, I dont understand how to test it with a PC terminal. Do I need any extra equipment, or will a bluetooth capable laptop suffice


Yes, your PIC (UART) send string to the bluetooth device ...
On a PC, connect a bluetooth key USB
You can use BlueSoleil software to create a bluetooth Lan
Associate the PC BT and the PIC BT
Lauch a COM Service to declare a virtual com Port connected to the BT device
After you can use Terminal windows, as it is directly connected to your PIC UART !

you can see my own test on my web page : Test du module BlueTooth via Terminal Vbray.


Can you see the reference of the main integrated circuit on your BT device ?
It's could be more easy if you can get the technical specsheet of the bluetooth device.
 

Hi,

Try sending the ASCII number from your pic; for example, to receive an "A' on your Blueterm, send 65 (decimal) from your pic to the BT module.

Let us know if that works for you.

Regards,

Anand Dhuru

I tried sending a '65' both as a *char* type as well as an *int* type. In the first case, I received a '3' but just once, even though it was a continuous coding 8-O . In the second case, i.e as an *int* I didnt receive anything at all. I tried other numbers as well, but to no avail.

---------- Post added at 23:42 ---------- Previous post was at 23:39 ----------

"I tried using unsigned char, to send a single character 'f', but on receiving the phone was displaying 'u'"

see if the UART uses NRZI encoding to send/receive data

I couldn't find the option to change the encoding type in the PIC datasheet. I think by default it uses ASCII. If that's not the case, can you please explain how to do it ?
 

do it in software ... encode and decode your data on send/receive
or to confirm things ..an easy way .. simply send and read as you are doing now ,, but verify that all bytes are NRZI encoded ..
understanding the encoding/decoding and handling it is easy once you know it is being used
 

hello

I couldn't find the option to change the encoding type in the PIC datasheet.

The option is not in the PIC, but in the Bluetooth device setting.

to go away , you must be able to check :
- if your device is in Serial Port Profile (SPP) mode by default ?
- if you are in DATA mode
- if you have this option:
SE,<1,0> Encryption 1 to enable, 0 to disable.
SE=0 no encryption!


Without Datasheet, impossible to help you !

If you can not get the datasheet of your BT device, you better have to change it...
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top