PIC16F877A OSCILLATOR

HOW TO CREATE 40 KHZ OSCILLATOR USING SOFATWARE (C) IN PIC16F877A ?
vpoolAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

steveiamCommented:
I do this sort of thing using the PWM output.  It means once its running, it doesn't take any processor time to keep it going, and you don't have to service any interrupts.

Alternatively, set up a timer interrupt at 80kHz (note: twice the frequency), and manually toggle the output pin high/low.

For using either the timer, or PWM, the timer load/reload/preload value for a given frequency is:
 
  Timer_Reload = (( <clockspeed> / 4 ) / <prescaler>)  / <frequency>

So, in your case:  <clockspeed> = 20MHz, <frequency> = 40000, and <prescaler> is 'x' the DIV_BY_x used for setting up timer1. If you use, say, DIV_BY_1, then Timer_Reload = 125

Using CCS's PIC-C compiler, my code is:
--------------------------------
// Generating a square wave, using the PWM output

void main()
{
   setup_ccp2(CCP_PWM); // set ccp2 pin as pwm output
   setup_timer_2(T2_DIV_BY_x, Timer_Reload , 1);
   set_pwm2_duty(Timer_Reload/2); // gives a 50% duty cycle

  for (;;)
   {
   // loop, doing nothing
   }
}

--------------------------------
// Generating a square wave, by manually toggling the output pin
#int_timer1
void timer1_isr()
{
  // Clock interrupt service routine. Invoked on Timer1 interrupt.
  set_timer1(-Timer_Reload); // note: negative on the Timer_Reload as it counts _up_

  currentOut = currentOut ^ 0xff;
  output_bit(PIN_xx, currentOut); // set the output pin here
}

void main()
{
  setup_timer_1(T1_INTERNAL|T1_DIV_BY_x);
  enable_interrupts(INT_TIMER1);
  enable_interrupts(GLOBAL);

  for (;;)
   {
   // loop, doing nothing
   }
}
--------------------------------
0
vpoolAuthor Commented:
I am triggering a Ultrasonic Transmitter with this 40 KHz wave.  The input of the driver circuit of Transmitter is attached to PIC  pin SCL , What I exactly trying to achieve is It should trigger TX then wait for a while then again Trigger.....Thus creating......Beep !! .......Beep !! ....Beep !!!
0
vpoolAuthor Commented:
Thanks for reply.....Seteveiam....Cheers
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

steveiamCommented:
If you want to turn the output on or off, you can just enable/disable the timer interrupt, or have a routine that enables/disables the PWM output.

If you can't move the transmitter to one of the CCP pins (or connect it there as well), you'll have to use the interrupt approach.  

Be aware that at an interrupt rate of 80kHz, that's one interrupt every 12.5 microseconds, or 62 processor instructions (at 200nS per instruction).  This means that between each interrupt, you only have enough time to do 62 processor opcodes!  Depending on how much else your processor has to do as well, it may not have a lot of spare time.  This is why I prefer to use the PWM.
0
vpoolAuthor Commented:
Please Advise me On such Gated Transmitter In PIC16f877a
0
steveiamCommented:
Example code for turning it on and off is below.  Is that what you mean? If not, can you please be more specific as to what you're having trouble with.

-------
// If you're using the bit-banged timer interrupt approach
// (once you've set it up as per the code I posted earlier):
  enable_interrupts(INT_TIMER1);  // turns it on
  disable_interrupts(INT_TIMER1); // turns it off
-------
// For PWM:
// (once you've set it up as per the code I posted earlier):
  setup_ccp2(CCP_PWM); // turns it on
  setup_ccp2(CCP_OFF); // turns it off
-------

If you want to transmit a specific number of pulses, you can have the timer1 interrupt counting them as it sends them.  For PWM, you can set an interrupt on the PWM generation, so you can turn it off after the required number of pulses.
0
vpoolAuthor Commented:
I wanted 5 pulses@ 40 khz then a delay doing nothing for a while...And then again five pulses..........

0
vpoolAuthor Commented:
Please tell me how to send 5 to 10 pulses using PWM code that you have given.........
0
steveiamCommented:
You don't need much more than what I've given above.

- Set up the PWM oscillator
- Have a variable that keeps track of how many pulses need to be sent.
- Have an interrupt on the PWM.  This will get triggered as each pulse is started.  e.g. If the PWM is using timer2, set up a timer2 interrupt.
- As each interrupt comes in, decrement the counter.
- When counter is zero, turn off the PWM (in the interrupt routine)

When you want to send another train of pulses:
- Reload the pulse count variable
- Start the PWM

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.