I've put the following code in my sketch to speed up the shiftOut() function. I'm using the typical MC74HC595 shift register but the maximum clock frequency for my application is (conservatively) 4 MHz. If my code works, my clock frequency will be considerably faster (see thread below - I'm using Stimmer's code; it takes about 24 nS to write a pin high). I need a "delay" of say ... 250 nS between each clock write cycle. So ... can I modify an existing (e.g. micros()) or write my own function to have better 100 nS resolution? Or a better way to accomplish what I want?
inline void digitalWriteDirect(int pin, boolean val){
if(val) g_APinDescription[pin].pPort -> PIO_SODR = g_APinDescription[pin].ulPin;
else g_APinDescription[pin].pPort -> PIO_CODR = g_APinDescription[pin].ulPin;
}
inline int digitalReadDirect(int pin){
return !!(g_APinDescription[pin].pPort -> PIO_PDSR & g_APinDescription[pin].ulPin);
}
//High speed shift register function
void shiftOutFast( uint32_t ulDataPin, uint32_t ulClockPin, uint32_t ulBitOrder, uint32_t ulVal )
{
uint8_t i ;
for ( i=0 ; i < 8 ; i++ ){
if ( ulBitOrder == LSBFIRST ){
digitalWriteDirect( ulDataPin, !!(ulVal & (1 << i)) );
}
else{
digitalWriteDirect( ulDataPin, !!(ulVal & (1 << (7 - i))) );
}
digitalWriteDirect( ulClockPin, HIGH );
//need to delay 200 nS
digitalWriteDirect( ulClockPin, LOW );
//need to delay 200 nS
}
}