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 Previous  1, 2
 
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: 48

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

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

PCM programmer wrote:
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.


Yes, that is what the code i currently posted do exactly, but it is not my final goal.

Let me state my final goal below:

The code shall be used to enter the menu screen on LCD (am first testing it as an LED in this case).

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


This is the only action i want to execute, i don't need button A3

Overall goal to be put in the final project is:

1. If you push and hold the menu button (button A2) for 5 seconds continuously, the menu or settings menu shall be displayed on the LCD so that you can input the desired system settings, then exit the menu when your done.

2. If you push the menu button (button A2) momentarily, the menu/settings screen shall not be displayed on the LCD, only the LCD back-light shall be turned on for 30s so that you can clearly read what is on the LCD (maybe during night time), then the back-light will be turned off.

So i don't need button A3.

Hope it is quite better now.
colesha



Joined: 09 Jan 2012
Posts: 48

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

PostPosted: Wed Dec 09, 2020 9:40 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 PCM Programmer,

How can i get this code work with 18f4550?

Its not compiling

thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 09, 2020 10:00 am     Reply with quote

It compiles OK for me. I changed the #include line for the PIC to use
18F4550.h and got this result:
Quote:

Compiling C:\Program Files\...\PCH_Test on 09-Dec-20 at 08:00
Memory usage: ROM=1% RAM=1% - 1%
0 Errors, 0 Warnings.
Build Successful.
Loaded C:\Program Files\...\PCH_Test.cof.
BUILD SUCCEEDED: Wed Dec 09 08:00:31 2020

colesha



Joined: 09 Jan 2012
Posts: 48

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

PostPosted: Wed Dec 09, 2020 10:20 am     Reply with quote

PCM programmer wrote:
It compiles OK for me. I changed the #include line for the PIC to use
18F4550.h and got this result:
Quote:

Compiling C:\Program Files\...\PCH_Test on 09-Dec-20 at 08:00
Memory usage: ROM=1% RAM=1% - 1%
0 Errors, 0 Warnings.
Build Successful.
Loaded C:\Program Files\...\PCH_Test.cof.
BUILD SUCCEEDED: Wed Dec 09 08:00:31 2020



It compiled, thanks.
However, the timer doesn't seem to be working as expected. Instead of the led comes on for 5 seconds, its on permanently.

Can you assist with it, please?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 09, 2020 10:51 am     Reply with quote

Change the following line so it adds the parameter shown in bold below:
Quote:
setup_timer_0(T0_INTERNAL | T0_DIV_256 | T0_8_BIT);
colesha



Joined: 09 Jan 2012
Posts: 48

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

PostPosted: Wed Dec 09, 2020 11:04 am     Reply with quote

Thanks, PCM,

It has worked.
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 Previous  1, 2
Page 2 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