edward0429 / arduino

Automatically exported from code.google.com/p/arduino
0 stars 0 forks source link

Document need to set clock pin LOW before call to shiftout() for devices clocked with a rising edge. #696

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago

See http://arduino.cc/forum/index.php/topic,76530.0.html

The current implementation of shiftOut() assumes the clockpin being LOW. A new 
implementation is proposed to set the clockpin explicitly befor the inner loop. 

proposed code - thanks to rayshobby and bperrybap

void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
{
  uint8_t i;
  digitalWrite(clockPin, LOW);  // additional line
  for (i = 0; i < 8; i++)  
  {
    if (bitOrder == LSBFIRST)
      digitalWrite(dataPin, !!(val & (1 << i)));
    else    
      digitalWrite(dataPin, !!(val & (1 << (7 - i))));
    digitalWrite(clockPin, HIGH);
    digitalWrite(clockPin, LOW);    
  }
}

The change will slow down shiftOut approx 4% (25 digitalWrite()'s iso 24) and 
it will be a few bytes larger.

Original issue reported on code.google.com by rob.till...@gmail.com on 24 Oct 2011 at 7:07

GoogleCodeExporter commented 9 years ago
Won't this create problems for devices that accept a data bit on a falling 
edge?  That is, if the clock pin is high when the function is called, the 
initial digitalWrite() call will generate a falling edge, clocking in an 
unknown bit (since the data pin hasn't been set yet).  If so, it seems better 
to omit it, since people using devices that require rising edges can still 
include the digitalWrite(clock, LOW) call outside the call to shiftOut().

Original comment by dmel...@gmail.com on 24 Oct 2011 at 5:48

GoogleCodeExporter commented 9 years ago

Think you have a point, in short: If the state of the clockpin is not defined 
or 'wrong' it is up to the user to make it right.

documentation fix?

Original comment by rob.till...@gmail.com on 25 Oct 2011 at 5:52

GoogleCodeExporter commented 9 years ago
I updated the reference: http://arduino.cc/en/Reference/ShiftOut

Original comment by dmel...@gmail.com on 25 Oct 2011 at 6:11

GoogleCodeExporter commented 9 years ago
What about some additional clarity to indicate that the clock level is only for 
the first call and not all calls.
And also a mention about the need for a high level for falling edges?
And perhaps a small addition to mention that the clock line shouldn't be 
modified between calls to shiftOut() and if it is, that the clock line must be 
reinitialized
to the proper state depending on whether the clocking is rising or falling edge?

Original comment by bperry...@gmail.com on 25 Oct 2011 at 6:23

GoogleCodeExporter commented 9 years ago
Sounds like it would made sense to pass in an additional parameter to indicate 
whether the device accepts data on rising edge or falling edge. 

Original comment by rayshobb...@gmail.com on 27 Oct 2011 at 2:33