====== Digit E/S avec la Carte d'Etude ====== {{:et:naited:digi:io.jpg?300|}} La Carte d'Etude "Basique" comprend 3 sorties (LED) et 3 entrées (microswitch). 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 ==== {{:et:harjutused:digi:1a_skeem.jpg?500|}} ==== Exemple de code C ==== /* Labor 1 example Description: Example works with Studyboard Basic v3 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 //Definitions for easier bit manipulations #define SET(x) |= (1< ==== Exemple (version courte) ==== /* Lab 1 example Author: Maido Hiiemaa Date: 2009 */ #include int main(void) { DDRC = 0x38; // (00111000) while(1) { PORTC &= ~((PINC << 3) | 0xC7 ); // zeros PORTC |= ((PINC << 3) & ~0xC7 ); // ones } } ==== Elimination du //bouncing// ==== Source : [[http://www.micahcarrick.com/05-15-2006/avr-tutorial-switch-debounce.html|tutoriel de Micah Carrick]] #include #include #define BUTTON_PORT PORTC /* PORTx - nupu register */ #define BUTTON_PIN PINC /* PINx - nupu sisendi register */ #define BUTTON_BIT PC0 /* nupu sisend/väljund bit */ int button_is_pressed() { /* button is on when BIT is low (0) */ if (bit_is_clear(BUTTON_PIN, BUTTON_BIT)) { _delay_ms(25); /* de-bouncing delay time */ if (bit_is_clear(BUTTON_PIN, BUTTON_BIT)) return 1; } return 0; } int main (void){ /* switch on internel pull-ups */ BUTTON_PORT |= _BV(BUTTON_BIT); if (button_is_pressed()){ // do somthing // wait if needed } }