Pages: [1]   Go Down
Author Topic: Optimizing two control outputs from 00 to 03  (Read 304 times)
0 Members and 1 Guest are viewing this topic.
Venezuela
Offline Offline
Sr. Member
****
Karma: 12
Posts: 434
Ground.......ground........always ground
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello. I am controlling the select address pins (S1 S2) of a MUX 74HCT4052. For design reasons, those pins are connected to pins 5 and 6 of the Arduino. The code works good...but, is this the optimal solution? Here the code:

Code:
int Pin5 = 5;
int Pin6 = 6;
void setup() {
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(Pin5, LOW);
  digitalWrite(Pin6, LOW);
  delay(1000);

  digitalWrite(Pin5, LOW);
  digitalWrite(Pin6, HIGH);
  delay(1000);

  digitalWrite(Pin5, HIGH);
  digitalWrite(Pin6, LOW);
  delay(1000);

  digitalWrite(Pin5, HIGH);
  digitalWrite(Pin6, HIGH);
  delay(1000);
}

 Thank you
« Last Edit: October 19, 2012, 09:35:11 pm by Palliser » Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

As pins 5 and 6 are both on PORTD you can use direct port instructions, it will be faster but not much cleaner at the source level because you have to preserve the other bits so the HIGH/LOW and LOW/HIGH combinations still need two instructions.

PORTD |= B01100000; // sets both pins high

PORTD &= ~B01100000; // sets both pins low

PORTD &= ~B00100000; // pin 5  low
PORTD |= B01000000; // pin 6  high

PORTD &= ~B01000000; // pin 6  low
PORTD |= B00100000; // pin 5  high


You get the idea.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Faster hardly seems relevant when he has a 1 second delay there.
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 121
Posts: 8458
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

True, but this looks like some test code that allows him to see the pins toggling. None the less when switching a MUX speed is not normally of the essence.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12480
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

1) use uint8_t for the pin variables (that is what digitalWrite expects), or as they are constant better use define
2) make a function to set the values

Code:
#define PIN5  5
#define PIN6  6

void setup()
{
  pinMode(PIN5, OUTPUT);
  pinMode(PIN6, OUTPUT);
}

void loop()
{
  setMUX(LOW, LOW);
  setMUX(LOW, HIGH);
  setMUX(HIGH, LOW);
  setMUX(HIGH, HIGH);
}

void setMUX(uint8_t a, uint8_t b)
{
  digitalWrite(PIN5, a);
  digitalWrite(PIN6, b);
  delay(1000);
}

Better would be to define 4 states (enumeration) the MUX can be in, and derive the (HIGH/LOW) values from that. That would make the MUX interface more abstract and it would hide the details of the implementation. In the end you wrap it in a (small) class of its own.

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Venezuela
Offline Offline
Sr. Member
****
Karma: 12
Posts: 434
Ground.......ground........always ground
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello Gray/Nick/Rob,
Thank you for your replays. It seems that the setMUX will work for me. As Gray said, this is a test (for a 4x4 kepad interfacing) and I have reduced the frequency to see the pins toggling. What I really pursue here is to execute another section of the code between steps of the sequence. Let's call it //Read here the four columns. The status of the S1 S2 pins controls the row selection.

I am not sure if this is part of a new post but what I need now is something like this:

Code:
void loop()
{
  setMUX(LOW, LOW); //Selects row 1
  //Read here the four columns
  setMUX(LOW, HIGH); //Selects row 2
  //Read here the four columns
  setMUX(HIGH, LOW); //Selects row 3
  //Read here the four columns
  setMUX(HIGH, HIGH); //Selects row 4
  //Read here the four columns
}

Thank you again.
Logged

Pages: [1]   Go Up
Jump to: