We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

PIC16F877A OSCILLATOR

vpool
vpool asked
on
Medium Priority
791 Views
Last Modified: 2012-08-13
HOW TO CREATE 40 KHZ OSCILLATOR USING SOFATWARE (C) IN PIC16F877A ?
Comment
Watch Question

Commented:
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
   }
}
--------------------------------

Author

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 !!!

Author

Commented:
Thanks for reply.....Seteveiam....Cheers

Commented:
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.

Author

Commented:
Please Advise me On such Gated Transmitter In PIC16f877a

Commented:
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.

Author

Commented:
I wanted 5 pulses@ 40 khz then a delay doing nothing for a while...And then again five pulses..........

Author

Commented:
Please tell me how to send 5 to 10 pulses using PWM code that you have given.........
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.