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

set_timer4() problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Jody



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

set_timer4() problem
PostPosted: Thu Jan 23, 2020 5:30 am     Reply with quote

Hi,
I have a PIC18F87J50 and compiler version 4.119.

I am using an interrupt to control a output.
and want to change the period of that timer.
I am trying to use the set_timer4() function but whatever I put there the period of the output pulse is the same.
I included a sample program.
Anybody have an idea what I am doing wrong ?
Thanks in advance,
Jody

Code:

#include <main.h>

#INT_TIMER4
void  TIMER4_isr(void)
{
   output_toggle(LED);
}

void main()
{
   setup_timer_4(T4_DIV_BY_1,255,1);

   enable_interrupts(INT_TIMER4);
   enable_interrupts(GLOBAL);

   while(true)
   {
      set_timer4(20);//whatever I put here the interupt time is the same
      delay_ms(DELAY);
   }

}


and the main.h
Code:

#include <18F87J50.h>
#device ADC=16

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(oscillator=24MHz,clock=24MHz)

#define LED PIN_J6
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Thu Jan 23, 2020 5:40 am     Reply with quote

Why not just change the timer period?.

setup_timer_4(T4_DIV_BY_1,127,1);

will give you twice the frequency.

However 'beware'. Why not use a PWM fed from the timer, rather than the
interrupt?. Currently this is being called every 256 machine instructions
so will be using about 50% of your entire processor resources.

Set up a PWM, and 0% processor resources to do the same thing.
Jody



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

PostPosted: Thu Jan 23, 2020 6:50 am     Reply with quote

Ok I can do that.
But still... why is it not working ?

PWM is a little too late at this moment. The pcb is already made, and I can not change the PWM output in.

Is there a quick way to stop the interrupts ?
Disable takes a lot of time.


Last edited by Jody on Thu Jan 23, 2020 7:04 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Thu Jan 23, 2020 7:00 am     Reply with quote

What clock frequency are you running?.
What pin are you wanting the output on?.
What frequencies do you want?.
A lot of the pins on that chip can be accessed by one of the PWM
peripherals.
Jody



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

PostPosted: Thu Jan 23, 2020 7:10 am     Reply with quote

I am running at 48MHz.
What I want in total:
When a interrupt (pulse) on RB0 happens, I want to hold high the RB1 pin for 100uS... adjustable.
After the 100uS the RB1 pin must go low and stay low till another interrupt on RB0.

What I have made so far is the interrupt routine for RB0 (this works) and enable timer4 in the interrupt routine. This seems to work,
but stopping the timer4 interrupt (disable_interrupts(int_Timer4)) takes a lot of time.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Thu Jan 23, 2020 7:32 am     Reply with quote

No, the disable_interrupts is one or two instructions (depends on the current
BSR).
What takes a long time is getting out of one interrupt service routine, and
then into the second interrupt service routine.
Probably nearly 70 instructions.....
Honestly if you only need about 100uSec, you are probably going to have to just stay in the INT0 ISR, and handle the pulse yourself.
Jody



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

PostPosted: Thu Jan 23, 2020 7:37 am     Reply with quote

And the interrupt gets cleared after leaving the interrupt routine ?
I will try this right away.
Thanks so far!!
Jody



Joined: 08 Sep 2006
Posts: 182

View user's profile Send private message Send e-mail

PostPosted: Thu Jan 23, 2020 8:13 am     Reply with quote

Yep this works!!!!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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