This shows you the differences between two versions of the page.
| lt:supervisors:solutions:digi [2011/03/25 14:59] – sukurtas Marius | lt:supervisors:solutions:digi [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== L1 Digital ====== | ||
| + | Comment: Partial translation is needed! | ||
| + | |||
| + | ====== Digital input/ | ||
| + | |||
| + | ==== Warm-Up ==== | ||
| + | |||
| + | Lights up: | ||
| + | * pressing S1 1 LEDs, | ||
| + | * pressing S2 2 LEDs, | ||
| + | * pressing S3 3 LEDs | ||
| + | |||
| + | <code c> | ||
| + | // | ||
| + | // HomeLab User interface module | ||
| + | // Warm-up exercise | ||
| + | // | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | pin led_red | ||
| + | pin led_yellow = PIN(C, 4); | ||
| + | pin led_green | ||
| + | |||
| + | pin button1 = PIN(C, 0); | ||
| + | pin button2 = PIN(C, 1); | ||
| + | pin button3 = PIN(C, 2); | ||
| + | |||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | // LED setup | ||
| + | pin_setup_output(led_red); | ||
| + | pin_setup_output(led_yellow); | ||
| + | pin_setup_output(led_green); | ||
| + | |||
| + | // Button setup | ||
| + | pin_setup_input_with_pullup(button1); | ||
| + | pin_setup_input_with_pullup(button2); | ||
| + | pin_setup_input_with_pullup(button3); | ||
| + | |||
| + | // Switches LEDs off | ||
| + | pin_set(led_green); | ||
| + | pin_set(led_yellow); | ||
| + | pin_set(led_red); | ||
| + | |||
| + | while(true) | ||
| + | { | ||
| + | // Pressing S1 lights up LED1 (green) | ||
| + | if(!pin_get_debounced_value(button1)) | ||
| + | { | ||
| + | pin_clear(led_green); | ||
| + | pin_set(led_yellow); | ||
| + | pin_set(led_red); | ||
| + | } | ||
| + | // Pressing S2 lights up LED2 and LED3 (green and yellow) | ||
| + | if(!pin_get_debounced_value(button2)) | ||
| + | { | ||
| + | pin_clear(led_green); | ||
| + | pin_clear(led_yellow); | ||
| + | pin_set(led_red); | ||
| + | } | ||
| + | // Pressing S3 lights up all LEDs | ||
| + | if(!pin_get_debounced_value(button3)) | ||
| + | { | ||
| + | pin_clear(led_green); | ||
| + | pin_clear(led_yellow); | ||
| + | pin_clear(led_red); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Beginners exercise 1 ==== | ||
| + | Imiteerib ülekäiguraja autode valgusfoori tööd. Kuni pole vajutatud ühelegi nupule, põleb autodele roheline LED. Pärast suvalisele nupule vajutamist hakkab roheline kolmeks sekundiks vilkuma, seejärel süttib kolmeks sekundiks kollane ning kümneks sekundiks punane ja lõpuks jääb uuesti pidevalt põlema roheline LED. | ||
| + | |||
| + | <code c> | ||
| + | // | ||
| + | // Traffic light | ||
| + | // | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | pin led_red | ||
| + | pin led_yellow = PIN(C, 4); | ||
| + | pin led_green | ||
| + | |||
| + | pin button1 = PIN(C, 0); | ||
| + | pin button2 = PIN(C, 1); | ||
| + | pin button3 = PIN(C, 2); | ||
| + | |||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | unsigned char new_value, old_value = 0; | ||
| + | unsigned short i; | ||
| + | |||
| + | // Seab LEDid töökorda | ||
| + | pin_setup_output(led_red); | ||
| + | pin_setup_output(led_yellow); | ||
| + | pin_setup_output(led_green); | ||
| + | |||
| + | // Seab nupud töökorda | ||
| + | pin_setup_input_with_pullup(button1); | ||
| + | pin_setup_input_with_pullup(button2); | ||
| + | pin_setup_input_with_pullup(button3); | ||
| + | |||
| + | // Lülitab rohelise LED sisse ja teised LEDid välja | ||
| + | pin_clear(led_green); | ||
| + | pin_set(led_yellow); | ||
| + | pin_set(led_red); | ||
| + | |||
| + | while(true) | ||
| + | { | ||
| + | // Eelmise ja uue nupuväärtuse lugemine | ||
| + | new_value = pin_get_debounced_value(button1) & pin_get_debounced_value(button2) & pin_get_debounced_value(button3); | ||
| + | |||
| + | // Kui nuppu on vajutatud ja seejärel vabastatud | ||
| + | if((!new_value) && (old_value)) | ||
| + | { | ||
| + | // Vilgutab rohelist LED'i 3 sekundit | ||
| + | for(i = 0; i < 6; i++) | ||
| + | { | ||
| + | pin_toggle(led_green); | ||
| + | sw_delay_ms(500); | ||
| + | } | ||
| + | // Kustutab rohelise ja lülitab 3 sekundiks sisse kollase LEDi | ||
| + | pin_set(led_green); | ||
| + | pin_clear(led_yellow); | ||
| + | sw_delay_ms(3000); | ||
| + | // Kustutab kollase ja lülitab 10 sekundiks sisse punase LEDi | ||
| + | pin_set(led_yellow); | ||
| + | pin_clear(led_red); | ||
| + | sw_delay_ms(10000); | ||
| + | // Kustutab punase ja lülitab uuesti sisse rohelise LEDi | ||
| + | pin_set(led_red); | ||
| + | pin_clear(led_green); | ||
| + | } | ||
| + | |||
| + | old_value = new_value; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Beginners exercise 2 ==== | ||
| + | Loendab nupuvajutusi. Vajutus loetakse lõppenuks alles siis, kui nupp vabastatakse. Tulemus kuvatakse kahendkoodis LED-idel. Maksimaalne tulemus kolmel LED-il on 23-1. Roheline LED tähistab 1. bitti, kollane 2. bitti, punane 3. bitti. | ||
| + | |||
| + | <code c> | ||
| + | // | ||
| + | // Nupuvajutusi loendav programm | ||
| + | // Väljund binaarkoodis, | ||
| + | // | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | pin led_red | ||
| + | pin led_yellow = PIN(C, 4); | ||
| + | pin led_green | ||
| + | |||
| + | pin button1 = PIN(C, 0); | ||
| + | pin button2 = PIN(C, 1); | ||
| + | pin button3 = PIN(C, 2); | ||
| + | |||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | unsigned char new_value, old_value = 0; | ||
| + | unsigned short count = 0; | ||
| + | |||
| + | // Seab LEDid töökorda | ||
| + | pin_setup_output(led_red); | ||
| + | pin_setup_output(led_yellow); | ||
| + | pin_setup_output(led_green); | ||
| + | |||
| + | // Seab nupud töökorda | ||
| + | pin_setup_input_with_pullup(button1); | ||
| + | pin_setup_input_with_pullup(button2); | ||
| + | pin_setup_input_with_pullup(button3); | ||
| + | |||
| + | // Lülitab LEDid välja | ||
| + | pin_set(led_green); | ||
| + | pin_set(led_yellow); | ||
| + | pin_set(led_red); | ||
| + | |||
| + | while(true) | ||
| + | { | ||
| + | // Eelmise ja uue nupuväärtuse lugemine | ||
| + | new_value = pin_get_debounced_value(button1) & pin_get_debounced_value(button2) & pin_get_debounced_value(button3); | ||
| + | |||
| + | // Kui nuppu on vajutatud ja seejärel vabastatud | ||
| + | if((!new_value) && (old_value)) | ||
| + | { | ||
| + | // Liidab loendurile ühe juurde | ||
| + | count++; | ||
| + | |||
| + | // Kõrgeim bitt (4) | ||
| + | // Aktiveeritakse kui loendurit neljaga jagades on tulemus >= 1 | ||
| + | if(((count % 8) / 4) > 0) | ||
| + | pin_clear(led_red); | ||
| + | else | ||
| + | pin_set(led_red); | ||
| + | |||
| + | // Keskmine bitt (2) | ||
| + | // Aktiveeritakse kui eelmise tehte jääki kahega jagades on tulemus >= 1 | ||
| + | if(((count % 4) / 2) > 0) | ||
| + | pin_clear(led_yellow); | ||
| + | else | ||
| + | pin_set(led_yellow); | ||
| + | |||
| + | // Madalaim bitt (1) | ||
| + | // Aktiveeritakse kui eelmise tehte jääk on >= 1 | ||
| + | if((count % 2) > 0) | ||
| + | pin_clear(led_green); | ||
| + | else | ||
| + | pin_set(led_green); | ||
| + | } | ||
| + | |||
| + | old_value = new_value; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Beginners exercise 3 ==== | ||
| + | Vajutades nupule S1, süttib korraga LED1 ja LED3, vajutades nupule S2, süttib LED2, vajutades nupule S3, kustuvad kõik LED-id. Operatsioonid teostatakse otse vastavate registrite väärtusi muutes (ilma Kodulabori teegita). | ||
| + | <code c> | ||
| + | // | ||
| + | // Digitaalsete sisendite-väljundite ilma kodulabori teegita kasutamise näide | ||
| + | // | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define LED1 PC5 | ||
| + | #define LED2 PC4 | ||
| + | #define LED3 PC3 | ||
| + | |||
| + | #define S1 0 | ||
| + | #define S2 1 | ||
| + | #define S3 2 | ||
| + | |||
| + | int checkButton(int pin); | ||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | DDRC = (1<< | ||
| + | PORTC = (1<< | ||
| + | |||
| + | while(1) | ||
| + | { | ||
| + | // Nupu S1 vajutamisel lülitatakse sisse LED1 ja LED3 | ||
| + | if(checkButton(S1)) | ||
| + | { | ||
| + | PORTC &= ~(1<< | ||
| + | } | ||
| + | // Nupu S2 vajutamisel lülitatakse sisse LED2 | ||
| + | if(checkButton(S2)) | ||
| + | { | ||
| + | PORTC &= ~(1<< | ||
| + | } | ||
| + | // Nupu S3 vajutamisel lülitatakse kõik LEDid välja | ||
| + | if(checkButton(S3)) | ||
| + | { | ||
| + | PORTC |= (1<< | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | // | ||
| + | // Kontrollib antud nupu väärtust | ||
| + | // | ||
| + | int checkButton(int pin) | ||
| + | { | ||
| + | int i; | ||
| + | // Nupp on sees kui bitt on madal (0) | ||
| + | if ((PINC & (1<< | ||
| + | { | ||
| + | // Põrkamise silumine | ||
| + | for(i=0; | ||
| + | { | ||
| + | if ((PINC & (1<< | ||
| + | return 0; | ||
| + | _delay_ms(1); | ||
| + | } | ||
| + | } | ||
| + | else | ||
| + | return 0; | ||
| + | |||
| + | return 1; | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Beginners exercise 4 ==== | ||
| + | Loendab nupuvajutusi. Tulemus kuvatakse LED-i vilkumistega. Pärast igat nupule vajutamist suureneb vilgutamiste arv ühe võrra. Valida võib suvalise nupu. LED-i vilgutamiseks kasutada alamfunktsiooni, | ||
| + | <code c> | ||
| + | // | ||
| + | // Nupuvajutusi loendav programm | ||
| + | // Väljund LEDi vilkumisega | ||
| + | // | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | void blink(int c); // Deklareerib alamfunktsiooni LEDi vilgutamiseks | ||
| + | |||
| + | pin led_yellow = PIN(C, 4); | ||
| + | |||
| + | pin button1 = PIN(C, 0); | ||
| + | pin button2 = PIN(C, 1); | ||
| + | pin button3 = PIN(C, 2); | ||
| + | |||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | unsigned char new_value, old_value = 0; | ||
| + | unsigned short count = 0; | ||
| + | |||
| + | // Seab LEDi töökorda | ||
| + | pin_setup_output(led_yellow); | ||
| + | |||
| + | // Seab nupud töökorda | ||
| + | pin_setup_input_with_pullup(button1); | ||
| + | pin_setup_input_with_pullup(button2); | ||
| + | pin_setup_input_with_pullup(button3); | ||
| + | |||
| + | // Lülitab LEDi välja | ||
| + | pin_set(led_yellow); | ||
| + | |||
| + | while(true) | ||
| + | { | ||
| + | // Eelmise ja uue nupuväärtuse lugemine | ||
| + | new_value = pin_get_debounced_value(button1) & pin_get_debounced_value(button2) & pin_get_debounced_value(button3); | ||
| + | |||
| + | // Kui nuppu on vajutatud ja seejärel vabastatud | ||
| + | if((!new_value) && (old_value)) | ||
| + | { | ||
| + | // Liidab loendurile ühe juurde | ||
| + | count++; | ||
| + | |||
| + | // Vilgutab õige arv kordi LEDi | ||
| + | blink(count); | ||
| + | } | ||
| + | |||
| + | old_value = new_value; | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | // | ||
| + | // Alamfunktsioon LEDi vilgutamiseks | ||
| + | // | ||
| + | void blink(int c) | ||
| + | { | ||
| + | int i = 0; | ||
| + | |||
| + | // Muudab LEDi olekut iga 500ms tagant | ||
| + | // Üheks vilgutuseks on tarvis LEDi olekut 2x muuta | ||
| + | // Üks vilgutus võtab seega 1s | ||
| + | for(i = 0; i < (c * 2); i++) | ||
| + | { | ||
| + | pin_toggle(led_yellow); | ||
| + | sw_delay_ms(500); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | ==== Beginners exercise 5 ==== | ||
| + | Vajutades nupule S1, vilgutab punane LED morsekoodis „SOS“, vajutades nupule S2, vilgutab kollane LED „CQD“ ja, vajutades nupule S3, vilgutab roheline LED „OK“. | ||
| + | <code c> | ||
| + | // | ||
| + | // Morsekoodis teateid edastav programm | ||
| + | // Nupp 1 : punane SOS | ||
| + | // Nupp 2 : kollane CQD | ||
| + | // Nupp 3 : roheline OK | ||
| + | // | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | // Defineeri vajaminevad morsetähed | ||
| + | #define C 1 | ||
| + | #define D 2 | ||
| + | #define K 3 | ||
| + | #define O 4 | ||
| + | #define Q 5 | ||
| + | #define S 6 | ||
| + | |||
| + | // Deklareeri alamfunktsioonid | ||
| + | void blinkSos(void); | ||
| + | void blinkCqd(void); | ||
| + | void blinkOk(void); | ||
| + | void blinkChar(int c, pin led); | ||
| + | void blinkDot(pin led); | ||
| + | void blinkDash(pin led); | ||
| + | |||
| + | pin led_red | ||
| + | pin led_yellow = PIN(C, 4); | ||
| + | pin led_green | ||
| + | |||
| + | pin button1 = PIN(C, 0); | ||
| + | pin button2 = PIN(C, 1); | ||
| + | pin button3 = PIN(C, 2); | ||
| + | |||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | // Seab LEDid töökorda | ||
| + | pin_setup_output(led_red); | ||
| + | pin_setup_output(led_yellow); | ||
| + | pin_setup_output(led_green); | ||
| + | |||
| + | // Seab nupud töökorda | ||
| + | pin_setup_input_with_pullup(button1); | ||
| + | pin_setup_input_with_pullup(button2); | ||
| + | pin_setup_input_with_pullup(button3); | ||
| + | |||
| + | // Lülitab LEDid välja | ||
| + | pin_set(led_green); | ||
| + | pin_set(led_yellow); | ||
| + | pin_set(led_red); | ||
| + | |||
| + | while(true) | ||
| + | { | ||
| + | // Nupu 1 vajutamisel vilgutatakse SOS | ||
| + | if(!pin_get_value(button1)) | ||
| + | blinkSos(); | ||
| + | // Nupu 2 vajutamisel vilgutatakse CQD | ||
| + | if(!pin_get_value(button2)) | ||
| + | blinkCqd(); | ||
| + | // Nupu 3 vajutamisel vilgutatakse OK | ||
| + | if(!pin_get_value(button3)) | ||
| + | blinkOk(); | ||
| + | } | ||
| + | |||
| + | } | ||
| + | |||
| + | // Vilgutab SOS punase LEDiga | ||
| + | void blinkSos(void) | ||
| + | { | ||
| + | blinkChar(S, | ||
| + | blinkChar(O, | ||
| + | blinkChar(S, | ||
| + | } | ||
| + | |||
| + | // Vilgutab CQD kollase LEDiga | ||
| + | void blinkCqd(void) | ||
| + | { | ||
| + | blinkChar(C, | ||
| + | blinkChar(Q, | ||
| + | blinkChar(D, | ||
| + | } | ||
| + | |||
| + | // Vilgutab OK rohelise LEDiga | ||
| + | void blinkOk(void) | ||
| + | { | ||
| + | blinkChar(O, | ||
| + | blinkChar(K, | ||
| + | } | ||
| + | |||
| + | // Vilgutab parameetris määratud LEDiga punkti | ||
| + | void blinkDot(pin led) | ||
| + | { | ||
| + | pin_clear(led); | ||
| + | sw_delay_ms(100); | ||
| + | pin_set(led); | ||
| + | sw_delay_ms(100); | ||
| + | } | ||
| + | |||
| + | // Vilgutab parameetris määratud LEDiga kriipsu | ||
| + | void blinkDash(pin led) | ||
| + | { | ||
| + | pin_clear(led); | ||
| + | sw_delay_ms(300); | ||
| + | pin_set(led); | ||
| + | sw_delay_ms(100); | ||
| + | } | ||
| + | |||
| + | // Vilgutab eeldefineeritud mustri järgi | ||
| + | // parameetris ' | ||
| + | void blinkChar(int c, pin led) | ||
| + | { | ||
| + | switch(c) | ||
| + | { | ||
| + | // -.-. | ||
| + | case C: | ||
| + | blinkDash(led); | ||
| + | blinkDot(led); | ||
| + | blinkDash(led); | ||
| + | blinkDot(led); | ||
| + | break; | ||
| + | // -.. | ||
| + | case D: | ||
| + | blinkDash(led); | ||
| + | blinkDot(led); | ||
| + | blinkDot(led); | ||
| + | break; | ||
| + | // -.- | ||
| + | case K: | ||
| + | blinkDash(led); | ||
| + | blinkDot(led); | ||
| + | blinkDash(led); | ||
| + | break; | ||
| + | // --- | ||
| + | case O: | ||
| + | blinkDash(led); | ||
| + | blinkDash(led); | ||
| + | blinkDash(led); | ||
| + | break; | ||
| + | // --.- | ||
| + | case Q: | ||
| + | blinkDash(led); | ||
| + | blinkDash(led); | ||
| + | blinkDot(led); | ||
| + | blinkDash(led); | ||
| + | break; | ||
| + | // ... | ||
| + | case S: | ||
| + | blinkDot(led); | ||
| + | blinkDot(led); | ||
| + | blinkDot(led); | ||
| + | break; | ||
| + | } | ||
| + | sw_delay_ms(200); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | |||
| + | |||
| + | ===== Alternative solutions without library ===== | ||
| + | |||
| + | For all exercises: Project-> | ||
| + | * Frequency: 14745600 | ||
| + | * Optimization: | ||
| + | |||
| + | ==== Exercise 1 ==== | ||
| + | Pressing S1 lights up 1 LED, pressing S2 lights up 2 LEDs and pressing S3 lights up 3 LEDs. | ||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | Title: Lab 1.1 LEDs | ||
| + | Platform: Atmega128 & Digital i/o board v3 | ||
| + | Author: Raivo Sell | ||
| + | Date: 2008 | ||
| + | Comment: | ||
| + | LED = 0 (turned on) LED = 1 (turned off) | ||
| + | S = 0 (switch is on) S = 1 (switch is off) | ||
| + | PORT direction settings: 1-out 0-in | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | |||
| + | int main(void) { | ||
| + | |||
| + | DDRC = 0x38; // DDRC 0b00111000 | ||
| + | PORTC = 0x3F; // PORTC 0b00111111 | ||
| + | |||
| + | //Infinite loop | ||
| + | while(1) { | ||
| + | // If a button is pressed | ||
| + | if (bit_is_clear(PINC, | ||
| + | PORTC=0x30; | ||
| + | else if (bit_is_clear(PINC, | ||
| + | PORTC=0x20; | ||
| + | else if (bit_is_clear(PINC, | ||
| + | PORTC& | ||
| + | else //Nothing pressed | ||
| + | PORTC=0x3F; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Exercise 2 ==== | ||
| + | Imitates a traffic light on key press (blinking of the yellow and green lights etc.) | ||
| + | |||
| + | NB! Optimization must be turned off and the frequency defined. | ||
| + | |||
| + | <code c> | ||
| + | |||
| + | /* | ||
| + | Title: Lab 1.2 Traffic lights | ||
| + | Platform: Atmega128 & Digital i/o board v3 | ||
| + | Author: Raivo Sell | ||
| + | Date: 2006-2008 | ||
| + | Comment: Optimization -O0 | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | |||
| + | #define SET(x) |= (1<< | ||
| + | #define CLR(x) & | ||
| + | #define INV(x) ^=(1<< | ||
| + | |||
| + | // Empty cycle just to waste time | ||
| + | void aeg(int t) { | ||
| + | int i,j; | ||
| + | for (i=0; | ||
| + | for (j=0; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | // LED = 0 (on) LED = 1 (off) | ||
| + | // Switch on = 0 | ||
| + | |||
| + | int main(void) { | ||
| + | |||
| + | int z; | ||
| + | |||
| + | DDRC = 0x38; // PORTC three first bits are inputs, three next ports are outputs | ||
| + | PORTC |= 0x3F; // All LEDs off and button pull-ups on | ||
| + | |||
| + | //loops until S1 is pressed | ||
| + | while (bit_is_set(PINC, | ||
| + | PORTC INV(4); // Blink yellow | ||
| + | aeg(10); | ||
| + | } | ||
| + | PORTC SET(4); // Yellow off | ||
| + | |||
| + | //Infinite loop | ||
| + | while(1) { | ||
| + | |||
| + | |||
| + | PORTC CLR(3); // Green on | ||
| + | aeg(100); | ||
| + | for (z=0; | ||
| + | PORTC INV(3); // Blink green | ||
| + | aeg(10); | ||
| + | } | ||
| + | PORTC CLR(4); // Yellow on | ||
| + | aeg(50); | ||
| + | PORTC SET(4); // Yellow off | ||
| + | PORTC CLR(5); // Red on | ||
| + | aeg(100); | ||
| + | PORTC CLR(4); // Add yellow | ||
| + | aeg(50); | ||
| + | PORTC SET(4); // Yellow off | ||
| + | PORTC SET(5); // Red off | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Exercise 3 ==== | ||
| + | Counts the key presses (key press ends when the button is released) in binary - max 7 | ||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | Title: Lab 1.3 Binary counter | ||
| + | Platform: Atmega128 & Digital i/o board v3 | ||
| + | Author: Raivo Sell, Rain Ellermaa | ||
| + | Date: 2006-2008 | ||
| + | Comment: | ||
| + | */ | ||
| + | |||
| + | #define TRUE 1 | ||
| + | #define FALSE 0 | ||
| + | #define SET(x) |= (1<< | ||
| + | #define CLR(x) & | ||
| + | |||
| + | #define bit_get(p, | ||
| + | |||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | uint8_t button_debounce(); | ||
| + | uint8_t button_read(); | ||
| + | int i= 0; | ||
| + | |||
| + | uint8_t button_debounce(){ | ||
| + | | ||
| + | |||
| + | | ||
| + | if ( button_state == 0xF000 ) return TRUE; | ||
| + | | ||
| + | } | ||
| + | |||
| + | uint8_t button_read(){ | ||
| + | return bit_get( PINC , 1 ); | ||
| + | | ||
| + | |||
| + | int main(void) { | ||
| + | |||
| + | DDRC = 0x38; // DDRC 0b00111000 // PORTC three first bits are inputs, three next ports are outputs | ||
| + | PORTC |= 0x3F; // PORTC 0b00111000 // All LEDs off and button pull-ups on | ||
| + | unsigned char led; | ||
| + | |||
| + | while(1) { | ||
| + | if ( button_debounce() == TRUE ) i++; | ||
| + | _delay_ms( 1 ); | ||
| + | if (i>7) i=0; | ||
| + | led=~i; | ||
| + | led <<=3; | ||
| + | PORTC &= (0xC7 | led); | ||
| + | PORTC |= (0x38 & led); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Exercise 4 ==== | ||
| + | Simulates door code lock. When pressing switches in this order S3 – S2 - S1 green LED goes on. All other combination will end up red LED. Every button pressing is indicated with yellow LED. | ||
| + | |||
| + | //NOT COMPLETED !!!// | ||
| + | |||
| + | <code c> | ||
| + | /* | ||
| + | Title: Lab 1.4 Code lock | ||
| + | Platform: Atmega128 & Digital i/o board v3 | ||
| + | Author: Rain Ellermaa | ||
| + | Date: 2009 | ||
| + | Comment: | ||
| + | */ | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define LOCKED 1 | ||
| + | #define UNLOCKED 2 | ||
| + | |||
| + | uint8_t code[3]; | ||
| + | static uint8_t password[3] = {3,2,1}; | ||
| + | |||
| + | //Blink LED on " | ||
| + | static inline void Blink(uint8_t pin) | ||
| + | { | ||
| + | PORTC &= ~(1<< | ||
| + | _delay_ms(100); | ||
| + | PORTC |= (1<< | ||
| + | _delay_ms(100); | ||
| + | } | ||
| + | |||
| + | int Button(uint8_t pin) | ||
| + | { | ||
| + | /* button is on when BIT is low (0) */ | ||
| + | if ((PINC & (1<< | ||
| + | { | ||
| + | for(uint8_t i=0; | ||
| + | { | ||
| + | if ((PINC & (1<< | ||
| + | return 0; | ||
| + | _delay_ms(1); | ||
| + | } | ||
| + | } | ||
| + | else | ||
| + | return 0; | ||
| + | return 1; | ||
| + | } | ||
| + | |||
| + | int CheckPress() | ||
| + | { | ||
| + | if(Button(0)) //SW1 pressed | ||
| + | { | ||
| + | Blink(PC4); | ||
| + | _delay_ms(100); | ||
| + | return 1; | ||
| + | } | ||
| + | else if (Button(1)) //SW2 pressed | ||
| + | { | ||
| + | Blink(PC4); | ||
| + | _delay_ms(100); | ||
| + | return 2; | ||
| + | } | ||
| + | else if (Button(2)) //SW3 pressed | ||
| + | { | ||
| + | Blink(PC4); | ||
| + | _delay_ms(100); | ||
| + | return 3; | ||
| + | } | ||
| + | else | ||
| + | return 4; | ||
| + | } | ||
| + | |||
| + | //get the user input 3 digit code. | ||
| + | void GetCode() | ||
| + | { | ||
| + | for(uint8_t i=0; | ||
| + | { | ||
| + | while(1) | ||
| + | { | ||
| + | uint8_t temp = CheckPress(); | ||
| + | if(temp != 4) | ||
| + | { | ||
| + | code[i] = temp; | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | Blink(PC4); | ||
| + | } | ||
| + | |||
| + | //code to compare user inputted " | ||
| + | int CheckCode() | ||
| + | { | ||
| + | for(uint8_t i=0; | ||
| + | { | ||
| + | if(code[i] != password[i]) | ||
| + | return 0; | ||
| + | } | ||
| + | return 1; | ||
| + | } | ||
| + | |||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | DDRC = (1<< | ||
| + | PORTC = (1<< | ||
| + | |||
| + | uint8_t status = UNLOCKED; | ||
| + | |||
| + | while(1) | ||
| + | { | ||
| + | if((status == UNLOCKED) && (CheckPress() == 2)) | ||
| + | { | ||
| + | status = LOCKED; | ||
| + | |||
| + | } | ||
| + | else if(status == LOCKED) | ||
| + | { | ||
| + | PORTC &= ~(1<< | ||
| + | PORTC |= (1<< | ||
| + | GetCode(); | ||
| + | if(!CheckCode()) | ||
| + | { | ||
| + | PORTC &= ~(1<< | ||
| + | PORTC |= (1<< | ||
| + | status = LOCKED; | ||
| + | } | ||
| + | else if (CheckCode()) | ||
| + | { | ||
| + | PORTC &= ~(1<< | ||
| + | PORTC |= (1<< | ||
| + | status = UNLOCKED; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== Exercise 5 ==== | ||
| + | Simulates memory game. Every LED corresponds a button (LED1⇒S1, etc.) Controller flashed LEDs in random order and user have to repeat this order. Every next step in the sequence goes longer. After every insertion controller test the result. If wrong insertion is detected the game is over. | ||
| + | <code c> | ||
| + | /* | ||
| + | Title: Lab 1.5 Memory game | ||
| + | Platform: Atmega128 & Digital i/o board v3 | ||
| + | Author: Rain Ellermaa | ||
| + | Date: 2009 | ||
| + | Comment: | ||
| + | ===================================================== | ||
| + | ++ INSTRUCTIONS: | ||
| + | |||
| + | When the MCU powers up, all four lights will be active. | ||
| + | Press any of the four corresponding buttons to start | ||
| + | the game. Once the button is released, a single LED | ||
| + | will flash briefly - press the corresponding button. | ||
| + | |||
| + | All four LEDs will flash three times to indicate a | ||
| + | | ||
| + | by another random LED. Repeat the sequence, the LEDs | ||
| + | will all flash again and the sequence will begin again, | ||
| + | | ||
| + | at any point you make an error in repeating the sequence, | ||
| + | all four LEDs will turn on and you will need to reset | ||
| + | the MCU to begin another game. | ||
| + | | ||
| + | */ | ||
| + | |||
| + | // INCLUDES | ||
| + | #include < | ||
| + | #include < | ||
| + | #include " | ||
| + | #include " | ||
| + | |||
| + | // STATE DEFINES | ||
| + | #define STATE_Setup | ||
| + | #define STATE_PlayNextSeq | ||
| + | #define STATE_WaitForPlayer | ||
| + | #define STATE_CorrectSeq | ||
| + | #define STATE_LoseGame | ||
| + | #define STATE_WinGame | ||
| + | |||
| + | // MACROS | ||
| + | #define GetButton() (uint8_t)(~PINC & 7) | ||
| + | #define Timer0On() | ||
| + | |||
| + | |||
| + | // PROTOTYPES | ||
| + | uint8_t CreateTimerRand(void); | ||
| + | |||
| + | |||
| + | // PROGRAM ROUTINES | ||
| + | int main(void) | ||
| + | { | ||
| + | uint8_t CurrentState | ||
| + | uint8_t SequenceBuffer[20] = {}; | ||
| + | uint8_t CurrSeqPosC | ||
| + | uint8_t CurrentLevel | ||
| + | unsigned char echo[10]; | ||
| + | |||
| + | DDRC = (1<< | ||
| + | PORTC = (1<< | ||
| + | | ||
| + | lcd_clrscr(); | ||
| + | |||
| + | Timer0On(); | ||
| + | // Random sequence bytes; it's value is read after the last sequence byte is | ||
| + | // entered by the user. | ||
| + | |||
| + | while (1) // Infinite Loop | ||
| + | { | ||
| + | switch (CurrentState) | ||
| + | { | ||
| + | case STATE_Setup: | ||
| + | CurrentLevel = 1; // Reset current level variable | ||
| + | CurrSeqPosC | ||
| + | |||
| + | PORTC &= ~(7<< | ||
| + | |||
| + | while (!(GetButton())) // | ||
| + | ; | ||
| + | |||
| + | PORTC |= (7<< | ||
| + | |||
| + | SequenceBuffer[0] = CreateTimerRand(); | ||
| + | |||
| + | _delay_ms(400); | ||
| + | |||
| + | CurrentState = STATE_PlayNextSeq; | ||
| + | break; | ||
| + | |||
| + | case STATE_PlayNextSeq: | ||
| + | PORTC &= (~SequenceBuffer[CurrSeqPosC]<< | ||
| + | _delay_ms(200); | ||
| + | PORTC |= (SequenceBuffer[CurrSeqPosC]<< | ||
| + | _delay_ms(200); | ||
| + | |||
| + | if (++CurrSeqPosC == CurrentLevel) | ||
| + | { | ||
| + | CurrSeqPosC | ||
| + | CurrentState = STATE_WaitForPlayer; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | CurrentState = STATE_PlayNextSeq; | ||
| + | } | ||
| + | break; | ||
| + | |||
| + | case STATE_WaitForPlayer: | ||
| + | while (GetButton()) {}; // Wait until all buttons released before accepting key | ||
| + | |||
| + | uint8_t PressedButton = 0; | ||
| + | |||
| + | while (!(PressedButton)) | ||
| + | PressedButton = GetButton(); | ||
| + | |||
| + | PORTC &= (~PressedButton<< | ||
| + | _delay_ms(200); | ||
| + | PORTC |= (PressedButton<< | ||
| + | _delay_ms(200); | ||
| + | |||
| + | if (PressedButton == SequenceBuffer[CurrSeqPosC]) | ||
| + | { | ||
| + | if (++CurrSeqPosC == CurrentLevel) | ||
| + | { | ||
| + | CurrentLevel++; | ||
| + | CurrSeqPosC = 0; // Reset sequence position counter to 0 | ||
| + | |||
| + | if (CurrentLevel > 20) // A genious has completed the entire sequence | ||
| + | { | ||
| + | CurrentState = STATE_WinGame; | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | SequenceBuffer[CurrentLevel - 1] = CreateTimerRand(); | ||
| + | |||
| + | CurrentState = STATE_CorrectSeq; | ||
| + | } | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | CurrentState = STATE_WaitForPlayer; | ||
| + | } | ||
| + | } | ||
| + | else | ||
| + | { | ||
| + | CurrentState = STATE_LoseGame; | ||
| + | } | ||
| + | break; | ||
| + | |||
| + | case STATE_CorrectSeq: | ||
| + | for (uint8_t FlashCount=0; | ||
| + | { | ||
| + | PORTC &= ~(7<< | ||
| + | _delay_ms(100); | ||
| + | PORTC |= (7<< | ||
| + | _delay_ms(100); | ||
| + | } | ||
| + | |||
| + | CurrentState = STATE_PlayNextSeq; | ||
| + | break; | ||
| + | |||
| + | case STATE_LoseGame: | ||
| + | PORTC &= ~(7<< | ||
| + | // Put string to display (line 1) with linefeed | ||
| + | itoa(CurrentLevel-1, | ||
| + | lcd_gotoxy(0, | ||
| + | lcd_puts(" | ||
| + | lcd_gotoxy(0, | ||
| + | lcd_puts(echo); | ||
| + | |||
| + | CurrentState = STATE_LoseGame; | ||
| + | break; | ||
| + | |||
| + | case STATE_WinGame: | ||
| + | PORTC ^= (7<< | ||
| + | _delay_ms(200); | ||
| + | |||
| + | CurrentState = STATE_WinGame; | ||
| + | break; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | uint8_t CreateTimerRand(void) | ||
| + | { | ||
| + | uint8_t RVal = TCNT0; | ||
| + | |||
| + | // Priority encoder: Uses ordered tests to save code so that only | ||
| + | // the first matching test code is executed. | ||
| + | if (RVal <= 85) RVal = (1<< | ||
| + | else if (RVal <= 170) RVal = (1<< | ||
| + | else RVal = (1<< | ||
| + | |||
| + | return RVal; // Shift 1 by the new random number between 0 and 2, return the new sequence byte | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== Exercise 6 ==== | ||
| + | Measures reaction time. Program start when one button is pressed, then a LED goes on and user have to press a button under the LED. Sequence and time when LED goes on is random. Best result in milliseconds is presented to the user (on the 7-seg display or LCD). ({{: | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | #include " | ||
| + | |||
| + | |||
| + | volatile uint8_t ovf = 0; | ||
| + | |||
| + | //Timer 1 Overflow interrupt to indicate too slow input | ||
| + | ISR(TIMER1_OVF_vect) | ||
| + | { | ||
| + | ovf =1; | ||
| + | } | ||
| + | |||
| + | //Starts the Timer Counter unit | ||
| + | static inline void TimerStart() | ||
| + | { | ||
| + | TCNT1 = 0; | ||
| + | TCCR1B = (4<< | ||
| + | } | ||
| + | |||
| + | //Stops the Timer | ||
| + | static inline void TimerStop() | ||
| + | { | ||
| + | TCCR1B = 0; | ||
| + | } | ||
| + | |||
| + | //Blink all LEDs | ||
| + | static inline void Blink() | ||
| + | { | ||
| + | PORTC &= ~((1<< | ||
| + | _delay_ms(100); | ||
| + | PORTC |= (1<< | ||
| + | _delay_ms(100); | ||
| + | } | ||
| + | |||
| + | static inline int Button(uint8_t pin) | ||
| + | { | ||
| + | /* button is on when BIT is low (0) */ | ||
| + | if ((PINC & (1<< | ||
| + | { | ||
| + | for(uint8_t i=0; | ||
| + | { | ||
| + | if ((PINC & (1<< | ||
| + | return 0; | ||
| + | _delay_ms(1); | ||
| + | } | ||
| + | } | ||
| + | else | ||
| + | return 0; | ||
| + | return 1; | ||
| + | } | ||
| + | |||
| + | int CheckPress() | ||
| + | { | ||
| + | if(Button(0)) //SW1 pressed | ||
| + | { | ||
| + | Blink(); | ||
| + | _delay_ms(500); | ||
| + | return 1; | ||
| + | } | ||
| + | else if (Button(1)) //SW2 pressed | ||
| + | { | ||
| + | Blink(); | ||
| + | _delay_ms(500); | ||
| + | return 2; | ||
| + | } | ||
| + | else if (Button(2)) //SW3 pressed | ||
| + | { | ||
| + | Blink(); | ||
| + | _delay_ms(500); | ||
| + | return 3; | ||
| + | } | ||
| + | else | ||
| + | return 4; | ||
| + | } | ||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | DDRC = (1<< | ||
| + | PORTC = (1<< | ||
| + | |||
| + | TIMSK = (1<< | ||
| + | sei(); | ||
| + | |||
| + | // | ||
| + | lcd_init(LCD_DISP_ON); | ||
| + | lcd_clrscr(); | ||
| + | lcd_puts(" | ||
| + | _delay_ms(1000); | ||
| + | lcd_puts(" | ||
| + | |||
| + | uint16_t rng = 500; | ||
| + | uint16_t temp = 0; | ||
| + | |||
| + | while(1) | ||
| + | { | ||
| + | if(CheckPress()!=4) // | ||
| + | { | ||
| + | char buffer[6]; | ||
| + | rng = rng * 10; | ||
| + | lcd_clrscr(); | ||
| + | |||
| + | _delay_ms(rng); | ||
| + | // | ||
| + | |||
| + | if(rng < 2000) // | ||
| + | { | ||
| + | PORTC &= ~(1<< | ||
| + | TimerStart(); | ||
| + | while((PINC & (1<< | ||
| + | ; | ||
| + | TimerStop(); | ||
| + | } | ||
| + | else if((rng >= 2000)&& | ||
| + | { | ||
| + | PORTC &= ~(1<< | ||
| + | TimerStart(); | ||
| + | while((PINC & (1<< | ||
| + | ; | ||
| + | TimerStop(); | ||
| + | } | ||
| + | else if(rng >= 3500) | ||
| + | { | ||
| + | PORTC &= ~(1<< | ||
| + | TimerStart(); | ||
| + | while((PINC & (1<< | ||
| + | ; | ||
| + | TimerStop(); | ||
| + | } | ||
| + | |||
| + | lcd_clrscr(); | ||
| + | lcd_puts(" | ||
| + | |||
| + | if(ovf == 1) //if it took too long to input say so | ||
| + | { | ||
| + | lcd_puts(" | ||
| + | ovf = 0; | ||
| + | } | ||
| + | else // | ||
| + | { | ||
| + | temp = TCNT1 / 576 * 10 ; | ||
| + | itoa(temp, | ||
| + | lcd_puts(buffer); | ||
| + | } | ||
| + | lcd_puts(" | ||
| + | PORTC = (1<< | ||
| + | _delay_ms(500); | ||
| + | } | ||
| + | rng++; | ||
| + | |||
| + | if(rng > 500) | ||
| + | rng = 50; | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== Exercise 7 ==== | ||
| + | Flashes LEDs with different frequency using timer. Frequency can be changed by the buttons. (S1 – 1 Hz, S2 – 0,5 Hz, S3 – 0,1 Hz). | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | ISR(TIMER1_COMPA_vect) | ||
| + | { | ||
| + | PORTC ^= (1<< | ||
| + | } | ||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | DDRC = (1<< | ||
| + | PORTC = (1<< | ||
| + | |||
| + | // | ||
| + | TCCR1A = 0; | ||
| + | TCCR1B = (1<< | ||
| + | |||
| + | TIMSK = (1<< | ||
| + | sei(); | ||
| + | |||
| + | while(1) | ||
| + | { | ||
| + | if((PINC & 7) == 6) //SW1 pressed | ||
| + | { | ||
| + | OCR1A = 14400; | ||
| + | TCNT1 = 0; | ||
| + | TCCR1B |= (5<< | ||
| + | } | ||
| + | else if ((PINC & 7) == 5) //SW2 pressed | ||
| + | { | ||
| + | OCR1A = 7200; | ||
| + | TCNT1 = 0; | ||
| + | TCCR1B |= (5<< | ||
| + | } | ||
| + | else if ((PINC & 7) == 3) //SW3 pressed | ||
| + | { | ||
| + | OCR1A = 1440; | ||
| + | TCNT1 = 0; | ||
| + | TCCR1B |= (5<< | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== Exercise 8 ==== | ||
| + | Flashes LEDs with different frequency using timer. S1 increases flashing frequency and S3 decreases. | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | |||
| + | ISR(TIMER1_COMPA_vect) | ||
| + | { | ||
| + | PORTC ^= (1<< | ||
| + | } | ||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | uint16_t freq; | ||
| + | |||
| + | DDRC = (1<< | ||
| + | PORTC = (1<< | ||
| + | |||
| + | // | ||
| + | TCCR1A = 0; | ||
| + | TCCR1B = (1<< | ||
| + | OCR1A = 1000; | ||
| + | freq = 1000; | ||
| + | |||
| + | TIMSK = (1<< | ||
| + | |||
| + | sei(); | ||
| + | |||
| + | while(1) | ||
| + | { | ||
| + | if((PINC & 7) == 6) //SW1 pressed | ||
| + | { | ||
| + | if (freq > 100) | ||
| + | freq = OCR1A - 100; | ||
| + | else | ||
| + | freq = 100; | ||
| + | OCR1A = freq; | ||
| + | TCNT1 = 0; | ||
| + | } | ||
| + | else if ((PINC & 7) == 3) //SW3 pressed | ||
| + | { | ||
| + | if (freq < 20000) | ||
| + | freq = OCR1A + 100; | ||
| + | else | ||
| + | freq = 20000; | ||
| + | |||
| + | OCR1A = freq; | ||
| + | TCNT1 = 0; | ||
| + | } | ||
| + | _delay_ms(100); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== Exercise 9 ==== | ||
| + | Counts numbers on 7-seg display from 1 to 9. If button S1 is pressed the counting starts to go backward. If S3 is pressed the counting resumes to go forward. Digit change frequency is 1 second. | ||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define FORWARDS 0 | ||
| + | #define BACKWARDS 1 | ||
| + | |||
| + | //global variables | ||
| + | volatile uint8_t direction = FORWARDS; | ||
| + | volatile uint8_t num = 0; | ||
| + | |||
| + | //function declarations | ||
| + | void WriteDisplay(uint8_t); | ||
| + | int main(); | ||
| + | |||
| + | |||
| + | //Timer 1 interrupt (every 1 second) | ||
| + | ISR(TIMER1_COMPA_vect) | ||
| + | { | ||
| + | if((direction == FORWARDS) && (num < 9)) //count up until 9 | ||
| + | num++; | ||
| + | else if ((direction == BACKWARDS) && (num > 0)) //count down until 0 | ||
| + | num--; | ||
| + | WriteDisplay(num); | ||
| + | } | ||
| + | |||
| + | void WriteDisplay (uint8_t digit) | ||
| + | { | ||
| + | volatile uint8_t nr; | ||
| + | switch (digit) //table read to get segment map representation of the digit | ||
| + | { | ||
| + | case 0 : {nr = 0b00111111; | ||
| + | case 1 : {nr = 0b00000110; | ||
| + | case 2 : {nr = 0b01011011; | ||
| + | case 3 : {nr = 0b01001111; | ||
| + | case 4 : {nr = 0b01100110; | ||
| + | case 5 : {nr = 0b01101101; | ||
| + | case 6 : {nr = 0b01111100; | ||
| + | case 7 : {nr = 0b00000111; | ||
| + | case 8 : {nr = 0b01111111; | ||
| + | case 9 : {nr = 0b01100111; | ||
| + | default: {nr = 0b01111001; | ||
| + | } | ||
| + | |||
| + | PORTG &= ~(1<< | ||
| + | for(uint8_t i=8; i>0; i--) //for every bit in the byte | ||
| + | { | ||
| + | //if bit is set, sets the data out pin | ||
| + | if (nr & (1<< | ||
| + | PORTC |= (1<< | ||
| + | else | ||
| + | PORTC &= ~(1<< | ||
| + | |||
| + | PORTC |= (1<< | ||
| + | _delay_us(10); | ||
| + | |||
| + | PORTC &= ~(1<< | ||
| + | _delay_us(10); | ||
| + | } | ||
| + | PORTG |= (1<< | ||
| + | } | ||
| + | |||
| + | int main(void) | ||
| + | { | ||
| + | DDRC = (1<< | ||
| + | DDRG = (1<< | ||
| + | |||
| + | PORTC = (1<< | ||
| + | |||
| + | // | ||
| + | TCCR1A = 0; | ||
| + | TCCR1B = (1<< | ||
| + | |||
| + | TIMSK = (1<< | ||
| + | OCR1A = 14400; | ||
| + | |||
| + | sei(); | ||
| + | |||
| + | while(1) | ||
| + | { | ||
| + | if((PINC & 7) == 6) //SW1 pressed | ||
| + | { | ||
| + | direction = BACKWARDS; | ||
| + | } | ||
| + | else if ((PINC & 7) == 3) //SW3 pressed | ||
| + | { | ||
| + | direction = FORWARDS; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | </ | ||
| + | |||
| + | ~~DISCUSSION~~ | ||