bluetooth

General discussion on mikroC.
Post Reply
Author
Message
marco
Posts: 57
Joined: 03 Jan 2008 20:34

bluetooth

#1 Post by marco » 22 Oct 2009 22:59

hello my friends.

First, i would like to thank every one in this forum.
This is my favorit, and in my opinion the best forum ever.
People here ,are very helpful,and they did realy help me a lot.

well, today i´m here for bluetooth.

I want to send and recive data from pic to pc.
I´m using pic16f877A, the bluethoot module is :Mini Wireless Bluetooth TTL Transceiver Module 9600 .

First, i tried to transcive data via rs232 (cable ) using pic 16f877A conected with MAX232 Board (mikroElektronika) .using only vcc,gnd,rx,and tx pins.

with this programme:

#include "built_in.h"

unsigned int Vin;
unsigned short i;

void main() {
UART_Init(9600); // Initalize USART (9600 baud rate, 1 stop bit, ...

// Select Vref and analog inputs, in order to use ADC_Read
ADCON1 = 0; // All porta pins as analog, VDD as Vref
TRISA = 0xFF; // PORTA is input

TRISD = 0x01;
do {

// Read ADC results and send via UART
Vin = ADC_Read(0);
UART_Write(Hi(Vin));
UART_Write(Lo(Vin));


UART_Write(PORTD.F0);

Delay_ms(10);


if (UART_Data_Ready()) { // only do the following if data is received
i = UART_Read(); // read the received data & assign to i
PORTD.F1 = i; // assign i to PORTD
} // end if
Delay_ms(10);

} while (1); // endless loop
}
Using hyperteminal, i did transcive data, and did work very well.


Now , i want to use bluetooth to transcive data.
Instead of using MAX232 Board (mikroElektronika), i´m using bluetooth module, and the conection pins are the same,vcc,gnd,rx and tx.In pc a´m using a bluetooth dongle. the programme for pic is the same,baud rate the same, rx is conected to tx, and tx conected to rx............
I can detect the module and the connection ,but i can´t transcive data.is it the pic programme ? is there any probleme with usart ? do i use uart or usart ??
how can i change boud rate of bluetooth??

thank you my friends




[/img]

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

#2 Post by anikolic » 25 Oct 2009 09:59

Hi,
First of all, if you are using mikroC for PIC, then use USART library functions, and if you are using mikroC PRO for PIC, then use UART library function. Because this is mikroC forum topic, I will assume you use this compiler.
I must point out that PORTD.F0 means that you read or write the value to the RD0 pin (bit), and the line UART_Write(PORTD.F0) only writes a single pin value. If you want to send the value of the entire port, you should use the following: Usart_Write(PORTD). The same is for assigning a byte value to a bit: PORTD.F1 = i should be replaced by PORTD = i. Take a look at the corrected code:

Code: Select all

#include "built_in.h"

unsigned int Vin;
unsigned short i;

void main() {
  Usart_Init(9600); // Initalize USART (9600 baud rate, 1 stop bit, ...

  // Select Vref and analog inputs, in order to use ADC_Read
  ADCON1 = 0; // All porta pins as analog, VDD as Vref
  TRISA = 0xFF; // PORTA is input

  TRISD = 0x01;
  do {

    // Read ADC results and send via UART
    Vin = Adc_Read(0);
    Usart_Write(Hi(Vin));
    Usart_Write(Lo(Vin));


    Usart_Write(PORTD);

    Delay_ms(10);


    if (Usart_Data_Ready()) { // only do the following if data is received
      i = Usart_Read(); // read the received data & assign to i
      PORTD = i; // assign i to PORTD
    } // end if
    Delay_ms(10);

  } while (1); // endless loop
}
I can detect the module and the connection ,but i can´t transcive data.is it the pic programme ? how can i change boud rate of bluetooth??
There must be some problem in the communication protocol of your PC Bluetooth configuration. Check the specification for any specific software handshake or some other parameters.

Best regards,
Aleksandar
Web Department Manager

marco
Posts: 57
Joined: 03 Jan 2008 20:34

#3 Post by marco » 27 Oct 2009 17:24

Sory !!!

It was a hardware probleme!!
now it is transciving data, but i have a question!!!
I´m using hyperterminal and Data is:

Connected to COM15
Received: 0x03
Received: 0xA9 0x00
Received: 0x03 0xA9 0x00
Received: 0x03
Received: 0xA9 0x00
Received: 0x03 0xA9 0x00
Received: 0x03
Received: 0xA9 0x00
Received: 0x03 0xA9 0x00
Received: 0x03
Received: 0xA9 0x00
Received: 0x03
Received: 0xA9 0x00
Received: 0x03
Received: 0xA9 0x00
Received: 0x03
Received: 0xA9 0x00
Received: 0x03
Received: 0xA9 0x00
Received: 0x03 0xA9 0x00
Received: 0x03
Received: 0xA9 0x00
Received: 0x03
Received: 0xA9 0x00
Received: 0x03
Received: 0xA9 0x00


Why data is not always like this : Received: 0x03 0xA9 0x00
Received: 0x03 0xA9 0x00
Received: 0x03 0xA9 0x00
Received: 0x03 0xA9 0x00

Does it have something to do whith XTAL ??? i´m using 8Mhz, is it RTS ? I´m not using it.
Does it make any diference, that data is not send at the samme line??


thank you very mutch my friend for the reply.

User avatar
anikolic
mikroElektronika team
Posts: 1775
Joined: 17 Aug 2009 16:51
Location: Belgrade
Contact:

#4 Post by anikolic » 28 Oct 2009 14:18

Hi,
I cannot seem to recreate your error, because I am always receiving a correct inline triad of bytes. What compiler version are you using? Have you tried your code in mikroC PRO for PIC v3.20 with USART Terminal from v3.20 IDE?

Best regards,
Aleksandar
Web Department Manager

adlawan89
Posts: 3
Joined: 25 Jul 2011 00:29

Re: bluetooth

#5 Post by adlawan89 » 25 Jul 2011 02:38

Hi,

Can you help me find what's wrong with my code? My plan is to measure the total insolation (W/m²) in a given area and I am using PIC16f877a for the analogue input. I used AN2 for my analogue input and I want to send the converted 10 bits through UART/hyperterminal. The problem is I can't see any numbers, instead I can only see some smileys, hearts,etc.(very annoying!). Please help me with this.

Here's my code:

unsigned long tlong;
unsigned char ch;
unsigned int adc_rd;

void main()
{
UART1_Init(9600);
Delay_ms(100);
ADCON1 = 0; //All PORTA pins as analog.
TRISA = 0xFF; //PORTA as input.

while(1)
{
adc_rd = ADC_Read(2);
tlong = (long)adc_rd*5000;
tlong = tlong/1023;
ch = tlong/1000;
UART1_Write_Text(ch);
Delay_ms(1000);

}
}

I'll surely be glad if anyone will help me.

Thanks Ahead!

marco
Posts: 57
Joined: 03 Jan 2008 20:34

Re: bluetooth

#6 Post by marco » 25 Jul 2011 15:11

Hello my friend .

Have you tried the exemples from Mikroelektronika ?
You have ADC_UART, it was from this exemple that i have started !

unsigned short temp_res;

void main() {
USART_Init(9600); // Initalize USART (19200 baud rate, 1 stop bit, no parity...)

ANSEL = 0x04; // Configure AN2 pin as analog input
TRISA = 0xFF;
ANSELH = 0; // Configure other AN pins as digital I/O

do {
temp_res = ADC_Read(2) >> 2; // Read 10-bit ADC from AN2 and discard 2 LS bits
USART_Write(temp_res); // Send ADC reading as byte
Delay_ms(100);
} while (1); // endless loop
}



If you run this exemple ,and you have the same probleme ( smiles and hearts), try the configuration on hyperterminal.

were it is Format : ASCII : HEX :DEC .

I think that you should use hex !I don´t use this for a while, but if the configuration don´t help i will try run the exemple on pic board !

Please replay , any time .

Post Reply

Return to “mikroC General”