我正在尝试使用 C、MPLAB X(IDE 和 IPE)以及 PICKit 3 开始使用 PIC 18F4550。 我已经设法让一个 LED 闪烁而没有任何问题,但当我尝试同时闪烁多个 LED 时,它不起作用。
请注意,我将在问题末尾发布完整的代码。在那之前,我将编写伪代码,希望能让我的问题更清晰一些。
假设我想要闪烁 4 个 LED,每个 LED 都连接到芯片的输出引脚,您显然会输入类似的内容
loop{
output1 = 1;
output2 = 1;
output3 = 1;
output4 = 1;
delay();
output1 = 0;
output2 = 0;
output3 = 0;
output4 = 0;
delay();
}
您可能会期望所有 LED 都会同时打开和关闭。然而,我注意到只有连接到输出 4 的 LED 会闪烁,其余的将保持关闭状态。 所以我尝试翻转输出引脚的顺序
loop{
output1 = 1;
output2 = 1;
output4 = 1;
output3 = 1;
delay();
output1 = 0;
output2 = 0;
output4 = 0;
output3 = 0;
delay();
}
因此,只有连接到输出 3 的 LED 会闪烁,其余部分将保持关闭状态。
所以我想,不知何故,代码并没有像我预期的那样按顺序执行。 任何人都可以向我提供解释和可能的解决方案吗?
非常感谢!
这是完整的代码
#include <xc.h>
#include <p18f4450.h>
#pragma config FOSC = HS
#define outRed PORTBbits.RB0
#define outBlue PORTBbits.RB1
#define outYellow PORTBbits.RB2
#define outGreen PORTBbits.RB3
#define _XTAL_FREQ 10000000
void delay(unsigned int);
void main(void) {
TRISBbits.TRISB0 = 0;
TRISBbits.TRISB1 = 0;
TRISBbits.TRISB2 = 0;
TRISBbits.TRISB3 = 0;
while(1) {
outRed = 1;
outGreen = 1;
outBlue = 1;
outYellow = 1;
delay(1000);
outRed = 0;
outGreen = 0;
outBlue = 0;
outYellow = 0;
delay(1000);
}
}
void delay(unsigned int delayInput) {
unsigned int mul = delayInput/50;
unsigned int count = 0;
for (count = 0; count <= mul; count ++)
__delay_ms(50);
}
最佳答案
这可能是 LATCH 问题。我刚开始的时候也遇到过几次这个问题。尝试写入 LATB(输出锁存器)寄存器而不是 PORTB 寄存器。我总是使用 LATx 进行输出,使用 PORTx 进行输入。
关于c - 使用 PIC 18F4550 使 3 个 LED 闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39548630/