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

Dependencies:   mbed-src MODSERIAL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Author: Edoardo De Marchi
00003  * Date: 12-08-14
00004  * Notes: HC05 AT command
00005 */
00006 
00007 #include "mbed.h"
00008 #include "MODSERIAL.h"
00009 
00010 
00011 MODSERIAL pc(USBTX, USBRX); 
00012 DigitalOut led1(LED1);
00013 
00014     
00015 #if   defined(TARGET_LPC1768)
00016 MODSERIAL blue(p9, p10);          // TX = P9  RX = P10
00017 //MODSERIAL blue(p13, p14);         // TX = P13  RX = P14
00018 #elif defined(TARGET_LPC4330_M4)
00019 MODSERIAL blue(UART0_TX, UART0_RX);        // P6_4, P6_5
00020 //MODSERIAL blue(UART3_TX, UART3_RX);        // P2_3, P2_4
00021 #endif
00022 
00023 
00024 
00025 
00026 bool new_send = false;
00027 bool new_response = false;
00028 char ATCmd[80];
00029 char blueChar[80];
00030 
00031 
00032 void commandAT(char *v)         // Send the AT command
00033 {        
00034 
00035   int i=0;
00036   
00037   while(v[i] != NULL)
00038   { 
00039     blue.putc(v[i]);
00040     i++;
00041   } 
00042   blue.printf("\r\n");
00043 }
00044 
00045 
00046 // This function is called when a character goes into the RX buffer.
00047 void rxBlueCallback(MODSERIAL_IRQ_INFO *q) 
00048 {
00049     new_response = true;
00050 }
00051 
00052 // This function is called when a character goes into the RX buffer.
00053 void rxPcCallback(MODSERIAL_IRQ_INFO *q) 
00054 {
00055     new_send = true;
00056 }
00057 
00058 
00059 int main()
00060 {
00061    blue.baud(38400);
00062    pc.baud(115200);
00063    
00064    blue.attach(&rxBlueCallback);
00065    pc.attach(&rxPcCallback);
00066     
00067    pc.printf("AT Mode Start\r\n"); 
00068    int i = 0;
00069    
00070    while(1)
00071    {
00072         if(new_send)
00073         {
00074             int i = 0;
00075         
00076             while(pc.readable())
00077             {
00078                 ATCmd[i] = pc.getc();
00079                 i++;
00080             }
00081             commandAT(ATCmd);
00082             memset(ATCmd, 0, sizeof(ATCmd));
00083             new_send = false;
00084         }else
00085         if(new_response)
00086         {
00087             int i = 0;
00088             while(blue.readable())
00089             {
00090                 blueChar[i] = blue.getc();
00091                 i++;
00092             }
00093             printf("Response: %s", blueChar); 
00094             memset(blueChar, 0, sizeof(blueChar)); 
00095             new_response = false;   
00096         }    
00097         wait_ms(100);
00098         i++;
00099         if(i == 5)
00100         {
00101             led1 = !led1;
00102             i=0;    
00103         }
00104     } 
00105 }