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 CCS Technical Support

Simultaneous control of delay operated LEDs
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
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Thu May 01, 2014 7:28 am     Reply with quote

As Ttelmah writes, timer 2 works differently to the others. Its easy to get them confused.

Timer 2 counts up and automatically resets at a preset count determined by a period register. So, if the period is 249, it counts from 0 to 249, then resets to 0 and counts again. Every time it resets, you get a interrupt. There is no need to keep on resetting the count. That makes it good for accurate, repetitive timing with minimum firmware overhead, such as a clock tick/timebase.

The other timers, 1 and 3, are set to a value by the user, then they count up until they reach their maximum count and wrap to 0. An interrupt is generated when they wrap. Thus the wrap interval is related to the max count minus the desired time. To get repetitive time periods, the firmware must reset the count in the ISR. This can give timing errors depending on the count value.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu May 01, 2014 8:43 am     Reply with quote

Try this code.
I've used my previous code with your pin defines, and the new requirements.
You will have to modify to suit your clock and PIC.
The code compiles OK, but I have not tested on real hardware.
It's fairly trivial so should be close.

What SHOULD happen:-

The LED_ONE turns on when PUSH_BUTTON_ONE pressed.
The LED_TWO turns on when PUSH_BUTTON_TWO pressed.

The LED_ONE stays on 5 seconds after PUSH_BUTTON_ONE released.
The LED_TWO stays on 10 seconds after PUSH_BUTTON_TWO released.

Code:
#include <18F458.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=16000000)

#define LED_ONE PIN_B2
#define LED_TWO PIN_B3

#define PUSH_BTN_ONE PIN_B4
#define PUSH_BTN_TWO PIN_A3

signed int16   led_one_timer;            //
signed int16   led_two_timer;

#int_timer2
void update_timers()
{
   if (led_one_timer > 0) { led_one_timer--; }
   if (led_two_timer > 0) { led_two_timer--; }
}

void main()
{
   setup_timer_2(T2_DIV_BY_16, 249, 1);          // Generates 1ms tick
   enable_interrupts(int_timer2);
   enable_interrupts(global);

   led_one_timer = led_two_timer = 0;          // clears timers
   output_low(LED_ONE);                        // forces defined state
   output_low(LED_TWO);                        //   "

   while( TRUE )
   {
      // Tests each timer for time out & resets led on zero

      if (led_one_timer==0) { output_low(LED_ONE); }
      if (led_two_timer==0) { output_low(LED_TWO); }

      // Tests each button, turns correct LED on when pressed and sets timer
      // Input assumed to be low when button pressed, ie using pull-up resistors

      if (PUSH_BTN_ONE==0) 
      {
         led_one_timer = 5000; 
         output_high (LED_ONE) ;
      }

      if (PUSH_BTN_TWO==0) 
      {
         led_two_timer = 10000; 
         output_high (LED_TWO) ;
      }
   }
}

You will be able to operate both buttons either one after the other or together.
I have not done any de-bouncing etc. I leave that as a exercise for you.

Mike
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