Tianyou Tong
/
HM-10_NucleoF401RE_LED_Demo
LED toggle demo of HM-10 and NucleoF401RE Software serial communication
main.cpp@1:ee3b26d82277, 2018-07-18 (annotated)
- Committer:
- TianyouTong
- Date:
- Wed Jul 18 09:32:04 2018 +0000
- Revision:
- 1:ee3b26d82277
- Parent:
- 0:f0e23c4a605f
comment style change
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TianyouTong | 0:f0e23c4a605f | 1 | #include "mbed.h" |
TianyouTong | 0:f0e23c4a605f | 2 | #include "SoftSerial.h" |
TianyouTong | 0:f0e23c4a605f | 3 | |
TianyouTong | 1:ee3b26d82277 | 4 | /*Set up serial communication between the HM-10 module and the Nucleo board |
TianyouTong | 1:ee3b26d82277 | 5 | For debugging and setting up the module, set up serial between Nucleo and PC |
TianyouTong | 1:ee3b26d82277 | 6 | Set up LED for debugging*/ |
TianyouTong | 0:f0e23c4a605f | 7 | SoftSerial hm10(D3,D2); |
TianyouTong | 0:f0e23c4a605f | 8 | Serial pc(USBTX, USBRX); |
TianyouTong | 0:f0e23c4a605f | 9 | DigitalOut LED(PA_5); |
TianyouTong | 0:f0e23c4a605f | 10 | |
TianyouTong | 0:f0e23c4a605f | 11 | char s; |
TianyouTong | 0:f0e23c4a605f | 12 | char w; |
TianyouTong | 0:f0e23c4a605f | 13 | |
TianyouTong | 0:f0e23c4a605f | 14 | void serial_config(); |
TianyouTong | 0:f0e23c4a605f | 15 | |
TianyouTong | 0:f0e23c4a605f | 16 | int main(){ |
TianyouTong | 0:f0e23c4a605f | 17 | pc.baud(9600); |
TianyouTong | 0:f0e23c4a605f | 18 | hm10.baud(9600);//Set up baud rate for serial communication |
TianyouTong | 0:f0e23c4a605f | 19 | |
TianyouTong | 0:f0e23c4a605f | 20 | while (!hm10.writeable()) { } //wait until the HM10 is ready |
TianyouTong | 0:f0e23c4a605f | 21 | |
TianyouTong | 0:f0e23c4a605f | 22 | while(1) { |
TianyouTong | 0:f0e23c4a605f | 23 | if (hm10.readable()) |
TianyouTong | 0:f0e23c4a605f | 24 | { |
TianyouTong | 0:f0e23c4a605f | 25 | s = hm10.getc(); |
TianyouTong | 0:f0e23c4a605f | 26 | pc.putc(s); |
TianyouTong | 0:f0e23c4a605f | 27 | if(s == '1'){ |
TianyouTong | 0:f0e23c4a605f | 28 | LED = 1; |
TianyouTong | 0:f0e23c4a605f | 29 | } |
TianyouTong | 0:f0e23c4a605f | 30 | if(s == '0'){ |
TianyouTong | 0:f0e23c4a605f | 31 | LED = 0; |
TianyouTong | 0:f0e23c4a605f | 32 | } |
TianyouTong | 0:f0e23c4a605f | 33 | } |
TianyouTong | 0:f0e23c4a605f | 34 | serial_config(); |
TianyouTong | 0:f0e23c4a605f | 35 | } |
TianyouTong | 0:f0e23c4a605f | 36 | } |
TianyouTong | 0:f0e23c4a605f | 37 | |
TianyouTong | 0:f0e23c4a605f | 38 | /*serial_config allows you to set up your HM-10 module via USB serial port*/ |
TianyouTong | 0:f0e23c4a605f | 39 | void serial_config(){ |
TianyouTong | 0:f0e23c4a605f | 40 | if (pc.readable()){ |
TianyouTong | 0:f0e23c4a605f | 41 | w = pc.getc(); |
TianyouTong | 0:f0e23c4a605f | 42 | hm10.putc(w); |
TianyouTong | 0:f0e23c4a605f | 43 | } |
TianyouTong | 0:f0e23c4a605f | 44 | } |
TianyouTong | 0:f0e23c4a605f | 45 | |
TianyouTong | 0:f0e23c4a605f | 46 |