PIC16F883 LED闪烁 [英] PIC16F883 Led Blink

查看:161
本文介绍了PIC16F883 LED闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to program a PIC16F883 to blink / light up LED's at the same time. The oscillator is running at 3,2768, and I'm using TIMER0 to help me with the timing.

现在,我将预分频器设置为1:256,所以我每隔50ms就会收到一次中断,并且有一个据此计算出的变量,以显示经过了多少秒.

Right now, I have a prescaler set to 1:256, so I get an interrupt every 50ms, and I have a variable that is calculated from that, to display how many seconds has gone.

如果更改了输入,那么当然会再次重置seconds变量.

If the input is changed, the seconds variable is of course reset again.

这是我老师的作业:

如果输入为0(关闭): 红色和绿色LED应该同时打开15秒钟.在这之后,绿色的LED应该被转动 完全熄灭,红色LED应每五秒钟闪烁10分钟

If Input is 0 (Closed): The Red And The Green LED should be turned on at the same time for 15 seconds. After this the green LED should be turned off completely, and the red LED should blink every fifth second for 10 minutes

如果输入为1(打开): 红色LED指示灯应完全关闭,绿色LED指示灯应打开10分钟,之后 也应该将其关闭.

If input is 1 (Opened): The red LED should be turned off completely, and the green LED should be turned on for 10 minutes, and after that it should be turned off too.

我的计时器工作正常.我已经测试过了.该程序也可以正常运行,并保持2个LED熄灭15秒钟,然后将它们关闭,但我的红色LED没有闪烁.我整天拼命坐在办公桌前,试图找到代码中的错误.

My timer is working fine. I have tested that. The program runs fine too and keeps the 2 LED's turned off for 15 seconds, then turns them off, but my red LED isn't blinking. I have been sitting at my desk all day desperately trying to find the error in my code.

印刷品图片:

这是我的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();
        }
    }
}

我真的希望您能在这里帮助我:)

I really hope that you can help me here :)

这是我的问题所在的子例程input0的流程图.顺便说一句,我的一些变量在代码本身中具有不同的名称,但是不难看出.

Here is my flowchart for the subroutine input0 where my problem is. Btw some of my variables have different names in the code itself, but it shouldn't be hard to see.

推荐答案

您的main()尽可能频繁地调用input0().当input0()处于闪烁状态时,它将使用条件sec > count.我怀疑您的意图是input0()仅应每隔一秒更改一次LED状态.但是,在这种情况下,您需要关闭两个LED.该另一面执行了很多次,因为main()如此频繁地调用input0().尝试删除关闭LED的else条件.

Your main() calls input0() as often as possible. When input0() is in the flashing state it uses the conditional sec > count. I suspect that your intention is that input0() should only change the LED state at intervals of a second. But then on the else side of that conditional you turn both LEDs off. This else side is executing many times because main() is calling input0() so often. Try deleting the else condition where you turn the LEDs off.

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闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆