• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 788
  • Last Modified:

PIC16f877 Application ?

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


  • 3
  • 3
1 Solution
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).
vpoolAuthor Commented:
Please tell me how to do it with software.......C Programming
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.
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

vpoolAuthor Commented:
You have been the best help....
Please explain the anding procedure in detail...
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;
vpoolAuthor Commented:
It woould be great if u can provide the code with the comment....I am getting really confused....

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

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now