PIC16F883 LED 闪烁
PIC16F883 Led Blink
我需要对 PIC16F883 进行编程,使其同时闪烁/点亮 LED。振荡器 运行 在 3,2768,我正在使用 TIMER0 来帮助我计时。
现在,我有一个预分频器设置为 1:256,所以我每 50 毫秒得到一个中断,我有一个从中计算的变量,以显示已经过去了多少秒。
如果输入改变了,seconds变量当然要重新设置。
这是我老师布置的作业:
如果输入为 0(关闭):
红色和绿色 LED 应同时亮起 15 秒。在此之后,绿色 LED 应打开
完全关闭,红色 LED 应每五秒闪烁一次,持续 10 分钟
如果输入为 1(打开):
红色 LED 应完全关闭,绿色 LED 应打开 10 分钟,然后
它也应该被关闭。
我的计时器工作正常。我已经测试过了。该程序也运行良好,并保持 2 个 LED 关闭 15 秒,然后将它们关闭,但我的红色 LED 没有闪烁。我整天坐在办公桌前拼命地试图找出我的代码中的错误。
打印图片:
这是我的 C 代码。我正在使用 MPLab 和 HI-TECH C 编译器:)
#include <pic.h>
//Variabler
int B = 0; //Definerer variablen B, used as a flag
int A = 0; //Definerer veriablen A, used as a flag
int E = 0;
int savedstatus = 1; //Definere variablen savedstatus used to check last status for input
int millicounter = 0; //Variabel to calculate seconds
int sec = 0; //Variabel holding seconds gone
int count = 0; //For counting seconds passed, used in input0 subroutine
int onesec = 0; //Used to counting seconds for blinking LED in input0 subroutine
int scount = 0;
//Variabler slut
void interrupt jesper(void)
{
T0IF = 0x00;
TMR0 = 96;
millicounter++;
if(millicounter == 20)
{
sec++;
millicounter = 0;
}
}
//Subrutines
void input0()
{
if(sec<=15 && E==0)
{
PORTA = 0x21;
}
else if(A==0)
{
scount = 0;
sec = 0;
count = sec;
A = 1;
E = 1;
}
else if(sec<=600 && sec>count)
{
count++;
if((scount+5)>=count)
{
if(B==0)
{
onesec = sec;
B = 1;
PORTA = 0x01;
}
else if(sec>onesec)
{
PORTA = 0x00;
B = 0;
scount = count;
scount;
}
else
{
PORTA = 0x01;
}
}
else
{
PORTA = 0x00;
}
}
else PORTA = 0x00;
}
void input1()
{
if(sec<=600)
{
PORTA = 0x20;
}
else
{
PORTA = 0x00;
}
}
//Subrutines over
int main(void)
{
TRISA = 0x00; //Sets all A-PORTS to output
TRISB = 0x01; //Sets all PORTB to output with the exception of BIT0
TRISC = 0x00; //Sets All PORTC to output
ANSEL = 0x00; //Disable Analog ports
ANSELH = 0x00; //Disable Analog ports
//Timer Config
PSA = 0x00;
PS0 = 0x01;
PS1 = 0x01;
PS2 = 0x01;
TMR0 = 0x60;
GIE = 0x01;
T0IE = 0x01;
T0IF = 0x00;
T0CS = 0x00;
//Timer Config Over
while(0x01)
{
if(savedstatus != RB0)
{
savedstatus = RB0;
sec = 0;
E = 0;
A = 0;
}
if(savedstatus == 1)
{
input1();
}
else
{
input0();
}
}
}
我真的希望你能在这里帮助我:)
这是我的问题所在的子例程 input0 的流程图。顺便说一句,我的一些变量在代码本身中有不同的名称,但应该不难看出。
您的 main()
尽可能多地打电话给 input0()
。当 input0()
处于闪烁状态时,它使用条件 sec > count
。我怀疑您的意图是 input0()
应该仅每隔一秒更改一次 LED 状态。但是在那个条件的另一边,你关闭了两个 LED。这个 else 端执行了很多次,因为 main()
经常调用 input0()
。尝试删除关闭 LED 的 else 条件。
void input0()
{
<snip>
else if(sec<=600 && sec>count)
{
<snip>
}
else PORTA = 0x00; // <-- delete this line so you're not always turning the LED off
}
我需要对 PIC16F883 进行编程,使其同时闪烁/点亮 LED。振荡器 运行 在 3,2768,我正在使用 TIMER0 来帮助我计时。
现在,我有一个预分频器设置为 1:256,所以我每 50 毫秒得到一个中断,我有一个从中计算的变量,以显示已经过去了多少秒。
如果输入改变了,seconds变量当然要重新设置。
这是我老师布置的作业:
如果输入为 0(关闭): 红色和绿色 LED 应同时亮起 15 秒。在此之后,绿色 LED 应打开 完全关闭,红色 LED 应每五秒闪烁一次,持续 10 分钟
如果输入为 1(打开): 红色 LED 应完全关闭,绿色 LED 应打开 10 分钟,然后 它也应该被关闭。
我的计时器工作正常。我已经测试过了。该程序也运行良好,并保持 2 个 LED 关闭 15 秒,然后将它们关闭,但我的红色 LED 没有闪烁。我整天坐在办公桌前拼命地试图找出我的代码中的错误。
打印图片:
这是我的 C 代码。我正在使用 MPLab 和 HI-TECH C 编译器:)
#include <pic.h>
//Variabler
int B = 0; //Definerer variablen B, used as a flag
int A = 0; //Definerer veriablen A, used as a flag
int E = 0;
int savedstatus = 1; //Definere variablen savedstatus used to check last status for input
int millicounter = 0; //Variabel to calculate seconds
int sec = 0; //Variabel holding seconds gone
int count = 0; //For counting seconds passed, used in input0 subroutine
int onesec = 0; //Used to counting seconds for blinking LED in input0 subroutine
int scount = 0;
//Variabler slut
void interrupt jesper(void)
{
T0IF = 0x00;
TMR0 = 96;
millicounter++;
if(millicounter == 20)
{
sec++;
millicounter = 0;
}
}
//Subrutines
void input0()
{
if(sec<=15 && E==0)
{
PORTA = 0x21;
}
else if(A==0)
{
scount = 0;
sec = 0;
count = sec;
A = 1;
E = 1;
}
else if(sec<=600 && sec>count)
{
count++;
if((scount+5)>=count)
{
if(B==0)
{
onesec = sec;
B = 1;
PORTA = 0x01;
}
else if(sec>onesec)
{
PORTA = 0x00;
B = 0;
scount = count;
scount;
}
else
{
PORTA = 0x01;
}
}
else
{
PORTA = 0x00;
}
}
else PORTA = 0x00;
}
void input1()
{
if(sec<=600)
{
PORTA = 0x20;
}
else
{
PORTA = 0x00;
}
}
//Subrutines over
int main(void)
{
TRISA = 0x00; //Sets all A-PORTS to output
TRISB = 0x01; //Sets all PORTB to output with the exception of BIT0
TRISC = 0x00; //Sets All PORTC to output
ANSEL = 0x00; //Disable Analog ports
ANSELH = 0x00; //Disable Analog ports
//Timer Config
PSA = 0x00;
PS0 = 0x01;
PS1 = 0x01;
PS2 = 0x01;
TMR0 = 0x60;
GIE = 0x01;
T0IE = 0x01;
T0IF = 0x00;
T0CS = 0x00;
//Timer Config Over
while(0x01)
{
if(savedstatus != RB0)
{
savedstatus = RB0;
sec = 0;
E = 0;
A = 0;
}
if(savedstatus == 1)
{
input1();
}
else
{
input0();
}
}
}
我真的希望你能在这里帮助我:)
这是我的问题所在的子例程 input0 的流程图。顺便说一句,我的一些变量在代码本身中有不同的名称,但应该不难看出。
您的 main()
尽可能多地打电话给 input0()
。当 input0()
处于闪烁状态时,它使用条件 sec > count
。我怀疑您的意图是 input0()
应该仅每隔一秒更改一次 LED 状态。但是在那个条件的另一边,你关闭了两个 LED。这个 else 端执行了很多次,因为 main()
经常调用 input0()
。尝试删除关闭 LED 的 else 条件。
void input0()
{
<snip>
else if(sec<=600 && sec>count)
{
<snip>
}
else PORTA = 0x00; // <-- delete this line so you're not always turning the LED off
}