I'm just learning assembly and can now set a delay either by checking TF0 or waiting for an interrupt. How can I write a routine that will accept a variable time? Everything I google is not variable. For now I don't need to reload the timer. I have a 1us cycle time so anything between 1 and 65 ms would be great.

I'm a freshman high school student and I want to thank you in advance.


1 Answer 1


Question is broadish and may be more suited to stackoverflow.com, but I can help since I use an 8051 based processor myself.

In order to write a routine, you need to know how your input device is connected to the 8051 processor chip and what signals are sent based on the input device, This input device can be as simple as a button connected to an input pin of the microcontroller.

Since you want to define a variable amount of time, you could create a circuit with three buttons and the 8051 processor. One button can be increment value, one can be decrement value, and one can be accept.

Then the user can press the increment or decrement until the desired number is entered. Then use that number in your desired timer routine.

You can check the state of the buttons in code by checking to see that the port the button is connected to is in the correct state.

For example, if when the button is pressed, the value at the pin is at logic low and the pin the button is connected to is highest bit of port 3, then this code will work:

jb P3.7,skipPress
# button is pressed - stall until button is released
jnb P3.7,$
# button is released

Now say you have an UP button connected to port 3 and the 3rd highest bit (P3.5) that increments a number when button causes a logic low at the pin and a DOWN button connected to port 3 and highest bit (P3.7) that decrements a number when button causes a logic low at the pin. Here's example of code that works:

jb P3.5,skipUp
jnb P3.5,$
inc R7

jb P3.7,skipDown
jnb P3.7,$
dec R7

Put the above in one subroutine and execute it every time user input is allowed in your program. I use R7 to store the 8-bit number. Then use R7 to compare with other data in your timer routines.

Do realize that the 8051 processor deals with 8-bit data, which means your arithmetic is generally limited to numbers from 0 to 255 inclusive. If you need higher than 255, then you need to create routines to store 16-bit numbers.

Go to http://www.8052.com/math to learn how to store 16-bit numbers.

Now if you really want a user to be able to enter a specific number and have the LCD show the number, it is possible, but you'll need more buttons, and depending how you invent the circuit, the project may be more expensive. Remember, each key costs money too.

I'm sorry I can't write your program from start to finish, but you need to provide more details about your hardware you're using with the chip since that dictates how the code is supposed to be written.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.