====== L1 Digital ====== Comment: Partial translation is needed! ====== Digital input/output ====== ==== Warm-Up ==== Lights up: * pressing S1 1 LEDs, * pressing S2 2 LEDs, * pressing S3 3 LEDs // // HomeLab User interface module // Warm-up exercise // #include #include pin led_red = PIN(C, 5); pin led_yellow = PIN(C, 4); pin led_green = PIN(C, 3); 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. // // Traffic light // #include #include pin led_red = PIN(C, 5); pin led_yellow = PIN(C, 4); pin led_green = PIN(C, 3); 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. // // Nupuvajutusi loendav programm // Väljund binaarkoodis, maksimaalselt 7 // #include #include pin led_red = PIN(C, 5); pin led_yellow = PIN(C, 4); pin led_green = PIN(C, 3); 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). // // 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< ==== 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, mille parameetriks on vilkumiste arv. // // 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“. // // 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(C, 5); pin led_yellow = PIN(C, 4); pin led_green = PIN(C, 3); 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, led_red); blinkChar(O, led_red); blinkChar(S, led_red); } // Vilgutab CQD kollase LEDiga void blinkCqd(void) { blinkChar(C, led_yellow); blinkChar(Q, led_yellow); blinkChar(D, led_yellow); } // Vilgutab OK rohelise LEDiga void blinkOk(void) { blinkChar(O, led_green); blinkChar(K, led_green); } // Vilgutab parameetris määratud LEDiga punkti void blinkDot(pin led) { pin_clear(led); sw_delay_ms(100); // Standardse pikkusega punkt pin_set(led); sw_delay_ms(100); // Märgile järgnev paus } // Vilgutab parameetris määratud LEDiga kriipsu void blinkDash(pin led) { pin_clear(led); sw_delay_ms(300); // Standardse pikkusega kriips pin_set(led); sw_delay_ms(100); // Märgile järgnev paus } // Vilgutab eeldefineeritud mustri järgi // parameetris 'c' olevat tähte parameetris 'led' oleval LEDil 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); // Lisapaus peale tähte } ===== Alternative solutions without library ===== For all exercises: Project->Configuration Options * Frequency: 14745600 * Optimization: -Os (if not said otherwise in exercise comment) ==== Exercise 1 ==== Pressing S1 lights up 1 LED, pressing S2 lights up 2 LEDs and pressing S3 lights up 3 LEDs. /* 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, 0)) //Button S1 PORTC=0x30; //LED 1 on - 0b00110000 else if (bit_is_clear(PINC, 1))//Button S2 PORTC=0x20; //LED 1&2 on - 0b00100000 else if (bit_is_clear(PINC, 2))//Button S3 PORTC&=0x00; //LED 1-3 on - 0b00000000 else //Nothing pressed PORTC=0x3F; // All LEDs off } } ==== 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. /* 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< ==== Exercise 3 ==== Counts the key presses (key press ends when the button is released) in binary - max 7 /* 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< #include #include uint8_t button_debounce(); uint8_t button_read(); int i= 0; uint8_t button_debounce(){ static uint16_t button_state = 0; button_state = ( (button_state << 1) | !button_read() | 0xE000 ); if ( button_state == 0xF000 ) return TRUE; return FALSE; } 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 !!!// /* 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 "pin" static inline void Blink(uint8_t pin) { PORTC &= ~(1< ==== 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. /* 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 correct entry. The same LED will again light, followed by another random LED. Repeat the sequence, the LEDs will all flash again and the sequence will begin again, getting longer by one LED after each correct entry. If 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 "lcd.h" #include "lcd.c" // STATE DEFINES #define STATE_Setup 0 #define STATE_PlayNextSeq 1 #define STATE_WaitForPlayer 2 #define STATE_CorrectSeq 3 #define STATE_LoseGame 4 #define STATE_WinGame 5 // MACROS #define GetButton() (uint8_t)(~PINC & 7) #define Timer0On() TCCR0 |= (1< 20) // A genious has completed the entire sequence { CurrentState = STATE_WinGame; } else // Still more room in the buffer, create a new random byte and set the state accordingly { SequenceBuffer[CurrentLevel - 1] = CreateTimerRand(); // Create the next sequence byte from the timer CurrentState = STATE_CorrectSeq; } } else { CurrentState = STATE_WaitForPlayer; } } else { CurrentState = STATE_LoseGame; } break; case STATE_CorrectSeq: for (uint8_t FlashCount=0; FlashCount<3; FlashCount++) // Flash the LEDs three times { PORTC &= ~(7< ==== 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). ({{:software:lcd.c|lcd.c}} {{:software:lcd.h|lcd.h}}) #include #include #include #include #include "lcd.h" 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<= 2000)&&(rng < 3500)) { PORTC &= ~(1<= 3500) { PORTC &= ~(1< 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). #include #include ISR(TIMER1_COMPA_vect) { PORTC ^= (1< ==== Exercise 8 ==== Flashes LEDs with different frequency using timer. S1 increases flashing frequency and S3 decreases. #include #include #include ISR(TIMER1_COMPA_vect) { PORTC ^= (1< 100) freq = OCR1A - 100; else freq = 100; OCR1A = freq; //increase frequency TCNT1 = 0; } else if ((PINC & 7) == 3) //SW3 pressed { if (freq < 20000) freq = OCR1A + 100; else freq = 20000; OCR1A = freq; //decrease frequency 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. #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;break;} //every bit corresponds to one segment case 1 : {nr = 0b00000110;break;} //"1" case 2 : {nr = 0b01011011;break;} //"2" and so on case 3 : {nr = 0b01001111;break;} case 4 : {nr = 0b01100110;break;} case 5 : {nr = 0b01101101;break;} case 6 : {nr = 0b01111100;break;} case 7 : {nr = 0b00000111;break;} case 8 : {nr = 0b01111111;break;} case 9 : {nr = 0b01100111;break;} default: {nr = 0b01111001;}//E } PORTG &= ~(1<0; i--) //for every bit in the byte { //if bit is set, sets the data out pin if (nr & (1<<(i-1))) PORTC |= (1< ~~DISCUSSION~~