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


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming Theory

From novice to tech pro — start learning today.