Well I am programming a micro (ATmega328p) using C for a school project. The thing is I am trying to represent this truth table (A,B and C are just inputs, buttons to be more specific) and XOR is the output on a LED, there are other 3 LEDs but they are doing a different gate and right now they are working OK.

I am having problem with this specific part because the XOR is not working, it is supposed to be off when 2 buttons are pressed but is not. It just is only Off when all the buttons are not pressed:
if ( ((BTS & _BV(BT1)) ^ (BTS & _BV(BT2))) || ((BTS & _BV(BT2)) ^ (BTS & _BV(BT3)))) //EXOR
{
LEDS |= _BV(LED2); //Set 1 on LED2
}
else
{
LEDS &= ~_BV(LED2); //Set 0 on LED2
}
I'll put the full code here just to reference.
#include <avr/io.h>
#define F_CPU 16000000UL
#include <util/delay.h>
#define delay 500
//--Tags
//-Inputs
#define BTS PINB
#define BT1 PINB0
#define BT2 PINB1
#define BT3 PINB2
//-Outputs
#define LEDS PORTD
#define LED0 PORTD2
#define LED1 PORTD3
#define LED2 PORTD4
#define LED3 PORTD5
void init_ports(void);
int main(void)
{
init_ports();
while (1)
{
//Output 1
if ((BTS & _BV(BT1)) && (BTS & _BV(BT2)) && (BTS & _BV(BT3))) // AND
{
LEDS |= _BV(LED0); //Set 1 on LED0
}
else
{
LEDS &= ~ _BV(LED0); //Set 0 on LED0
}
//Output 2
if ((BTS & _BV(BT1)) || (BTS & _BV(BT2)) || (BTS & _BV(BT3))) // OR
{
LEDS |= _BV(LED1); //Set 1 on LED1
}
else
{
LEDS &= ~ _BV(LED1); //Set 0 on LED1
}
//Output 3
if ( ((BTS & _BV(BT1)) ^ (BTS & _BV(BT2))) || ((BTS & _BV(BT2)) ^ (BTS & _BV(BT3)))) //EXOR
{
LEDS |= _BV(LED2); //Set 1 on LED2
}
else
{
LEDS &= ~_BV(LED2); //Set 0 on LED2
}
//Output 4
if (!((BTS & _BV(BT1)) && (BTS & _BV(BT2)) && (BTS & _BV(BT3)))) // NOR
{
LEDS |= _BV(LED3); //Set 1 on LED3
}
else
{
LEDS &= ~_BV(LED3); //Set 0 on LED3
}
}
}
void init_ports (void)
{
//--Inputs
DDRB &= ~(_BV(BT1) | _BV(BT2) | _BV(BT3));
//-PULL-UP
PORTB &= ~(_BV(BT1) | _BV(BT2) | _BV(BT3));
//--Outputs
DDRD |= (_BV(LED0) | _BV(LED1) | _BV(LED2) | _BV(LED3));
//-Off
PORTD &= ~(_BV(LED0) | _BV(LED1) | _BV(LED2) | _BV(LED3));
}
Any ideas of what could be wrong? TY!! :D
I tried using this boolean expression y = A'B'C + A'BC' + AB'C' + ABC
Write some code to construct a truth table using your boolean expression and compare it to the table given in the assignment. Do they match? If not, think carefully about the truth table you were given and construct a boolean expression that will match. You can also search Google for something called a Karnaugh map, which can help you construct an expression from a truth table.