There is no such "instruction". However, there is undoubtedly at least one peripheral peripheral device (the exact peripheral device depends on the particular part you are using). Exit the data table / user manual for the part you are using and find out how to program the timer; You can interrogate it or use interrupts. Typically, you set up a timer to generate a periodic interrupt, which then increments the count variable.
Two things you should know about timer interrupts: firstly, if your counter variable is greater than 8-bit, access to it will not be atomic, so outside the context of an interrupt, you must either temporarily disable interrupts to read it, or read it twice in a row with the same value to check it. Secondly, the timer counter variable must be declared volatile to prevent the compiler from optimizing access to it; this applies to all variables shared between interrupts and threads.
Another alternative is to use low power mode, if supported; you set a timer to wake up the processor after the required period of time and issue the necessary sleep command (this can be provided as “internal” by your compiler, or you can control the peripheral register. This is a general tip, not 8051, I don’t know if it supports your part is sleep mode.
In any case, you need to go through the documentation specific to the particular part. If you could tell us the exact part, you can get help with this.
The third solution is to use the 8051 specific RTOS core, which will provide exactly the periodic delay function you are looking for, as well as multithreading and IPC.
source share