![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Tehnicko Veleuciliste Zagreb konstrukcijski rad Bozicne lampice
Pri pokretanju programa svijetle led lampice redoslijedom crvena, zelena, žuta u razmaku od 0.2 sekunde i svira melodija we wish You a Merry Christmas. Dovođenjem prekidača sw1 u položaj 0 led lampice se skupa pale i gase također u razmaku od 0.2 sekunde. Ticker služi za provjeru rada programa i treperi plavom led lampicom u razmaku od 0.8 sekundi. Hardverski interrupt radi pritiskom na user button pri čemu program staje na 5 sekundi i pali se led2 na Nucleo pločici. Nakon isteka 5 sek program nastavlja dalje s radom ali led2 ostaje upaljena do ponovog pokretanja interrupat-a.
main.cpp@0:aeccbc47e077, 2021-12-01 (annotated)
- Committer:
- zdenko12
- Date:
- Wed Dec 01 19:15:02 2021 +0000
- Revision:
- 0:aeccbc47e077
uredivanje koda
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
zdenko12 | 0:aeccbc47e077 | 1 | #include "mbed.h" |
zdenko12 | 0:aeccbc47e077 | 2 | #include"flasher.h" |
zdenko12 | 0:aeccbc47e077 | 3 | |
zdenko12 | 0:aeccbc47e077 | 4 | PwmOut buzzer(D7); |
zdenko12 | 0:aeccbc47e077 | 5 | DigitalIn sw1(D8); |
zdenko12 | 0:aeccbc47e077 | 6 | DigitalOut led1(D2); //ticker plava ledica |
zdenko12 | 0:aeccbc47e077 | 7 | Ticker flipper1; |
zdenko12 | 0:aeccbc47e077 | 8 | InterruptIn button(USER_BUTTON); |
zdenko12 | 0:aeccbc47e077 | 9 | DigitalOut workingLED(LED2); //interupt ledica |
zdenko12 | 0:aeccbc47e077 | 10 | Timer debounce; |
zdenko12 | 0:aeccbc47e077 | 11 | void interrupt(void); |
zdenko12 | 0:aeccbc47e077 | 12 | void flip1() { |
zdenko12 | 0:aeccbc47e077 | 13 | led1=!led1; |
zdenko12 | 0:aeccbc47e077 | 14 | } |
zdenko12 | 0:aeccbc47e077 | 15 | //frekvencija melodije |
zdenko12 | 0:aeccbc47e077 | 16 | float a4=440.00; |
zdenko12 | 0:aeccbc47e077 | 17 | float b3=246.94; |
zdenko12 | 0:aeccbc47e077 | 18 | float b4=493.88; |
zdenko12 | 0:aeccbc47e077 | 19 | float c4=261.63; |
zdenko12 | 0:aeccbc47e077 | 20 | float c5=523.25; |
zdenko12 | 0:aeccbc47e077 | 21 | float c5s=554.40; |
zdenko12 | 0:aeccbc47e077 | 22 | float d4=293.66; |
zdenko12 | 0:aeccbc47e077 | 23 | float d5=587.33; |
zdenko12 | 0:aeccbc47e077 | 24 | float e4=329.63; |
zdenko12 | 0:aeccbc47e077 | 25 | float e5=659.25; |
zdenko12 | 0:aeccbc47e077 | 26 | float f4=349.23; |
zdenko12 | 0:aeccbc47e077 | 27 | float f4s=370.00; |
zdenko12 | 0:aeccbc47e077 | 28 | float f5=698.46; |
zdenko12 | 0:aeccbc47e077 | 29 | float g4=392.00; |
zdenko12 | 0:aeccbc47e077 | 30 | float g4s=415.30; |
zdenko12 | 0:aeccbc47e077 | 31 | float g5=783.99; |
zdenko12 | 0:aeccbc47e077 | 32 | //trajanje tona |
zdenko12 | 0:aeccbc47e077 | 33 | float t1=16; |
zdenko12 | 0:aeccbc47e077 | 34 | float t2=8; |
zdenko12 | 0:aeccbc47e077 | 35 | float t3=4; |
zdenko12 | 0:aeccbc47e077 | 36 | float t4=2; |
zdenko12 | 0:aeccbc47e077 | 37 | float t5=1; |
zdenko12 | 0:aeccbc47e077 | 38 | |
zdenko12 | 0:aeccbc47e077 | 39 | float wish_freq[30] = {e4,a4,a4,b4,a4,g4s,f4s,f4s,f4s,b4,b4,c5s,b4,a4,g4s,e4,e4,c5s,c5s,d5,c5s,b4,a4,f4s,e4,e4,f4s,b4,g4s,a4}; |
zdenko12 | 0:aeccbc47e077 | 40 | float wish_tempo[30] = {t3,t3,t2,t2,t2,t2,t3,t3,t3,t3,t2,t2,t2,t2,t3,t3,t3,t3,t2,t2,t2,t2,t3,t3,t2,t2,t3,t3,t3,t4}; |
zdenko12 | 0:aeccbc47e077 | 41 | |
zdenko12 | 0:aeccbc47e077 | 42 | |
zdenko12 | 0:aeccbc47e077 | 43 | int main() |
zdenko12 | 0:aeccbc47e077 | 44 | { |
zdenko12 | 0:aeccbc47e077 | 45 | debounce.start(); |
zdenko12 | 0:aeccbc47e077 | 46 | button.rise(&interrupt); |
zdenko12 | 0:aeccbc47e077 | 47 | |
zdenko12 | 0:aeccbc47e077 | 48 | led1 = 1; |
zdenko12 | 0:aeccbc47e077 | 49 | led1 = 0; |
zdenko12 | 0:aeccbc47e077 | 50 | flipper1.attach(&flip1, 0.8); |
zdenko12 | 0:aeccbc47e077 | 51 | |
zdenko12 | 0:aeccbc47e077 | 52 | |
zdenko12 | 0:aeccbc47e077 | 53 | sw1.mode(PullUp); |
zdenko12 | 0:aeccbc47e077 | 54 | while(1) { |
zdenko12 | 0:aeccbc47e077 | 55 | |
zdenko12 | 0:aeccbc47e077 | 56 | for (int i=0; i<=30; i++) { |
zdenko12 | 0:aeccbc47e077 | 57 | buzzer.period(0.2/(wish_freq[i])); |
zdenko12 | 0:aeccbc47e077 | 58 | buzzer=0.8; |
zdenko12 | 0:aeccbc47e077 | 59 | wait(0.02 *wish_tempo[i]); |
zdenko12 | 0:aeccbc47e077 | 60 | if(sw1 == 1) { |
zdenko12 | 0:aeccbc47e077 | 61 | bozicnelampice(); //poziv funkcije |
zdenko12 | 0:aeccbc47e077 | 62 | } |
zdenko12 | 0:aeccbc47e077 | 63 | else if(sw1==0) { //ako promjenimo polozaj prekidaca |
zdenko12 | 0:aeccbc47e077 | 64 | redled = 1; |
zdenko12 | 0:aeccbc47e077 | 65 | greenled = 1; |
zdenko12 | 0:aeccbc47e077 | 66 | yellowled = 1; |
zdenko12 | 0:aeccbc47e077 | 67 | wait(0.2); |
zdenko12 | 0:aeccbc47e077 | 68 | redled = 0; |
zdenko12 | 0:aeccbc47e077 | 69 | greenled = 0; |
zdenko12 | 0:aeccbc47e077 | 70 | yellowled = 0; |
zdenko12 | 0:aeccbc47e077 | 71 | wait(0.2); |
zdenko12 | 0:aeccbc47e077 | 72 | } |
zdenko12 | 0:aeccbc47e077 | 73 | } |
zdenko12 | 0:aeccbc47e077 | 74 | } |
zdenko12 | 0:aeccbc47e077 | 75 | } |
zdenko12 | 0:aeccbc47e077 | 76 | |
zdenko12 | 0:aeccbc47e077 | 77 | //Zaustavlja rad na 5 sec |
zdenko12 | 0:aeccbc47e077 | 78 | void interrupt() |
zdenko12 | 0:aeccbc47e077 | 79 | { |
zdenko12 | 0:aeccbc47e077 | 80 | |
zdenko12 | 0:aeccbc47e077 | 81 | if (debounce.read_ms()>200) { |
zdenko12 | 0:aeccbc47e077 | 82 | workingLED = !workingLED; |
zdenko12 | 0:aeccbc47e077 | 83 | wait_us(5000000);//Zaustavlja rad na 5 sec |
zdenko12 | 0:aeccbc47e077 | 84 | } |
zdenko12 | 0:aeccbc47e077 | 85 | debounce.reset(); |
zdenko12 | 0:aeccbc47e077 | 86 | } |
zdenko12 | 0:aeccbc47e077 | 87 | |
zdenko12 | 0:aeccbc47e077 | 88 | |
zdenko12 | 0:aeccbc47e077 | 89 | |
zdenko12 | 0:aeccbc47e077 | 90 |