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 ISSUE

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



Joined: 18 Dec 2009
Posts: 27

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

TIMER ISSUE
PostPosted: Fri Jun 13, 2014 12:13 am     Reply with quote

Hello all

I am using PIC16F876 with 16Mhz crystal
I am unable to produce a 100KHz square waveform using timer2 interrupts.

the timer 2 command i give is

setup_timer_2(T2_div_BY_1, 40, 1)

and then in ISR i just toggle pin B2.
but the frequency i am getting is only 51KHz.

where i am going wrong.

2. Also what to do if i want to generate 100Khz waveform of different duty cycles.

Please help
Ttelmah



Joined: 11 Mar 2010
Posts: 19447

View user's profile Send private message

PostPosted: Fri Jun 13, 2014 12:36 am     Reply with quote

Use the PWM.

Interrupts take time. It takes typically about 30 instruction times to get into an ISR, and about the same to get out. So your 'toggle pin' ISR, will take about 65+ instruction times to execute. The behaviour will be erratic, as sometimes the interrupt may have triggered just as you leave the routine and sometimes at different points in the cycle, so you'll probably see momentary groups of pulses up to perhaps 60KHz, and then groups of slower pulses.

You just can't do things at this sort of frequency using an ISR on the PIC.

If you want a pulse train, then that is what the PWM is 'for' allowing pulses to be generated without the code having to do anything.
salmankhalid16



Joined: 18 Dec 2009
Posts: 27

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

PostPosted: Fri Jun 13, 2014 10:52 am     Reply with quote

I m using pic16F876..
can you please post the code to do so...
dyeatman



Joined: 06 Sep 2003
Posts: 1932
Location: Norman, OK

View user's profile Send private message

PostPosted: Fri Jun 13, 2014 3:22 pm     Reply with quote

Why did you start a new thread when you already had one going here:
http://www.ccsinfo.com/forum/viewtopic.php?t=52469

This sounds very much like a school project.
We don't write code for people here, we only help debug.
_________________
Google and Forum Search are some of your best tools!!!!
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