0

I am new to microcontrollers and just become confused trying to solve the following problem:

An ATmega32 chip is connected to four on/off switches (SW0-SW3) and 4 LEDs (LED0-LED3). SWi is connected to PAi. LEDi is connected to PA(4+i).

How will I code in such a way that turning a switch on, turns the corresponding LED on? For example, turning SW1 on will turn LED1 on. I am confused in the part that after taking input what will I send to port A? Multiple switches can be switched on at the same time.

For clarification:

switch0 is connected to PA0
switch1 is connected to PA1
switch2 is connected to PA2
switch3 is connected to PA3

LED0 is connected to PA4
LED1 is connected to PA5
LED2 is connected to PA6
LED3 is connected to PA7
1
  • 1
    Do you know that the ATMega328 doesn't have a port A? Commented Jan 31, 2016 at 16:46

1 Answer 1

1

A common approach is, to read the input pin, check the read value and then write to the output pin. As the events on the input pin are generally asynchronous to your code, you have to check it more than once, which means in fact you have to check it regularily.

So in your arduino sketch you could write:

loop()
{
  uint8_t pin1=digitalRead(PA0);
  if (pin1==HIGH)
  {
     digitalWrite(PA4, HIGH);
  }
  uint8_t pin2=digitalRead(PA1);
  if (pin2==HIGH)
  {
     digitalWrite(PA5, HIGH);
  }
  // and so on
}

I assume you have already done the prerequisites in your setup() routine.

3
  • digitalWrite(PA5, ...);? Commented Jan 31, 2016 at 14:13
  • What about and PA4? q-: Commented Jan 31, 2016 at 15:14
  • Only one thing at a time ... Commented Jan 31, 2016 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.