FastWrite

Help please,

Does any one have any idea why the following code outputs a jittery signal? Attached is the Scope image.

Thanks!!

int pinOne = 9;
int pinTwo = 10;


#define fastWrite(_pin_, _state_) ( _pin_ < 8 ? (_state_ ?  PORTD |= 1 << _pin_ : PORTD &= ~(1 << _pin_ )) : (_state_ ?  PORTB |= 1 << (_pin_ -8) : PORTB &= ~(1 << (_pin_ -8)  )))
// the macro sets or clears the appropriate bit in port D if the pin is less than 8 or port B if between 8 and 13

void setup() {
 
   pinMode(pinOne, OUTPUT);   // set outPin pin as output
   pinMode(pinTwo, OUTPUT);
 
}

void loop() 
{
  
 fastWrite(pinOne, HIGH);       // set Pin high
 //delayMicroseconds(20);    // waits "on" microseconds
 fastWrite(pinTwo, HIGH); 
 delayMicroseconds(20);
 fastWrite(pinOne, LOW);        // set pin low
 //delayMicroseconds(20);   //wait "off" microseconds
 fastWrite(pinTwo, LOW);
 delayMicroseconds(20);
 
}

main() has to call loop() and there are also interrupts going on in the background for micros()/millis() time tracking.

Try this:

void loop(){
while (1)
{
 fastWrite(pinOne, HIGH);       // set Pin high
 //delayMicroseconds(20);    // waits "on" microseconds
fastWrite(pinTwo, HIGH); 
 delayMicroseconds(20);
 fastWrite(pinOne, LOW);        // set pin low
//delayMicroseconds(20);   //wait "off" microseconds
fastWrite(pinTwo, LOW);
 delayMicroseconds(20);

}
}
[color=#222222]

Will help with the main() and loop() jitteryness anyway.

That has caught me a few times :wink:

Hey thanks for the reply!

If you mean like the below, I still see the jittery signals. Do did i not implement it correctly?

int pinOne = 9;
int pinTwo = 10;


#define fastWrite(_pin_, _state_) ( _pin_ < 8 ? (_state_ ?  PORTD |= 1 << _pin_ : PORTD &= ~(1 << _pin_ )) : (_state_ ?  PORTB |= 1 << (_pin_ -8) : PORTB &= ~(1 << (_pin_ -8)  )))
// the macro sets or clears the appropriate bit in port D if the pin is less than 8 or port B if between 8 and 13

void setup() {
 
   pinMode(pinOne, OUTPUT);   // set outPin pin as output
   pinMode(pinTwo, OUTPUT);
 
}

void loop(){
while (1)
{
 fastWrite(pinOne, HIGH);       // set Pin high
 //delayMicroseconds(20);    // waits "on" microseconds
fastWrite(pinTwo, HIGH); 
 delayMicroseconds(20);
 fastWrite(pinOne, LOW);        // set pin low
//delayMicroseconds(20);   //wait "off" microseconds
fastWrite(pinTwo, LOW);
 delayMicroseconds(20);
}
}

interrupts will still occur.

If you want a real steady signal you have to investigate hardware timers

Ok, Thanks!