Connect with us

PIC code oscillations at set frequencies?

Discussion in 'Microcontrollers, Programming and IoT' started by Oatamelian, Apr 6, 2013.

Scroll to continue with content
  1. Oatamelian

    Oatamelian

    21
    0
    Mar 9, 2013
    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.
     
  2. KrisBlueNZ

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,268
    Nov 28, 2011
    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?
     
  3. CocaCola

    CocaCola

    3,635
    5
    Apr 7, 2012
    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...
     
  4. Oatamelian

    Oatamelian

    21
    0
    Mar 9, 2013
    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
     
  5. CocaCola

    CocaCola

    3,635
    5
    Apr 7, 2012
    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...
     
  6. Oatamelian

    Oatamelian

    21
    0
    Mar 9, 2013
    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?
     
  7. Oatamelian

    Oatamelian

    21
    0
    Mar 9, 2013
    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?
     
  8. CocaCola

    CocaCola

    3,635
    5
    Apr 7, 2012
    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/

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

    Oatamelian

    21
    0
    Mar 9, 2013
    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?
     
  10. CocaCola

    CocaCola

    3,635
    5
    Apr 7, 2012
    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...
     
  11. Oatamelian

    Oatamelian

    21
    0
    Mar 9, 2013
    TTTTT
     
    Last edited: Apr 8, 2013
  12. Oatamelian

    Oatamelian

    21
    0
    Mar 9, 2013
    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?
     
  13. CocaCola

    CocaCola

    3,635
    5
    Apr 7, 2012
    You came here asking for code, am I to assume now that you wanted someone to write the code for you since you?

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

    Oatamelian

    21
    0
    Mar 9, 2013
    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.



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

    KrisBlueNZ Sadly passed away in 2015

    8,393
    1,268
    Nov 28, 2011
    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.
     
  16. CocaCola

    CocaCola

    3,635
    5
    Apr 7, 2012
    So this is homework, or a school project? If so you are not going to learn anything by asking for hand outs...
     
  17. Oatamelian

    Oatamelian

    21
    0
    Mar 9, 2013
    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.
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-