3
votes

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.

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.

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

Here is the assignment from my teacher:

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

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.

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.

Picture of the print: enter image description here


Here is my C Code. I am using MPLab and the HI-TECH C compiler :)

#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 :)

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.

enter image description here

1
I understand you are proud of what you did, but please do not post unnecessary pictures. Instead post text and indent & format your code properly!too honest for this site
The worst code format ever seen...LPs
If you use a variable that is modified in an interrupt, declare it as volatile, to avoid compiler optimize itLPs
Please guys, i started my study 1 month ago. I am very new to programming and i am seeking help. Instead of telling me my code is formatted wrong, then please tell me how to format it properly.Emil Wismann Kirkebæk-Jensen

1 Answers

1
votes

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
}