PIC16f877 Application ?

Posted on 2006-04-16
Last Modified: 2013-11-12
Hi ,

Please help me with following ...

1>> How to generate a 40 Khz and a 2 khz pulse ?
2>> How to "AND" them in order to send it to a I/O Pin of 16f877a ?

I am using PICC lite Hitech Compiler and Micro chip MPLab ...


Question by:vpool
    LVL 31

    Expert Comment

    If we are talking hardware logic circuits here, then you would have a crystal oscillator which you would feed into a counter.  The counter would be designed to produce one pulse every so many pulses (depends on the frequency of the crystal as to the detail).  This resulting pulse (operating at 40kHz) would then be used as the clock pulse for your system.  If you want to produce a pulse at 2kHz you would need to design a flip-flop counter which counted to 20, then reset itself.  You would do this by taking the correct Q and Not Q outputs from the Flip Flops and feeding them into Reset them when they are at Logic 1.  

    20 decimal = 10100 in binary so you need 5 Flip Flops.  The Reset pulse needs to use (from Most Significant first)  

    Q and Not Q and Q and Not Q and Not Q would be the AND logic you would need.  In practice you would invert the Q's for Not Q's and vice versa and use a NAND gate.

    The Reset pulse would also feed the I/O pin of your microcontroller.

    There's lots of different ways to do this, depending on which Flips Flops you use.  I don't know what devices are around these days (I used to do this kind of design in the 1970's with TTL 74XX devices).

    Author Comment

    Please tell me how to do it with software.......C Programming
    LVL 3

    Expert Comment

    You already asked a similar question that I've answered here:

    If you have an interrupt that's happening at 40kHz, to get a 2kHz output as well, you need to divide it by 20.  Have a counter in your 40kHz interrupt, and when it hits 20, toggle the state of the 2kHz output.  At the same time, on each 40kHz interrupt, you can calculate the "AND" of the two states (40kHz and 2kHz) to arrive at your combined output.

    - initialise divide-by counter to 20
    - set up a 40kHz interrupt (it needs to run at 80kHz to get the 'up' and 'down' transitions at the right speed)
    - start the counter that the interrupt runs from

    In the 40kHz interrupt service routing:
    - swap state of 40kHz output
    - decrement divide-by counter
    - if counter is 0, then:
           - reset counter to 20
           - swap state of 2kHz output
    - "AND" the two outputs together
    - set output pin to result of "AND" calculation

    A lot of the code for doing this in the article linked above.

    Author Comment

    You have been the best help....
    Please explain the anding procedure in detail...
    LVL 3

    Accepted Solution

    In C you can do a bitwise AND (i.e. operating on the binary bits of the values involved) like this:

    int8 value1 = 1;
    int8 value2 = 0;

    int8 andResult = value1 & value2;

    Author Comment

    It woould be great if u can provide the code with the comment....I am getting really confused....

    LVL 3

    Expert Comment

    What part are you specifically confused about, and what is causing the confusion?

    Rather than me writing the entire program, and doing all the work for you, I'm happy to help with specific parts.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Suggested Solutions

    Pretext and Context In previous  (, I designed the data model of a basic Identity Management…
    Dependencies in Software Design In software development, the idea of dependencies ( is an issue of some importance. This article seeks to explain what dependencies are and where they …
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now