This program send an AT command to the HC05 module and shows the response into a terminal.

Dependencies:   MODSERIAL

Fork of HC05_AT_mode by Edoardo De Marchi

Committer:
edodm85
Date:
Tue Aug 12 16:16:02 2014 +0000
Revision:
5:73ac83c240ec
Parent:
3:2f5485de3328
Child:
7:804c6a1f49ce
Added support for LPC4330

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edodm85 0:7bb229e58814 1 /*
edodm85 0:7bb229e58814 2 * Author: Edoardo De Marchi
edodm85 5:73ac83c240ec 3 * Date: 12-08-14
edodm85 2:08505f23f37a 4 * Notes: HC05 AT command
edodm85 0:7bb229e58814 5 */
edodm85 0:7bb229e58814 6
edodm85 0:7bb229e58814 7 #include "mbed.h"
edodm85 0:7bb229e58814 8 #include "MODSERIAL.h"
edodm85 0:7bb229e58814 9
edodm85 0:7bb229e58814 10
edodm85 2:08505f23f37a 11 MODSERIAL pc(USBTX, USBRX);
edodm85 0:7bb229e58814 12 DigitalOut led1(LED1);
edodm85 5:73ac83c240ec 13
edodm85 5:73ac83c240ec 14
edodm85 5:73ac83c240ec 15 #if defined(TARGET_LPC1768)
edodm85 5:73ac83c240ec 16 MODSERIAL blue(p9, p10); // TX = P9 RX = P10
edodm85 5:73ac83c240ec 17 //MODSERIAL blue(p13, p14); // TX = P13 RX = P14
edodm85 5:73ac83c240ec 18 #elif defined(TARGET_LPC4330_M4)
edodm85 5:73ac83c240ec 19 MODSERIAL blue(UART0_TX, UART0_RX); // P6_4, P6_5
edodm85 5:73ac83c240ec 20 //MODSERIAL blue(UART3_TX, UART3_RX); // P2_3, P2_4
edodm85 5:73ac83c240ec 21 #endif
edodm85 5:73ac83c240ec 22
edodm85 5:73ac83c240ec 23
edodm85 0:7bb229e58814 24
edodm85 0:7bb229e58814 25
edodm85 2:08505f23f37a 26 bool new_send = false;
edodm85 2:08505f23f37a 27 bool new_response = false;
edodm85 2:08505f23f37a 28 char ATCmd[80];
edodm85 2:08505f23f37a 29 char blueChar[80];
edodm85 2:08505f23f37a 30
edodm85 2:08505f23f37a 31
edodm85 2:08505f23f37a 32 void commandAT(char *v) // Send the AT command
edodm85 2:08505f23f37a 33 {
edodm85 0:7bb229e58814 34
edodm85 0:7bb229e58814 35 int i=0;
edodm85 0:7bb229e58814 36
edodm85 2:08505f23f37a 37 while(v[i] != NULL)
edodm85 2:08505f23f37a 38 {
edodm85 0:7bb229e58814 39 blue.putc(v[i]);
edodm85 0:7bb229e58814 40 i++;
edodm85 2:08505f23f37a 41 }
edodm85 0:7bb229e58814 42 blue.printf("\r\n");
edodm85 0:7bb229e58814 43 }
edodm85 0:7bb229e58814 44
edodm85 0:7bb229e58814 45
edodm85 0:7bb229e58814 46 // This function is called when a character goes into the RX buffer.
edodm85 2:08505f23f37a 47 void rxBlueCallback(MODSERIAL_IRQ_INFO *q)
edodm85 2:08505f23f37a 48 {
edodm85 2:08505f23f37a 49 new_response = true;
edodm85 2:08505f23f37a 50 }
edodm85 2:08505f23f37a 51
edodm85 2:08505f23f37a 52 // This function is called when a character goes into the RX buffer.
edodm85 2:08505f23f37a 53 void rxPcCallback(MODSERIAL_IRQ_INFO *q)
edodm85 2:08505f23f37a 54 {
edodm85 2:08505f23f37a 55 new_send = true;
edodm85 0:7bb229e58814 56 }
edodm85 0:7bb229e58814 57
edodm85 0:7bb229e58814 58
edodm85 2:08505f23f37a 59 int main()
edodm85 2:08505f23f37a 60 {
edodm85 0:7bb229e58814 61 blue.baud(38400);
edodm85 2:08505f23f37a 62 pc.baud(9600);
edodm85 2:08505f23f37a 63
edodm85 2:08505f23f37a 64 blue.attach(&rxBlueCallback, MODSERIAL::RxIrq);
edodm85 2:08505f23f37a 65 pc.attach(&rxPcCallback, MODSERIAL::RxIrq);
edodm85 2:08505f23f37a 66
edodm85 2:08505f23f37a 67 pc.printf("AT Mode Start\r\n");
edodm85 5:73ac83c240ec 68 int i = 0;
edodm85 0:7bb229e58814 69
edodm85 2:08505f23f37a 70 while(1)
edodm85 2:08505f23f37a 71 {
edodm85 2:08505f23f37a 72 if(new_send)
edodm85 2:08505f23f37a 73 {
edodm85 2:08505f23f37a 74 int i = 0;
edodm85 0:7bb229e58814 75
edodm85 2:08505f23f37a 76 while(pc.readable())
edodm85 2:08505f23f37a 77 {
edodm85 2:08505f23f37a 78 ATCmd[i] = pc.getc();
edodm85 2:08505f23f37a 79 i++;
edodm85 2:08505f23f37a 80 }
edodm85 2:08505f23f37a 81 commandAT(ATCmd);
edodm85 3:2f5485de3328 82 memset(ATCmd, 0, sizeof(ATCmd));
edodm85 2:08505f23f37a 83 new_send = false;
edodm85 2:08505f23f37a 84 }else
edodm85 2:08505f23f37a 85 if(new_response)
edodm85 2:08505f23f37a 86 {
edodm85 2:08505f23f37a 87 int i = 0;
edodm85 2:08505f23f37a 88 while(blue.readable())
edodm85 2:08505f23f37a 89 {
edodm85 2:08505f23f37a 90 blueChar[i] = blue.getc();
edodm85 2:08505f23f37a 91 i++;
edodm85 2:08505f23f37a 92 }
edodm85 3:2f5485de3328 93 printf("Response: %s", blueChar);
edodm85 3:2f5485de3328 94 memset(blueChar, 0, sizeof(blueChar));
edodm85 2:08505f23f37a 95 new_response = false;
edodm85 2:08505f23f37a 96 }
edodm85 2:08505f23f37a 97 wait_ms(100);
edodm85 5:73ac83c240ec 98 i++;
edodm85 5:73ac83c240ec 99 if(i == 5)
edodm85 5:73ac83c240ec 100 {
edodm85 5:73ac83c240ec 101 led1 = !led1;
edodm85 5:73ac83c240ec 102 i=0;
edodm85 5:73ac83c240ec 103 }
edodm85 0:7bb229e58814 104 }
edodm85 0:7bb229e58814 105 }