为什么 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/