zavrsni ricci

Dependencies:   mbed

Revision:
0:427833cc260e
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Nov 09 16:29:02 2018 +0000
@@ -0,0 +1,398 @@
+//Reads input through the ADC, and transfers to PC terminal
+#include "mbed.h"
+#include "types.h"
+#include "memoria.h"
+#include "Keyb2.h"
+
+// * -------------------------------------------- *
+// * assign HW                                    *
+// * -------------------------------------------- *  
+BusOut Seg1( p5, p6, p7, p8, p9,p10,p11);   // ABCDEFG
+BusOut Seg2(p12,p13,p14,p15,p16,p17,p18);   // ABCDEFG
+BusOut Seg3(p24,p25,p26,p27,p28,p29,p30);   // ABCDEFG
+DigitalOut DG (p19);                        // segment A 
+PwmOut buzzer(p21);
+DigitalOut led1(LED1);          // digital out to p5 
+DigitalOut led2(LED2);          // digital out to p5 
+DigitalOut led3(LED3);          // digital out to p5 
+DigitalOut led4(LED4);          // digital out to p5 
+InterruptIn buttonSEL(p22);     // Interrupt on digital pushbutton
+InterruptIn buttonOK(p23);      // Interrupt on digital pushbutton 
+Timer debounceSEL;              // define debounce timer
+Timer debounceOK;               // define debounce timer
+Timer TimVis;                   // time to visual
+Timer StartUP;                  // counter up
+Timer StartDW;                  // counter DW
+AnalogIn Ain(p20);
+
+// * -------------------------------------------- *
+// * declare prototypes                           *
+// * -------------------------------------------- *  
+void Voltmetro (void);
+void CounterUP (void);
+void CounterDW (void);
+void Visual7(int counter);
+int SetCounterDW (int countrDW);
+void CounterDWend(void);
+void Menu1(void);
+void Menu2(void);
+void Menu3(void);
+void BuzzerSEL (void);
+void BuzzerOK (void);
+
+
+
+int main() 
+{
+
+    // * -------------------------------------------- *
+    // * init HW                                      *
+    // * -------------------------------------------- *  
+    debounceOK.start();
+    buttonOK.rise(&KeybOk); // attach the address of the toggle
+    buttonOK.mode(PullUp);
+    debounceSEL.start();
+    buttonSEL.rise(&KeybSEL); // attach the address of the toggle
+    buttonSEL.mode(PullUp);
+    TimVis.start();
+    StartUP.start();
+    StartDW.start();
+    ExKeyPress = KEYNULL;
+    KeyPress = KEYNULL;
+    Level = LEVEL1;
+    stateMbd = ST_INIT;
+
+     do 
+     { // infinite loop
+    
+        // * -------------------------------------------- *
+        // * state mbed                                   *
+        // * -------------------------------------------- *    
+        if  (TimVis.read_ms() >= 200)
+        {    
+            TimVis.reset(); // restart timer           
+
+            switch (stateMbd) { 
+            case ST_INIT: 
+                stateMbd = ST_M1;
+                break;
+            case ST_M1:
+                Menu1();
+                break;
+            case ST_M2:
+                Menu2();
+                break;
+           case ST_M3:
+                Menu3();
+                break;
+            }
+        }
+    
+        // * -------------------------------------------- *
+        // * read keyboard  --> move to state machine     *
+        // * -------------------------------------------- *     
+        if (Keyboard() != KEYNULL) {  
+        
+            switch (KeyPress) {
+            case KEYSEL:
+                if (Level == LEVEL1) {
+                    if (stateMbd < (ST_MMAX-1)) stateMbd++;
+                    else  stateMbd = (ST_INIT+1);
+                }
+                break;
+            case KEYOK:
+                if (Level < LEVEL6) Level++; else Level = LEVEL1;
+                break;
+            }         
+        }  
+    } 
+    while (1);
+
+}
+
+
+
+// * --------------------------------------------------- *
+// * Menu                                                *
+// * --------------------------------------------------- *
+void Menu1(void)
+{
+    switch (Level) {                        
+    case LEVEL1:                            
+        Seg1 = tabFunc[0U];
+        DG = 0u;
+        Seg2 = tabSeg[0U];
+        Seg3 = tabSeg[1U];
+        break;
+    case LEVEL2:
+        Voltmetro();
+        break;  
+     case LEVEL3:
+        Level = LEVEL1;
+        break;  
+    }
+}
+
+void Menu2(void)
+{
+    switch (Level) {                        
+    case LEVEL1:                            
+        Seg1 = tabFunc[0U];
+        DG = 0u;
+        Seg2 = tabSeg[0U];
+        Seg3 = tabSeg[2U]; 
+        break;
+    case LEVEL2:  
+        DG = 0u;
+        Seg1 = tabFunc[1U];
+        Seg2 = tabFunc[2U];
+        Seg3 = tabFunc[3U];        
+        break;
+    case LEVEL3:  
+        StartUP.reset();
+        CntUp = 0U;
+        Level = LEVEL4;
+        break;
+    case LEVEL4:  
+        CounterUP(); 
+        break;
+    case LEVEL5:  
+        break;
+    case LEVEL6:  
+        StartUP.reset();
+        Level = LEVEL1;
+        break;
+    }
+}
+
+void Menu3(void)
+{
+    switch (Level) {                        
+    case LEVEL1:                            
+        Seg1 = tabFunc[0U];
+        DG = 0u;
+        Seg2 = tabSeg[0U];
+        Seg3 = tabSeg[3U];
+        break;
+  case LEVEL2:  
+        DG = 0u;
+        Seg1 = tabFunc[1U];
+        Seg2 = tabFunc[4U];
+        Seg3 = tabFunc[5U];        
+        break;
+   case LEVEL3:  
+        StartDW.reset();
+        CntDw = 15U;
+        CntDw = SetCounterDW(CntDw);
+        break;
+    case LEVEL4:  
+        CounterDW(); 
+        break;
+    case LEVEL5:
+        break;   
+    case LEVEL6:  
+        StartDW.reset();
+        Level = LEVEL1;
+        break;       
+    }
+}
+
+
+// * --------------------------------------------------- *
+// * Keyboard                                            *
+// * --------------------------------------------------- *
+// routine interrutpt
+void KeybOk() {
+     if (debounceOK.read_ms()>200) // only allow toggle if debounce timer 
+     {
+            bitFlag.keyB = 1;
+            bitFlag.keyOk = 1;
+     }
+     debounceOK.reset(); // restart timer when the toggle is performed 
+     BuzzerOK();
+}
+
+// routine interrutpt
+void KeybSEL() {
+     if (debounceSEL.read_ms()>200) 
+     { 
+            bitFlag.keyB = 1;
+            bitFlag.keySel = 1;            
+     }
+     debounceSEL.reset(); // restart timer when the toggle is performed 
+     BuzzerSEL();
+}
+
+char Keyboard(void)
+{
+    if (bitFlag.keyB == 0) return (KEYNULL);
+    if (bitFlag.keyOk) 
+    {
+        KeyPress = KEYOK;
+        bitFlag.keyOk = 0;
+    }
+    if (bitFlag.keySel) 
+    {
+        KeyPress = KEYSEL;
+        bitFlag.keySel = 0;
+    }
+    bitFlag.keyB = 0;
+    return (KeyPress);
+}
+
+// * --------------------------------------------------- *
+// * DC measure                                          *
+// * --------------------------------------------------- *
+void Voltmetro (void)
+{
+unsigned short unit,dec,cent;
+float tmpADCdata;
+ 
+     // lettura adc
+    ADCdata = Ain * 3.3;
+    
+    //unita
+    unit = (unsigned short)ADCdata;
+    Seg1 = tabSeg[unit];
+    DG = 0u;
+    
+    //decine
+    tmpADCdata = ADCdata - (unsigned short)ADCdata;
+    dec = (unsigned short)(tmpADCdata * 10U);
+    Seg2 = tabSeg[dec];
+    
+    //centinaia
+    tmpADCdata = ADCdata - (unsigned short)ADCdata;
+    tmpADCdata = tmpADCdata * 10U;
+    tmpADCdata = tmpADCdata - (unsigned short)tmpADCdata;
+    cent = (unsigned short)(tmpADCdata * 10U);
+    Seg3 = tabSeg[cent];
+}
+
+// * --------------------------------------------------- *
+// * Counters                                            *
+// * --------------------------------------------------- *
+void CounterUP (void)
+{
+
+    if (StartUP.read_ms()>= 1000)
+    {
+        if (CntUp < 999) 
+        {
+            CntUp++;
+            buzzer.period(1./4000.); // set PWM period
+            buzzer=0.5; // set duty cycle
+            wait(0.05);
+            buzzer.period(1./0.); // set PWM period
+        }
+        else 
+        {
+            CntUp = 0;
+        }
+        StartUP.reset();
+    }
+    Visual7(CntUp);     
+}
+
+void CounterDW (void)
+{
+ 
+    if (StartDW.read_ms()>= 1000U)
+    {
+        if (CntDw > 0U) 
+        {
+            CntDw--;
+            buzzer.period(1./4000.); // set PWM period
+            buzzer=0.5; // set duty cycle
+            wait(0.05);
+            buzzer.period(1./0.); // set PWM period
+        }
+        else 
+        {
+            buzzer.period(1./3000.); // set PWM period
+            buzzer=0.5; // set duty cycle
+            wait(3.5);
+            buzzer.period(1./0.); // set PWM period
+            Level = LEVEL5; 
+        }
+        StartDW.reset();
+    }
+    if (CntDw == 0) CounterDWend();
+    else Visual7(CntDw);
+}
+
+void Visual7(int counter)
+{
+unsigned short unit,dec,cent, tmpCntUP;
+
+    DG = 1U;
+    
+    //centinaia
+    tmpCntUP = counter/100U;
+    cent = tmpCntUP;
+    Seg1 = tabSeg[cent];
+    
+    //decine
+    tmpCntUP = counter- cent * 100U;
+    tmpCntUP = tmpCntUP / 10U;
+    dec = tmpCntUP;
+    Seg2 = tabSeg[dec];
+    
+    //unity
+    tmpCntUP = counter - cent * 100U - dec * 10U;
+    unit = tmpCntUP;
+    Seg3 = tabSeg[unit];     
+}
+
+int SetCounterDW (int countrDW)
+{
+char out = 1;
+        
+    do {
+        if (Keyboard() != KEYNULL) {  
+        
+            switch (KeyPress) {
+            case KEYSEL:
+                if (countrDW > 0U) countrDW--;
+                else countrDW = 180U;
+                break;
+            case KEYOK:
+                Level = LEVEL4; 
+                out = 0U;  
+                break;
+            }
+        }
+        Visual7(countrDW);
+    } while (out);
+    
+    return(countrDW);
+}
+
+void CounterDWend(void)
+{
+    Seg1 = tabFunc[6U];
+    Seg2 = tabFunc[7U];
+    Seg3 = tabFunc[8U];
+}
+
+
+// * --------------------------------------------------- *
+// * Buzzer                                              *
+// * --------------------------------------------------- *
+void BuzzerSEL (void)
+{
+    // buzzer
+    buzzer.period(1./2000.); // set PWM period
+    buzzer=0.5; // set duty cycle
+    wait(0.05);
+    buzzer.period(1./0); // set PWM period
+}
+
+void BuzzerOK (void)
+{
+    // buzzer
+    buzzer.period(1./1000.); // set PWM period
+    buzzer=0.5; // set duty cycle
+    wait(0.05);
+    buzzer.period(1./0); // set PWM period
+}
\ No newline at end of file