mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-19 09:42:11 +03:00
Added pulseIn function to measure pulse durations.
This commit is contained in:
@ -366,6 +366,39 @@ void delayMicroseconds(unsigned int us)
|
|||||||
sei();
|
sei();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
unsigned long pulseIn(int pin, int state)
|
||||||
|
{
|
||||||
|
unsigned long width = 0;
|
||||||
|
|
||||||
|
while (digitalRead(pin) == !state)
|
||||||
|
;
|
||||||
|
|
||||||
|
while (digitalRead(pin) != !state)
|
||||||
|
width++;
|
||||||
|
|
||||||
|
return width * 17 / 2; // convert to microseconds
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
unsigned long pulseIn(int pin, int state)
|
||||||
|
{
|
||||||
|
unsigned long width = 0;
|
||||||
|
int r = port_to_input[digitalPinToPort(pin)];
|
||||||
|
int bit = digitalPinToBit(pin);
|
||||||
|
int mask = 1 << bit;
|
||||||
|
|
||||||
|
state = (!!state) << bit;
|
||||||
|
|
||||||
|
while ((_SFR_IO8(r) & mask) != state)
|
||||||
|
;
|
||||||
|
|
||||||
|
while ((_SFR_IO8(r) & mask) == state)
|
||||||
|
width++;
|
||||||
|
|
||||||
|
return width * (16000000UL / F_CPU) * 20 / 23;
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
// this needs to be called before setup() or some functions won't
|
// this needs to be called before setup() or some functions won't
|
||||||
|
@ -82,6 +82,7 @@ unsigned long millis(void);
|
|||||||
void delay(unsigned long);
|
void delay(unsigned long);
|
||||||
void delay_ms(unsigned short ms);
|
void delay_ms(unsigned short ms);
|
||||||
void delayMicroseconds(unsigned int us);
|
void delayMicroseconds(unsigned int us);
|
||||||
|
unsigned long pulseIn(int pin, int state);
|
||||||
|
|
||||||
void setup(void);
|
void setup(void);
|
||||||
void loop(void);
|
void loop(void);
|
||||||
|
Reference in New Issue
Block a user