This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:digi:io [2009/03/23 14:42] – raivo.sell | en:examples:digi:io [2010/02/04 12:28] (current) – removed mikk.leini | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Digito i/o with Studyboard ====== | ||
| - | {{: | ||
| - | |||
| - | Studyboard ‚Basic’ includes three outputs (LED) and three inputs (microswitch) | ||
| - | LEDs are connected to PORTC[3..5] and switches PORTC[0..2]. | ||
| - | |||
| - | This simple examples demonstrates how to switch on LED when pushing appropriate switch. | ||
| - | |||
| - | ==== Connection schema: ==== | ||
| - | |||
| - | {{: | ||
| - | |||
| - | ==== Example C code: ==== | ||
| - | |||
| - | <code c> | ||
| - | /* Labor 1 example | ||
| - | Example works with Studyboard Basic v3 | ||
| - | |||
| - | Press S1 - LED1 goes ON | ||
| - | Press S2 - LED2 goes ON | ||
| - | Press S31 - LED3 goes ON | ||
| - | Raivo Sell 2008 | ||
| - | |||
| - | LED = 0 (ON) LED = 1 (OFF) | ||
| - | S = 0 (ON) S = 1 (OFF) | ||
| - | |||
| - | PORT direction: 1-output 0-input */ | ||
| - | |||
| - | #include < | ||
| - | |||
| - | // | ||
| - | #define SET(x) |= (1<< | ||
| - | #define CLR(x) & | ||
| - | |||
| - | int main(void) { //main program | ||
| - | |||
| - | DDRC = 0x38; // DDRC 0bXX111000 | ||
| - | PORTC = 0x3F; // PORTC 0bXX111111 | ||
| - | |||
| - | //endless loop | ||
| - | while(1) { | ||
| - | // if button is pressed (pin is low) | ||
| - | if (bit_is_clear(PINC, | ||
| - | PORTC CLR(3); //LED 1 ON | ||
| - | else if (bit_is_clear(PINC, | ||
| - | PORTC CLR(4); //LED 2 ON | ||
| - | else if (bit_is_clear(PINC, | ||
| - | PORTC CLR(5); //LED 3 ON | ||
| - | else // If nothing is pressed | ||
| - | PORTC=0xFF; | ||
| - | } | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | ==== Example (short version) ==== | ||
| - | |||
| - | |||
| - | <code c> | ||
| - | /* Lab 1 example | ||
| - | Author: Mikael Hellgren | ||
| - | Date: 080429 | ||
| - | */ | ||
| - | |||
| - | #include < | ||
| - | |||
| - | int main(void) { | ||
| - | unsigned char temp; | ||
| - | DDRC = 0x38; // DDRC 0bXX111000 | ||
| - | PORTC = 0x3F; // PORTC 0bXX111111 | ||
| - | |||
| - | while(1) { | ||
| - | temp=PINC; | ||
| - | temp& | ||
| - | temp<< | ||
| - | PORTC &= (0xC7 | temp); | ||
| - | PORTC |= (0x38 & temp); | ||
| - | } | ||
| - | } | ||
| - | |||
| - | </ | ||
| - | |||
| - | |||
| - | ==== Elliminating bouncing ==== | ||
| - | |||
| - | Source: [[http:// | ||
| - | |||
| - | <code c> | ||
| - | |||
| - | #define BUTTON_PORT PORTC /* PORTx - nupu register */ | ||
| - | #define BUTTON_PIN PINC /* PINx - nupu sisendi register */ | ||
| - | #define BUTTON_BIT PC0 /* nupu sisend/ | ||
| - | |||
| - | #include < | ||
| - | #include < | ||
| - | |||
| - | int nupuvajutus() | ||
| - | { | ||
| - | /* button is on when BIT is low (0) */ | ||
| - | if (bit_is_clear(BUTTON_PIN, | ||
| - | { | ||
| - | _delay_ms(25); | ||
| - | if (bit_is_clear(BUTTON_PIN, | ||
| - | } | ||
| - | |||
| - | return 0; | ||
| - | } | ||
| - | |||
| - | int main (void){ | ||
| - | |||
| - | /* switch on internel pull-ups */ | ||
| - | BUTTON_PORT |= _BV(BUTTON_BIT) | ||
| - | if (button_is_pressed()){ | ||
| - | // do somthing | ||
| - | // wait if needed | ||
| - | } | ||
| - | } | ||
| - | </ | ||