;Delay Measurement Example, using Timer 0 with simple polling ;http://www.pjrc.com/tech/8051/board4/timers.html ;This examples demonstrates how to use Timer 0 to measure the ;elapsed time between two events. Timer 0 is used in mode 1 ;(16 bit), and it is started with a count of zero at the first ;event. A simple polling loop is used to check for the second ;event and also to check for timer 0 overflow. Each time the ;timer overflows, we increment a 16 bit overflow count, so we ;can measure elapsed time up to 2^32 cycles (2330 seconds). .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: 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 r3, #0 mov r2, #0 ;clear the overflow count mov dptr, #msg_begin lcall pstr lcall cin ;wait for the user to start the test setb tr0 ;start the timing waiting_loop: jb ri, done_waiting ;did we receive another byte ?? jnb tf0, waiting_loop ;did timer 0 overflow? mov a, r2 add a, #1 ;increment the overflow count mov r2, a mov a, r3 addc a, #0 mov r3, a mov a, #'.' lcall cout ;print a dot, so the user sees something clr tf0 sjmp waiting_loop ;keep waiting until the press another key done_waiting: clr tr0 ;stop timer 0 lcall cin ;get the key the pressed mov dptr, #msg_elapsed lcall pstr mov a, r3 lcall phex ;print a nice summary of the elapsed time mov a, r2 lcall phex mov a, th0 lcall phex mov a, tl0 lcall phex mov dptr, #msg_end lcall pstr lcall cin ljmp 0 msg_begin: .db 13,10,13,10 .db " Delay Measurement Example",13,10,13,10 .db "Press a key to start, and then another to stop",13,10,0 msg_elapsed: .db 13,10,13,10,"Elapsed Cycles: ",0 msg_end: .db 13,10,13,10,"Press a key to reboot",0