File tree 2 files changed +34
-0
lines changed
2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -366,6 +366,39 @@ void delayMicroseconds(unsigned int us)
366
366
sei ();
367
367
}
368
368
369
+ /*
370
+ unsigned long pulseIn(int pin, int state)
371
+ {
372
+ unsigned long width = 0;
373
+
374
+ while (digitalRead(pin) == !state)
375
+ ;
376
+
377
+ while (digitalRead(pin) != !state)
378
+ width++;
379
+
380
+ return width * 17 / 2; // convert to microseconds
381
+ }
382
+ */
383
+
384
+ unsigned long pulseIn (int pin , int state )
385
+ {
386
+ unsigned long width = 0 ;
387
+ int r = port_to_input [digitalPinToPort (pin )];
388
+ int bit = digitalPinToBit (pin );
389
+ int mask = 1 << bit ;
390
+
391
+ state = (!!state ) << bit ;
392
+
393
+ while ((_SFR_IO8 (r ) & mask ) != state )
394
+ ;
395
+
396
+ while ((_SFR_IO8 (r ) & mask ) == state )
397
+ width ++ ;
398
+
399
+ return width * (16000000UL / F_CPU ) * 20 / 23 ;
400
+ }
401
+
369
402
int main (void )
370
403
{
371
404
// this needs to be called before setup() or some functions won't
Original file line number Diff line number Diff line change @@ -82,6 +82,7 @@ unsigned long millis(void);
82
82
void delay (unsigned long );
83
83
void delay_ms (unsigned short ms );
84
84
void delayMicroseconds (unsigned int us );
85
+ unsigned long pulseIn (int pin , int state );
85
86
86
87
void setup (void );
87
88
void loop (void );
You can’t perform that action at this time.
0 commit comments