Filip Stevanovic
/
19E042PIM_T2_2020_0039
Filip Stevanovic 2020/0039
main.cpp@0:569d2e559397, 2021-11-13 (annotated)
- Committer:
- filip_ste
- Date:
- Sat Nov 13 08:28:59 2021 +0000
- Revision:
- 0:569d2e559397
- Child:
- 1:f62564cb5261
Filip Stevanovic 2020/0039
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
filip_ste | 0:569d2e559397 | 1 | /* |
filip_ste | 0:569d2e559397 | 2 | Zasto ljudi cudno cuju? |
filip_ste | 0:569d2e559397 | 3 | -Filip Stevanovic |
filip_ste | 0:569d2e559397 | 4 | |
filip_ste | 0:569d2e559397 | 5 | Da bi smo bili bolji predatori, evolucija je coveku razvila veoma osetljiva cula. |
filip_ste | 0:569d2e559397 | 6 | Da bi se to ostvarilo, mozak signale sa cula logaritmuje, i tako se promene |
filip_ste | 0:569d2e559397 | 7 | na veoma malim nivoima jasno uocavaju dok se promene na velikim nivoima skoro uopste |
filip_ste | 0:569d2e559397 | 8 | ne primecuju. |
filip_ste | 0:569d2e559397 | 9 | |
filip_ste | 0:569d2e559397 | 10 | Hvala na paznji |
filip_ste | 0:569d2e559397 | 11 | */ |
filip_ste | 0:569d2e559397 | 12 | |
filip_ste | 0:569d2e559397 | 13 | #include "mbed.h" |
filip_ste | 0:569d2e559397 | 14 | |
filip_ste | 0:569d2e559397 | 15 | //Constants |
filip_ste | 0:569d2e559397 | 16 | #define DebouncePeriod 100 //Period that a button needs to stabilise in miliseconds. 100 should be okay |
filip_ste | 0:569d2e559397 | 17 | #define ArbitraryWaitPeriod 1 //Aritrary pooling waiting periods, its should be the in the scale of tens of miliseconds to max 100 |
filip_ste | 0:569d2e559397 | 18 | |
filip_ste | 0:569d2e559397 | 19 | //Constants for frequency spectrum function |
filip_ste | 0:569d2e559397 | 20 | const int n = 5000; |
filip_ste | 0:569d2e559397 | 21 | const int k = -4950; |
filip_ste | 0:569d2e559397 | 22 | |
filip_ste | 0:569d2e559397 | 23 | //Pins |
filip_ste | 0:569d2e559397 | 24 | #define A PA_10 |
filip_ste | 0:569d2e559397 | 25 | #define B PA_9 |
filip_ste | 0:569d2e559397 | 26 | #define C PA_8 |
filip_ste | 0:569d2e559397 | 27 | #define D PB_10 |
filip_ste | 0:569d2e559397 | 28 | #define E PB_5 |
filip_ste | 0:569d2e559397 | 29 | #define F PB_4 |
filip_ste | 0:569d2e559397 | 30 | #define G PB_3 |
filip_ste | 0:569d2e559397 | 31 | #define SEL1 PB_6 |
filip_ste | 0:569d2e559397 | 32 | #define SEL2 PC_7 |
filip_ste | 0:569d2e559397 | 33 | #define SW1 PC_9 |
filip_ste | 0:569d2e559397 | 34 | #define POT1 PA_0 |
filip_ste | 0:569d2e559397 | 35 | #define POT2 PA_1 |
filip_ste | 0:569d2e559397 | 36 | #define BUZZ PA_11 |
filip_ste | 0:569d2e559397 | 37 | #define LD2 PB_15 |
filip_ste | 0:569d2e559397 | 38 | |
filip_ste | 0:569d2e559397 | 39 | //Pin/Object declaration |
filip_ste | 0:569d2e559397 | 40 | PwmOut buzzer (BUZZ); |
filip_ste | 0:569d2e559397 | 41 | DigitalOut led2 (LD2); |
filip_ste | 0:569d2e559397 | 42 | InterruptIn sw1 (SW1); |
filip_ste | 0:569d2e559397 | 43 | AnalogIn pot1 (POT1); |
filip_ste | 0:569d2e559397 | 44 | AnalogIn pot2 (POT2); |
filip_ste | 0:569d2e559397 | 45 | BusOut display (A,B,C,D,E,F,G); |
filip_ste | 0:569d2e559397 | 46 | BusOut select (SEL2,SEL1); |
filip_ste | 0:569d2e559397 | 47 | |
filip_ste | 0:569d2e559397 | 48 | //Timer declaration |
filip_ste | 0:569d2e559397 | 49 | Timer t; |
filip_ste | 0:569d2e559397 | 50 | |
filip_ste | 0:569d2e559397 | 51 | //Global variable for updating the display when SW1 is pressed |
filip_ste | 0:569d2e559397 | 52 | int value = 0; |
filip_ste | 0:569d2e559397 | 53 | |
filip_ste | 0:569d2e559397 | 54 | //ISR for Switch1 (Fall) |
filip_ste | 0:569d2e559397 | 55 | void ISR_sw1(){ |
filip_ste | 0:569d2e559397 | 56 | //If the button recently fell, dont do the ISR again (Debounce) |
filip_ste | 0:569d2e559397 | 57 | //If it didnt, do it and save the new time when you did it |
filip_ste | 0:569d2e559397 | 58 | if(t.read_ms()> DebouncePeriod){ |
filip_ste | 0:569d2e559397 | 59 | value = pot1 * 100; |
filip_ste | 0:569d2e559397 | 60 | led2 = !led2; |
filip_ste | 0:569d2e559397 | 61 | t.reset(); |
filip_ste | 0:569d2e559397 | 62 | } |
filip_ste | 0:569d2e559397 | 63 | } |
filip_ste | 0:569d2e559397 | 64 | |
filip_ste | 0:569d2e559397 | 65 | //Show digit on display |
filip_ste | 0:569d2e559397 | 66 | void SegDisp (int num){ |
filip_ste | 0:569d2e559397 | 67 | switch(num){ |
filip_ste | 0:569d2e559397 | 68 | case 0:display = 0b1000000;break; |
filip_ste | 0:569d2e559397 | 69 | case 1:display = 0b1111001;break; |
filip_ste | 0:569d2e559397 | 70 | case 2:display = 0b0100100;break; |
filip_ste | 0:569d2e559397 | 71 | case 3:display = 0b0110000;break; |
filip_ste | 0:569d2e559397 | 72 | case 4:display = 0b0011001;break; |
filip_ste | 0:569d2e559397 | 73 | case 5:display = 0b0010010;break; |
filip_ste | 0:569d2e559397 | 74 | case 6:display = 0b0000010;break; |
filip_ste | 0:569d2e559397 | 75 | case 7:display = 0b1111000;break; |
filip_ste | 0:569d2e559397 | 76 | case 8:display = 0b0000000;break; |
filip_ste | 0:569d2e559397 | 77 | case 9:display = 0b0010000;break; |
filip_ste | 0:569d2e559397 | 78 | default:display = 0x7f; break; |
filip_ste | 0:569d2e559397 | 79 | |
filip_ste | 0:569d2e559397 | 80 | } |
filip_ste | 0:569d2e559397 | 81 | } |
filip_ste | 0:569d2e559397 | 82 | |
filip_ste | 0:569d2e559397 | 83 | //Convert number to digits and show on display |
filip_ste | 0:569d2e559397 | 84 | void PrintDisp(int num){ |
filip_ste | 0:569d2e559397 | 85 | int j = num%10,d = num/10; |
filip_ste | 0:569d2e559397 | 86 | if(d==0) |
filip_ste | 0:569d2e559397 | 87 | SegDisp(-1); |
filip_ste | 0:569d2e559397 | 88 | else |
filip_ste | 0:569d2e559397 | 89 | SegDisp(d); |
filip_ste | 0:569d2e559397 | 90 | select = 0b01; |
filip_ste | 0:569d2e559397 | 91 | wait_ms(ArbitraryWaitPeriod); |
filip_ste | 0:569d2e559397 | 92 | SegDisp(j); |
filip_ste | 0:569d2e559397 | 93 | select = 0b10; |
filip_ste | 0:569d2e559397 | 94 | wait_ms(ArbitraryWaitPeriod); |
filip_ste | 0:569d2e559397 | 95 | } |
filip_ste | 0:569d2e559397 | 96 | |
filip_ste | 0:569d2e559397 | 97 | //Main |
filip_ste | 0:569d2e559397 | 98 | int main(){ |
filip_ste | 0:569d2e559397 | 99 | //Starting timer |
filip_ste | 0:569d2e559397 | 100 | t.start(); |
filip_ste | 0:569d2e559397 | 101 | //Binding ISR to fall for Switch1 |
filip_ste | 0:569d2e559397 | 102 | sw1.fall(&ISR_sw1); |
filip_ste | 0:569d2e559397 | 103 | |
filip_ste | 0:569d2e559397 | 104 | //Main Loop |
filip_ste | 0:569d2e559397 | 105 | while(1){ |
filip_ste | 0:569d2e559397 | 106 | //Pooling Pot1 i Pot2 |
filip_ste | 0:569d2e559397 | 107 | buzzer=pot1*0.5f; //Arbitrarno deljenje sa 2 jer je apsolutno isto da li je duty 0.25 ili 0.75 |
filip_ste | 0:569d2e559397 | 108 | buzzer.period_us(k*pot2+n); //Linearise frequency from 20Hz to 20kHz |
filip_ste | 0:569d2e559397 | 109 | |
filip_ste | 0:569d2e559397 | 110 | //Print Pot1 to Display |
filip_ste | 0:569d2e559397 | 111 | PrintDisp(value); //It never gets to 1 so it wont get to 100 thus its limited to 99 |
filip_ste | 0:569d2e559397 | 112 | |
filip_ste | 0:569d2e559397 | 113 | //Pooling time is integrated in PrintDisp function |
filip_ste | 0:569d2e559397 | 114 | } |
filip_ste | 0:569d2e559397 | 115 | } |
filip_ste | 0:569d2e559397 | 116 |