View previous topic :: View next topic |
Author |
Message |
salmankhalid16
Joined: 18 Dec 2009 Posts: 27
|
TIMER ISSUE |
Posted: Fri Jun 13, 2014 12:13 am |
|
|
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: 19488
|
|
Posted: Fri Jun 13, 2014 12:36 am |
|
|
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
|
|
Posted: Fri Jun 13, 2014 10:52 am |
|
|
I m using pic16F876..
can you please post the code to do so... |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1933 Location: Norman, OK
|
|
Posted: Fri Jun 13, 2014 3:22 pm |
|
|
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!!!! |
|
|
|