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.

8051 interfacing Bluetooth/Hyperterminal example/help

Status
Not open for further replies.

johnjameson

Junior Member level 3
Joined
Jan 30, 2012
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,516
Hey guys,
Just looking for a bit of guidance when using a bluetooth module.
I have a HC-06 Bluetooth module,I put it on a breadboard,powered it up,paired it with my pc.Then I used Teraterm to open a connection to it,which it did(the red light stayed continuously on),entered in the command “AT” but I got no response back.(I thought I could send commands to it without it having to be connected to a micro,like to change its baud rate or something).
I presume this has something to do with the HC-06 being a slave only module and it has to be connected to a microcontroller in order to view data on the hyperterminal.
So my query is can anyone direct to where I might view some example code which I could load onto a 8051 micro(something that would send a word or number to the bluetooth) and then I could view the word/number on the hyperterminal
thanks
 

Hi,

Ifi remember right, then the AT mode is only acessed from the uart side (ucontroller), and not from the radio side.

Additionally i think there must be some function to enable the AT mode,
How else could you send the string "AT*" via bluetooth.

Klaus
 

Hi James,

Klaus is absolutely right, to access the AT set you must be connected thru the uart, not the bluetooth.

Secondly, any command you send, the characters need to be sent in very quick succession, or else the module doesnt recognose it. Its easier to write your complete AT command in say notepad, and then copy-paste it into your terminal emulator.

Regards,

Anand
 

Ok thanks guys.
Leaving the AT commands aside,if I were to load onto the 8051 micro something that would send a word or number to the bluetooth,would I then be able to view the word/number on the hyperterminal?
 

Absolutely.

Just to check out your setup, leave aside the 8051. Just short the Tx and Rx on the HC-06, pair it with your PC, configure the hyperterminal with the right com port.

Now, whatever characters you hammer out on the hyperterminal should echo back.
 
Absolutely.

Just to check out your setup, leave aside the 8051. Just short the Tx and Rx on the HC-06, pair it with your PC, configure the hyperterminal with the right com port.

Now, whatever characters you hammer out on the hyperterminal should echo back.
I'll do that and then report back.
 

Absolutely.

Just to check out your setup, leave aside the 8051. Just short the Tx and Rx on the HC-06, pair it with your PC, configure the hyperterminal with the right com port.

Now, whatever characters you hammer out on the hyperterminal should echo back.
Yip did the test,whatever I typed was echoed back.
Next step is probably test it with an 8051 chip,so as I wrote earlier can anyone point me in the direction of some example code I can load onto the 8051 that can be sent to the bluetooth which I can view on the hyperterminal
 

hello,


an example of init for HC06 , written in MilroC Pro 6.40
UART interrupt and a buffer is used to receive answer from HC06
i use also Timer1 to go out the waiting response if no answer from HC06
i add also a wire on Reset input of HC06 .. (because no pin out for this signal)



Code:
int Init_BT_With_feeback()
{  
  int State=0;
  LCD_putcmd(LCD_LINE2,1);
  LCD_Write_CText("INIT.BT HC06 BLEU   ");
  LCD_putcmd(LCD_LINE3,1);
  LCD_Write_CText("Etape: 1 sur 4");  Delay_ms(500);
  Delay_ms(500);
  State=0;
  // on nettoye avant..
  Raz_Buffer();
  UART1_Write_CText("AT");  // PAS de CR apres cde !
  Delay_ms(500);
  Flag_Timer1=0; TMR1IE_bit=1;
  do
  {
    if(memcmp(buffer,"OK",2)==0)
    {
      State=1;
      break;
     }
  }while(Flag_Timer1==0);
  if(Flag_Timer1==1)return State;
  buffer[19]=0;  LCD_putcmd(LCD_LINE4,1); LCD_puts(buffer);
  Delay_ms(500);

  //-----------------------------
  LCD_putcmd(LCD_LINE3,1);
  LCD_Write_CText("Etape: 2 sur 4");  Delay_ms(500);
  Raz_Buffer();
  UART1_Write_CText("AT+PIN1234");  // PAS de CR apres cde !
  Delay_ms(500);
  Flag_Timer1=0; TMR1IE_bit=1;
  do
  {
    if(memcmp(buffer,"OKsetPIN",8)==0)
    {
      State=2;
      break;
     }
  }while(Flag_Timer1==0);
  if(Flag_Timer1==1) return State;
  buffer[19]=0;  LCD_putcmd(LCD_LINE4,1); LCD_puts(buffer);
  Delay_ms(500);
   // -------------------------------
  LCD_putcmd(LCD_LINE3,1);
  LCD_Write_CText("Etape: 3 sur 4");    Delay_ms(500);
  Raz_Buffer();
  UART1_Write_CText("AT+NAMEHC06-7D69B");  // PAS de CR apres cde !
  Delay_ms(500);    buffer[19]=0;  LCD_putcmd(LCD_LINE4,1); LCD_puts(buffer);
  Flag_Timer1=0; TMR1IE_bit=1;
  do
  {
    if(memcmp(buffer,"OKsetname",9)==0)
    {
      State=3;
      break;
     }
  }while(Flag_Timer1==0);
  if(Flag_Timer1==1) return State;
  buffer[19]=0;  LCD_putcmd(LCD_LINE4,1); LCD_puts(buffer);
  Delay_ms(500);
  // --------------------------------------
   LCD_putcmd(LCD_LINE3,1);
  LCD_Write_CText("Etape: 4 sur 4");    Delay_ms(500);
  Raz_Buffer();
  UART1_Write_CText("AT+VERSION");
  Delay_ms(500);   buffer[19]=0;  LCD_putcmd(LCD_LINE4,1); LCD_puts(buffer);
  Flag_Timer1=0; TMR1IE_bit=1;
  do
  {
    if(memcmp(buffer,"OKlinvor",8)==0)State++;
    {
      State=4;
      break;
     }
  }while(Flag_Timer1==0);
  if(Flag_Timer1==1) return State;
  Delay_ms(500);   buffer[19]=0;  LCD_putcmd(LCD_LINE4,1); LCD_puts(buffer);
  CRLF();
  State=5;
  Delay_ms(500);
  Raz_Buffer();
  return(State);
 }

in the main ...


Code:
  BT_Reset=0;
 
  UART1_Init(19200); 
...
  Init_Timer1(); 
  BT_Reset=1;
 .....

  k=Init_BT_With_feeback()  ;
  Delay_ms(500);
  LCD_putcmd(LCD_LINE4,1); Delay_ms(25);
  if(k>4)
   PrintOut(LCDHandler, "Init BT OK State=%02d ",k);
   else
    PrintOut(LCDHandler, "Init BT BAD State=%02d",k);
    Delay_ms(500);
 

Does the code have to be as long as that?
For example would this work for sending data to hyperterminal via bluetooth
example for uart
Code:
#include <REG51.H>                /* special function register declarations   */ 
#include <stdio.h>                /* prototype declarations for I/O functions */   
void serial_init(void); 
//------------------------------------------------- 
//Setup the serial port for 9600 baud at 11.0592MHz. 
//------------------------------------------------- 

void serial_init(void) 
{     
SCON  = 0x50;                         /* SCON: mode 1, 8-bit UART, enable rcvr         */     
TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload                    */     
TH1   = 0xFD;               /* TH1:  reload value for 9600 baud @ 11.0592MHz*/    
TR1   = 1;                  /* TR1:  timer 1 run                               */     
TI    = 1;                  /* TI:   set TI to send first char of UART           */ 
}  

 //-------------------------- 
//Main Program Starts Here
 //-------------------------- 

void main(void)
{             
serial_init();            
printf (" PS - PrimerC51 UART Demo\n\n\r");             
while (1)             
{             
printf ("Hello World!! \n\r");   /* Print "Hello World" */             
}         
}
 

hello,


In fact, the HC06 must be ready to dialog in SSP mode by default
and use factory configuration..
Youre code can work if your device has same COM speed.


To know his configuration, the best way, is to connect the BT module directly (trough a MAX322(if 3,3V level)
to a PC terminal
and send the request with the PC keyboard
at the first AT command you must get a OK response,
in this case ,it means that you connect with the correct speed..else try other speed

AT
ATNAMEnewname
ATVERSION
ATBAUD5 for 19200
if you change the speed , don't forget to change also on the PC ..
ATPINxxxx with xxxx 4 digits

it depends of your model.. mine is comming from MiniIntheBox RPC vendor..

After you can play with your MCU ....

The minimum test is to send AT and test if you receive OK
else you don't know if the link between MCU and BT device is OK or not..

after the bluettooh link with another external device is another subject..
 

Does the code have to be as long as that?
For example would this work for sending data to hyperterminal via bluetooth
example for uart

Hi, If your 8051 is communicating with the PC directly over the com port, you'd need absolutely no changes to your code while using the HC-06 (as long as the baud rate on the HC-06, usually 9600 by default) matches what you're using on the 8051.
 

hello

we suppose also, that the device pairing , PC side, is correct ..
and you allready have a driver to get the connexion with a virtual com port from your PC Bluetooth interface.
and software authorisation to do that...

example with BlueSoleil on PC side
after pairing OK with the Bluetooth device
need to connect to a virtual com PORTA
wich define after ..a COM#4 (or other!) to use with the terminal software..



BT_Autorisation_PortA_virtuel.jpg
 

we suppose also, that the device pairing , PC side, is correct ..
and you allready have a driver to get the connexion with a virtual com port from your PC Bluetooth interface.
and software authorisation to do that...

Yes thats true. But since the OP has already successfully tested the HC-06 to PC connection with a loopback (shorting the Tx and Rx on the HC-06), I assume that part is already in place.
 

Right so I tried the loopback test and that worked.
I then loaded my 8051 with this code
Code:
#include <REG51.H>                /* special function register declarations   */ 
#include <stdio.h>                /* prototype declarations for I/O functions */   
void serial_init(void); 
//------------------------------------------------- 
//Setup the serial port for 9600 baud at 11.0592MHz. 
//------------------------------------------------- 

void serial_init(void) 
{     
SCON  = 0x50;                         /* SCON: mode 1, 8-bit UART, enable rcvr         */     
TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload                    */     
TH1   = 0xFD;               /* TH1:  reload value for 9600 baud @ 11.0592MHz*/    
TR1   = 1;                  /* TR1:  timer 1 run                               */     
TI    = 1;                  /* TI:   set TI to send first char of UART           */ 
}  

 //-------------------------- 
//Main Program Starts Here
 //-------------------------- 

void main(void)
{             
serial_init();            
printf (" PS - PrimerC51 UART Demo\n\n\r");             
while (1)             
{             
printf ("Hello World!! \n\r");   /* Print "Hello World" */             
}         
}

But nothing came through on the hyperterminal,it was able to open a connection alright but then nothing but a blank screen.
I am using a 11.0592MHz with my 8051 too.
 

hello,

sorry, i don't know 8051

try to add some delay in your code ...

and maybe try also to reverse " \n\r" by "\r\n" or only "\r"
i allready encounter this kind of problem with external BT equipement..with RFOBasic and bluetooh application.

Code:
Delay_ms(500);  
while (1)             
{             
printf ("Hello World!! \n\r");   /* Print "Hello World" */  
Delay_ms(500);           
}
 

But nothing came through on the hyperterminal,it was able to open a connection alright but then nothing but a blank screen.
I am using a 11.0592MHz with my 8051 too.

When you connect your 8051 project to hyperterminal directly using a cable, does it work as expected?
 
Last edited:

hello,

sorry, i don't know 8051

try to add some delay in your code ...

and maybe try also to reverse " \n\r" by "\r\n" or only "\r"
i allready encounter this kind of problem with external BT equipement..with RFOBasic and bluetooh application.

Code:
Delay_ms(500);  
while (1)             
{             
printf ("Hello World!! \n\r");   /* Print "Hello World" */  
Delay_ms(500);           
}
I'll try that.
Its code I saw on this webpage
**broken link removed**
but maybe I should be using this
**broken link removed**

When you connect your 8051 project to hyperterminal directly using a cable, does it work as expected?
I didn't connect it directly as I dont have a cable.
 
Last edited:

Right so I tried the loopback test and that worked.
I then loaded my 8051 with this code
Code:
#include <REG51.H>                /* special function register declarations   */ 
#include <stdio.h>                /* prototype declarations for I/O functions */   
void serial_init(void); 
//------------------------------------------------- 
//Setup the serial port for 9600 baud at 11.0592MHz. 
//------------------------------------------------- 

void serial_init(void) 
{     
SCON  = 0x50;                         /* SCON: mode 1, 8-bit UART, enable rcvr         */     
TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload                    */     
TH1   = 0xFD;               /* TH1:  reload value for 9600 baud @ 11.0592MHz*/    
TR1   = 1;                  /* TR1:  timer 1 run                               */     
TI    = 1;                  /* TI:   set TI to send first char of UART           */ 
}  

 //-------------------------- 
//Main Program Starts Here
 //-------------------------- 

void main(void)
{             
serial_init();            
printf (" PS - PrimerC51 UART Demo\n\n\r");             
while (1)             
{             
printf ("Hello World!! \n\r");   /* Print "Hello World" */             
}         
}

But nothing came through on the hyperterminal,it was able to open a connection alright but then nothing but a blank screen.
I am using a 11.0592MHz with my 8051 too.

So I went back and tried this again but again no success,so I tried swapping 8051 chips and then it worked fine so I think my original chip is fried.

- - - Updated - - -

Kinda was trying adding adding the above serial code this code now
Code:
#include<reg51.h>
 
#include<string.h>
 
 
//heart beat monitor 8051 based
#define lcdport P1 // change for for hardware
sbit rw = P2^1; // LCD connection may be different
sbit rs=P2^0; // LCD interface with microcontroller
sbit en=P2^2; // Enable pin of LCD
unsigned char sec,sec100;
unsigned int bt,tick,r,bpm;
void lcdinit();
void lcdcmd(unsigned char);
void lcddata(unsigned char);
void send_string(unsigned char *s);
void msdelay(unsigned int);
void extrint (void) interrupt 0 // external Interrupt to detect the heart pulse
{
bt=tick; // number of ticks are picked
tick=0; // reset for next counting
}
void timer0 (void) interrupt 1 using 1 // Timer 0 for one second time
{
TH0 = 0xdc; //The value is taken for Ssc/100 at crystal 11.0592MHz
sec100++; // It is incremented every Ssc/100 at crystal 11.0592MHz
tick++; // This variable counts the time period of incoming pulse in Sec/100
if(tick>
=3500){tick=0;} // tick are limited to less trhan 255 for valid calculation
if(sec100 >
=100) // 1 sec = sec100 * 100
{
sec++;
sec100=0;
}
}
void main()
{
P0=0xff;
P1=0xff;
P2=0xff;
P3=0xff;
rw=0;
EA = 1;
TMOD = 0x21;
IT0 = 1;
EX0 = 1;
ET0 = 1;
TR0 = 1;
msdelay(1000);
lcdinit();
msdelay(1000);
send_string("Heart beat ");
msdelay(1500);
msdelay(500);
//delay(15000);
bpm=0;bt=0;
while(1)
{
if(sec >
=1)
{
sec=0;
/*
The sampling time is fixed 1 sec.
A variable "tick" is incremented with one tick per 100mSc in the timer 0 interrupt routine.
Each on occurring of external interrupt the value in the "tick" is picked up
and it is set to zero for recounting.
The process continues till next external interrupt.
Formula for calculating beats per minutes (microcontroller based heartbeat monitor ) is
as tick is the time period in Sec/100. so extract the frequency of pulses at external interrupt
Frequency = (1/tick)* 100 i.e pulses /sec
Then
bpm = frequency * 60 for one minutes i.e pulses per minute
in short we can do it as
bpm = 6000/ bt
*/
lcdcmd(0x02);
if(bt >
=7){
bpm = 6000/bt; // for valid output bt is limited so that it should be greater than 6
msdelay(500);
send_string("Pulse. ");
lcddata((bpm/100)+0x30);
r=bpm%100;
lcddata((r/10)+0x30);
lcddata((r%10)+0x30);
send_string(" bpm ");
}
else {
send_string("out of range");} // otherwise bpm will be shown zero, if limit does not fit for your project you can change it.
}
}
}
void lcdinit()
{
msdelay(100);
lcdcmd(0x01);
msdelay(500);
lcdcmd(0x38);
msdelay(500);
lcdcmd(0x38);
msdelay(500);
lcdcmd(0x38);
msdelay(500);
lcdcmd(0x06);
msdelay(500);
lcdcmd(0x0c);
msdelay(500);
lcdcmd(0x03);
msdelay(500);
msdelay(500);
}
void lcdcmd(unsigned char value)
{
rs=0;
lcdport=value;
msdelay(100);
en=1;
msdelay(100);
en=0;
msdelay(100);
rs=1;
}
void lcddata(unsigned char value)
//heart beat monitoring system using microcontroller
{
rs=1;
lcdport=value;
msdelay(10);
en=1;
msdelay(100);
en=0;
rs=0;
}
void msdelay(unsigned int i)
{
//unsigned int i;
while(i --);
}
void send_string(unsigned char *s)
{
unsigned char l,i;
l = strlen(s); // get the length of string
for(i=1;i <=l;i++)
{
lcddata(*s); // write every char one by one
s++;
}
}

But wanna remove all the lcd related parts and add in the serial parts so would something like this work?
the serial parts I added have ++ before them here but there not in the actual code
Code:
++#include <REG51.H>
                /* special function register declarations   */ 
++#include <stdio.h>
                /* prototype declarations for I/O functions */   
++void serial_init(void); 
//------------------------------------------------- 
//Setup the serial port for 9600 baud at 11.0592MHz. 
//-------------------------------------------------[/b]
++void serial_init(void) 
{     
++SCON  = 0x50;                         /* SCON: mode 1, 8-bit UART, enable rcvr         */     
++TMOD |= 0x20;               /* TMOD: timer 1, mode 2, 8-bit reload                    */     
++TH1   = 0xFD;               /* TH1:  reload value for 9600 baud @ 11.0592MHz*/    
++TR1   = 1;                  /* TR1:  timer 1 run                               */     
++TI    = 1;                  /* TI:   set TI to send first char of UART           */ 
} [/b] 
 
unsigned char sec,sec100;
unsigned int bt,tick,r,bpm;
void msdelay(unsigned int);
 
void extrint (void) interrupt 0 // external Interrupt to detect the heart pulse
{
bt=tick; // number of ticks are picked
tick=0; // reset for next counting
}
void timer0 (void) interrupt 1 using 1 // Timer 0 for one second time
{
TH0 = 0xdc; //The value is taken for Ssc/100 at crystal 11.0592MHz
sec100++; // It is incremented every Ssc/100 at crystal 11.0592MHz
tick++; // This variable counts the time period of incoming pulse in Sec/100
if(tick>
=3500){tick=0;} // tick are limited to less trhan 255 for valid calculation
if(sec100 >
=100) // 1 sec = sec100 * 100
{
sec++;
sec100=0;
}
}
void main()
{
++serial_init(); [/b]
P0=0xff;
P1=0xff;
P2=0xff;
P3=0xff;
 
EA = 1;
TMOD = 0x21;
IT0 = 1;
EX0 = 1;
ET0 = 1;
TR0 = 1;
msdelay(1000);
 
msdelay(1000);
++printf("Heart beat ");
msdelay(1500);
msdelay(500);
//delay(15000);
bpm=0;bt=0;
while(1)
{
if(sec >
=1)
{
sec=0;
/*
The sampling time is fixed 1 sec.
A variable "tick" is incremented with one tick per 100mSc in the timer 0 interrupt routine.
Each on occurring of external interrupt the value in the "tick" is picked up
and it is set to zero for recounting.
The process continues till next external interrupt.
Formula for calculating beats per minutes (microcontroller based heartbeat monitor ) is
as tick is the time period in Sec/100. so extract the frequency of pulses at external interrupt
Frequency = (1/tick)* 100 i.e pulses /sec
Then
bpm = frequency * 60 for one minutes i.e pulses per minute
in short we can do it as
bpm = 6000/ bt
*/
 
if(bt >
=7){
bpm = 6000/bt; // for valid output bt is limited so that it should be greater than 6
msdelay(500);
++printf[/b]("Pulse. ");
r=bpm%100;
++printf[/b](" bpm ");
}
else {
++printf[/b]("out of range");} // otherwise bpm will be shown zero, if limit does not fit for your project you can change it.
}
}
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top