|
|
View previous topic :: View next topic |
Author |
Message |
Blob
Joined: 02 Jan 2006 Posts: 75 Location: Neeroeteren, Limburg, Belgium
|
polling on Timer interrupt flags |
Posted: Thu Oct 08, 2009 8:48 am |
|
|
Hello All,
I am making a 4 way pulse enlarger.
There are up to 4 inputs of an unknown length, frequency and an unknown sequence.
(once running the sequence remains the same)
With a potmeter the user can change the value that he wants to add to the pulse up to 2ms.
So when the input pulse drops down, the pic has to make the output pulse high during the defined period.
I was thinking on polling the input pins
Code: | while(1)
{
if(!input(IN_0) && IN_0_prev)
{
output_high(LED_0);
}
if(!input(IN_1) && IN_1_prev)
{
output_high(LED_1);
}
if(!input(IN_2) && IN_2_prev)
{
output_high(LED_2);
}
if(!input(IN_3) && IN_3_prev)
{
output_high(LED_3);
}
IN_0_prev = input(IN_0);
IN_1_prev = input(IN_1);
IN_2_prev = input(IN_2);
IN_3_prev = input(IN_3);
output_low(LED_1);
output_low(LED_2);
output_low(LED_0);
output_low(LED_3);
} |
This code works, I checked with an oscilloscope (because LED has no time to charge the junction)
And I am planning to use timer0, timer1, timer2 and timer3 to do the adding job.
I have to use seperate timers because I can not predict when the pulses will come in.
It is possible that IN_0 and IN_1 come up at almost the same time. but it is also possible that IN_0 and IN_3 are at the same time, or not at all at the same time.
So using 1 timer will not do.
Since it takes a while to go to the timer interrupt subroutine, i was thinking of polling on the TIMER interrupt flags TMR0IF/TMR1IF/TMR2IF/TMR3IF in the main. Like we do if we want to poll on an extern interrupt flag.
so the timers will be set according to the potmeter stand when the input pin drops. at the same time the output is set.
when the timer flag is set, the output goes low
Is this possible and is it a good idea to do it like this?
Thanks in advance,
Blob |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Oct 08, 2009 9:45 am |
|
|
I don't normally post my solution to the problem but you may find it interesting.
Code: |
int t0 = 0;
int t1 = 0;
int t2 = 0;
int t3 = 0;
int count = 10;
while(1)
{
if(input(IN_0))
t0 = count; // Reset t0 count
if(input(IN_1))
t1 = count; // Reset t1 count
if(input(IN_2))
t2 = count; // Reset t2 count
if(input(IN_3))
t3 = count; // Reset t3 count
if (t0)
{
output_high(LED_0);
t0--;
}
else
output_low(LED_0);
if (t1)
{
output_high(LED_1);
t1--;
}
else
output_low(LED_1);
if (t2)
{
output_high(LED_2);
t2--;
}
else
output_low(LED_2);
if (t3)
{
output_high(LED_3);
t3--;
}
else
output_low(LED_3);
count = A2D value; // Adjust count here
delay_us(10); // this is used to control the timing!
}
|
Actually, I have not tested this and don't know if it will work at all, let alone to your timings.
But as you can see, you don't need to use timers, unless you need very acurate timing for extending the pulse, in which case I would just use 1 timer and measure the interval.
Something like
Code: |
if(input(IN_0))
t1 = timer1 + count; // int8 or int16 wrap around problem
if (timer1 < t1) // int8 or int16 wrap around problem
output_high(LED_1);
else
{
output_low(LED_1);
t1 = timer1;
}
|
you would need to hadle the wrap around. |
|
|
Blob
Joined: 02 Jan 2006 Posts: 75 Location: Neeroeteren, Limburg, Belgium
|
|
Posted: Fri Oct 09, 2009 12:56 am |
|
|
Thanks Wayne,
It is a nice solution using no timer at all!
But over a night sleep, I figured out I have a short on timers if I want to go for my solution.
I have 4 timers on the 18f242, but I need 1 to determine the frequency of the pulses.
So your second solution will do the job.
I can use timer1 for both the frequency and the extension.
Each time I have a pulse on IN_1, timer 1 will be read and resetted.
In that way I have the frequency, and I am sure that I do not meet a timer overflow during the period. (frequency will be high enough)
Thanks for the hint,
Blob |
|
|
|
|
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
|