Newbie PIC18FLK50

Thread Starter

skybox

Joined Mar 2, 2009
68
Hi Guys,

I am finally entering the microcontroller world and will be working on my first project. One of the designs for the projects includes an event X happening when a button is held LOW for 3 seconds. I am not the best programmer but wanted to brainstorm with you guys on how I am going to approach this, and if someone can give me tips, that would be great!.

The microcontroller I will be using is a PIC18F13K50

My basic design is this:

Oscillator will be in circuit and set to 1 MHz with a prescaler of 256:1

The contact switch which will be held for 3 seconds will be connected to Pin 14(RC2) which has an INT2. When the switch is pressed, I will set INT2 to set high when it sees an active low. In turn, INT2 will be an high priority interrupt. From there, the code will go to a Push Button function I will make that will see how long the button is pressed for. In order to achieve this, I will start a timer (Timer0 - 16 bit). The Timer0 will also have an interrupt where it will over flow if button is held for 3 seconds or longer. Now to monitor how long the button is pressed for, I will put in a while loop to monitor the status of RC7 (should be active low) and within the loop, the timer will increment. If it takes the timer0 interrupt to overflow for 3 seconds or longer, than I will do event X. If the timer0 interrupt bit doesn't get set within 3 seconds, than I know the button was pressed for less than 3 seconds, thus the PIC will do nothing.

If you guys can give me tips on the logic of my program, I would greatly appreciate it. In addition, I want to conserve as much power as possible so right when the microprocessor starts, the PIC will be sleeping until INT2 (button press) wakes it up.

Thanks in advance! :)
 

Thread Starter

skybox

Joined Mar 2, 2009
68
Ok guys. Here is what I am stuck on, the logic at least.

So say the user holds down the button, ignore time in this case. This is what happens:

1)INT2 = 1 --> falling edge triggered
2)Program goes to address 0008h since this is a high priority interrupt
3)TImer0 is started

Now INT2 is going to stay high as long as it is not cleared by the software. So even if the user holds down the button for 1 second, INT2 still equals 1. My question is, how do I increment the timer with resepect to how long the button is held? I can't figure out anyway I can put that condition in a loop other than if the voltage is low at that button pin, than increment timer. If not, don't increment. So my logic would be:

INT2 is set high.
While voltage is low
increment Timer 0
if Timer 0 overflows and Timer0 interrupt flag is set high
Do Event X
if not
Do nothing
Reset Timer0 value
 
Last edited:

Vaughanabe13

Joined May 4, 2009
102
It sounds like you have it figured out. What I would do is something like this:

Rich (BB code):
//Inside the interrupt routine
//start Timer0, have it configured to overflow at 3 seconds

ButtonPressed = TRUE;
while (Timer0 overflow flag is not set) {
        if (INT2 == 1) {
               ButtonPressed = FALSE;
               break;
        }
}
if (ButtonPressed == TRUE) {
        //Do code here when the button was pressed for 3 seconds
}
else {
        //Reset Timer0 for the next interrupt and re-enable interrupts
}
Then what will happen is inside the while loop you are just constantly polling to make sure the button is still pressed. If the person lets go, the INT2 pin will go back to high voltage and the program will exit the while loop with the ButtonPressed = False condition. Otherwise if the button is held for the whole 3 seconds the Timer0 overflow flag will be set and the program will exit the while loop with the ButtonPressed = True condition. Then just check to see if ButtonPressed is true or false and do whatever code you want.
 
Top