How to wait one second on the 8051 microcontroller?

I have to write a program that will send some values ​​to registers and then wait one second and then change the values. The fact is that I can not find an instruction that will stop operations for one second.

+4
source share
3 answers

How to configure timer interrupt?

Some helpful tips and code snippets in this Keil 8051 Application Note.

+10
source

I would set the timer so that it interrupts every 10 ms. In this interrupt, add a variable.

You will also need to write a function to disable interrupts and read this variable.

In your main program, you will read the timer variable, and then wait until it is 10 100 more than when you started.

Remember to keep track of the transition of the timer variable.

+4
source

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.

+3
source

Source: https://habr.com/ru/post/1303709/


All Articles