Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

micros() sometimes returns incorrect value #49

Closed
Labels
conclusion: resolvedIssue was resolved topic: codeRelated to content of the project itself type: imperfectionPerceived defect in any part of project
@greiman

Description

Here is the problem - in this program bgn is often larger than end.

void setup() {
 Serial.begin(9600);
 while(!Serial) {}
 Serial.println("bgn, end, bgn - end");
 for (uint32_t i = 0; i < 10000; i++) {
 uint32_t bgn = micros();
 uint32_t end = micros();
 if (bgn > end) {
 Serial.print(bgn);
 Serial. print(',');
 Serial.print(end);
 Serial.print(',');
 Serial.println(bgn - end);
 }
 }
}
void loop() {
}

Here is the print out for the first few cases with bgn > end.

bgn, end, bgn - end
25992,25000,992
43992,43000,992
65993,65001,992
83994,83002,992

Wonder if the interrupt protection is correct. 1000 is a clue.

unsigned long micros() {
	// Convert time to us
	NVIC_DisableIRQ(main_timer.get_cfg()->cycle_end_irq);
 	uint32_t time_us = ((main_timer.get_period_raw() - main_timer.get_counter()) * 1000 / main_timer.get_period_raw()) + (agt_time_ms * 1000); 
	NVIC_EnableIRQ(main_timer.get_cfg()->cycle_end_irq);
	return time_us;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    conclusion: resolvedIssue was resolved topic: codeRelated to content of the project itself type: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        AltStyle によって変換されたページ (->オリジナル) /