Skip to content

Commit 35f7428

Browse files
author
Martino Facchin
committed
pulseIn: fix maxloops calculation and add port defines
1 parent 1767dae commit 35f7428

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

hardware/arduino/avr/cores/arduino/wiring_pulse.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,19 @@
2525
#include "wiring_private.h"
2626
#include "pins_arduino.h"
2727

28+
//include PORT similar to the ones defined in Arduino.h
29+
#define PA 1
30+
#define PB 2
31+
#define PC 3
32+
#define PD 4
33+
#define PE 5
34+
#define PF 6
35+
#define PG 7
36+
#define PH 8
37+
#define PJ 10
38+
#define PK 11
39+
#define PL 12
40+
2841
uint16_t (*countPulseASM)(const uint8_t port, const uint8_t bit, uint16_t maxloops, uint8_t state);
2942

3043
/* Measures the length (in microseconds) of a pulse on the pin; state is HIGH
@@ -62,9 +75,9 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
6275
}
6376

6477
// convert the timeout from microseconds to a number of times through
65-
// the initial loop; it takes 16 clock cycles per iteration.
78+
// the initial loop; it takes 11 clock cycles per iteration.
6679
unsigned long numloops = 0;
67-
unsigned long maxloops = microsecondsToClockCycles(timeout);
80+
unsigned long maxloops = microsecondsToClockCycles(timeout)/11;
6881

6982
// wait for any previous pulse to end
7083
while ((*portInputRegister(port) & bit) == stateMask)

0 commit comments

Comments
 (0)