0

So I am trying to use Port Manipulation to turn on LEDS but I need to use PORT Manipulations for specific reasons.

//22-29PORTA (0x01-0x80)
//30-37PORTC (0x80-0x01)
//38-45PORTD (0x80-0x01)
void setup(){
PORTA = B0000000; //29--->22
PORTC = B0000000; //37--->30
PORTD = B0000000; //45--->38
}
void loop(){
 PORTA = B1000100;
 PORTC = B0101000;
 PORTD = B0000010;
 delay(1000);
 PORTA = B1010000;
 PORTC = B0001010;
 PORTD = B0100000;
 delay(1000);
 PORTA = B0010100;
 PORTC = B0100010;
 PORTD = B0001000;
 delay(1000); 
}```
Heres the code for Arduino Mega 2560
asked Oct 28, 2019 at 21:42

1 Answer 1

2

You need to set the ports to output first, before you can use them.

To do that you use the DDRx (Data Direction) registers. Your code should look like:

//22-29PORTA (0x01-0x80)
//30-37PORTC (0x80-0x01)
//38-45PORTD (0x80-0x01)
void setup(){
 // Set the pins all to output
 DDRA = 0xFF;
 DDRC = 0xFF;
 DDRD = 0xFF;
 PORTA = B0000000; //29--->22
 PORTC = B0000000; //37--->30
 PORTD = B0000000; //45--->38
}
void loop(){
 PORTA = B1000100;
 PORTC = B0101000;
 PORTD = B0000010;
 delay(1000);
 PORTA = B1010000;
 PORTC = B0001010;
 PORTD = B0100000;
 delay(1000);
 PORTA = B0010100;
 PORTC = B0100010;
 PORTD = B0001000;
 delay(1000); 
}

You can read more about direct port manipulation here.

answered Oct 28, 2019 at 21:47

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.