I got a simple hardware project with simple task: I have to turn on LED buttons with responsive regular buttons, using ATMega32 microcontroller. As an IDE we use Proteus 8.6 with C as programming language. I could realise turning on/off first button, but the problem is that the similiar method does not work with other buttons (you can see it on the first image - first two buttons are pressed, but only first LED is on). I don`t know if the problem is in schema or the code. If you have any idea how to implement it or even simplify the bit logic without 8 "if"-blocks, I would be really thankfull!
Here are images of schema and the code: Schema C Code
Solved (with one line of code, PORTC - output for LED buttons, PIND - value of input buttons):
PORTC = PIND;
Despite this being marked 'solved' I would like to point out that this line has a problem:
if (PIND & 1 << PD0 == 1)
In fact, it has 2 problems: the condition, and operator precedence.
What you seem to want to do, is to check if a certain bit is on. What the code actually does, is this:
if
block if the result of step 3 is not 0.To actually check if a certain bit is set, you need a condition like this:
if (PIND & (1 << PD0)) { ... }
This uses the fact that C evaluates any non-zero value as true.
To make the code more explicitly state the intent, you could write it like this:
if ((PIND & (1 << PD0)) > 0) { ... }
And a comment on the hardware side of things:
Looking at your diagram (schema) you do not seem to have pullup or pulldown resistors on your buttons.
Make sure to either activate the built in pullups on your microcontroller inputs, or add external resistors. (In the first case, you might need to reverse the logic so your buttons are active low).
If you do not do this, when buttons are not pressed the inputs will be "floating" which means they may randomly give "button pressed" signal.