Adding delay using counters in arduino mega

Hi,
I want to write a custom delay function which takes input in microseconds and add a delay of that time period in the code. For this purpose i am using counter 1 of arduino mega which is used to count time in microseconds and another function which i have written is written is delay_us_1() which add delay.

But when i use this function to generate an waveform of 200us time period. But it is not giving expected results and it is generating waveform of time period 930us. Also it is giving incorrect time delays when i change the delay to some other values.

Here is my code

volatile uint32_t microseconds = 0;

ISR(TIMER1_COMPA_vect)
{
PORTB ^= ( 1 << 7);
microseconds += 4;
// seconds++;
}
void setupTimer1()
{
// WGM1[3:0] = 15 for fast PWM with TOP = OCR1A
TCCR1A = ( 1 << WGM10 ) | ( 1 << WGM11 );
// ICES1 == 1 for rising edge capture
TCCR1B = ( 1 << WGM12 ) | ( 1 << WGM13 ) | ( 1 << ICES1 ) | ( 1 << CS11 );

// OCR1A set TOP to 15 or (111)
OCR1A = 0x7;

TIMSK1 = (1 << OCIE1A );

}

void delay_us_1( uint64_t delay )
{
uint64_t start = microseconds;
while ( ( microseconds - start ) < delay );
}

void setup() {

DDRB |= ( 1 << 7 ) | ( 1 << 6 );
PORTB |= ( 1 << 7 ) | ( 1 << 6);

setupTimer1();
sei();

Serial.begin(115200);
Serial.println("Serial started");

}

void loop() {
delay_us_1( 100 );
PORTB ^= ( 1 << 6);
}

and here is the result on oscilloscope.

don't take code comments seriosly those are wrong

What is the point to write you own function using timers, if your function is a same blocking, as standard delay?

Hi b707,
I just learning about timers and counters and to prove the concept i am making this. I just want to know that why it is not working ( I mean not adding delay of required time ).

I moved your topic to an appropriate forum category @ramvilas_setech.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

For more precise results study the source code of delayMicroseconds().

Your code can not work for a couple of reasons. E.g. ISR takes too long, ignoring function call and housekeeping time, no atomic access...

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.