Bluetooth HC05 + KL25Z communicate with Bluetooth dongle in PC in both directions.

Dependencies:   mbed

Committer:
GerritPathuis
Date:
Fri Dec 08 19:27:13 2017 +0000
Revision:
2:cbc675b0abd5
Parent:
1:e73a7c99f767
Works

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GerritPathuis 0:0e236e004748 1 /*
GerritPathuis 0:0e236e004748 2 * Author: G. Pathuis
GerritPathuis 0:0e236e004748 3 * Date: 02-12-17
GerritPathuis 0:0e236e004748 4 * Notes: HC05 connected to a KL25Z
GerritPathuis 0:0e236e004748 5 * Pair with PC use password 1234
GerritPathuis 0:0e236e004748 6 *
GerritPathuis 0:0e236e004748 7 * Four wire connection
GerritPathuis 0:0e236e004748 8 * HC05 to KL25Z
GerritPathuis 0:0e236e004748 9 * GND to GND
GerritPathuis 0:0e236e004748 10 * +5V to 5V
GerritPathuis 0:0e236e004748 11 * TX to pin PTE0
GerritPathuis 0:0e236e004748 12 * RX to pin PTE1
GerritPathuis 0:0e236e004748 13 *
GerritPathuis 0:0e236e004748 14 * Open Tera Term session to connect to KL25Z
GerritPathuis 0:0e236e004748 15 * and
GerritPathuis 0:0e236e004748 16 * Open second Tera Term session to read the Bluetooth
GerritPathuis 2:cbc675b0abd5 17 * dongle in the PC set at 38400 baud
GerritPathuis 0:0e236e004748 18 *
GerritPathuis 1:e73a7c99f767 19 * http://www.wavesen.com
GerritPathuis 0:0e236e004748 20 */
GerritPathuis 0:0e236e004748 21
GerritPathuis 0:0e236e004748 22 #include "mbed.h"
GerritPathuis 1:e73a7c99f767 23 #include "MODSERIAL.h"
GerritPathuis 0:0e236e004748 24
GerritPathuis 2:cbc675b0abd5 25 MODSERIAL pc(USBTX, USBRX, 256, 256);
GerritPathuis 2:cbc675b0abd5 26 MODSERIAL blue(PTE0, PTE1, 256, 256); // TX, RX
GerritPathuis 1:e73a7c99f767 27
GerritPathuis 1:e73a7c99f767 28 DigitalOut myled1(LED1); //Blue
GerritPathuis 1:e73a7c99f767 29 DigitalOut myled2(LED2); //Green
GerritPathuis 1:e73a7c99f767 30
GerritPathuis 1:e73a7c99f767 31 // This function is called when a character received from PC
GerritPathuis 1:e73a7c99f767 32 void pc_rxCallback(MODSERIAL_IRQ_INFO *q)
GerritPathuis 1:e73a7c99f767 33 {
GerritPathuis 2:cbc675b0abd5 34 int c;
GerritPathuis 0:0e236e004748 35
GerritPathuis 1:e73a7c99f767 36 c= pc.getc();
GerritPathuis 1:e73a7c99f767 37 blue.putc(c); // Send from PC to Blue
GerritPathuis 1:e73a7c99f767 38 pc.putc(c); // Echo to PC
GerritPathuis 1:e73a7c99f767 39 }
GerritPathuis 0:0e236e004748 40
GerritPathuis 1:e73a7c99f767 41 // This function is called when a character received from Bluetooth
GerritPathuis 1:e73a7c99f767 42 void blue_rxCallback(MODSERIAL_IRQ_INFO *q)
GerritPathuis 1:e73a7c99f767 43 {
GerritPathuis 2:cbc675b0abd5 44 //char b;
GerritPathuis 2:cbc675b0abd5 45 int b;
GerritPathuis 0:0e236e004748 46
GerritPathuis 1:e73a7c99f767 47 myled1 = !myled1;
GerritPathuis 1:e73a7c99f767 48 b= blue.getc();
GerritPathuis 1:e73a7c99f767 49 pc.putc(b);
GerritPathuis 1:e73a7c99f767 50 }
GerritPathuis 0:0e236e004748 51
GerritPathuis 0:0e236e004748 52 int main()
GerritPathuis 0:0e236e004748 53 {
GerritPathuis 2:cbc675b0abd5 54 char c = 'A';
GerritPathuis 2:cbc675b0abd5 55
GerritPathuis 2:cbc675b0abd5 56 pc.baud(9600);
GerritPathuis 2:cbc675b0abd5 57 pc.format(8,SerialBase::None,1);
GerritPathuis 1:e73a7c99f767 58 blue.baud(38400); // Default Bluetooth Baudrate
GerritPathuis 2:cbc675b0abd5 59 blue.format(8,SerialBase::None,1);
GerritPathuis 1:e73a7c99f767 60
GerritPathuis 1:e73a7c99f767 61 pc.printf("Bluetooth HC-05\r\n");
GerritPathuis 1:e73a7c99f767 62 pc.printf("Make sure the terminal programm ends with CR-LF\r\n");
GerritPathuis 1:e73a7c99f767 63 pc.printf("\r\n");
GerritPathuis 0:0e236e004748 64
GerritPathuis 1:e73a7c99f767 65 wait_ms(100);
GerritPathuis 1:e73a7c99f767 66
GerritPathuis 1:e73a7c99f767 67 // Char from PC, interrupt service routine
GerritPathuis 1:e73a7c99f767 68 pc.attach(&pc_rxCallback, MODSERIAL::RxIrq);
GerritPathuis 1:e73a7c99f767 69
GerritPathuis 1:e73a7c99f767 70 // Char from Bluetooth, interrupt service routine
GerritPathuis 1:e73a7c99f767 71 blue.attach(&blue_rxCallback, MODSERIAL::RxIrq);
GerritPathuis 1:e73a7c99f767 72
GerritPathuis 0:0e236e004748 73
GerritPathuis 1:e73a7c99f767 74 // Set up
GerritPathuis 2:cbc675b0abd5 75 pc.puts("\r\nAT=");
GerritPathuis 1:e73a7c99f767 76 blue.puts("AT\r\n");
GerritPathuis 2:cbc675b0abd5 77 wait_ms(1000);
GerritPathuis 1:e73a7c99f767 78
GerritPathuis 2:cbc675b0abd5 79 pc.puts("\r\nAT+VERSION?= ");
GerritPathuis 1:e73a7c99f767 80 blue.puts("AT+VERSION?\r\n");
GerritPathuis 2:cbc675b0abd5 81 wait_ms(1000);
GerritPathuis 0:0e236e004748 82
GerritPathuis 2:cbc675b0abd5 83 pc.puts("\r\nAT+ADDR?= ");
GerritPathuis 1:e73a7c99f767 84 blue.puts("AT+ADDR?\r\n");
GerritPathuis 2:cbc675b0abd5 85 wait_ms(1000);
GerritPathuis 1:e73a7c99f767 86
GerritPathuis 2:cbc675b0abd5 87 pc.puts("\r\nAT+NAME?= ");
GerritPathuis 1:e73a7c99f767 88 blue.puts("AT+NAME?\r\n");
GerritPathuis 2:cbc675b0abd5 89 wait_ms(1000);
GerritPathuis 2:cbc675b0abd5 90
GerritPathuis 2:cbc675b0abd5 91 pc.puts("\r\nAT+UART=38400,0,0"); //No parity, 1 Stop bit
GerritPathuis 2:cbc675b0abd5 92 blue.puts("AT+UART=38400,0,0\r\n"); //No parity, 1 Stop bit
GerritPathuis 2:cbc675b0abd5 93 wait_ms(1000);
GerritPathuis 1:e73a7c99f767 94
GerritPathuis 2:cbc675b0abd5 95 pc.puts("\r\nAT+UART?= ");
GerritPathuis 2:cbc675b0abd5 96 blue.puts("AT+UART?\r\n");
GerritPathuis 2:cbc675b0abd5 97 wait_ms(1000);
GerritPathuis 2:cbc675b0abd5 98
GerritPathuis 2:cbc675b0abd5 99 pc.puts("\r\nAT+Role?= ");
GerritPathuis 2:cbc675b0abd5 100 blue.puts("AT+Role?\r\n");
GerritPathuis 2:cbc675b0abd5 101 wait_ms(1000);
GerritPathuis 1:e73a7c99f767 102
GerritPathuis 2:cbc675b0abd5 103 pc.puts("\r\nAT+RESET= ");
GerritPathuis 2:cbc675b0abd5 104 blue.puts("AT+RESET\r\n");
GerritPathuis 2:cbc675b0abd5 105 wait_ms(1000);
GerritPathuis 2:cbc675b0abd5 106
GerritPathuis 2:cbc675b0abd5 107
GerritPathuis 2:cbc675b0abd5 108 // Send the alphabet
GerritPathuis 0:0e236e004748 109 while (1) {
GerritPathuis 2:cbc675b0abd5 110 for (int loop = 0; loop < 512; loop++) {
GerritPathuis 2:cbc675b0abd5 111
GerritPathuis 2:cbc675b0abd5 112 blue.printf("%c", c);
GerritPathuis 2:cbc675b0abd5 113 c++;
GerritPathuis 2:cbc675b0abd5 114 if (c > 'Z') c = 'A';
GerritPathuis 2:cbc675b0abd5 115 if (loop > 510) loop = 0;
GerritPathuis 2:cbc675b0abd5 116
GerritPathuis 2:cbc675b0abd5 117 wait_ms(100);
GerritPathuis 0:0e236e004748 118 }
GerritPathuis 0:0e236e004748 119 }
GerritPathuis 0:0e236e004748 120 }