View previous topic :: View next topic |
Author |
Message |
darrepac
Joined: 19 Jan 2005 Posts: 22
|
|
Posted: Thu Jan 20, 2005 8:14 am |
|
|
ok I will do this, and other tricks you told me, this evening....
Thanks, keep you in touch! |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Jan 20, 2005 8:45 am |
|
|
And as a further optimization you can replace: Code: | unsigned int8 TMR0;
#locate TMR0=0x0001
#INT_TIMER0
void timer_interrupt(void)
{
static int1 toggle=0;
TMR0 += 117; //(256-139);
if (toggle)
{
output_high(LED);
toggle = 1;
}
else
{
output_low(LED);
toggle = 0;
}
return;
} |
by: Code: | unsigned int8 TMR0;
#locate TMR0=0x0001
#INT_TIMER0
void timer_interrupt(void)
{
TMR0 += 117; //(256-139);
output_toggle(LED);
return;
} |
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Jan 20, 2005 9:12 am |
|
|
ckielstra wrote: | And as a further optimization you can replace: Code: | unsigned int8 TMR0;
#locate TMR0=0x0001
#INT_TIMER0
void timer_interrupt(void)
{
static int1 toggle=0;
TMR0 += 117; //(256-139);
if (toggle)
{
output_high(LED);
toggle = 1;
}
else
{
output_low(LED);
toggle = 0;
}
return;
} |
by: Code: | unsigned int8 TMR0;
#locate TMR0=0x0001
#INT_TIMER0
void timer_interrupt(void)
{
TMR0 += 117; //(256-139);
output_toggle(LED);
return;
} |
|
Note that the output_toggle() is a "relatively" new command. Older versions of the compiler may not have it. There are other ways around it but since this is just a "test" Quote: | but I have done the following trials (quick and dirty code, teh final goal is not to generate output pulse but internal timing) |
I would worry too much about it. |
|
|
darrepac
Joined: 19 Jan 2005 Posts: 22
|
|
Posted: Thu Jan 20, 2005 2:42 pm |
|
|
Ok Let's Go!
First of all, come back from work I have tested again my first original code (you always need a reference before to apply changes)....Oh! It is workign nicely with a good fixed time for Level 0 +1 (=time between 2 rising edges)...
Ok, after taht I implemented the recommendation given about the way to add value to timer 0
Code: | unsigned int8 TMR0;
#locate TMR0=0x0001
#INT_TIMER0
void timer_interrupt(void) //4800*3 /sec , 1/(4800*3) = 69.444us => 69.5us by dividing by 4*2 + overflow at 139
{
//set_timer0(256-139); //cause around 10us time more
//set_timer0 (get_timer0()+256-139);
TMR0 += 118; //(256-139)+1; |
New surprise the result was coming back to the strange I describe in my first post : most of the time 2 rising edges time was 140us but sometimes it was 130us...quite strange...
As I was quite sure it was not coming from the new implementation above, I have continued with the better code from Mark
Code: | //if (debug_pin1 == 0) debug_pin1 = 1;
//else debug_pin1 = 0;
//output_bit (LED, debug_pin1);
if (debug_pin1 == 0)
{
debug_pin1 = 1;
output_high(LED);
}
else
{
debug_pin1 = 0;
output_low(LED);
}
return;
} |
And when you check the resulting compilation result you understand it is far cleaner...
OK! this time it works well!! no more strange thing, no more different time for 2 rising edge...about 140us (target was 139) but it is ok and if needed I presume I can tweak a little bit the 118 value to have the result I want...
yes with 119, it is just perfect...
Thanks to all of you...it is nice to receive your support |
|
|
pat
Joined: 07 Sep 2003 Posts: 40 Location: Adelaide, Australia
|
|
Posted: Tue Jun 28, 2005 5:27 am |
|
|
Does this method of adding an offset to the timer also work for a 16-bit timer? eg timer-1 in 16F876?
The part I'm not sure about is the big endian/little endian order. ie
TMR1L is at 0x0E
TMR1H is at 0x0F
I presume to achieve a 50ms rollover interrupt rate at 4MHz with no prescale you would use this.
Code: |
unsigned int16 TMR1;
#locate TMR1=0x000E
#INT_TIMER1
void timer_interrupt (void) {
TMR1 += 15536; //(65536-50000);
|
|
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Jun 28, 2005 6:02 am |
|
|
Why not use the CCP module and set it up to interrupt on match and reset the timer. You simply load the CCP value and you are done. All that is left is what you want to do every 50ms. |
|
|
pat
Joined: 07 Sep 2003 Posts: 40 Location: Adelaide, Australia
|
|
Posted: Tue Jun 28, 2005 7:26 am |
|
|
Yes that would be easier! I'll give it a go. It would still be interesting to know if the other method of adding an offset to timer-1 would work.
Thanks |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Tue Jun 28, 2005 7:31 am |
|
|
Of course it would work. You just have to be careful since it is a 16 bit value that is changing as you are writing to it. The simplest solution would be to stop the timer, add the offset, start the timer. The CCP is a better approach though and can still be done without resetting the timer if you just keep adding the offset value to the CCP value. But I would do it as a posted before. |
|
|
|