Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
only 16 µswhich gives an approximate 1600 uS - microsecond pulse length
//the code measures time elapsed between two active high signals (IN1 and IN2)
//the elapsed time is shown on OUT_PORT.
#include <regx51.h>
#include "gpio.h"
#define OUT_PORT P3
#define OUT_DDR P3
#define OUT_PINs 0xff //output pins on p2
#define IN_PORT P2
#define IN_DDR P2
#define IN1 (1<<0) //first sensor, active high
#define IN2 (1<<1) //2nd sensor, active high
void tmr0_init(void) { //set up tmr0
TMOD &= 0xf0; //clear tmod's lower four bits
TMOD |= 0x01; //tmr0 as 16-bit timer, not gated
TR0=0; //stop the timer
}
void char_display(unsigned char val_char) { //display tmr0's readings
OUT_PORT = val_char; //display tl0 only
}
void mcu_init(void) { //reset mcu and set-up the pins
IO_CLR(OUT_PORT, OUT_PINs); //drive out_pins low
IO_OUT(OUT_DDR, OUT_PINs); //out_pins as output
IO_IN(IN_DDR, IN1 | IN2); //in1 and in2 as input
}
int main(void) {
mcu_init(); //reset the mcu / pins
tmr0_init(); //initialize the tmr
while (1) {
TH0=0, TL0=0; //reset tmr counters
while (!IO_GET(IN_PORT, IN1))
continue; //wait for IN1 to go high
TR0=1; //start tmr0
while (!IO_GET(IN_PORT, IN2))
continue; //wait for IN2 to go high
TR0=0; //stop tmr0
char_display(TH0); //display th0
char_display(TL0); //display tl0
}
}
//the code measures time elapsed between two active high signals (IN1 and IN2)
//the elapsed time is shown on OUT_PORT.
#include <regx51.h>
#include "gpio.h"
#define OUT_PORT P2
#define OUT_DDR P2
#define OUT_PINs 0xff //output pins on p2
#define IN_PORT P3
#define IN_DDR P3
#define IN1 (1<<0) //first sensor, active high
#define IN2 (1<<2) //2nd sensor, active high, on int0
void tmr0_init(void) { //set up tmr0
TMOD &= 0xf0; //clear tmod's lower four bits
TMOD |= 0x01 | 0x08; //tmr0 as 16-bit timer and gated
TR0=0; //stop the timer
}
void char_display(unsigned char val_char) { //display tmr0's readings
OUT_PORT = val_char; //display tl0 only
}
void mcu_init(void) { //reset mcu and set-up the pins
IO_CLR(OUT_PORT, OUT_PINs); //drive out_pins low
IO_OUT(OUT_DDR, OUT_PINs); //out_pins as output
IO_IN(IN_DDR, IN1 | IN2); //in1 and in2 as input
}
int main(void) {
mcu_init(); //reset the mcu / pins
tmr0_init(); //initialize the tmr
while (1) {
TH0=0, TL0=0; //reset tmr counters
while (!IO_GET(IN_PORT, IN1))
continue; //wait for IN1 to go high
TR0=1; //start tmr0
while (IO_GET(IN_PORT, IN2))
continue; //wait for IN2 to go low
TR0=0; //stop tmr0
char_display(TH0); //display th0
char_display(TL0); //display tl0
}
}