Aufgabenstellung

Bild1

Lies die zwei Tasten ein und gib den eingelesenen Code als 1 aus 4 Code aus

IN: Port B    Pin 7 und Pin 6

OUT: Port C  .. Pin 3..0

Theorie

  • Tasten brauchen interne Pullups

  • Taste-Gedrückt-> Pegel = 0

  • Keine Taste gedrückt →Output = 1000; Beide Tasten gedrückt → 0001

Achtung!   Beim Setzen des unteren Nibbles am Ausgang darf das obere Nibble nicht verändert werden.

 

 

/*
 * bin_1aus4.cpp    Buttons on PB7/PB6 as binary input => 1-of-4 code on PC3..PC0 as output
 e.g.: both buttons pressed = pattern 00 => 0001 as output

       no button pressed = pattern 11 => 1000 as output
 * Author: kner
 *
 */

#include <avr/io.h>
void setup();
int main(void)
{   setup();
      while(1)
  {
      uint8_t in = PINB & 0b11000000; //mask out the upper 2 bits
      in = in >> 6;                   // get bin code
      uint8_t out = 1 << in;          // calculate the output
      PORTC |= out; // do not change rest of pattern on port C
      uint8_t mask = (out | 0b11110000);
      PORTC &= mask;                  // set the zeros
  }
}
void setup(){
      DDRB =0b00000000; //inputs
      PORTB=0b11111111; //pullups
      DDRC =0b00001111; //outputs
      PORTC=0b00001000; // no key pressed  => code 11 => 1000 as output
}