如何在8051微控制器上等待一秒?

发布于 2024-08-25 04:29:39 字数 62 浏览 5 评论 0原文

我应该编写一个程序,将一些值发送到寄存器,然后等待一秒钟,然后更改值。问题是,我找不到将操作暂停一秒钟的指令。

I'm supposed to write a program that will send some values to registers, then wait one second, then change the values. The thing is, I'm unable to find the instruction that will halt operations for one second.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泡沫很甜 2024-09-01 04:29:39

设置定时器中断怎么样?

Keil 8051 应用笔记中的一些有用提示和代码片段。

How about setting up a timer interrupt ?

Some useful hints and code snippets in this Keil 8051 application note.

财迷小姐 2024-09-01 04:29:39

没有这样的“指示”。然而,毫无疑问至少有一个硬件定时器外设(确切的外设集取决于您正在使用的确切部件)。拿出您正在使用的部件的数据表/用户手册,并了解如何对计时器进行编程;然后您可以轮询它或使用中断。通常,您会配置定时器来生成周期性中断,然后递增计数器变量。

关于定时器中断,您必须了解两件事:首先,如果您的计数器变量大于 8 位,则对它的访问将不是原子的,因此在中断上下文之外,您必须暂时禁用中断才能读取它,或者读取它两次连续用相同的值来验证它。其次,定时器计数器变量必须声明为易失性,以防止编译器优化对其的访问;对于中断和线程之间共享的所有变量都是如此。

另一种选择是使用低功耗“睡眠”模式(如果支持);您设置一个计时器以在所需时间后唤醒处理器,并发出必要的睡眠指令(这可能由您的编译器提供为“内在”指令,或者您可能由外围寄存器控制。这是一般建议,而不是8051 具体;我不知道你的部分是否支持睡眠模式,

需要仔细阅读部分特定的文档,如果你能告诉我们确切的部分,你可能会得到帮助。

无论哪种方式,你都 使用 8051 特定的 RTOS 内核,它将提供您正在寻找的周期性延迟功能,以及多线程和 IPC。

There is no such 'instruction'. There is however no doubt at least one hardware timer peripheral (the exact peripheral set depends on the exact part you are using). Get out the datasheet/user manual for the part you are using and figure out how to program the timer; you can then poll it or use interrupts. Typically you'd configure the timer to generate a periodic interrupt that then increments a counter variable.

Two things you must know about timer interrupts: Firstly, if your counter variable is greater than 8-bit, access to it will not be atomic, so outside of the interrupt context you must either temporarily disable interrupts to read it, or read it twice in succession with the same value to validate it. Secondly, the timer counter variable must be declared volatile to prevent the compiler optimising out access to it; this is true of all variables shared between interrupts and threads.

Another alternative is to use a low power 'sleep' mode if supported; you set up a timer to wake the processor after the desired period, and issue the necessary sleep instruction (this may be provided as an 'intrinsic' by your compiler, or you may be controlled by a peripheral register. This is general advice, not 8051 specific; I don't know if your part even supports a sleep mode.

Either way you need to wade through the part specific documentation. If you could tell us the exact part, you may get help with that.

A third solution is to use an 8051 specific RTOS kernel which will provide exactly the periodic delay function you are looking for, as well as multi-threading and IPC.

幸福不弃 2024-09-01 04:29:39

我会设置一个计时器,使其每 10 毫秒中断一次。在该中断中,递增一个变量。

您还需要编写一个函数来禁用中断并读取该变量。

在主程序中,您将读取计时器变量,然后等到它比开始时多 10100。

不要忘记留意计时器变量的翻转。

I would setup a timer so that it interrupts every 10ms. In that interrupt, increment a variable.

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

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

Don't forget to watch out for the timer variable rolling over.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文