CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Turn LCD Light ON and OFF to save power
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
colesha



Joined: 09 Jan 2012
Posts: 45

View user's profile Send private message Send e-mail

Turn LCD Light ON and OFF to save power
PostPosted: Mon Jul 27, 2020 11:47 am     Reply with quote

Hellos,

I am trying to set up a simple code that can turn the LCD light on when any button is placed so that the user can navigate through the menu or view what is on the LCD and after a certain time delay the light goes OFF. I want to do this without letting my program become trapped in the LCD light OFF delay Loop until it's turned off.

I have tried to look all over but i failed, maybe I am using a wrong term to look for it.

Please kindly help about it.

Thanks.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Jul 27, 2020 12:26 pm     Reply with quote

One way is to have the 'buttons' activate an interrupt.
When any button initiates the ISR, the first action within the ISR is to turn on the LCD backlighting. After a fixed time,either the ISR or main() will turn off the LCD backlighting.
I'm sure there's 100 different ways to do it....
colesha



Joined: 09 Jan 2012
Posts: 45

View user's profile Send private message Send e-mail

PostPosted: Tue Jul 28, 2020 11:48 am     Reply with quote

temtronic wrote:
One way is to have the 'buttons' activate an interrupt.
When any button initiates the ISR, the first action within the ISR is to turn on the LCD backlighting. After a fixed time,either the ISR or main() will turn off the LCD backlighting.
I'm sure there's 100 different ways to do it....


Thanks Temtronic,

Can you kindly give a structure how i can do that ? Since i already have my menu push button to perform a ISR for the menu, how can i best handle it?
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jul 28, 2020 12:26 pm     Reply with quote

Simply add a line of code within the ISR to turn on the LCD backlight power.
You'll need to add some sort of code to turn it off, say after 500ms of no 'button' action ?
It's hard to tell you what to do without seeing your code.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jul 29, 2020 3:02 am     Reply with quote

colesha wrote:

How can i best handle it?

Here is one way to do it. In this program, the isr runs all the time.
It can be re-written so the isr only runs when needed.

In the program below, you turn on the LED by calling the turn_on_led()
function with the desired run time in seconds. The isr counts off the
number of seconds and then turns off the LED.

I think you are using a 16F887. I don't have a working one so I used a
16F877 instead. Test program:
Code:
#include<16F877.h>
#fuses HS, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=20M)
#use rs232(baud=9600, UART1, ERRORS)

#define INTS_PER_SECOND  76
#define LED_PIN  PIN_B0

int8 led_seconds_timer = 0;

//--------------------------
#INT_TIMER0
void t0_isr(void)
{
static int8 int_count = INTS_PER_SECOND;

int_count--;

if(int_count == 0)  // Has 1 second passed ?
  {
   int_count = INTS_PER_SECOND;  // If so, reload counter

   if(led_seconds_timer)  // Is the LED timer running ?
     {
      led_seconds_timer--;  // If so, decrement it

      if(led_seconds_timer == 0)  // Is it done ?
        {
        output_low(LED_PIN); // If so, turn off the LED
        }
     }
  }

}

//--------------------------------
// This function turns on the LED and sets the
// number of seconds that it will stay on.

void turn_on_led(int8 seconds)
{
output_high(LED_PIN);  // Turn on the LED

led_seconds_timer = seconds; // Start the LED timer
}

//======================
void main()     
{
output_low(LED_PIN);  // LED is initially off

setup_timer_0(T0_INTERNAL | T0_DIV_256);
set_timer0(0);
clear_interrupt(INT_TIMER0);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);

turn_on_led(5);   // Run LED for 5 seconds
 
while(TRUE);           
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Jul 29, 2020 4:21 am     Reply with quote

and the key 'point' here is that the main program can keep running,
scanning the keys etc., and override the LED settings and time settings
if required. Instead of sitting 'in' a wait, the wait is being done by the
counter in the interrupt.
colesha



Joined: 09 Jan 2012
Posts: 45

View user's profile Send private message Send e-mail

PostPosted: Wed Jul 29, 2020 1:22 pm     Reply with quote

Thanks, temtronic, PCM programmer and Ttelmah.

Let me do it as you have suggested.

True am using a 16F877 as PCM programmer has stated.

Will give you feedback on the progress.

Thanks
colesha



Joined: 09 Jan 2012
Posts: 45

View user's profile Send private message Send e-mail

PostPosted: Sat Aug 01, 2020 2:16 am     Reply with quote

PCM programmer wrote:
colesha wrote:

How can i best handle it?

Here is one way to do it. In this program, the isr runs all the time.
It can be re-written so the isr only runs when needed.

In the program below, you turn on the LED by calling the turn_on_led()
function with the desired run time in seconds. The isr counts off the
number of seconds and then turns off the LED.

I think you are using a 16F887. I don't have a working one so I used a
16F877 instead. Test program:
Code:
#include<16F877.h>
#fuses HS, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=20M)
#use rs232(baud=9600, UART1, ERRORS)

#define INTS_PER_SECOND  76
#define LED_PIN  PIN_B0

int8 led_seconds_timer = 0;

//--------------------------
#INT_TIMER0
void t0_isr(void)
{
static int8 int_count = INTS_PER_SECOND;

int_count--;

if(int_count == 0)  // Has 1 second passed ?
  {
   int_count = INTS_PER_SECOND;  // If so, reload counter

   if(led_seconds_timer)  // Is the LED timer running ?
     {
      led_seconds_timer--;  // If so, decrement it

      if(led_seconds_timer == 0)  // Is it done ?
        {
        output_low(LED_PIN); // If so, turn off the LED
        }
     }
  }

}

//--------------------------------
// This function turns on the LED and sets the
// number of seconds that it will stay on.

void turn_on_led(int8 seconds)
{
output_high(LED_PIN);  // Turn on the LED

led_seconds_timer = seconds; // Start the LED timer
}

//======================
void main()     
{
output_low(LED_PIN);  // LED is initially off

setup_timer_0(T0_INTERNAL | T0_DIV_256);
set_timer0(0);
clear_interrupt(INT_TIMER0);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);

turn_on_led(5);   // Run LED for 5 seconds
 
while(TRUE);           
}


Hello,

This code worked perfectly as expected thanks.

Can i use the same interrupt to say like, if i want the menu to be displayed, one has to press and hold the menu key for 3 secs, then the menu will display or i have to set a similar code for specifically the menu key?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Aug 01, 2020 9:09 am     Reply with quote

colesha wrote:

Can i use the same interrupt to say like, if i want the menu to be displayed, one has to press and hold the menu key for 3 secs, then the menu will display or i have to set a similar code for specifically the menu key?

Yes, you can use the same interrupt. It runs continuously.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sat Aug 01, 2020 10:54 am     Reply with quote

and (of course), there is nothing to stop you having multiple separate
counters all operated by the one interrupt.
colesha



Joined: 09 Jan 2012
Posts: 45

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 11, 2020 4:56 pm     Reply with quote

Ttelmah wrote:
and (of course), there is nothing to stop you having multiple separate
counters all operated by the one interrupt.


Hello guys, it seems i just can't figure this out. I have tried it several times but not getting off the hook. I have come up with one but not working as i like it to. If i first press the button on PIN_A3 once then i press and hold the button on PIN_A2 for 5 seconds, the LED will come off. To turn it on, I have to press again button on PIN_A3 first once, always.
I just want only button on A2 to be pressed and held for 5 seconds so that the Led can come off.

Below is what i have
Code:

#include<16F877.h>
#fuses HS, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=20M)
//#use rs232(baud=9600, UART1, ERRORS)

#define INTS_PER_SECOND  76
#define LED_PIN  PIN_C2


int8 button_seconds_timer = 0;




//--------------------------
#INT_TIMER0
void t0_isr(void)
{
static int8 int_count = INTS_PER_SECOND;

int_count--;

if(int_count == 0)  // Has 1 second passed ?
  {
   int_count = INTS_PER_SECOND;  // If so, reload counter
 
   if(input(PIN_A2)==0) // Is the button timer running ?
     {
      button_seconds_timer--;  // If so, decrement it
 
      if(button_seconds_timer == 0)  // Is it done ?
        {
     
        output_low(LED_PIN); // If so, turn off the LED
        }
     }
  }

}

//--------------------------------
// This function turns on the LED and sets the
// number of seconds that it will stay on.

void turn_on_led(int8 seconds)
{
output_high(LED_PIN);  // Turn on the LED

button_seconds_timer = seconds; // Start the button timer
}

//======================
void main()     
{
output_low(LED_PIN);  // LED is initially off

setup_timer_0(T0_INTERNAL | T0_DIV_256);
set_timer0(0);
clear_interrupt(INT_TIMER0);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
while(TRUE)
{
if(input(PIN_A3)==0) // Menu key
turn_on_led(5);   // Wait 5 seconds to turn on LED
output_high(PIN_C0);
delay_ms(100);
output_low(PIN_C0);
delay_ms(100);
  }
}


Some assistance please
gaugeguy



Joined: 05 Apr 2011
Posts: 286

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 6:21 am     Reply with quote

If button_seconds_timer is at zero, then it will take 25.6 seconds for the decrement to get it back to zero again.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Aug 12, 2020 9:27 pm     Reply with quote

Quote:
If i first press the button on PIN_A3 once then i press and hold the button on PIN_A2 for 5 seconds, the LED will come off. To turn it on, I have to press again button on PIN_A3 first once, always.
I just want only button on A2 to be pressed and held for 5 seconds so that the Led can come off.

Can you re-state this more clearly ? I can't figure out exactly what you want.
colesha



Joined: 09 Jan 2012
Posts: 45

View user's profile Send private message Send e-mail

PostPosted: Thu Aug 13, 2020 4:10 am     Reply with quote

PCM programmer wrote:
Quote:
If i first press the button on PIN_A3 once then i press and hold the button on PIN_A2 for 5 seconds, the LED will come off. To turn it on, I have to press again button on PIN_A3 first once, always.
I just want only button on A2 to be pressed and held for 5 seconds so that the Led can come off.

Can you re-state this more clearly ? I can't figure out exactly what you want.


I have a push button connected on pin A2. I want to use this push button to turn off an led, but only if i press and hold it for say 5 seconds. Then the led shall be turned off (i intend to use this code to enter a menu screen only if the user presses and holds the menu button for 5 seconds).

Now back to the code i have posted.

I included this in the ISR so that the counter can start when this button is pressed.

Code:
if(input(PIN_A2)==0) // Is the button timer running ?
     {
      button_seconds_timer--;  // If so, decrement it
 


However in my main, it won't work if i only call this function:
Code:

turn_on_led(5); 


But if i write it like this and add another push button, it will work only if i first press the newly added button once. (i.e press button 2 once, then press and hold button 1 for 5 seconds, thus the led will turn off. Again press button 2 once the led will come on and then press button 1 for 5 seconds, led will go off, always thats the routine).
Code:

if(input(PIN_A3)==0) // Menu key
turn_on_led(5);   // Wait 5 seconds to turn on LED


I want to use only button 1 to achieve this, just like you press and hold down a computer power button for some seconds if you what to force it to shut down without following the normal shutdown process.

Thanks again.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 13, 2020 1:06 pm     Reply with quote

You still didn't completely state your goal, so I'll do it:

1. If you push button A3 momentarily, the LED will go on.

2. If you push and hold button A2 for 5 seconds continuously, then the LED
will go off.


If that's not it, then say so.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group