Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Will this code work propertly?

Status
Not open for further replies.

Beginner0001

New Member
I have 4 switches and 4 LEDs. I want to turn off the first LED with the first switch, the second one with the second switch, and so on. When I turn off the switch, LED should turn on. All switches connected to PORTA, all LEDs to PORTB. Initially, all LEDs are on. Will this code do the job?
Code:
PORTB=0b00001111;
   
    if (PINA & (1<<n))
    {
        PORTB = PORTB & ~(1 << n)
    }
    else
    {
        PORTB = PORTB | (1 << n);
    }
 
It will work if,
The TRIS registers are setup correctly.
Pullup resistors are on port A or the internal pullups are on. And, the switch connects the port pin to GND.
Analogue is turned off.
Anything else (osc etc.) required is setup correctly.

Mike.
 
The TRIS registers are setup correctly.
Pullup resistors are on port A or the internal pullups are on. And, the switch connects the port pin to GND.
Analogue is turned off.

1614171790744.png


This is the way all four switches and LEDs are connected.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top