PIC16F883 Led Blink

我需要对PIC16F883进行编程,以同时闪烁/点亮LED。 振荡器运行在3,2768,我正在使用TIMER0来帮助我计时。

现在,我有一个预分频器设置为1:256,所以我每50ms得到一个中断,并且我有一个由此计算的变量,以显示已经过了多少秒。

如果输入已更改,则当然会再次重置seconds变量。

这是我老师的作业:

如果输入为0(关闭):红色和绿色LED应同时打开15秒。 此后,绿色LED应完全关闭,红色LED应每隔五秒闪烁10分钟

如果输入为1(打开):红色LED应完全关闭,绿色LED应打开10分钟,之后应关闭。

我的计时器工作正常。 我测试过了。 该程序运行正常,并保持2个LED关闭15秒,然后关闭它,但我的红色LED不闪烁。 我一整天都坐在办公桌前拼命想找到代码中的错误。

印刷图片: 在此处输入图像描述


这是我的C代码。 我正在使用MPLab和HI-TECH C编译器:)

#include  //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(seccount) { 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。 其他方面正在执行多次,因为main()经常调用input0() 。 尝试删除关闭LED的其他条件。

 void input0() {  else if(sec<=600 && sec>count) {  } else PORTA = 0x00; // <-- delete this line so you're not always turning the LED off }