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

Dependencies:   mbed-src MODSERIAL

Notebook page HERE

Revision:
2:08505f23f37a
Parent:
1:a11db105c379
Child:
3:2f5485de3328
--- a/main.cpp	Thu May 31 19:56:30 2012 +0000
+++ b/main.cpp	Fri Oct 18 21:31:21 2013 +0000
@@ -1,58 +1,94 @@
 /*
  * Author: Edoardo De Marchi
- * Date: 31-05-12
- * Notes: AT command
+ * Date: 02-07-13
+ * Notes: HC05 AT command
 */
 
 #include "mbed.h"
 #include "MODSERIAL.h"
 
 
-MODSERIAL pc(USBTX, USBRX);      
+#define SERIAL_1
+    
+#ifdef SERIAL_1
 MODSERIAL blue(p9,p10);          // HC05
+#endif
+#ifdef SERIAL_2
+MODSERIAL blue(p13,p14);         // HC05
+#endif
+MODSERIAL pc(USBTX, USBRX); 
 DigitalOut led1(LED1);
 DigitalOut led4(LED4);
 
 
-void commandAT(char *v){        // Send the AT command
+bool new_send = false;
+bool new_response = false;
+char ATCmd[80];
+char blueChar[80];
+
+
+void commandAT(char *v)         // Send the AT command
+{        
 
   int i=0;
   
-  while(v[i] != NULL){ 
+  while(v[i] != NULL)
+  { 
     blue.putc(v[i]);
     i++;
-  }
-  
+  } 
   blue.printf("\r\n");
 }
 
 
 // This function is called when a character goes into the RX buffer.
-void rxCallback(MODSERIAL_IRQ_INFO *q) {
-    led4 = !led4;
-    pc.putc(blue.getc());
+void rxBlueCallback(MODSERIAL_IRQ_INFO *q) 
+{
+    new_response = true;
+}
+
+// This function is called when a character goes into the RX buffer.
+void rxPcCallback(MODSERIAL_IRQ_INFO *q) 
+{
+    new_send = true;
 }
 
 
-int main(){
-  
+int main()
+{
    blue.baud(38400);
-   pc.baud(115200);
-
-   char s[80];
+   pc.baud(9600);
+   
+   blue.attach(&rxBlueCallback, MODSERIAL::RxIrq);
+   pc.attach(&rxPcCallback, MODSERIAL::RxIrq);
+    
+   pc.printf("AT Mode Start\r\n"); 
    
-   while(1){
-        pc.printf("Command: ");
-        pc.scanf("%s",s);
-      
-        wait_us(100);
-      
-        commandAT(s);
-        wait_us(100);
+   while(1)
+   {
+        if(new_send)
+        {
+            int i = 0;
         
-        pc.printf("Response: ");
-      
-        blue.attach(&rxCallback, MODSERIAL::RxIrq);
-        wait(1);
+            while(pc.readable())
+            {
+                ATCmd[i] = pc.getc();
+                i++;
+            }
+            commandAT(ATCmd);
+            new_send = false;
+        }else
+        if(new_response)
+        {
+            int i = 0;
+            while(blue.readable())
+            {
+                blueChar[i] = blue.getc();
+                i++;
+            }
+            printf("Response: %s", blueChar);  
+            new_response = false;   
+        }    
+        wait_ms(100);
     } 
 }
\ No newline at end of file