Discussion in "PIC Microcontroller Discussion" started by    chalma    May 21, 2014.
Wed May 21 2014, 11:00 AM
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:
some switch statement (MODE)
(0) function zero ()//sleep
(1) function one();
(2) function two();
(3)function three();
default: mode=0;

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;

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



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, 11:07 AM ]
Wed May 21 2014, 11:15 AM
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, 12:54 AM
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
    tick_timeout = 1;

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

        if (key_pressed) {
            if (key_timer >
= 50) { // I am using 50 assuming tick is of 100ms
                key_timer = 0; // key is pressed for more than 5 sec
            } 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, 10:43 AM
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


Powered by e107 Forum System


Wed Mar 25 2020, 09:39 AM
Sir kindly send the code file i2c for 8051
Fri Mar 13 2020, 06:36 PM
@ Rohitnani,ABEBE..which project ?
Fri Mar 13 2020, 08:55 AM
how can get hadwere of this project
Sat Mar 07 2020, 12:11 PM
Sir, Could you please send me the zip file input password....
Wed Feb 26 2020, 11:58 AM
Peter Parker
Is it possible to send multiple messages using only 1 GSM module?
Sat Feb 01 2020, 05:12 AM
Pearl Olin
Wed Jan 22 2020, 01:16 PM
I m getting errors while simulation process during interfacing socket output and lcd... Can i get the report of ur project which is same as us.
Tue Jan 14 2020, 02:08 AM
shubham kharade
when i am trying to simulate the program in Proteus 8 professional, I am getting some errors as cannot find module file dhtxx.md can u help me
Tue Dec 17 2019, 08:44 AM
hello..i am programming industrial timer using 8051 with 24lc64 eeprom. i have query in storing the 3 diff value in eeprom with enter button. can anyone share c code ?
Tue Dec 17 2019, 05:30 AM
Hello. Can anyone share the code in Bascon and scheme in Proteus?



Thu Apr 09 2020, 12:05 AM
Wed Apr 08 2020, 11:08 PM
Wed Apr 08 2020, 04:57 PM
Wed Apr 08 2020, 03:38 AM
Mon Apr 06 2020, 07:54 PM
Mon Apr 06 2020, 09:13 AM
Sun Apr 05 2020, 10:33 PM
Sun Apr 05 2020, 03:26 AM


Guests: 44, Members: 0 ...

most ever online: 182184
(Members: , Guests: 182184) on 06 Aug 2010: 01:37 AM

Members: 38149
Newest member: ukkorikerist
Forum Activity
PHANTOM28 Avatar
Posted By PHANTOM28
Fri Feb 28 2020, 10:25 AM
Hi.I hope you all can help me. I am relatively ne...
ExperimenterUK Avatar
Posted By ExperimenterUK
Fri Feb 21 2020, 05:05 PM
ajay_bhargav Avatar
Posted By ajay_bhargav
Fri Feb 21 2020, 04:45 AM
Yes you can do that.1. Create a channel @Thingspea...
ananyagupta Avatar
Posted By ananyagupta
Thu Feb 20 2020, 03:58 AM
With regards to the particular characters sent to ...
sarrahjuzar Avatar
Posted By sarrahjuzar
Wed Feb 19 2020, 04:24 AM
I have a gsm module sim900a and dht22 connected to...
ExperimenterUK Avatar
Posted By ExperimenterUK
Tue Feb 18 2020, 04:52 PM
figureyang Avatar
Posted By figureyang
Tue Feb 18 2020, 04:59 AM
figureyang Avatar
Posted By figureyang
Tue Feb 18 2020, 04:50 AM
wounder Avatar
Posted By wounder
Mon Feb 10 2020, 03:32 AM
From below sample code for nuvoton 8051 timer0 mod...
Just4Fun Avatar
Posted By Just4Fun
Sat Feb 01 2020, 09:20 AM
If you want to use a CH55x MCU there isn't any adv...