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

Timer2 with 18F2431

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







Timer2 with 18F2431
PostPosted: Mon Apr 14, 2008 1:44 am     Reply with quote

Hello,

I need to generate a PWM manually, so i use the timer2 function.
I speak english language too bad to understand the help of the software.

Code:
setup_timer2(T2_DIV_BY_4,0xc0,2); 
//sets the prescale as 4, period as 0xc0 and postscales as 2.
//At 20Mhz timer2 will increment every .8us in this
//setup overflows every 154.4us and interrupt every 308.2us


I use too an extern clock at 20Mhz and the 18F2431 PIC.

I would generate an overflow at 10 us.

If somebody could help me. Thanks.
Matro
Guest







PostPosted: Mon Apr 14, 2008 1:56 am     Reply with quote

Code:

setup_timer_2(T2_DIV_BY_1,49,1);

At 20 Mhz, the frequency of PWM will be 100 kHz (timer 2 overflows every 100µs).

Matro.
Guest_18F2431
Guest







PostPosted: Mon Apr 14, 2008 2:10 am     Reply with quote

Matro wrote:
Code:

setup_timer_2(T2_DIV_BY_1,[color=red]49[/color],1);

At 20 Mhz, the frequency of PWM will be 100 kHz (timer 2 overflows every 100µs).



Every 10 us? 1/100000Hz = 10us i think.

What the 49 correspond to?
Ttelmah
Guest







PostPosted: Mon Apr 14, 2008 2:20 am     Reply with quote

The '49', is the count to be used to reset the timer. Since the timer counts from '0', the actual number of counts used, is this number plus 1.
So the timer takes the master oscillator, divided by 4, then divided by 1 (prescaler =1), then by 50.
Gives 100KHz, and as you say 10uSec (I'd suspect just a simple typing error).

Best Wishes
Guest_18F2431
Guest







PostPosted: Mon Apr 14, 2008 2:30 am     Reply with quote

Ok, i begin to understand.

Code:
setup_timer_2(T2_DIV_BY_1,49,1);

Quote:
So the timer takes the master oscillator, divided by 4, then divided by 1 (prescaler =1), then by 50.


Where i can find the value 4?
Matro
Guest







PostPosted: Mon Apr 14, 2008 2:35 am     Reply with quote

That was a mistake. Of course it is 10µs.
Sorry for that but I see that you correct by yourself. ;-)

Matro.
Matro
Guest







PostPosted: Mon Apr 14, 2008 2:36 am     Reply with quote

Guest_18F2431 wrote:
Ok, i begin to understand.

Code:
setup_timer_2(T2_DIV_BY_1,49,1);

Quote:
So the timer takes the master oscillator, divided by 4, then divided by 1 (prescaler =1), then by 50.


Where i can find the value 4?

4 is present in all datasheets. An instruction cycle is done in 4 clock cycles in a PIC.

Matro.
18F2431
Guest







PostPosted: Mon Apr 14, 2008 2:37 am     Reply with quote

Okay, governement still hiding facts !

Thank you, i will probably continue asking question here ;)
Ttelmah
Guest







PostPosted: Mon Apr 14, 2008 2:38 am     Reply with quote

The 'peripheral clock', used by just about everything inside the PIC, is the same rate as the 'instruction clock', Fosc/4. This is in the data sheet. If you look at the diagrams for the timers, and a lot of other stuff, the sources (depending on the ones you look at), will be various external pins or oscillators, and 'Fosc/4'. This is the /4 factor.

Best Wishes
18F2431
Guest







PostPosted: Mon Apr 14, 2008 3:01 am     Reply with quote

Quote:
time=get_timer2();


So, with the previously configurated timer, i would generate an overflow at 10 us.
I have to get the value in a variable. But after, how make an "if" with this value.

Example :

Quote:
if (time==10us)
{
TMR2IF = 1 //to do an overflow ?
}


I have problems here. 10us, i cannot put like that.
Will my TMR2IF=1 working? [/code]
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