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

PROPORTIONAL PRESCALER
Goto page Previous  1, 2, 3, 4
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Mon Apr 07, 2008 3:03 pm     Reply with quote

Tell me if i'm wrong but the slowest interrupt time I can get with using Timer2 is:

setup_timer_2(T2_INTERNAL|T2_DIV_BY_16|255|16)

So for a 4MHz Crystal:

(1/(4M/4/16)) x 256 x 16 = 65.536ms

If this is correct then I cant use this technique to set my variable tick rate because I need the tick to vary between 125ms and 37.5seconds.

Carl
Ttelmah
Guest







PostPosted: Tue Apr 08, 2008 2:13 am     Reply with quote

Of course you can.
Count.....!
Program the interrupt for 62.5mSec. Then do an update every 2 interrupts for the 'fastest' requirement in your range, up to every 600 interrupts for your slowest. Gives 62.5mSec 'steps' over the required range.
Without hardware changes, you are not going to get your slow clock, so you have to use tricks to get it. This is entirely your decision. If instead you fed the clock from (say) a 3.3KHz source, then the hardware could do what you want....

Best Wishes
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Tue Apr 08, 2008 5:17 am     Reply with quote

Yep thats what I thought I'd have to do. the problem is though that the count value will be 300 different values. when a particular count value is reached it needs to do 'what it needs to do' and then reset the count back to zero. Can this be done easily for the different 300 count times??
I suppose its just a matter of including a bit of logic into the statements.

I like your comment Ttelmah:
Quote:
so you have to use tricks to get it


Thankyou again
Carl
Ttelmah
Guest







PostPosted: Tue Apr 08, 2008 10:08 am     Reply with quote

All you do, is count downwards.
Code:

int16 count_to_use=2;

void count_code (void) {
    static int_16 count=1;
    if (--count==0) {
        count=count_to_use;
        //perform required operation here

    }
}

Then when you want to change the count, just set 'count_to_use' to the new count value required. (to avoid glitches, disable the interrupt, and write the new value into the variable, then re-enable the interrupt - otherwise if the value is 'half updated' when the interrupt occurs, you can get an unexpected behaviour).

Best Wishes
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Tue Apr 08, 2008 10:35 am     Reply with quote

Thanks Ttelmah,
you have been a great help.

I will give it a go and let you know



Carl
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Tue Apr 08, 2008 2:19 pm     Reply with quote

Completed

It works!!!!!!!
The code below is compilable for a (1s to 300s) selectable stepup/down and stepdown/up 16bit output.

Code:
#include <18F4525.h>
#fuses NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP,NOPBADEN
#use delay(clock=4000000)
void count_code (void);
long int stepDir3; 
//long int stepDir4;
//float time_init();
long int z,outs2;
long int count_to_use;
static long int count=1;

#int_TIMER2
TIMER2_isr()
{
 
     if (--count==0) {
        count=count_to_use;                     //perform required operation here                                                                                       
    if (stepDir3 == 1){                              //Step Up                                       
         outs2 += 16383;
         output_B(make8(outs2,0));
         output_D(make8(outs2,1));
    }
    else if (stepDir3 == 0) {                        //Step Down
         outs2 -= 16383;
         output_B(make8(outs2,0));
         output_D(make8(outs2,1)); }
     if  (outs2 == 0){                               //Change to Step up next time
         stepDir3 = 1;  }                                                 
     else if (outs2 >= 65532){                    //Change to Step down next time
         stepDir3 = 0;                                                                                                   
 }
}
}
void main() {
   stepDir3 =1;
    z=300;
   count_to_use=(z*4);                        // enable all interupts
    setup_timer_2(T2_DIV_BY_16,243,8);
    enable_interrupts(INT_TIMER2);
    enable_interrupts(global);
    while (TRUE) ;
}



Thats more or less it for my project - all modes are done - the time option is done - and it works - and i've got 3 weeks left to spare.

Thankyou so much everybody for your help (especially Ttelmah, Robert and of course PCM programmer) - I'll try not to bother you again - at least not untill my next project!

Best Regards
Carl
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3, 4
Page 4 of 4

 
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