/*
* andGate.cpp
* emulate a and-gate;
in1 = PB4, in2 = PB5, Q = PB0
*/
#include <avr/io.h>
#define PORT PORTB
#define DDR DDRB
#define IN1PIN 5
#define IN2PIN 4
#define QPIN 0
typedef enum {UNDEFINED, HIGH,LOW} logic_t;
void setup();
logic_t AND(logic_t in1, logic_t in2){
logic_t h;
if (in1 == UNDEFINED || in2 == UNDEFINED) h = UNDEFINED;
else if (in1 == HIGH && in2 == HIGH) h = HIGH;
else h= LOW;
return h;
}
int main(void)
{ logic_t in1 = UNDEFINED;
logic_t in2 = UNDEFINED;
logic_t q = UNDEFINED;
setup();
while(1)
{
//read input
uint8_t in = PINB;
in1 = in & (1<<IN1PIN)?HIGH:LOW;
in2 = in & (1<<IN2PIN)?HIGH:LOW;
q = AND(in1,in2);
switch (q){
case HIGH: PORT |= (1<<QPIN); break;
case LOW : PORT &= ~(1<<QPIN); break;
default :PORTD |= (1<<5); //error
}
}
}
void setup(){
DDR = 0b00000001; //PB7 as output
PORT = 0b11111110; //PB7=0; Pullups active on PB6..PB0
DDRD |= 1<<5; //PD5 red error LED
PORTD &=~(1<<5);
}