View previous topic :: View next topic |
Author |
Message |
Guest Guest
|
Newbee question regarding lcd |
Posted: Mon Jun 06, 2005 9:39 am |
|
|
Good day to all!
This is my situation and my problem.
I want to shut down my lcd after 10 minutes, but 10 minutes after my last action. How to make this?. How to define this as some "global" check, so that the timeout will be triggered 10 minutes after some action ( eg. keypress ) I have timer0 running on 16F84a. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Mon Jun 06, 2005 11:01 am |
|
|
Setup a timer to interrupt at a certain interval.
Set a var to some value which would be your timeout.
Decrement this var at the given interval.
If the var reaches 0 then turn off the LCD.
If a keypress comes in then reset the value. |
|
|
valemike Guest
|
|
Posted: Mon Jun 06, 2005 2:26 pm |
|
|
In my case, i have timer 1 being used as a timer interrupt.
Code: |
#int TIMER_1
void clock_isr(void)
{
...
if (GLOBAL_keypad_inactivity_timer > 0)
{
GLOBAL_keypad_inactivity_timer--;
}
}
|
Part of my main() loop looks at the value of my timer value:
Code: |
void main(void)
{
...
while (1)
{
....
else if (GLOBAL_keypad_inactivity_timer == 0)
{
blank_display();
GLOBAL_substate_keypad = KEYPAD_STATE_IDLE;
}
...
}
}
|
In the event of a key hit, then I reset this timer:
Code: |
void restart_keypad_inactivity_timer(void)
{
GLOBAL_keypad_inactivity_timer = 5400;
}
|
In my case if timer1 overflows (and thus interrupts) 5400 times, then my value will be decremented down to 0. You would call the function restart_keypad_inactivity_timer() everytime you get a keyhit.
I used 5400 in my example to get a 2 minute inactivity timer figured out, but in your case, you neeed to figure out the value for 10 minutes. You'll most likely need to use 'unsigned long' since your number can get pretty big. |
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
|
Posted: Mon Jun 06, 2005 2:28 pm |
|
|
My example above is basically the same thing as what Mark says. I just happened to be doing the same thing currently in the program i'm writing, and figured a few cut and pastes onto this thread would help illustrate things better.
Mike |
|
|
jahan
Joined: 04 Apr 2005 Posts: 63
|
|
Posted: Thu Jul 14, 2005 2:57 pm |
|
|
valemike wrote: | In my case, i have timer 1 being used as a timer interrupt.
Code: |
#int TIMER_1
void clock_isr(void)
{
...
if (GLOBAL_keypad_inactivity_timer > 0)
{
GLOBAL_keypad_inactivity_timer--;
}
}
|
Part of my main() loop looks at the value of my timer value:
Code: |
void main(void)
{
...
while (1)
{
....
else if (GLOBAL_keypad_inactivity_timer == 0)
{
blank_display();
GLOBAL_substate_keypad = KEYPAD_STATE_IDLE;
}
...
}
}
|
In the event of a key hit, then I reset this timer:
Code: |
void restart_keypad_inactivity_timer(void)
{
GLOBAL_keypad_inactivity_timer = 5400;
}
|
In my case if timer1 overflows (and thus interrupts) 5400 times, then my value will be decremented down to 0. You would call the function restart_keypad_inactivity_timer() everytime you get a keyhit.
I used 5400 in my example to get a 2 minute inactivity timer figured out, but in your case, you neeed to figure out the value for 10 minutes. You'll most likely need to use 'unsigned long' since your number can get pretty big. |
At what point you will turn on the LCD?
I'm missing the last point here ...
Thankx |
|
|
MikeValencia
Joined: 04 Aug 2004 Posts: 238 Location: Chicago
|
|
Posted: Thu Jul 14, 2005 3:29 pm |
|
|
The function i showed you merely keeps track of time elapsed. When the timer hits zero, and stays at zero, you will have timed out.
It's your code; you can turn on the LED whenever you feel like it. But when you do turn on the LED, don't forget to call the restart_keypad_inactivity_timer() function. And if your LCD is already on, don't forget to call restart_keypad_inactivity_timer() everytime you press a key. |
|
|
Guest
|
|
Posted: Mon Jul 18, 2005 12:42 pm |
|
|
Here is the code I said i'll give you.
RA4: Pushbutton
After two minutes of inactivity, it will printf "Turn off LCD here" on Hyperterminal.
Code: |
#include <18F448.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT
#use delay(clock=8000000)
#case
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#ignore_warnings 202
#byte port_a=0xf80
void scan_pushbutton(void);
void restart_countdown(void);
unsigned long GLOBAL_keypad_inactivity_timer;
unsigned int GLOBAL_button_flag;
enum
{
STATE_BACKLIGHT_OFF,
STATE_BACKLIGHT_ON
};
#INT_TIMER1 // This function is called every time
void clock_isr() // timer 1 overflows (65535->0), which is
{ // approximately 19 times per second for
// this program.
if (GLOBAL_keypad_inactivity_timer > 0)
{
GLOBAL_keypad_inactivity_timer--;
}
}
void main(void)
{
int state;
restart_countdown();
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1);
set_timer1(0);
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
state = STATE_BACKLIGHT_OFF;
printf ("Start: Backlight initially OFF\r\n");
while (1)
{
scan_pushbutton(); // This updates the state of the pushbutton.
switch (state)
{
case STATE_BACKLIGHT_OFF:
if (GLOBAL_button_flag)
{
printf ("Turn on LCD here.\r\n");
printf (" restart x-minute timer\r\n");
restart_countdown();
state = STATE_BACKLIGHT_ON;
}
break;
case STATE_BACKLIGHT_ON:
if (GLOBAL_button_flag)
{
printf (" restart x-minute timer\r\n");
}
else if (GLOBAL_keypad_inactivity_timer == 0)
{
printf ("Turn off LCD here.\r\n");
state = STATE_BACKLIGHT_OFF;
}
break;
default:
break;
}
}
}
void scan_pushbutton(void)
{
if (GLOBAL_button_flag)
{
if (port_a & 0x10) // input has gone up
{
delay_cycles(10);
if (port_a & 0x10)
{
GLOBAL_button_flag = 0;
}
}
}
else
{
if (!(port_a & 0x10))
{
delay_cycles(10);
if (!(port_a & 0x10))
{
GLOBAL_button_flag = 1;
}
}
}
}
// 8000000/(4*1*65535) * 60 minutes/sec * 2 minutes = 3662
void restart_countdown(void)
{
GLOBAL_keypad_inactivity_timer = 3662;
}
|
|
|
|
|