View previous topic :: View next topic |
Author |
Message |
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
Timer2 flag control |
Posted: Fri Dec 17, 2021 7:24 am |
|
|
Hello everyone
In this code I wrote, I should normally see 5ms ON 5ms OFF pulse in PIN_D0, is it true? The status I see is 30us ON, 30us OFF. Where do you think is the mistake?
Code: |
#include <18F45K40.h>
//#device ADC=12
#FUSES NOWDT //No Watch Dog Timer
#use delay(crystal=4MHz)
void scan_pins(void)
{
set_timer2(0); //clear timer
clear_interrupt(INT_TIMER2); //clear interrupt flag
while(interrupt_active(INT_TIMER2) == FALSE)
{
//your code to test the pins
output_toggle(PIN_D0);
}
}
void main(void)
{
setup_timer_2(T2_DIV_BY_16 | T2_CLK_INTERNAL,124,5); //2.0 ms overflow, 10.0 ms interrupt
while(TRUE)
{
scan_pins();
}
} |
_________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 17, 2021 7:52 am |
|
|
Quote: |
void scan_pins(void)
{
set_timer2(0); //clear timer
clear_interrupt(INT_TIMER2); //clear interrupt flag
while(interrupt_active(INT_TIMER2) == FALSE)
{
//your code to test the pins
output_toggle(PIN_D0);
}
} |
The routine above is not an interrupt routine. You are missing
#int_timer2 above the function.
I don't know what your intention is. Do you want it to be an interrupt
routine ? If so, do it like this:
Code: |
#int_timer2
void scan_pins(void)
{
}
|
|
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Fri Dec 17, 2021 7:55 am |
|
|
Your code says you want to toggle the pin while the interrupt is not active and skip toggling it when the interrupt triggers. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19500
|
|
Posted: Fri Dec 17, 2021 7:58 am |
|
|
Inside the routine, he clears the interrupt, and then loops so long as
the interrupt is not set. Result a really fast loop (should be only about
ten instructions). Then the interrupt sets, so it takes about twice the time
to then go round the outer loop.
At no point is he actually waiting for the interrupt!.... |
|
|
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Fri Dec 17, 2021 7:59 am |
|
|
Instead of using the timer's interrupt, I set the timer's duration to 10ms and poll the timer's interrupt flag to determine when the duration expires. _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19500
|
|
Posted: Fri Dec 17, 2021 8:50 am |
|
|
But you are not. You are testing when it is _not_ set. Logic is the wrong
way round.....
You need:
Code: |
while(interrupt_active(INT_TIMER2) == FALSE)
; //wait for the flag to set.
//your code to test the pins
output_toggle(PIN_D0); //now do the toggle.
|
|
|
|
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Fri Dec 17, 2021 9:00 am |
|
|
I did not misunderstand because I used parentheses in this way. What is the difference between what you wrote and what I wrote?
Another purpose here is that the flag is scanning that pin for 10ms. It comes out when 10ms is up. Am I wrong? So until the time expires, PIN_D0 pin will be toggled. Code: |
while(interrupt_active(INT_TIMER2) == FALSE)
{
//your code to test the pins
output_toggle(PIN_D0);
} |
_________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Fri Dec 17, 2021 9:07 am |
|
|
You don't seem to understand what while() does. |
|
|
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Fri Dec 17, 2021 9:15 am |
|
|
interrupt_active(INT_TIMER2) == FALSE
so I think it will toggle the PIN_D0 pin until it is FALSE. Am I wrong? _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19500
|
|
Posted: Fri Dec 17, 2021 9:16 am |
|
|
MCUprogrammer wrote: | I did not misunderstand because I used parentheses in this way. What is the difference between what you wrote and what I wrote?
Another purpose here is that the flag is scanning that pin for 10ms. It comes out when 10ms is up. Am I wrong? So until the time expires, PIN_D0 pin will be toggled. Code: |
while(interrupt_active(INT_TIMER2) == FALSE)
{
//your code to test the pins
output_toggle(PIN_D0);
} |
|
No. It sits executing the toggle as fast as it can, till the flag sets.
What I post 'does nothing' till the flag sets. |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Fri Dec 17, 2021 9:18 am |
|
|
It is not an until command, it is a while command. |
|
|
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Fri Dec 17, 2021 9:24 am |
|
|
But what I want is to scan the relevant pin for 10 ms. So I assume the expected 10ms time expires when the flag is TRUE. So I want it to actually check the relevant pin until the flag is set. Was I able to express? _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
gaugeguy
Joined: 05 Apr 2011 Posts: 303
|
|
Posted: Fri Dec 17, 2021 9:58 am |
|
|
Quote: | In this code I wrote, I should normally see 5ms ON 5ms OFF pulse in PIN_D0, is it true? |
No. |
|
|
MCUprogrammer
Joined: 08 Sep 2020 Posts: 221
|
|
Posted: Fri Dec 17, 2021 10:10 am |
|
|
Yes, you are right. that's what i was wondering about. so it is not possible for the code to work in this way then. Did I get right. So, is there an error while waiting for 10ms to expire? I mean, is there such a thing as wrong counting? _________________ Best Regards...
MCUprogrammer
_______________________________
Work Hard |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19500
|
|
Posted: Fri Dec 17, 2021 11:08 am |
|
|
A cosmic ray hit exactly into the cell holding a bit of the counter can
set/clear this. However you are talking probabilities out beyond those
involved in winning a national lottery (hundreds of billions to one), and
all that would happen is that the counter would run correctly next time
round.
If you have error conditions that ned to be tested, then just test these
in the 'while' that waits for the bit to set. |
|
|
|