Maker Pro
Maker Pro

PIC code oscillations at set frequencies?

Oatamelian

Mar 9, 2013
21
Joined
Mar 9, 2013
Messages
21
What code can I use to generate 4 different frequency waves (to my spec) on a 16F84 PIC chip that can output to a small speaker for a little game? I understand I may have to use a transistor to up the power for the speaker a tad.
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
Do you want to generate four frequencies simultaneously? Or just one at a time?
Read up on Microchip application notes that relate to square wave generation using timers and the output compare feature.
Consider what frequency accuracy you need, and consider the accuracy of the clock source - do you need a crystal, or is a ceramic reasonator close enough, or would the built-in oscillator be accurate enough?
 

CocaCola

Apr 7, 2012
3,635
Joined
Apr 7, 2012
Messages
3,635
Most compilers have a 'sound' or 'tone' command built in that simplifies this to dead simple as long as you only want basic tones... If not code examples for what you want to do can almost certainly be found for your compiler with a little Googling...

BTW, unless there is a specific reason requiring you to use the 16F84, get rid of it and go with the 16F628a... The 16F84 is an outdated old school chip, the newer chips are generally cheaper, faster most have a boat load more hardware function and many have build in oscillators that reduces parts count and simplifies the circuit...
 

Oatamelian

Mar 9, 2013
21
Joined
Mar 9, 2013
Messages
21
I would like 1 frequency to be generated at any one time, with a choice of four different frequencies. I believe the clock frequency for the 16F84 worked out at 250KHz not sure what it uses. Accuracy isn't really an issue.

I want the chip to be able to output it to a piezo speaker. We have to use the 16F84 chips unfortunately, cause like you said it's an old school chip.

So when a button connected to the PIC chip is pressed the PIC chip will generate a tone of a certain frequency to the piezo speaker. each button pressed has a different frequency set to it.

If it's as easy as "sound" or "tone" then that is brilliant :D
 

CocaCola

Apr 7, 2012
3,635
Joined
Apr 7, 2012
Messages
3,635
If it's as easy as "sound" or "tone" then that is brilliant :D

Fully dependent upon the compiler you use, some make it easy others make you do all the programming yourself...

I mostly use PicBasic Pro and for me it's this simple using said compiler

SOUND pin, [Note, Duration]

Note is a value from 0 to 127 equal to about value x 78.74Hz, notes 128-255 are white noise...

As I said this is for PicBasic Pro, other compilers will differ from easy like this to DIY in the code the hard way...
 

Oatamelian

Mar 9, 2013
21
Joined
Mar 9, 2013
Messages
21
A compiler? So... you're saying I don't have to furiously type out line after line of code? I can just use this "Compiler" and be done in hours as opposed to days?
 

Oatamelian

Mar 9, 2013
21
Joined
Mar 9, 2013
Messages
21
Could you recommend a PIC compiler freeware type program? Do you know of a program that uses flowcharts to make the program that can then be converted into the code required for a PIC chip?
 

CocaCola

Apr 7, 2012
3,635
Joined
Apr 7, 2012
Messages
3,635
Could you recommend a PIC compiler freeware type program?

For a small program like this I would download the free trial of any of the Mikroe Compilers choose the language you are most familiar with, they all have sound libraries built in that will make it painless...

http://www.mikroe.com/

Do you know of a program that uses flowcharts to make the program that can then be converted into the code required for a PIC chip?

Yep, it's called Flowcode and if it works for you it's an interesting program, and quite reasonably priced as well... I have only tinkered with it here and there, didn't float my boat so I can't really comment much...
 

Oatamelian

Mar 9, 2013
21
Joined
Mar 9, 2013
Messages
21
Okay, it turns out that the chip cannot produce a tone on it's own.
Are there any work arounds? Could I link up 4 different frequency oscillators to the same piezo speaker and have the chip output to these oscillators at different times?
 

CocaCola

Apr 7, 2012
3,635
Joined
Apr 7, 2012
Messages
3,635
Okay, it turns out that the chip cannot produce a tone on it's own.

Who and/or what led you to that false and incorrect statement?

Just for kicks I did a quick Google search, based on the PIC16F84 and the exact compiler I referred you to use, and presto...


If you mean it's not going to play sounds for you without a little coding, well yeah it won't as you have to tell it what to do...

If you want to avoid programming, make four 555 timer circuits and use the buttons to turn the appropriate timer on...
 

Oatamelian

Mar 9, 2013
21
Joined
Mar 9, 2013
Messages
21
Who and/or what led you to that false and incorrect statement?

If you mean it's not going to play sounds for you without a little coding, well yeah it won't as you have to tell it what to do...

I've done countless searches on how to get a 16F84 to send some sort of oscillation to a speaker, I can find nothing. I've obviously not learnt enough to search using the correct terms.

I must admit, this mikrobasic does sound promising at first but I cannot program. This alternate method with 555 timers, this would produce a desired frequency I could send to piezo?
 

CocaCola

Apr 7, 2012
3,635
Joined
Apr 7, 2012
Messages
3,635
I must admit, this mikrobasic does sound promising at first but I cannot program.

You came here asking for code, am I to assume now that you wanted someone to write the code for you since you?

This alternate method with 555 timers, this would produce a desired frequency I could send to piezo?

Sure, you build the circuits specific to the frequency you want it to output, lots of online calculators that will do the math for you, or use the equation in the datasheet and do the math yourself...
 

Oatamelian

Mar 9, 2013
21
Joined
Mar 9, 2013
Messages
21
You came here asking for code, am I to assume now that you wanted someone to write the code for you since you?

I came here looking for someone with pre-knowledge of this code yes. I was hoping someone would share it with me. I know the basic stuff such as: movlw, movwf, trisb, trisa, btfss etc etc I just wasn't sure how to use these commands or the 20more there are to get what I wanted.



Sure, you build the circuits specific to the frequency you want it to output, lots of online calculators that will do the math for you, or use the equation in the datasheet and do the math yourself...

Yes, I will have to do the math myself if I am to be awarded any form of merit.
 

KrisBlueNZ

Sadly passed away in 2015
Nov 28, 2011
8,393
Joined
Nov 28, 2011
Messages
8,393
If you only want one tone at a time, you only need one 555. You can enable and disable the oscillator via the 555's reset input (pin 4) and you can set the oscillation frequency using four different resistors that are selected according to your four control signals.
 

Oatamelian

Mar 9, 2013
21
Joined
Mar 9, 2013
Messages
21
So this is homework, or a school project? If so you are not going to learn anything by asking for hand outs...

We don't get awarded anything for the PIC program. I've spent ages looking for a pre-programmed one to save myself some time. We get marks for believe it or not:

-Working in a safe environment
-6 sub-systems (PIC counts for 4, strange but true)
-A couple of workings out for these sub-systems
-testing our circuit

And this is A-Level Electronics.
 
Top