;Timeout Waiting For Input Data, using Timer 0 with simple polling ;http://www.pjrc.com/tech/8051/board4/timers.html ;This examples demonstrates how to use Timer 0 to implement a ;simple timeout while waiting for input data. This simple example ;waits for the user to type 10 characters. The timeout for the ;first character is 5 seconds, and then 1 second for each character ;thereafter. If the user does not respond within this time, a ;timeout message is printed. .equ cout, 0x0030 ;Send Acc to serial port .equ cin, 0x0032 ;Get Acc from serial port .equ phex, 0x0034 ;Print Hex value of Acc .equ pstr, 0x0038 ;Print string pointed to by DPTR, .equ esc, 0x003E ;Check for ESC key .equ newline, 0x0048 ;print CR/LF (13 and 10) .org 0x2000 begin: mov r0, #0 ;r0 counts number of bytes received clr tr0 ;make sure timer 0 is stopped clr tf0 ;clear the overflow flag anl tmod, #0xF0 orl tmod, #0x01 ;set to mode 1 (without touching timer 1) mov tl0, #0 mov th0, #0 ;clear the timer 0 value mov r2, #140 ;140 overflows is 5 seconds mov dptr, #msg_begin lcall pstr setb tr0 ;start the timing waiting_loop: jb ri, get_input ;did we receive another byte ?? jnb tf0, waiting_loop ;did timer 0 overflow? clr tf0 ;mov a, r2 ;uncomment to "see" the countdown ;lcall phex djnz r2, waiting_loop timeout: mov dptr, #msg_timeout lcall pstr sjmp ending get_input: lcall cin ;get the user input lcall cout ;echo it back to their screen mov r2, #28 ;reset timeout to 1 second inc r0 cjne r0, #10, waiting_loop got_all_input: mov dptr, #msg_ok lcall pstr ending: mov dptr, #msg_end lcall pstr lcall cin ljmp 0 msg_begin: .db 13,10,13,10 .db " Timeout Waiting For Input Data",13,10,13,10 .db "Please type 10 characters, before it's too late.....",13,10,0 msg_timeout: .db 13,10,13,10,"Timeout: Waited too long.",0 msg_ok: .db 13,10,13,10,"Ok: All 10 characters received within the allowed time.",0 msg_end: .db 13,10,13,10,"Press a key to reboot",0