Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 7:47e8eccc7a01
- Parent:
- 6:9a905505f955
- Child:
- 8:9c0161fd67e2
--- a/main.cpp Wed Feb 12 10:33:39 2020 +0000
+++ b/main.cpp Thu Feb 13 12:06:03 2020 +0000
@@ -17,8 +17,11 @@
#define OPT_LEFT "000010001111011100001000111101111"
#define OPT_RIGHT "000010001111011100000001111111101"
-DigitalIn g_dpinIrReceiver(D2);
-Serial g_serial(USBTX, USBRX);
+#define DARK 1
+#define BRIGHT 0
+
+DigitalIn ir_in(D2);
+Serial pc(USBTX, USBRX);
DigitalOut myled(LED1);
DigitalOut ir_out(D3);
@@ -26,6 +29,8 @@
int IR_transmitte(char tx_data[])
{
+ timer.start();
+
//flame
int t_start = timer.read_us();
ir_out = 1;
@@ -51,68 +56,74 @@
int IR_receive(char rx_data[])
{
+ int t_negedge,t_posedge,t_delta,iState;
int iState_prev = 1;
- int data_en,t_negedge,t_posedge,t_delta;
int busy = 0;
+ int data_en = 0;
+
+ timer.start();
+ memset(rx_data, '\0', sizeof(rx_data));
while(busy == 0) {
- int iState = g_dpinIrReceiver;
-
- if(iState_prev == 1 && iState == 0) {//detect negedge
+ iState = ir_in;
+ if(iState_prev == DARK && iState == BRIGHT) {//detect negedge
t_negedge = timer.read_us();//start(detect negedge
+ t_delta = t_negedge - t_posedge;
if(data_en == 1) {
- t_delta = t_negedge - t_posedge;
data_en = 0;
- if(t_delta > 1750) strcat(rx_data, "1");//625*2.8
+ if(t_delta > 1523) strcat(rx_data, "1");//625*2.5
else strcat(rx_data, "0");
- if(strlen(rx_data) == 33) busy = 1;
+ if(strlen(rx_data) == 33) {
+ busy = 1;
+ pc.printf("\r\n");
+ }
}
- } else if(iState_prev == 0 && iState == 1) {//detect posedge
+ } else if(iState_prev == BRIGHT && iState == DARK) {//detect posedge
t_posedge = timer.read_us();
- t_delta = t_posedge - t_negedge;//compare and output
- if(t_delta > 9875) rx_data[0] = '0';//625*15.8, flame
- else data_en = 1;
+ t_delta = t_posedge - t_negedge;
+ if(t_delta > 500 && t_delta < 750) {//16T
+ data_en = 1; //This is flame
+ }
}
iState_prev = iState;
}
return busy;
}
+
int main()
{
- char rx_data[64] = "";
- char tx_data[64] = "";
+ char rx_data[64],tx_data[64];
int busy = 0;
- int tx_delay;
+ int tx_delay,tx_en;
- g_serial.baud(115200);
- timer.start();
+ pc.baud(115200);
while(1) {
if(busy == 1) {
+ pc.printf("%s\r\n",rx_data);
+
+ if (strcmp(rx_data,OPT_PWR) == 0) {
+ tx_delay = 5;
+ tx_en = 1;
+ strcpy(tx_data, OPT_PWR);//enter Arbitrary Values
+ } else {
+ tx_delay = 0;
+ tx_en = 0;
+ }
myled = 1;
for(int i=0; i<tx_delay; i++) {
wait_ms(1000);
}
//tx IR
- if(tx_data[0] != '\0') IR_transmitte(tx_data);
+ if(tx_en == 1) IR_transmitte(tx_data);
+ tx_en = 0;
myled = 0;
busy = 0;
} else {
busy = IR_receive(rx_data);
- if(busy == 1) {
- if (strcmp(rx_data,OPT_PWR) == 0) {
- g_serial.printf("received=PWR\r\n");
- tx_delay = 5;
- tx_data[0] = '\0';
- strcmp(tx_data,OPT_PWR);
- } else {
- tx_delay = 0;
- tx_data[0] = '\0';
- }
- }
}
}
}
\ No newline at end of file