Link to home
Start Free TrialLog in
Avatar of vpool
vpool

asked on

PIC16F877A OSCILLATOR

HOW TO CREATE 40 KHZ OSCILLATOR USING SOFATWARE (C) IN PIC16F877A ?
Avatar of steveiam
steveiam

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
   }
}
--------------------------------
Avatar of vpool

ASKER

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 !!!
Avatar of vpool

ASKER

Thanks for reply.....Seteveiam....Cheers
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.
Avatar of vpool

ASKER

Please Advise me On such Gated Transmitter In PIC16f877a
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.
Avatar of vpool

ASKER

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

Avatar of vpool

ASKER

Please tell me how to send 5 to 10 pulses using PWM code that you have given.........
ASKER CERTIFIED SOLUTION
Avatar of steveiam
steveiam

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial