是否可以用HI-TECH C为PIC12 MCU创建多线程程序

发布于 2025-01-02 12:45:52 字数 851 浏览 1 评论 0原文

我的朋友让我帮他写一个PIC12单片机的小程序。我们希望

  1. 当输入电压在2秒内低于1.9V时程序停止运行。
  2. 当输入电压超过2.5V时,程序立即做出反应。

我尝试通过阅读和比较系统的时间戳来解决第一个问题:

#include <time.h>
... ...
time_t beg, end;
beg = 0;
end = 0;
while(1){
    if(INP_VOL < 1.9){
        if(beg == 0){
            /* Read timestamp when voltage < 1.9 */
            gmtime(&beg);
        }
        /* Campare timestamp */
        gmtime(&end);
        if(end - beg > 2){
            break; /* !!stop running!! */
        }
    }
    else{
        /* if voltage back to normal, reset beg timestamp. */
        beg = 0;
    }
}

我在PIC12用户手册中找到了函数gmtime(time_t *),但我不确定这是否是一个好的解决方案。

但我不知道如何解决第二个问题。它应该是一种独立的线程,在程序执行期间监视输入电压。并且程序应该在电路损坏之前立即做出反应(通过调用其他函数)。

我是计算机程序员,但我从未为 MCU 编写过代码。我想知道在 HI-TECH C 中是否可以做这样的事情?

My friend asks me to help him write a small program for PIC12 MCU. We want

  1. The program stops running when input voltage is less than 1.9V during 2 seconds.
  2. The program reacts immediately when input voltage exceeds 2.5V.

I try to solve the 1st problem by reading and comparing the system's timestamp:

#include <time.h>
... ...
time_t beg, end;
beg = 0;
end = 0;
while(1){
    if(INP_VOL < 1.9){
        if(beg == 0){
            /* Read timestamp when voltage < 1.9 */
            gmtime(&beg);
        }
        /* Campare timestamp */
        gmtime(&end);
        if(end - beg > 2){
            break; /* !!stop running!! */
        }
    }
    else{
        /* if voltage back to normal, reset beg timestamp. */
        beg = 0;
    }
}

I've found the function gmtime(time_t *) in PIC12 User Manual, but I'm not sure if it a good solution.

But I can't figure out how to solve the 2nd problem. It should be kind of independent thread which moniters the input voltage during the execution of the program. And the program should react immediately (by calling an other function) before the circuit is damaged.

I'm computer programmer, but I've never coded for MCU. I'd like to know if it's possible to do such thing in HI-TECH C ?

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

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

发布评论

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

评论(3

风和你 2025-01-09 12:45:52

这里典型的做法是使用中断,特别是定时器中断。

您设置一个中断运行一次,例如每 1 毫秒运行一次,并在该中断代码中执行程序需要快速响应的任何操作。这将保留正常的执行流程,并模拟两个任务是并行完成的。

The typical thing to do here is to use interrupts, specifically timer interrupts.

You set up an interrupt to run e.g. every 1 ms, and in that interrupt code you do whatever the program needs to react quickly to. That leaves the normal execution flow alone, and emulates that the two tasks are done in parallel.

超可爱的懒熊 2025-01-09 12:45:52

您可以将一个电路连接到外部中断引脚,当电压高于 2.5 时该电路给出 1。可以对外部中断进行编程,使其在输入从 0 变为 1 时触发。

You could have a circuit attached to the external interrupt pin, that gives 1 when voltage goes above 2.5. The external interrupt can be programmed to kick whenever its input goes from 0 to 1.

暖心男生 2025-01-09 12:45:52

我不认为C语言是PIC12系列的最佳解决方案。

我的建议是使用 ASM。只需几条指令,就非常简单。

设置ADC后,您可以使用减法指令并检查C(进位)
通过这种方式您可以验证 IF >或如果<
测试 C,如果为零则跳过。跳过下一条指令,即带有调用的指令。

您还可以更改 micro 并使用 PIC18 以获得更好的 C 代码性能。

I don't think C language is the best solutions for PIC12 family.

My suggest is to use ASM. It's very simple by few instructions.

After set the ADC you can use substraction instruction and check the C (carry)
In this manner you can verify IF > or IF <
Test C and skip if zero. Skip the next instruction, the one with the call.

you can also change micro and use PIC18 for better c code performance.

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