c - 我无法让 LED 闪烁 6 次,然后终止 C 中的程序

标签 c pic terminate

为什么 exit() 不适用于 PIC16f877?

#include <htc.h>
#include <pic16f877.h>
#include <stdlib.h>

#define _XTAL_FREQ 4e6

int main(void){
    int count;
    int max_count = 6;
    TRISD = 0xBF;
    for (count=0; count<max_count; ++count){
        PORTD   = 0x40;
        __delay_ms(500);
        PORTD   = 0x00;
        __delay_ms(500);
    }
    exit(0);
}

错误消息:

Error   [482] lab1_home.obj; 20. symbol "_exit" is defined more than once in "startup.obj"
Error   [482] lab1_home.obj; 20. symbol "_exit" is defined more than once in "startup.obj"

这些代码程序构建成功,但 LED 一直闪烁,是什么原因?

尝试1:

#include <htc.h>
#include <pic16f877.h>

#define _XTAL_FREQ 4e6

void main(void){
    int count;
    int max_count = 6;
    TRISD = 0xBF;
    for (count=0; count<max_count; ++count){
        PORTD   = 0x40;
        __delay_ms(500);
        PORTD   = 0x00;
        __delay_ms(500);
    }
    return;
}

尝试2:

#include <htc.h>
#include <pic16f877.h>
#include <stdio.h>
#include <stdlib.h>

#define _XTAL_FREQ 4e6

int main(void){
    int count = 0;
    int max_count = 6;
    TRISD = 0xBF;
    do {
        count++;
        PORTD   = 0x40;
        __delay_ms(500);
        PORTD   = 0x00;
        __delay_ms(500);
    } while ( count != max_count );
    abort;
}

循环没有退出吗?或者我试图终止程序的方式是错误的?我还考虑了另外一件事...WDT 与这个循环永远重复有什么关系吗?

最佳答案

这段代码成功调用了 sleep 函数并退出了无限循环(感谢@francis):

#include <htc.h>
#include <pic16f877.h>

#define _XTAL_FREQ 4e6
#define SLEEP() asm("sleep")

int main(void){
    int count = 0;
    int max_count = 6;
    TRISD = 0xBF;
    while(count<max_count){
        count++;
        PORTD   = 0x40;
        __delay_ms(500);
        PORTD   = 0x00;
        __delay_ms(500);
    }
    SLEEP();
}

关于c - 我无法让 LED 闪烁 6 次,然后终止 C 中的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386148/

相关文章:

c - 使用 sun rpc 从服务器发送结构数组到客户端

pic - 如何加速 MPLAB X

c - 在没有内置函数的情况下舍入 C 中的 Float 值

opengl - GLSL 顶点着色器取消渲染

c - C 中的二维数组是否需要使所有元素连续?

c - 定时器实现超时功能

C 套接字 : why is my server appending extra characters in the end?

c - PIC24F16KA102 上的定时器 1 不工作

docker - 如何在进程终止后停止docker-compose

c# - 立即终止无环线程,无需 Abort 或 Suspend