Gerrit Pathuis / Mbed 2 deprecated HC05_KL25Z_Hello_PC

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Author: G. Pathuis
00003  * Date: 02-12-17
00004  * Notes: HC05 connected to a KL25Z
00005  * Pair with PC use password 1234
00006  *
00007  * Four wire connection
00008  * HC05 to KL25Z
00009  * GND to GND
00010  * +5V to 5V
00011  * TX to pin PTE0
00012  * RX to pin PTE1
00013  *
00014  * Open Tera Term session to connect to KL25Z
00015  * and
00016  * Open second Tera Term session to read the Bluetooth
00017  * dongle in the PC set at 38400 baud
00018  *
00019  * http://www.wavesen.com
00020 */
00021 
00022 #include "mbed.h"
00023 #include "MODSERIAL.h"
00024 
00025 MODSERIAL  pc(USBTX, USBRX, 256, 256);
00026 MODSERIAL  blue(PTE0, PTE1, 256, 256);          // TX, RX
00027 
00028 DigitalOut myled1(LED1);    //Blue
00029 DigitalOut myled2(LED2);    //Green
00030 
00031 // This function is called when a character received from PC
00032 void pc_rxCallback(MODSERIAL_IRQ_INFO *q)
00033 {
00034     int c;
00035 
00036     c= pc.getc();
00037     blue.putc(c);   // Send from PC to Blue
00038     pc.putc(c);     // Echo to PC
00039 }
00040 
00041 // This function is called when a character received from Bluetooth
00042 void blue_rxCallback(MODSERIAL_IRQ_INFO *q)
00043 {
00044     //char b;
00045     int b;
00046 
00047     myled1 = !myled1;
00048     b= blue.getc();
00049     pc.putc(b);
00050 }
00051 
00052 int main()
00053 {
00054     char c = 'A';
00055 
00056     pc.baud(9600);
00057     pc.format(8,SerialBase::None,1);
00058     blue.baud(38400);                // Default Bluetooth Baudrate
00059     blue.format(8,SerialBase::None,1);
00060 
00061     pc.printf("Bluetooth HC-05\r\n");
00062     pc.printf("Make sure the terminal programm ends with CR-LF\r\n");
00063     pc.printf("\r\n");
00064 
00065     wait_ms(100);
00066 
00067     // Char from PC, interrupt service routine
00068     pc.attach(&pc_rxCallback, MODSERIAL::RxIrq);
00069 
00070     // Char from Bluetooth, interrupt service routine
00071     blue.attach(&blue_rxCallback, MODSERIAL::RxIrq);
00072 
00073 
00074     // Set up
00075     pc.puts("\r\nAT=");
00076     blue.puts("AT\r\n");
00077     wait_ms(1000);
00078 
00079     pc.puts("\r\nAT+VERSION?= ");
00080     blue.puts("AT+VERSION?\r\n");
00081     wait_ms(1000);
00082 
00083     pc.puts("\r\nAT+ADDR?= ");
00084     blue.puts("AT+ADDR?\r\n");
00085     wait_ms(1000);
00086 
00087     pc.puts("\r\nAT+NAME?= ");
00088     blue.puts("AT+NAME?\r\n");
00089     wait_ms(1000);
00090 
00091     pc.puts("\r\nAT+UART=38400,0,0");       //No parity, 1 Stop bit
00092     blue.puts("AT+UART=38400,0,0\r\n");     //No parity, 1 Stop bit
00093     wait_ms(1000);
00094 
00095     pc.puts("\r\nAT+UART?= ");
00096     blue.puts("AT+UART?\r\n");
00097     wait_ms(1000);
00098 
00099     pc.puts("\r\nAT+Role?= ");
00100     blue.puts("AT+Role?\r\n");
00101     wait_ms(1000);
00102 
00103     pc.puts("\r\nAT+RESET= ");
00104     blue.puts("AT+RESET\r\n");
00105     wait_ms(1000);
00106 
00107 
00108     // Send the alphabet
00109     while (1) {
00110         for (int loop = 0; loop < 512; loop++) {
00111 
00112             blue.printf("%c", c);
00113             c++;
00114             if (c > 'Z') c = 'A';
00115             if (loop > 510) loop  = 0;
00116 
00117             wait_ms(100);
00118         }
00119     }
00120 }