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

Question Re Timer2 and Timer 3

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



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

Question Re Timer2 and Timer 3
PostPosted: Tue Jul 07, 2009 1:20 pm     Reply with quote

Hi all,

There is a block of code on the forum (Multi-Event Switch Action for Microchip Microcontrollers) that I would like to use, however the code uses Timer2 set for 1 ms ticks. In my code T2 is used for the PWM.

Is there anyway to use the T3 timer to produce the 1 ms ticks (or something close)?

I am using a 18F2520 and I can use a 4 Mhz, 10 Mhz or 20 Mhz xtal. I have V 4.073.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jul 07, 2009 1:50 pm     Reply with quote

This thread has a modified version of Mark's code, so that it will run with
a 4 MHz oscillator:
http://www.ccsinfo.com/forum/viewtopic.php?t=36222

Timer3 is a 16-bit timer, so you will need to preload it with a 16-bit
value, so it can count up to 0xFFFF and overflow, and then cause an
interrupt.

Referring to the code in the link given above, substitute this code for the
Timer2 interrupt routine:
Code:

#define TIMER3_PRESET  0xFF12   // Accurate for 4 MHz

#int_timer3
void timer3_isr(void)
{
 set_timer3(TIMER3_PRESET);

if (Miliseconds < 0xFF)
    Miliseconds++;
}


Substitute this code for the Timer2 code in main():
Code:
set_timer3(TIMER3_PRESET);
setup_timer_3(T3_INTERNAL | T3_DIV_BY_4 );  // Interrupt every 1ms
enable_interrupts(INT_TIMER3);
enable_interrupts(GLOBAL);
frierray



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

PostPosted: Fri Jul 10, 2009 12:55 pm     Reply with quote

PCM programmer,

I made the changes you recommended and it worked on the T3 or T1 time. However at 4 MHz it is slow to react to a key pressed. Mark's code at 20Mhz was much more responsive. To use Mark's code at 20 Mhz would timer T1 be set up like this?

#define TIMER1_PRESET 0xFF5A // 0x12 * 5 = 5A

Ray
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 10, 2009 1:06 pm     Reply with quote

What code are you using to test this ?

Are you using my modified code, given in the link ?

Or are you using Mark's original code ?

Or Mark's code, with the bug fixes given in the post after Mark's
original post in the Code Library ?
frierray



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

PostPosted: Fri Jul 10, 2009 1:23 pm     Reply with quote

PCM programmer,

I am running this on a 18F252 at 4 Mhz to test. I have another card with a 18F252 at 20 Mhz as well.

I used Mark's code as per the link. The code now looks like this. I changed your code from T3 to T1 as it was a unused timer. The T3 will be need later in the project.
Code:

#define TIMER1_PRESET  0xFF12   // Accurate for 4 MHz

#int_timer1
void timer1_isr(void)
{
 set_timer1(TIMER1_PRESET);

if (Miliseconds < 0xFF)
    Miliseconds++;
}

void main (void)
{
  int8 i;

  // Initialize all of the timers and states
  for(i=0; i<NUM_SWITCHES; i++)
     {
      Click_Timer[i] = 0;
      Switch_State[i] = SWITCH_IDLE;
     }
   set_timer1(TIMER1_PRESET);
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_4 );  // Interrupt every 1ms
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);

On the 252 at 20 Mhz the Multi-Event Switch Action for Microchip Microcontrollers code worked great. But the problem is, I need the T2 timer for the PWM. So I thought of using this code, at 20 Mhz but with the T1 timer. From your suggestion I thought I would need to change the preload value by a factor of 5 times.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 10, 2009 2:15 pm     Reply with quote

Which of the three variations of the code do you want to use as the basis
for the final version that runs at 20 MHz ?


1. Mark's original code in the Code Library:
http://www.ccsinfo.com/forum/viewtopic.php?t=22145

2. Mark's code, modified with the bug fixes listed in the post at the end of
the thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=22145
http://www.ccsinfo.com/forum/viewtopic.php?t=22145&start=5

3. My modification of Mark's code:
http://www.ccsinfo.com/forum/viewtopic.php?t=36222
frierray



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

PostPosted: Fri Jul 10, 2009 2:35 pm     Reply with quote

PCM Programmer,

Thanks for all your help.

I would like to use either the code you recommended or Mark's code, modified (with the bug fixes) at 20 Mhz with the T1 timer.

I tested the code in the link you recommended and it works ok, however at 4 Mhz the key released sequence is very slow.

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 10, 2009 2:48 pm     Reply with quote

I agree it does seem a little slow. I'll look at it. Might be later this afternoon.
frierray



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

PostPosted: Fri Jul 10, 2009 2:59 pm     Reply with quote

PCM Programmer,

Thank you so much.

Ray
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 10, 2009 4:06 pm     Reply with quote

Here are the changes that I made to make it work with a 20 MHz crystal
with Timer1. I measured the interrupt period, and it is just about 1 ms.

If you still have a problem with the key release time, explain in detail
how to duplicate it.

Fuse settings and #use delay():
Code:
#include <18F452.h>
#fuses HS, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=20000000)


Timer1 isr:
Code:
#define TIMER1_PRESET (65536 - 5000)  // Interrupt every 5000 counts

#int_timer1
void timer1_isr(void)
{
 set_timer1(TIMER1_PRESET);

  if (Miliseconds < 0xFF)
    Miliseconds++;
}



The Timer1 setup in main():
Code:

setup_timer_1(T1_INTERNAL | T1_DIV_BY_1); // T1 clk = 5 MHz
  set_timer1(TIMER1_PRESET);
  clear_interrupt(INT_TIMER1);
  enable_interrupts(INT_TIMER1);
  enable_interrupts(GLOBAL);
frierray



Joined: 11 Nov 2008
Posts: 29

View user's profile Send private message

PostPosted: Sun Jul 12, 2009 5:13 am     Reply with quote

PCM programmer,

That works great at 20Mhz. Just what I needed, thanks for the help.

Ray
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