【发布时间】:2016-09-17 15:38:23
【问题描述】:
我正在尝试使用 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 会同时打开和关闭。但是,我注意到只有连接到 output4 的 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);
}
【问题讨论】:
标签: c embedded pic microchip mplab