?
Solved

PIC16F877A OSCILLATOR

Posted on 2006-03-20
9
Medium Priority
?
725 Views
Last Modified: 2012-08-13
HOW TO CREATE 40 KHZ OSCILLATOR USING SOFATWARE (C) IN PIC16F877A ?
0
Comment
Question by:vpool
  • 5
  • 4
9 Comments
 
LVL 3

Expert Comment

by:steveiam
ID: 16243759
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
 

Author Comment

by:vpool
ID: 16243836
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
 

Author Comment

by:vpool
ID: 16243840
Thanks for reply.....Seteveiam....Cheers
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:steveiam
ID: 16243886
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
 

Author Comment

by:vpool
ID: 16253593
Please Advise me On such Gated Transmitter In PIC16f877a
0
 
LVL 3

Expert Comment

by:steveiam
ID: 16253668
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
 

Author Comment

by:vpool
ID: 16268022
I wanted 5 pulses@ 40 khz then a delay doing nothing for a while...And then again five pulses..........

0
 

Author Comment

by:vpool
ID: 16305737
Please tell me how to send 5 to 10 pulses using PWM code that you have given.........
0
 
LVL 3

Accepted Solution

by:
steveiam earned 1000 total points
ID: 16306270
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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question