Jump to content
43oh

Digital or analog Write multiple pins in one command ?


Recommended Posts

You could if you would use pin 9 instead of 6. We will have to get into the nitty gritty details of port registers though. The idea is that the pins you want to control at the same time are on the same port. In this case P2. Assuming that you do a pinMode(xxx, OUTPUT) on all your pins in setup then you can do the following to set all pins at once.

 

First let's figure out which port bits are associated with the pins. If you use pin 8, 9, 10 and 12 then:

 

8  = P2.0 = bit 0 = 1 < 0
9  = P2.1 = bit 1 = 1 < 1
10 = P2.2 = bit 2 = 1 < 2
12 = P2.4 = bit 4 = 1 < 4

 

To set all bits at once you would do:

P2OUT |= (1 < 0) | (1 < 1) | (1 < 2) | (1 < 4);

to clear all bits at once you would do:

 

 

P2OUT &= ~((1 < 0) | (1 < 1) | (1 < 2) | (1 < 4));

 

In your loop() it would look something like this.

 

loop() {
 /* Turn all LED's on */
 P2OUT |= (1 < 0) | (1 < 1) | (1 < 2) | (1 < 4);
 delay(500);
 /* Turn all LED's off */
 P2OUT &= ~((1 < 0) | (1 < 1) | (1 < 2) | (1 < 4));
 delay(500);
}

Link to post
Share on other sites
  • 2 weeks later...

I am doind a DC-AC inverter, and i need to toggle two pins at the same time, but i need a 1us accuracy, but for example, there is a 3us delay between a HIGH and a LOW for Q1 and Q2, thar are always complementary. This is my code:

 

//Primer semiciclo
  //2)Q1 y Q4
  digitalWrite(Q2,LOW); digitalWrite(Q3,LOW);	 //Q2 y Q3 apagado
     //delayMicroseconds(td);						  //Tiempo muerto
  digitalWrite(Q1,HIGH); digitalWrite(Q4,HIGH);   //Q1 y Q4 ENCENDIDO
  delayMicroseconds(duty);					    //Duracion del semiciclo (T/4)
  //1)Q2 y Q4
  digitalWrite(Q3,LOW); digitalWrite(Q1,LOW);	  //Q1 y Q3 apagado
     //delayMicroseconds(td);						   //Tiempo muerto
  digitalWrite(Q4,HIGH); digitalWrite(Q2,HIGH);    //Q4 y Q2 ENCENDIDO
  delayMicroseconds(tr-duty);					  //Duracion del semiciclo (T/4)

  //Segundo semiciclo

//2)Q1 y Q4
     digitalWrite(Q2,LOW); digitalWrite(Q3,LOW);     //Q2 y Q3 apagado
     //delayMicroseconds(td);                          //Tiempo muerto
     digitalWrite(Q1,HIGH); digitalWrite(Q4,HIGH);   //Q1 y Q4 ENCENDIDO
     delayMicroseconds(duty); 
  //3)Q2 y Q4
  digitalWrite(Q3,LOW);digitalWrite(Q1,LOW);	    //Q1 y Q3 apagado
     //delayMicroseconds(td);						    //Tiempo muerto
  digitalWrite(Q4,HIGH);digitalWrite(Q2,HIGH);    //Q2 y Q4 ENCENDIDO
  delayMicroseconds(tr-duty);					   //Duracion del semiciclo (T/4)

 

is there any chance to get a better response time? maybe with some modification like you suggested in this post? i mean... is faster to set and clear this bits in this way than if i use digitalWrite?

 

Thanks a lot for your Help.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...