【发布时间】:2015-12-04 03:29:45
【问题描述】:
我需要对 PIC16F883 进行编程以同时闪烁/点亮 LED。振荡器在 3,2768 处运行,我正在使用 TIMER0 来帮助我计时。
现在,我有一个预分频器设置为 1:256,所以我每 50 毫秒收到一次中断,我有一个从中计算出来的变量,以显示已经过去了多少秒。
如果输入改变了,秒变量当然会再次重置。
这是我老师的作业:
如果输入为 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 的流程图。顺便说一句,我的一些变量在代码本身中具有不同的名称,但应该不难看出。
【问题讨论】:
-
我了解您为自己的所作所为感到自豪,但请不要发布不必要的图片。而是发布文本并缩进和格式化您的代码正确!
-
有史以来最糟糕的代码格式...
-
如果使用在中断中修改的变量,声明为volatile,避免编译器优化
-
拜托各位,我是 1 个月前开始学习的。我对编程很陌生,我正在寻求帮助。与其告诉我我的代码格式错误,不如告诉我如何正确格式化。