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 support@ccsinfo.com

Timer setting on pic24JG16GA004

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



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

Timer setting on pic24JG16GA004
PostPosted: Sat Jan 15, 2022 11:47 am     Reply with quote

Dear friends,

I have a problem when i try to set my timer.
I'm not able to understand correctly how it works.

My code
Code:

#FUSES FRC_PLL, NOIOL1WAY, NOPROTECT, NOWDT, NOWRT, NOOSCIO, NODEBUG,SOSC,NOPR,NOCKSFSM
#USE delay(internal=32MHz)

setup_timer1(TMR_INTERNAL | TMR_DIV_BY_64,7500);
enable_interrupts(int_timer1);


And in my interruption (overflow) function :
Code:

#int_timer1
void timer1()
{
   disable_interrupts(int_timer1);
   
   output_high(led_rouge);
   delay_ms(1);
   output_low(led_rouge);

   setup_timer1(TMR_INTERNAL | TMR_DIV_BY_64,7500);
   enable_interrupts(int_timer1);
}


it's work but i can't validate by calculation...

For me, the formula is :

Freq = (Fosc) / { Tick * TMR_div * (16bits - value))
with Fosc = 32Mhz
Tick = 2 (4 for a 16F/18F)
TMR_DIV = 64
Value = 7500.

My scope say me 33Hz / by calculation 4 hertz...

What is wrong ?
Shocked
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sat Jan 15, 2022 1:02 pm     Reply with quote

Peripherals in the 16bit PIC's, by default are fed with Fosc/2.

Your timer will give:

32000000/(2*7500*64) = 33.3Hz.

Your calculation is simply wrong......

Don't see where you are getting '4' from anywhere....

You have the right values, but the maths is just wrong.

Freq = (Fosc) / { Tick * TMR_div * (16bits - value))
= 32000000/(2*64 *7500)

32000000/960000 = 33.33

No '4' anywhere!......
joseph20480



Joined: 21 Dec 2011
Posts: 42

View user's profile Send private message

PostPosted: Sat Jan 15, 2022 1:49 pm     Reply with quote

2 for 16bits mcu
4 for 8 bits mcu
... the number clock needed per instructions.

Thanks a lot
[Solved !]

Ttelmah wrote:
Peripherals in the 16bit PIC's, by default are fed with Fosc/2.

Your timer will give:

32000000/(2*7500*64) = 33.3Hz.

Your calculation is simply wrong......

Don't see where you are getting '4' from anywhere....

You have the right values, but the maths is just wrong.

Freq = (Fosc) / { Tick * TMR_div * (16bits - value))
= 32000000/(2*64 *7500)

32000000/960000 = 33.33

No '4' anywhere!......
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sun Jan 16, 2022 4:29 am     Reply with quote

You were saying "by calculation 4 hertz...". That is simply wrong.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sun Jan 16, 2022 7:28 am     Reply with quote

Some more comments though on the code posted:

Code:

#int_timer1
void timer1()
{
   //disable_interrupts(int_timer1); //not wanted or needed. The
   /interrupt is automatically disabled inside the handler
   
   output_high(led_rouge);
   delay_ms(1);
   output_low(led_rouge);

   //setup_timer1(TMR_INTERNAL | TMR_DIV_BY_64,7500); //Not wanted
   //or needed. The timer keeps it's settings.
   //enable_interrupts(int_timer1); //again not wanted or needed.
}

Hopefully using the delay inside here was just for test.
jeremiah



Joined: 20 Jul 2010
Posts: 1315

View user's profile Send private message

PostPosted: Sun Jan 16, 2022 9:50 am     Reply with quote

For some reason I also thought you wanted to put a period one less than your desired value? For example with a 32KHz crystal we typically load 32767 instead of 32768 for a 1 second ISR. I feel like the IF flag triggers after period value instead of immediately? Or am I remembering wrong.
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sun Jan 16, 2022 11:05 am     Reply with quote

No, you are remembering exactly right. However the time result is still
33.3, the difference is only in the next decimal.
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