I'm not sure but is no faster than 247 clock cycles
Rarely have I come across a microprocessor that took more than 2 cycles to execute a simple relative branch.
247 cycles is the rate glaciers move at.
Why aren't you looking at the generated code?
As others have said, it's not the loop, it's the implementation of digitalRead(). Here's the actual loop, as reported by "avr-objdump" (search the forums for this for further info on how to use this.)
That's what I intended. It's been a while since I last coded and I deleted the {} after the while because I could not remember what they did. TskTsk.
I still have a problem however.
//const int RPM = 2;
const int ledPin = 13; // the number of the LED pin
unsigned int Tmr = 0;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(57600); // initialize serial communications:
//set up timer 1 control registers here
TCCR1A = 0x00; //select Normal Mode,freerunning counter
TCCR1B = 0X01; //normal, prescaler equals clock.
DDRD = 0x00; // set PB to Inputs}
}
void loop() {
digitalWrite(ledPin, 1); // shows where the program stops.
while(PORTD|0xFB != 0xFB){}; // Wait for falling edge on bit 2 PORTD
digitalWrite(ledPin, 0);
while(PORTD&0x04 != 0x04){}; // Wait for rising edge on bit 2 PORTD
TCNT1 = 0; // Reset timer
while(PORTD|0xFB != 0xFB){}; // Wait for falling edge
while(PORTD&0x04 != 0x04){}; // Wait for rising edge
Tmr = TCNT1;
Serial.println(Tmr);
delay(1000);
}
Today I found out that I need to know more than just c. I also need to understand the .h files. This is a bit new to me coming from a Delphi and Basic background.
I found a file c:\Arduino\hardware\tools\avr\lib\avr5\iom328p.h and now things like
while ((PORTD & (1<<PD2)) != 0)
are starting to make sense.
OK When do I use, or what is the difference between ?
while ((PORTD & 4) != 0)
or
while ((PORTD & 0X04) != 0)
or
while ((PORTD & (1<<PD2)) != 0) // I see this code can be made portable to other devices.