Milos Novakovic
/
19E042PIM_T2_2020_0425
Milos Novakovic 2020/0425
main.cpp@0:b341edd1e54c, 2021-11-13 (annotated)
- Committer:
- mlos55
- Date:
- Sat Nov 13 09:20:47 2021 +0000
- Revision:
- 0:b341edd1e54c
Milos Novakovic 2020/0425
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mlos55 | 0:b341edd1e54c | 1 | // Milos Novakovic 2020/0425 |
mlos55 | 0:b341edd1e54c | 2 | |
mlos55 | 0:b341edd1e54c | 3 | /*Prikazana vrijednost ne odgovara onome sto se moze cuti u prvi i poslednjih 10% jer |
mlos55 | 0:b341edd1e54c | 4 | piezzo zahtjeva paljenje i gasenje tj konsantno smjenjivanje 0 i 1, |
mlos55 | 0:b341edd1e54c | 5 | sto npr na 99% skoro sve je visok napon i ne dobijamo zvuk*/ |
mlos55 | 0:b341edd1e54c | 6 | |
mlos55 | 0:b341edd1e54c | 7 | //Uvoz biblioteka i def. direktiva |
mlos55 | 0:b341edd1e54c | 8 | |
mlos55 | 0:b341edd1e54c | 9 | #include "mbed.h" |
mlos55 | 0:b341edd1e54c | 10 | |
mlos55 | 0:b341edd1e54c | 11 | //Globalne promjenljive |
mlos55 | 0:b341edd1e54c | 12 | |
mlos55 | 0:b341edd1e54c | 13 | PwmOut buzzer(PA_11); |
mlos55 | 0:b341edd1e54c | 14 | AnalogIn pot2(PA_1); |
mlos55 | 0:b341edd1e54c | 15 | AnalogIn pot1(PA_0); |
mlos55 | 0:b341edd1e54c | 16 | InterruptIn sw1(PC_9); |
mlos55 | 0:b341edd1e54c | 17 | DigitalOut SEL2(PC_7); |
mlos55 | 0:b341edd1e54c | 18 | DigitalOut SEL1(PB_6); |
mlos55 | 0:b341edd1e54c | 19 | BusOut display1(PA_10, PA_9,PA_8,PB_10,PB_5,PB_4,PB_3); |
mlos55 | 0:b341edd1e54c | 20 | BusOut display2(PA_10, PA_9,PA_8,PB_10,PB_5,PB_4,PB_3); |
mlos55 | 0:b341edd1e54c | 21 | static float x; |
mlos55 | 0:b341edd1e54c | 22 | static int x1; |
mlos55 | 0:b341edd1e54c | 23 | static int flag=0; |
mlos55 | 0:b341edd1e54c | 24 | static int postotak, desetice,jedinice; |
mlos55 | 0:b341edd1e54c | 25 | static char i=0; |
mlos55 | 0:b341edd1e54c | 26 | |
mlos55 | 0:b341edd1e54c | 27 | |
mlos55 | 0:b341edd1e54c | 28 | //deklaracija pomocnih f-ja |
mlos55 | 0:b341edd1e54c | 29 | |
mlos55 | 0:b341edd1e54c | 30 | void ISR_sw1(void); |
mlos55 | 0:b341edd1e54c | 31 | void display(void); |
mlos55 | 0:b341edd1e54c | 32 | |
mlos55 | 0:b341edd1e54c | 33 | //glavni program |
mlos55 | 0:b341edd1e54c | 34 | |
mlos55 | 0:b341edd1e54c | 35 | int main(){ |
mlos55 | 0:b341edd1e54c | 36 | |
mlos55 | 0:b341edd1e54c | 37 | SEL1=1; SEL2=1; |
mlos55 | 0:b341edd1e54c | 38 | sw1.fall(&ISR_sw1); |
mlos55 | 0:b341edd1e54c | 39 | |
mlos55 | 0:b341edd1e54c | 40 | while(1){ |
mlos55 | 0:b341edd1e54c | 41 | buzzer=pot1; |
mlos55 | 0:b341edd1e54c | 42 | if(pot1==0) buzzer=0.01f; |
mlos55 | 0:b341edd1e54c | 43 | |
mlos55 | 0:b341edd1e54c | 44 | |
mlos55 | 0:b341edd1e54c | 45 | |
mlos55 | 0:b341edd1e54c | 46 | if(flag==1) display(); |
mlos55 | 0:b341edd1e54c | 47 | |
mlos55 | 0:b341edd1e54c | 48 | |
mlos55 | 0:b341edd1e54c | 49 | |
mlos55 | 0:b341edd1e54c | 50 | x=pot2*20000; |
mlos55 | 0:b341edd1e54c | 51 | if(x==0) x=0.01f*20000; |
mlos55 | 0:b341edd1e54c | 52 | if(x<1000){ |
mlos55 | 0:b341edd1e54c | 53 | x1=1000/x; |
mlos55 | 0:b341edd1e54c | 54 | buzzer.period_ms(x1); |
mlos55 | 0:b341edd1e54c | 55 | } |
mlos55 | 0:b341edd1e54c | 56 | else{ |
mlos55 | 0:b341edd1e54c | 57 | x1=1000000/x; |
mlos55 | 0:b341edd1e54c | 58 | buzzer.period_us(x1); |
mlos55 | 0:b341edd1e54c | 59 | } |
mlos55 | 0:b341edd1e54c | 60 | |
mlos55 | 0:b341edd1e54c | 61 | wait_ms(30); |
mlos55 | 0:b341edd1e54c | 62 | } |
mlos55 | 0:b341edd1e54c | 63 | |
mlos55 | 0:b341edd1e54c | 64 | } |
mlos55 | 0:b341edd1e54c | 65 | |
mlos55 | 0:b341edd1e54c | 66 | |
mlos55 | 0:b341edd1e54c | 67 | |
mlos55 | 0:b341edd1e54c | 68 | void ISR_sw1(){ |
mlos55 | 0:b341edd1e54c | 69 | flag = 1; |
mlos55 | 0:b341edd1e54c | 70 | } |
mlos55 | 0:b341edd1e54c | 71 | |
mlos55 | 0:b341edd1e54c | 72 | |
mlos55 | 0:b341edd1e54c | 73 | void display(){ |
mlos55 | 0:b341edd1e54c | 74 | postotak=pot1*100; |
mlos55 | 0:b341edd1e54c | 75 | desetice=postotak/10; |
mlos55 | 0:b341edd1e54c | 76 | jedinice=postotak-(desetice*10); |
mlos55 | 0:b341edd1e54c | 77 | |
mlos55 | 0:b341edd1e54c | 78 | while(i<100){ |
mlos55 | 0:b341edd1e54c | 79 | SEL1=0; |
mlos55 | 0:b341edd1e54c | 80 | switch(desetice){ |
mlos55 | 0:b341edd1e54c | 81 | |
mlos55 | 0:b341edd1e54c | 82 | case 1: display2=0xF9; break; |
mlos55 | 0:b341edd1e54c | 83 | case 2: display2=0xA4; break; |
mlos55 | 0:b341edd1e54c | 84 | case 3: display2=0xB0; break; |
mlos55 | 0:b341edd1e54c | 85 | case 4: display2=0x99; break; |
mlos55 | 0:b341edd1e54c | 86 | case 5: display2=0x92; break; |
mlos55 | 0:b341edd1e54c | 87 | case 6: display2=0x82; break; |
mlos55 | 0:b341edd1e54c | 88 | case 7: display2=0xF8; break; |
mlos55 | 0:b341edd1e54c | 89 | case 8: display2=0x80; break; |
mlos55 | 0:b341edd1e54c | 90 | case 9: display2=0x90; break; |
mlos55 | 0:b341edd1e54c | 91 | default: SEL1=1; break; |
mlos55 | 0:b341edd1e54c | 92 | } |
mlos55 | 0:b341edd1e54c | 93 | wait_ms(5); |
mlos55 | 0:b341edd1e54c | 94 | SEL1=1; |
mlos55 | 0:b341edd1e54c | 95 | SEL2=0; |
mlos55 | 0:b341edd1e54c | 96 | switch(jedinice){ |
mlos55 | 0:b341edd1e54c | 97 | case 0: SEL1=1;display2=0x40; break; |
mlos55 | 0:b341edd1e54c | 98 | case 1: display2=0xF9; break; |
mlos55 | 0:b341edd1e54c | 99 | case 2: display2=0xA4; break; |
mlos55 | 0:b341edd1e54c | 100 | case 3: display2=0xB0; break; |
mlos55 | 0:b341edd1e54c | 101 | case 4: display2=0x99; break; |
mlos55 | 0:b341edd1e54c | 102 | case 5: display2=0x92; break; |
mlos55 | 0:b341edd1e54c | 103 | case 6: display2=0x82; break; |
mlos55 | 0:b341edd1e54c | 104 | case 7: display2=0xF8; break; |
mlos55 | 0:b341edd1e54c | 105 | case 8: display2=0x80; break; |
mlos55 | 0:b341edd1e54c | 106 | case 9: display2=0x90; break; |
mlos55 | 0:b341edd1e54c | 107 | } |
mlos55 | 0:b341edd1e54c | 108 | wait_ms(5); |
mlos55 | 0:b341edd1e54c | 109 | i++; |
mlos55 | 0:b341edd1e54c | 110 | SEL2=1; |
mlos55 | 0:b341edd1e54c | 111 | } |
mlos55 | 0:b341edd1e54c | 112 | |
mlos55 | 0:b341edd1e54c | 113 | SEL1=1; SEL2=1; |
mlos55 | 0:b341edd1e54c | 114 | flag=0; |
mlos55 | 0:b341edd1e54c | 115 | i=0; |
mlos55 | 0:b341edd1e54c | 116 | } |