This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| fr:examples:digi:io [2009/03/25 23:07] – sdeniaud | fr:examples:digi:io [2020/07/20 12:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Digit E/S avec la Carte d' | ||
| + | {{: | ||
| + | |||
| + | La Carte d' | ||
| + | Les LED sont connectées aux PORTC[3..5] et les switches aux PORTC[0..2]. | ||
| + | |||
| + | Cet exemple simple montre comment basculer sur les LED quand on appuie sur le switch approprié. | ||
| + | |||
| + | ==== Schéma de connexion ==== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | ==== Exemple de code C ==== | ||
| + | |||
| + | <code c> | ||
| + | /* Labor 1 example | ||
| + | |||
| + | Description: | ||
| + | Press S1 - LED1 goes ON; S2 - LED2 goes ON; S3 - LED3 goes ON | ||
| + | LED = 0 (ON) LED = 1 (OFF) | ||
| + | S= 0 (ON) S= 1 (OFF) | ||
| + | PORT direction: 1-output 0-input | ||
| + | |||
| + | Author: Raivo Sell 2008 | ||
| + | */ | ||
| + | |||
| + | #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; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Exemple (version courte) ==== | ||
| + | |||
| + | |||
| + | <code c> | ||
| + | /* Lab 1 example | ||
| + | Author: Maido Hiiemaa | ||
| + | Date: 2009 | ||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | |||
| + | int main(void) { | ||
| + | | ||
| + | | ||
| + | PORTC &= ~((PINC << 3) | 0xC7 ); // zeros | ||
| + | PORTC |= ((PINC << 3) & ~0xC7 ); // ones | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Elimination du // | ||
| + | |||
| + | Source : [[http:// | ||
| + | |||
| + | <code c> | ||
| + | #include < | ||
| + | #include < | ||
| + | |||
| + | #define BUTTON_PORT PORTC /* PORTx - nupu register */ | ||
| + | #define BUTTON_PIN PINC /* PINx - nupu sisendi register */ | ||
| + | #define BUTTON_BIT PC0 /* nupu sisend/ | ||
| + | |||
| + | int button_is_pressed() | ||
| + | { | ||
| + | /* 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 | ||
| + | } | ||
| + | } | ||
| + | </ | ||