E45_TTL_100, Lora transceiver, tested on KL25Z connection with E45 via RS232. Power 100 mW, 868 MHz, range 2 km

Dependencies:   MODSERIAL mbed

Committer:
GerritPathuis
Date:
Sun Apr 01 11:48:39 2018 +0000
Revision:
5:3b737b1cedf5
Parent:
4:1249c317e7f3
MODSERIAL dependency removed, printf removed from Timer Service Routine

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GerritPathuis 0:16ba41275340 1 #include "mbed.h"
GerritPathuis 4:1249c317e7f3 2 #include <string>
GerritPathuis 4:1249c317e7f3 3 #include <ctype.h>
GerritPathuis 4:1249c317e7f3 4
GerritPathuis 0:16ba41275340 5 // E45-TTL-100 RADIO------KL25Z
GerritPathuis 0:16ba41275340 6 // AUX--------------------PTD4
GerritPathuis 0:16ba41275340 7 // M1---------------------PTA12
GerritPathuis 0:16ba41275340 8 // M0---------------------PTA4
GerritPathuis 0:16ba41275340 9 // RXD--------------------PTE22(TX)
GerritPathuis 0:16ba41275340 10 // TXD--------------------PTE23(RX)
GerritPathuis 1:2b8fa0144c4f 11 // LORA Radio Transmits @ 868 mHz
GerritPathuis 2:329d3f88af27 12 // Expected range 2000m
GerritPathuis 2:329d3f88af27 13 // https://quadmeup.com/wp-content/uploads/2017/09/E45-TTL-100_Datasheet_EN_v1.2.pdf
GerritPathuis 0:16ba41275340 14
GerritPathuis 1:2b8fa0144c4f 15 Ticker timer;
GerritPathuis 5:3b737b1cedf5 16 Serial pc(USBTX, USBRX); // tx, rx of the pc
GerritPathuis 5:3b737b1cedf5 17 Serial e45(PTE22, PTE23); // tx, rx of the E45 radio
GerritPathuis 5:3b737b1cedf5 18
GerritPathuis 0:16ba41275340 19 InterruptIn aux(PTD4); // AUX the E45 radio
GerritPathuis 0:16ba41275340 20 DigitalOut m1(PTA12,PullUp); // M1 the E45 radio
GerritPathuis 0:16ba41275340 21 DigitalOut m0(PTA4, PullUp); // M0 the E45 radio
GerritPathuis 5:3b737b1cedf5 22 DigitalOut myled(LED_RED); // KL25Z RED led
GerritPathuis 0:16ba41275340 23
GerritPathuis 5:3b737b1cedf5 24 int ping=1; // this number bounces between radios
GerritPathuis 5:3b737b1cedf5 25 bool send_something_flag= 0; // Send flag
GerritPathuis 0:16ba41275340 26
GerritPathuis 0:16ba41275340 27 void flank_up()
GerritPathuis 0:16ba41275340 28 {
GerritPathuis 5:3b737b1cedf5 29 myled=1; //Rising Edge Detected
GerritPathuis 0:16ba41275340 30 }
GerritPathuis 0:16ba41275340 31
GerritPathuis 0:16ba41275340 32 void flank_down()
GerritPathuis 0:16ba41275340 33 {
GerritPathuis 5:3b737b1cedf5 34 myled=0; //Falling Edge Detected
GerritPathuis 0:16ba41275340 35 }
GerritPathuis 0:16ba41275340 36
GerritPathuis 5:3b737b1cedf5 37 void timer_func() // Send something every 3 seconds
GerritPathuis 1:2b8fa0144c4f 38 {
GerritPathuis 5:3b737b1cedf5 39 send_something_flag= 1; // Set the send Flag
GerritPathuis 1:2b8fa0144c4f 40 }
GerritPathuis 1:2b8fa0144c4f 41
GerritPathuis 0:16ba41275340 42 int main()
GerritPathuis 0:16ba41275340 43 {
GerritPathuis 3:19a244789b2b 44 int count=0;
GerritPathuis 4:1249c317e7f3 45 string incoming;
GerritPathuis 4:1249c317e7f3 46 char c;
GerritPathuis 3:19a244789b2b 47
GerritPathuis 1:2b8fa0144c4f 48 pc.baud(115200);
GerritPathuis 3:19a244789b2b 49 e45.baud(9600); // Default for E45
GerritPathuis 3:19a244789b2b 50 e45.format(8, SerialBase::None, 1); // Default for E45
GerritPathuis 5:3b737b1cedf5 51 pc.printf("\n\r\nE45-TTL-100 LORA\n\r");
GerritPathuis 0:16ba41275340 52
GerritPathuis 0:16ba41275340 53 aux.rise(&flank_up); //Service RISING EDGE Interrupt
GerritPathuis 0:16ba41275340 54 aux.fall(&flank_down); //Service FALLING EDGE Interrupt
GerritPathuis 0:16ba41275340 55
GerritPathuis 2:329d3f88af27 56 // select mode
GerritPathuis 2:329d3f88af27 57 // Transparant Transmission
GerritPathuis 3:19a244789b2b 58 if (myled ==1) { //Check ready or not
GerritPathuis 3:19a244789b2b 59 while(myled==0) { //Wait when not yet ready
GerritPathuis 3:19a244789b2b 60 count++;
GerritPathuis 4:1249c317e7f3 61 if (count > 2000000) { //Kill time
GerritPathuis 3:19a244789b2b 62 count=0;
GerritPathuis 5:3b737b1cedf5 63 pc.printf("Wait for AUX Rising edge ");
GerritPathuis 3:19a244789b2b 64 }
GerritPathuis 3:19a244789b2b 65 }
GerritPathuis 3:19a244789b2b 66 }
GerritPathuis 3:19a244789b2b 67 wait_ms(2);
GerritPathuis 3:19a244789b2b 68 m0= 0; //Set transparant mode
GerritPathuis 3:19a244789b2b 69 m1= 0; //Set transparant mode
GerritPathuis 2:329d3f88af27 70 wait_ms(1);
GerritPathuis 0:16ba41275340 71
GerritPathuis 5:3b737b1cedf5 72 pc.printf("E45 is now ready to send and receive \n\r");
GerritPathuis 1:2b8fa0144c4f 73 timer.attach(&timer_func, 3.0);
GerritPathuis 0:16ba41275340 74
GerritPathuis 0:16ba41275340 75 while(1) {
GerritPathuis 4:1249c317e7f3 76 if (ping>9999) ping=1;
GerritPathuis 4:1249c317e7f3 77
GerritPathuis 5:3b737b1cedf5 78 if (send_something_flag==1) {
GerritPathuis 5:3b737b1cedf5 79 if (myled == 1) { // AUX is high (Buffer empty), send something
GerritPathuis 5:3b737b1cedf5 80 wait_ms(3);
GerritPathuis 5:3b737b1cedf5 81 ping++; // Increase the value
GerritPathuis 5:3b737b1cedf5 82 e45.printf("%04d\n\r", ping);
GerritPathuis 5:3b737b1cedf5 83 while(myled==0) {
GerritPathuis 5:3b737b1cedf5 84 }
GerritPathuis 5:3b737b1cedf5 85 send_something_flag=0;
GerritPathuis 5:3b737b1cedf5 86 pc.printf("\n\rRadio has sent %04d\n\r", ping);
GerritPathuis 5:3b737b1cedf5 87 }
GerritPathuis 5:3b737b1cedf5 88 }
GerritPathuis 5:3b737b1cedf5 89
GerritPathuis 4:1249c317e7f3 90 if (myled == 0) { // AUX is low, Chars received
GerritPathuis 5:3b737b1cedf5 91 incoming= ""; // Clear the string
GerritPathuis 1:2b8fa0144c4f 92 while(myled==0) {
GerritPathuis 1:2b8fa0144c4f 93 if (e45.readable()) {
GerritPathuis 4:1249c317e7f3 94 c=e45.getc();
GerritPathuis 4:1249c317e7f3 95 if (isdigit(c))
GerritPathuis 4:1249c317e7f3 96 incoming += c;
GerritPathuis 4:1249c317e7f3 97 } else { // End transmission
GerritPathuis 4:1249c317e7f3 98 if (incoming.length()==4) {
GerritPathuis 4:1249c317e7f3 99 ping= atoi(incoming.c_str());
GerritPathuis 4:1249c317e7f3 100 pc.printf("incoming value %4d \n\r", ping);
GerritPathuis 4:1249c317e7f3 101 incoming= ""; // Clear the string
GerritPathuis 4:1249c317e7f3 102 }
GerritPathuis 1:2b8fa0144c4f 103 }
GerritPathuis 1:2b8fa0144c4f 104 }
GerritPathuis 0:16ba41275340 105 }
GerritPathuis 0:16ba41275340 106 }
GerritPathuis 0:16ba41275340 107 }