I'm assigned an exercise to program an AT89C51 for automatic door project.
I'm facing a problem: TIMEOUT
if the outer sensor detects people available, it will open the door and waiting the inner sensor detects people available. After 20s, if no signal is detected, it automatically close the door and return to the beginning of the program (waiting for signal from outer door.
with AT89C51, I'm programming in C but I don't know how to design TIMEOUT algorithm with AT89C51 (20 seconds).
Use TIMER1 in MODE1 (16-bit counter) to count (Fclock/12) : FFFFh.
If crystal is 11.0952MHz the interrupt will occur every (11095200/12)/65536 = 14.108.. times per second.
Create variable TimeH and TimeL.
Increase TimeHTimeL every interrupt. To reach 20 seconds you will need ≈282 interrupts and that will leave the TimeHTimeL = 01 1A (h) = 0282 (d).
Now you can reset 20s counter (TimeHTimeL) and execute whatever should be done after 20s..
but, I suggest to use 12MHz crystal, so one cycle is 1us.
Then, you use timer 1 as 16-bit timer and made 50ms timer (T1H=0x3C T1L=0xAF), so you have 20 counts in one second => to rich 20sec you need exactly 400 counts.