Discussion in "PIC Microcontroller Discussion" started by    chalma    May 21, 2014.
Wed May 21 2014, 08:30 pm
#1
Greetings, My current project entails having the pic work on items endlessly. A user can select modes via one button. This cycles through 4-6 different modes of operation, then to save power the last mode puts the unit into sleep. There is only one button, but it has 2 functions, one registeres a press and each press moves the operation mode up one. The second operation of this button is if it is held down for 5 seconds, the unit will alert the user it's going into sleep mode, then goes to sleep. When the user comes back they can enable the switch BUT rather than restart from the first mode of operation it'll continue off of it's last mode (from beginning of the function).

Here is my psuedo code:
:code:
main(){
{
some switch statement (MODE)
(0) function zero ()//sleep
(1) function one();
(2) function two();
(3)function three();
default: mode=0;
etc....
}
}

function zero(){sleep}

function one(){
does something; //50ms
check interrupt flag;
does something; //4 s
check interrupt flag;
}

function two{
does something; //2 s
check interrupt flag;
does something; //3.5s
check interrupt flag;
does something; //300ms
check interrupt flag;
}

interrupt{
wait 50ms //debounce
clear interrupt;
count ++; //global int (non timer)counter tell how long button held
trip = true; //tell us if button was tripped
}

check interrupt flag{
if trip==true && button ==1         //////BUTTON HELD
{count ++;}

if trip==true && button == 0
{count = 0; trip=0; mode  ++;               ////////BUTTON pressed and released
//reset count, reset trip, increase mode
}

if trip==true && count>
5000                   /////checks how long button held
{count=0;trip=0;sleep}

}

:code:

not that it's important but I am programming in C, not sure if I"m going pic or 8051 route. and trip, mode, and count are global variables. My idea is when the interrupt button is triggered, the micro will finish whatever process it's on and mode++ will cycle it to the next function, until it reaches the max number of functions then it will sleep. if the user however holds the button the micro will finish whatever sub functions its on (i.e. a 3 S process) but when the interrupt flag is checked it will goto sleep. The user can then press the button one more time during the sleep mode and continue (from the beginning of the function) where it left on. The MAIN feature and most important is that the micro continues to do what it does while the button is pressed. I know I'm not the best coder and I don't know micros as good as I should, but am I headed in the right direction? I was considering a timer, but as I said I"m not too fantastic in doing new things. I did code for my project here (which I can't post) and it works great but I saved the hold down button sleep mode as a last minute idea and upon trying out the modified idea it fails miserably. Any help or kind suggestions would be appreciated. *EDITED to clarify button operation*


[ Edited Wed May 21 2014, 08:37 pm ]
Wed May 21 2014, 08:45 pm
#2
looking at my code I realize right now A BIG MISTAKE. if I want the code to enact at 5secons, but during a 3.5S function it'll only count ONCE. If I was to implement a timer do you think it will still operate how I intend? (i.e. timer counts only when button is held, resets if not held)
Thu May 22 2014, 10:24 am
#3
I suggest you use timer in a tick form with a minimum resolution of 100 or 50ms. So your while loop will run every tick. Your while will look something like this.
// NOTE: This is just a pseudo code, there may be lot of scope for improvement
timer_isr()
{
    tick_timeout = 1;
    load_timer_again();
}

//inside main
main()
{
    while (1) {
        while (!tick_timeout);
        tick_timeout = 0;

        if (key_pressed) {
            key_timer++;
            if (key_timer >
= 50) { // I am using 50 assuming tick is of 100ms
                key_timer = 0; // key is pressed for more than 5 sec
                go_to_sleep();
            } else {
                key_timer = 0; //reset timer as key was pressed for only once...
            }
        }
        //rest of the functions you can do based on timer tick
    }
}

This will make your while loop non blocking as tick should never stop. and you do not have to use interrupt for button. You can make it a 100ms polling for key detection.
Thu May 22 2014, 08:13 pm
#4
Wow great idea on the not using a button for interrupt. Thank you. I might switch to your idea as you have a lot of experience (which is why I wanted feedback). Yesterday after the post I did implement a timer but I might scratch the whole idea I was doing (timer starts timer stops when it trips while button is held it would equate to button held). Thanks for the feedback and I will try working with this right away.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

carpinteyrowrl
Fri Apr 19 2024, 02:51 pm
DonaldJAX
Fri Apr 19 2024, 01:08 pm
Lewisuhakeply
Thu Apr 18 2024, 06:00 pm
Darrellciz
Thu Apr 18 2024, 11:07 am
Charlessber
Thu Apr 18 2024, 09:29 am
BartonSem
Thu Apr 18 2024, 04:56 am
DonaldKnown
Thu Apr 18 2024, 12:24 am
utaletxcyw
Wed Apr 17 2024, 10:21 am