Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

pic 16f84 program

Status
Not open for further replies.

wwwglro

New Member
Hello
I have a program that does this: I have 2 buttons and 2 LEDs.
When I push the first button, first LED lights for 5 seconds. If I push the second button while the first led is lighting, first LED stops lighting and the second LED starts lighting for 5 seconds.
I did this with interrupts.
My problem is: when one of the buttons is kept pushed for more then 0,3 seconds, I want the LED to light until the button is released. (not 5 seconds).

Here is the program. (It does the first part I described above: first LED lights for 5 seconds and if a button is pressed, it stops lighting first led and starts lighting the second one).

Thank you.

Code:
#include <16f84A.h>
#use delay(clock=4000000)
long bec0=1;
long bec1=1;

#int_rtcc
void clock_isr() {
bec0--;
bec1--;

if(bec0<1) bec0=1;
if(bec1<1) bec1=1;

if(bec0>1)
output_high(pin_b0);
else
output_low(pin_b0);
if(bec1>1)
output_high(pin_b1);
else
output_low(pin_b1);
}

void main()
{
set_rtcc(0);
setup_counters (RTCC_INTERNAL, RTCC_DIV_1);
enable_interrupts (INT_RTCC);
enable_interrupts(GLOBAL);

do {
if(input_state(PIN_a0)==0)
{
bec0=19530; //3906*5 seconds
bec1=1;
}

if(input_state(PIN_a1)==0)
{
bec1=19530; //3906*5 seconds
bec0=1;
}
} while(1);
}
 
I can't read c but you could just do a bit test on the button until a predetermined counter is empty. Then jump to the sub routine that constantly bit tests until you let go, then of course turn it off. If you don't hold it long enough for the counter to be empty then reset the counter and continue like you already have.
 
Hey wwwglro. When you post code here in the forum, PLEASE use Code tags. To do that, simply click on the # icon in the menu just before pasting your code. Alternately you can type [code] before pasting your source code and [/code] after it.

Code tags tell the forum software to leave your formatting alone. Keeps your source code nice looking and readable. People will actually look at readable code and help you. An unformatted mess, not so much.

I reformatted your source. Here's what it should have looked like:
Code:
#include <16f84A.h>
#use delay(clock=4000000)
long bec0=1;
long bec1=1;

#int_rtcc
void clock_isr()
{
    bec0--;
    bec1--;

    if(bec0<1)
        bec0=1;
    if(bec1<1)
        bec1=1;

    if(bec0>1)
        output_high(pin_b0);
    else
        output_low(pin_b0);
        if(bec1>1)
            output_high(pin_b1);
        else
            output_low(pin_b1);
}

void main()
{
    set_rtcc(0);
    setup_counters(RTCC_INTERNAL, RTCC_DIV_1);
    enable_interrupts(INT_RTCC);
    enable_interrupts(GLOBAL);

    do{
        if(input_state(PIN_a0)==0){
            bec0=19530; //3906*5 seconds
            bec1=1;
        }

        if(input_state(PIN_a1)==0){
            bec1=19530; //3906*5 seconds
            bec0=1;
        }
    } while(1);
}
 
Last edited:
wwwglro Why do you put up a post and not have the decency to answer anyone's replies.

That's why Solicitors charge so much.


People only appreciate something they PAY FOR.
 
Obviously that assignment is past due. Maybe there should be a default (fake) place where people can post and ask everyone to do there homework for them.
 
Hello and sorry for not answer back to you...
I followed the topic for some time and becouse there weren't any answers I kind of forgot about it.
I accidently looked at my bookmarks and saw the link to this forum and entered and ...I sow this answers...

So:
for collin 55
my uC doesn't do anything else but lighting LEDs in to diferent ways.
In fact, what I wnat to do is kind of auto signallizer (in highway mode). (when I am signalling to the left or to the right, with one impuls, the light should blink for 5 secondes, and when I held down the signallizer, the light blinks until the signallizer is released)

for jeremygaughan
My program does almost exactly what you said but the probleme apears when call the function (for led to blink about 5 secondes)- decrement a counter until it is equal to 1 (this is done for aprox. 5 seconds) but when i release the button, I think that the function is called again. (I don't know that exactly...) and led blinks for another 5 secondes.
I do this program with enabling interrupts timer0, and I define the 5 secondes blink code in interrupt function code
Thank you
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top