123

Dependencies:   TM1638 mbed HCSR04

Revision:
1:63541cb7c9d0
Parent:
0:6f4090a07409
--- a/main.cpp	Sat Mar 02 12:48:15 2019 +0000
+++ b/main.cpp	Mon Apr 08 15:06:22 2019 +0000
@@ -1,178 +1,93 @@
 #include "mbed.h"
-#include "IR_Def.h"
 #include "hcsr04.h"
-
-#define CAT0_PIN PC_1     // 11 PC_3        переставили с катодом 7
-#define CAT1_PIN PC_3     // 7  PC_2        переставили с катодом 6
-#define CAT2_PIN PB_0     // 4 PF_5         переставили с катодом 5
-#define CAT3_PIN PC_2     // 2   PA_0       переставили с катодом 4
-#define CAT4_PIN PA_4     // 1 PF_10
-#define CAT5_PIN PA_1    // 10 PF_2    
-#define CAT6_PIN PC_0    // 5  PD_5
-#define CAT7_PIN PF_1    // 3 PH_0
+#include "TM1638.h"  
 
-#define ANO0_PIN PB_4     // 12 PC_0
-#define ANO1_PIN PB_5     // 9 PF_3
-#define ANO2_PIN PB_3     // 8 PB_0
-#define ANO3_PIN PB_10     // 6 PA_3
-
-//------------------------------------
-// Hyperterminal configuration
-// 9600 bauds, 8-bit data, no parity
-//------------------------------------
 Serial pc(SERIAL_TX, SERIAL_RX);  
-InterruptIn IR(PC_12);
-DigitalOut myled(LED1);
-Timer T;  
-Ticker  system_clock;  
-Ticker pult;                                             //This will be used to increment a counter every msec inside it's ISR
-Ticker display;                   
+DigitalOut led1(PC_10);           
 Ticker distancee;
-Ticker muzika;
 AnalogOut Da(PA_5);
+/*********************************************************************************************************************/
+//Display
+TM1638::KeyData_t keydata; 
+TM1638_LEDKEY8 LEDKEY8(PB_5,PB_4,PB_3,PA_10);
+/*********************************************************************************************************************/
+//PULT
+/*InterruptIn D(PA_14); //D //D1
+InterruptIn A(PA_13); //A //D2
+InterruptIn B(PA_15); //B //D0
+InterruptIn C(PC_12); //C //D3*/
+/********************************************************************************************************************/
 //DigitalOut myled(LED2);
-DigitalOut cathodes[8] = {
-  DigitalOut(CAT0_PIN), 
-  DigitalOut(CAT1_PIN), 
-  DigitalOut(CAT2_PIN), 
-  DigitalOut(CAT3_PIN), 
-  DigitalOut(CAT4_PIN), 
-  DigitalOut(CAT5_PIN), 
-  DigitalOut(CAT6_PIN), 
-  DigitalOut(CAT7_PIN), 
-};
-DigitalOut anodes[4] = {
-  DigitalOut(ANO0_PIN), 
-  DigitalOut(ANO1_PIN), 
-  DigitalOut(ANO2_PIN), 
-  DigitalOut(ANO3_PIN), 
-};
 
-int p = 0;
-int o=0;
-void IR_ISR();                                                      //ISR for decoding the IR signal
-void dummy_ISR();                                                   //Required so that 'IR_ISR' is not called on both rising and falling edges of IR signal at the same time
-void sys_tick(); 
-int v=0;
-uint8_t state = 0;                       //State variable for IR state machine
-uint8_t Rx_count = 0;                                //Counter to count no. of bits received
-uint32_t Rx_buf = 0;                 //To store recevied IR data
-char Rx_flag = 0;                    //Flag which indicates that a valid IR command was received
-uint32_t Rx_rep_count = 0;           //To keep track of how many repeat pulses are received
-uint32_t Rx_rep_timeout_count = 0;   //Timer which clears the previously received IR command, once it reaches the set limit
-uint32_t T_buf = 0;                  //Buffer to hold value of timer T
-char Rx_ignore_rep_flag = 0;         //0 => Do not ignore repeat pulses 
-
-uint8_t digits[] = {0x03, 0x9F, 0x25, 0x0D, 0x99, 0x49,  0x41, 0x1F, 0x01, 0x09, 0xFD, 0x91,0x89, 0x83};
 //DigitalOut trig(PA_3);
 //DigitalIn echo(PD_7);
 HCSR04  usensor(PB_13,PB_14);
 unsigned int dist=0;
 int flag1, flag2, flag3 = 0;
 int i,j,k,m,size=0;
-
-void fireUpSign(int signNum){
-  anodes[signNum] = 1;
-}
+int a,b,c,d,e,f,g,h = 0;
 
-void shutDownSign(int signNum){
-  anodes[signNum] = 0;
-}
-void setDigit(int digit){
-  for(int isd=0; isd < 8; isd++){
-     ((digits[digit] & (1<<(7-isd))) > 0) ? cathodes[isd] =1 : cathodes[isd] =0;
-  }  
+/*void BB() {
+    led1 = !led1;
 }
-void fireUpAllSigns(){
-  fireUpSign(0);
-  fireUpSign(1);
-  fireUpSign(2);
-  fireUpSign(3);
-}
-
-void shutDownAllSigns(){
-  shutDownSign(0);
-  shutDownSign(1);
-  shutDownSign(2);
-  shutDownSign(3);
+void DD() {
+    led1= !led1;
 }
-void ddisplay ()
-{
-            setDigit(p); 
-            fireUpSign(3); 
-            wait_us(3000);
-            shutDownAllSigns();
-            setDigit(i); 
-            fireUpSign(2); 
-            wait_us(3000);
-            shutDownAllSigns();
-            setDigit(j);
-            fireUpSign(1);
-            wait_us(3000); 
-            shutDownAllSigns();
-            setDigit(k);
-            fireUpSign(0);
-            wait_us(3000); 
-            shutDownAllSigns();
-            //IR.fall(&IR_ISR);
-            //IR.rise(&dummy_ISR); 
-            if(i>9)
-                {
-                j++;
-                i=0;
-                }
-            if(j>9)
-            {
-                k++;
-                j=0;
-                i=0;
-                }
-                /*if(i == 1)
-                {
-                    myled = !myled;
-                    wait_ms(300);
-                    i = 0;
-                }*/
-    }
+void AA() {
+    led1 = !led1;
+}
+void CC() {
+    led1 = !led1;
+}*/
+
 void ddistance ()
 {
     usensor.start();
     dist=usensor.get_dist_cm();
-    //IR.fall(&IR_ISR);
-    //IR.rise(&dummy_ISR); 
     }  
-    
-void sys_tick()
-{
-    if(Rx_rep_timeout_count < 0xFFFF) { //Do not increment counter beyond 0xFFFF to prevent roll-over
-        ++Rx_rep_timeout_count;       //Increment this counter every 1msec.
-    }
-}
 
-void ppult ()
+void sbros ()
+{
+    a=0;
+    b=0;
+    c=0;
+    d=0;
+    e=0;
+    f=0;
+    g=0;
+    h=0;
+    }
+void perebros ()
 {
-                  //  if(o==0)
-     // {
-    //IR.mode(PullNone);
-    //wait_ms(20);
-  // IR.fall(&IR_ISR);
-  // IR.rise(&dummy_ISR); 
-//    }           
-} 
+    if(h>9)
+            {
+                g++;
+                h=0;
+                if(g>9)
+                {
+                    f++;
+                    g=0;
+                    h=0;
+                    if(f>9)
+                    {
+                        sbros();
+                        }
+                    }
+                
+                }
+    }
     
 int main()
 {
+   /* B.rise(&BB);
+    D.rise(&DD);
+    A.rise(&AA);
+    C.rise(&CC);*/
     int verh;
     int nij;
     while(1) {  
-           //if (o==0)
-      // {
-      // display.attach(&ddisplay, 0.02);   
-     //  distancee.attach(&ddistance, 0.02);
-      // pult.attach(&ppult, 2);
-      ddisplay ();
-      ddistance();
-    //   }
+    ddistance();
+    LEDKEY8.printf ("%d%d%d%d%d%d%d%d", a, b, c, d, e, f, g, h);
         verh = 80;
         nij = 10; 
         if(dist >verh)
@@ -192,202 +107,62 @@
             flag1=0;
             flag2=0;
             flag3=0;
-            i++;
+            h++;
+            perebros();
             }
-     if (v==1)
-                { 
-               // pc.printf(" RX= %x " , Rx_buf);
-
- 
-        if(Rx_buf == 0xbf40ff00)
-    {
-        //pc.printf("5");
-        p=0;
-        j=0;
-        i=0;
-        k=0;
-        
-      }
-    
-        if(Rx_buf == 0xf609ff00)
-    {
-       i++;
-            if(i>9)
-                {
-                j++;
-                i=0;
-                }
-            if(j>9)
+        if (LEDKEY8.getKeys(&keydata)) {
+     // pc.printf("Keydata 0..3 = 0x%02x 0x%02x 0x%02x 0x%02x\r\n", keydata[0], keydata[1], keydata[2], keydata[3]);
+       if (keydata[0] == 0x01) { //sw1  
+       int k;
+       k=h; 
+        h++;
+        if(h-k==1)
+        {
+           wait(0.3);
+           perebros();
+            }
+            }  
+        if (keydata[1] == 0x01) { //sw1  
+            sbros();
+            }  
+        if (keydata[2] == 0x01) { //sw1 
+            if (e<9)
             {
-                k++;
-                j=0;
-                i=0;
-                }
-       {
-           }
-       // pc.printf("9");
-      }
-    
-        if(Rx_buf == 0xf807ff00)
-    {
-       //pc.printf("7");
-        if (p>9)
+            e++; 
+            wait(0.3);
+            }
+            else e=0;
+            }
+        if (keydata[3] == 0x01) { 
+          /*if(h+g+f<e)
         {
-            p=0;
-            }
-            else
+           h=0;
+           g=0;
+           f=0;
+           e=0;
+            }  */
+      if(h>=e)
+      {
+          h=h-e;
+          }
+      else if(h<e)
+      {
+           if(g==0)
+           {
+               f=f-1;
+               g=g+10-(e-h);
+               h=h+10;
+               h=h-g;
+               }
+            else if(g!=0)
             {
-                p++;
-                }
-      }
-    
-        if(Rx_buf == 0xe619ff00)
-    {
-      if(i==0 && j==0 && k==0 && (i+j+k)-p<0 && i-p<0)
-        {
-           i=0;
-           j=0;
-           k=0;
-           p=0;
-            }  
-      if(i>=p)
-      {
-          i=i-p;
-          }
-      else if(i<p)
-      {
-           if(j==0)
-           {
-               k=k-1;
-               j=j+10-(p-i);
-               i=i+10;
-               i=i-p;
-               }
-            else if(j!=0)
-            {
-                i=i+10;
-                i=i-p;
-                j=j-1;
+                h=h+10;
+                h=h-e;
+                g=g-1;
                 }
-        }
-    
-        
-       //pc.printf("0");
-      }
-      v=0;
-      }
-    
-    // if(o==0)
-    // {
-    //IR.mode(PullNone);
-    wait_ms(10);
-   IR.fall(&IR_ISR);
-   IR.rise(&dummy_ISR); 
-    //}  
- }
-}
-
-void dummy_ISR()
-{
-    //Do nothing
-}
-
-void IR_ISR()
-{
-    o=1;
-    display.detach();
-    distancee.detach();
-    //pult.detach();
-    if(state == 0) {
-        T.stop();               //Stop timer
-        T.reset();              //Reset timer
-        T.start();              //Start timer
-        IR.rise(&IR_ISR);       //Set IR interrupt to occur on rising edge
-        IR.fall(&dummy_ISR);
-        state = 1;              //Go to next state
-        Rx_count = 0;           //Clear the received bits counter
-    } else if(state == 1) {
-        
-        T.stop();                          //Stop timer
-        T_buf = (uint32_t)T.read_us();     //Read timer
-        T.reset();                         //Reset timer
-        T.start();                         //Start timer
-        IR.fall(&IR_ISR);                  //Set IR interrupt to occur on falling edge
-        IR.rise(&dummy_ISR);
- 
-        if(T_buf <= IR_9000us_UL && T_buf >= IR_9000us_LL) {  //Check for preamble start pulse(9ms)
-            state = 2;  //Go to next state
-        } else {
-            state = 0; //Reset the state machine
-        }
-    } else if(state == 2) {
-    
-        T.stop();                          //Stop timer
-        T_buf = (uint32_t)T.read_us();     //Read the value in timer
-        T.reset();                         //Reset timer
-        T.start();                         //Start timer
-        IR.fall(&IR_ISR);                  //Set IR interrupt to occur on falling edge
-        IR.rise(&dummy_ISR);
- 
-        if(T_buf <= IR_4500us_UL && T_buf >= IR_4500us_LL) { //Check for preamble space(4.5ms)
-        
-            state = 3;                  //Go to next state
-            Rx_rep_timeout_count = 0;   //Reset counter
-            Rx_rep_count = 0;           //Reset the repeat pulse counter
-        } else if(T_buf <= IR_2250us_UL && T_buf >= IR_2250us_LL) { //Check for repeat pulse(2.25ms)
-            state = 0;                      //Reset the state machine
- 
-            if(Rx_rep_count < 0xFFFF) {
-                if(Rx_rep_timeout_count < IR_rep_timeout_ms) {  //Only increment the repeat pulse counter if the delay between two successive repeat pulses is less than 135msec.
-                    Rx_rep_timeout_count = 0;                   //Reset the counter everytime a valid repeat pulse is received
-                    ++Rx_rep_count;
-                } else {                                         //Invald repeat pulse received
-                    Rx_rep_count = 0;                            //Reset counter
-                    Rx_flag = 0;                                 //Clear the flag to indicate that an IR command was not received
-                    Rx_buf = 0;                                  //Clear the previously received command
                 }
-            }
-            goto ahead;                    //Repeat the previous command
-        } else { //Wrong pulse
-            Rx_rep_count = 0;     //Reset counter
-            state = 0;                       //Reset the state machine
-        }
-    } else if(state == 3) {
-        T.stop();                      //Stop timer
-        T_buf = T.read_us();          //Read the value in timer
-        T.reset();                     //Reset timer
-        T.start();                     //Start timer
-        IR.fall(&IR_ISR);              //Set IR interrupt to occur on falling edge
-        IR.rise(&dummy_ISR);
-v=1 ;
- 
-        if(T_buf <= IR_1_UL_us && T_buf >= IR_1_LL_us) { //Check if bit is '1'(2.25ms)
-            ++Rx_count;              //Increment the bit counter
-            Rx_buf >>= 1;
-            Rx_buf |= 0x80000000;     //Shift in a '1' from the left side
-            
-            state = 3;               //Remain in current state
-        } else if(T_buf <= IR_0_UL_us && T_buf >= IR_0_LL_us) { //Check if bit is '0'(1.12ms)
-            ++Rx_count;         //Increment the bit counter
-            Rx_buf >>= 1;        //Shift in a '0' from the left side
-            state = 3;          //Remain in current state
-        } else { //Invalid data received
-            Rx_count = 0;//Reset the bit counter
-            state = 0;   //Reset state m/c
-
-        }
- 
-        if(Rx_count == 32) { //Check if all 32 bits have been received
-            state = 0; 
-                 //Reset state m/c
- 
-ahead:
- 
-            if(!((Rx_rep_count > 0) && (Rx_ignore_rep_flag == 1))) {
-                Rx_flag = 1;                             //Set this flag for repeat pulses only if repeat pulses are to be considered
-            }
-            Rx_rep_timeout_count = 0;         //Reset the counter everytime a valid command is received
+                wait(0.3);
+            } 
         }
     }
-     o=0;
-}    
+}
\ No newline at end of file