 |
 |
| View previous topic :: View next topic |
| Author |
Message |
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Thu May 01, 2014 7:28 am |
|
|
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
|
|
Posted: Thu May 01, 2014 8:43 am |
|
|
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 |
|
 |
|
|
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
|