Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
de:examples:digi:io [2009/04/27 17:52] nierhoffde:examples:digi:io [2020/07/20 12:00] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +====== Input/Output am Study Board ======
 +{{:et:naited:digi:io.jpg?300|}}
 +
 +Study Board ‚Basic’ verfügt über drei Outputs (LEDs, verbunden mit PORT-C[3..5]) und drei Inputs (Mikroschalter an PORT-C[0..2]).
 +
 +Dieses Beispiel zeigt wie die LEDs beim Drücken des entsprechenden Schalters aktiviert werden.
 +
 +
 +==== Schaltplan: ====
 +
 +{{:et:harjutused:digi:1a_skeem.jpg?500|}}
 +
 +==== Beispiel C Code: ====
 +
 +<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 <avr/io.h>
 +
 +#define F_CPU 14745600UL
 +//Definitions for easier bit manipulations
 +#define SET(x) |= (1<<x)  //bit high in port x
 +#define CLR(x) &=~(1<<x)  //bit low in port 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, 0)) //Switch S1
 + PORTC CLR(3); //LED 1 ON
 + else if (bit_is_clear(PINC, 1))//Switch S2
 + PORTC CLR(4); //LED 2 ON
 + else if (bit_is_clear(PINC, 2))//Switch S3
 + PORTC CLR(5); //LED 3 ON
 + else // If nothing is pressed
 + PORTC=0xFF; //All LEDs OFF (LED=1)
 + }
 +}
 +</code>
 +
 +==== Beispiel (Kurzversion) ====
 +
 +
 +<code c>
 +/* Lab 1 example
 +Author: Maido Hiiemaa
 +Date: 2009
 +*/
 +
 +#include <avr/io.h>
 +
 +int main(void) {
 +     DDRC  = 0x38;  //  (00111000)
 +     while(1) {
 +        PORTC &= ~((PINC << 3) | 0xC7 ); // zeros
 + PORTC |= ((PINC << 3) & ~0xC7 ); // ones
 +        }
 +}
 +</code>
 +
 +
 +==== Elliminating bouncing ====
 +
 +Source: [[http://www.micahcarrick.com/05-15-2006/avr-tutorial-switch-debounce.html|Micah Carrick tutorial]]
 +
 +<code c>
 +#include <avr/io.h>
 +#include <util/delay.h>
 +
 +#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
 +        }
 +}
 +</code>
  
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0