![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
zavrsni ricci
Dependencies: mbed
Revision 0:427833cc260e, committed 2018-11-09
- Comitter:
- marcoricci
- Date:
- Fri Nov 09 16:29:02 2018 +0000
- Commit message:
- final
Changed in this revision
diff -r 000000000000 -r 427833cc260e Keyb2.c
diff -r 000000000000 -r 427833cc260e Keyb2.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Keyb2.h Fri Nov 09 16:29:02 2018 +0000 @@ -0,0 +1,7 @@ +#define KEYNULL 0x00 +#define KEYOK 0x01 +#define KEYSEL 0x02 + +void KeybOk(void); +void KeybSEL(void); +char Keyboard(void); \ No newline at end of file
diff -r 000000000000 -r 427833cc260e main.cpp --- /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
diff -r 000000000000 -r 427833cc260e mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Fri Nov 09 16:29:02 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/e95d10626187 \ No newline at end of file
diff -r 000000000000 -r 427833cc260e memoria.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/memoria.h Fri Nov 09 16:29:02 2018 +0000 @@ -0,0 +1,26 @@ +#define LEVEL1 0x01 +#define LEVEL2 0x02 +#define LEVEL3 0x03 +#define LEVEL4 0x04 +#define LEVEL5 0x05 +#define LEVEL6 0x06 +#define LEVEL7 0x07 +#define ST_INIT 32 // stato inizializzazione polling +#define ST_M1 33 // voltmetro +#define ST_M2 34 // conta UP +#define ST_M3 35 // conta DWN +#define ST_MMAX 36 // fine menu' + +unsigned char tabSeg[10U]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; // 0,1,2,3,4,5,6,7,8,9 +unsigned char tabFunc[9U] = {0x8E,0xC6,0xC1,0x8C,0xC7,0xC0,0x86,0xC8,0xA1}; // 0-F,1-C,2-U,3-P,4-L,5-O,6-E,7-n,8-d +unsigned short stateMach; +TBitFlag bitFlag; +char KeyPress; +char KeyPressInt; // tasto letto nell'nterrupt +char ExKeyPress; // tasto precedente +char cntEqualKey; // contatore di tasti uguali, successivi +char Level; +char stateMbd; +unsigned short CntUp; +unsigned short CntDw; +float ADCdata; \ No newline at end of file
diff -r 000000000000 -r 427833cc260e menu.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/menu.c Fri Nov 09 16:29:02 2018 +0000 @@ -0,0 +1,2 @@ + +
diff -r 000000000000 -r 427833cc260e types.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/types.h Fri Nov 09 16:29:02 2018 +0000 @@ -0,0 +1,18 @@ + +/* +**======================================================================= +** +** Tipo flag 1 +** +**======================================================================= +*/ +typedef struct { + char keyOk : 1; + char keySel : 1; + char keyB : 1; + char b3 : 1; + char b4 : 1; + char b5 : 1; + char b6 : 1; + char b7 : 1; + } TBitFlag; \ No newline at end of file