-1
\$\begingroup\$

I am new to microcontroller and just become confused 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 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 switch 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
\$\endgroup\$
5
  • 2
    \$\begingroup\$ Can you give us a diagram showing the relevant connections between the ATmega32 and the switches/LEDs? \$\endgroup\$ Commented Jan 30, 2016 at 20:25
  • \$\begingroup\$ I don't have any diagram. But I have edited the question. You can check @Robherc KV5ROB \$\endgroup\$ Commented Jan 30, 2016 at 20:33
  • 1
    \$\begingroup\$ This would be better in the Arduino forum \$\endgroup\$ Commented Jan 30, 2016 at 20:40
  • \$\begingroup\$ can you please provide me the link of the forum? @Matt \$\endgroup\$ Commented Jan 30, 2016 at 20:41
  • 1
    \$\begingroup\$ arduino.stackexchange.com But don't repost, one of the mod's will move it over. If you have a quick flick through you may even find the answer. \$\endgroup\$ Commented Jan 30, 2016 at 20:45

2 Answers 2

2
\$\begingroup\$

In the code you need to define the ports that are receiving input as input ports and the others as output. You then need to poll each switch and write the value you receive (0 or 1) to the corresponding LED.

You will have problems with the switches bouncing, but that's a whole other question :)

\$\endgroup\$
6
  • \$\begingroup\$ I've assumed you are using the Arduino IDE, in which case look at Examples>Basics>Blink and Examples>Basics>Button and you should be able to bolt those together. As always, start simple and work up, start with one led, add a switch, double it... \$\endgroup\$ Commented Jan 30, 2016 at 20:47
  • \$\begingroup\$ I am using Atmega @Matt \$\endgroup\$ Commented Jan 30, 2016 at 20:53
  • \$\begingroup\$ can you please tell me when multiple switches are turned on, which value I will assign to port A? what will be the logic? @Matt \$\endgroup\$ Commented Jan 30, 2016 at 20:54
  • \$\begingroup\$ Can I just check that when you press switch 0 you will read 1 on port A? If that's the case then won't 16 or 0x10 be the value you need to write? If so all you need to do is shift the bit pattern you read by 4 places. \$\endgroup\$ Commented Jan 30, 2016 at 21:06
  • \$\begingroup\$ then how will i check the input to see which switched has been pressed? @Matt \$\endgroup\$ Commented Jan 30, 2016 at 21:12
0
\$\begingroup\$

perhaps something like this:

   LDI R1 240
   OUT DDRA R1
busyloop:
   IN   R1 PINA
   SWAP R1
   OR   15 R1
   OUT  PoRTA R1
   RJMP busyloop
\$\endgroup\$

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.