### You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Frequency

 NerdKits » Forums » Microcontroller Programming » Frequency (20 posts)
 March 27, 2011 by hariharan Hey, i was wondering, how can i output a specific frequency? Probable the closesest you will get to frequency output is using PWM. Ralph If you have an op-amp I can post here a few circuits to generate square, sine, and triangular waves at variable frequencies. But Ralph is right, with only microcontroller you can output a square wave using PWM or timer. Yeah Hexorg, having some signal generating circuits would be good to have. I was thinking of making a signal generator to learn how to use my oscilloscope. I figured if I had a known signal, where I knew all of the parameters then I could learn how to identify them using the oscilloscope. So I for one will use your circuits. Ralph Ralph, If you don't need exacting frequencies but rather want something decent to test your o-scope with, don't forget a 555 with a potentiometer can give you a pretty wide range of freq's and duty cycles. I recently built a pulse counter to hook up to a flow meter at work(post soon to appear elsewhere), but I used a 555 with a trimpot to test and do quick cals on it. I don't know exactly how to make a frequency output andor program a PWM. what is a PWM? hariharan, you need to read the specsheet. Of course I know one can diligently read the specsheet and not come away knowing what PWM is, as it is rather boring reading cover to cover, until you want to find something specific (like PWM). So search the specsheet for PWM and then search the Nerdkits forum. A very simplified example of using PWM is to brighten and dim a led. Ever thought of doing that or how you would do that short of wiring in a potentiometer? Well PWM will do that for you and a lot else. Ralph I don't think PWM is relevant to the original question. PWM is Pulse Width Modulation. It's about changing the duty cycle (pulse width) of a fixed-frequency signal. The original question was "how can I output a specific frequency". PWM may be completely unnecessary for this, depending on what he's doing. On the Atmega168 there are two main ways to output a specific frequency. One is to do it manually by toggling a pin periodically, with the period calculated to produce the desired frequency, and the other is to set up a timer and let the timer toggle the pin for you. The "best" method depends on what the frequency (or range of frequencies) is, whether the frequency changes over time or in response to some external influence, and to some extent why you want to output a specific frequency. So let's start with : What frequency do you want to generate? I've posted the oscillation circuits here unfortunately, I wasn't able to come up with some formials. :\ i want to generate 516 hz for 1 sec. how do you do it? hariharan, I had hoped that bretm would finish answering your question on frequency. I'll try to enlighten you further with my limited knowledge. Ok here we go, those more knowledgable than I please jump right in: The mcu is a "digital" device "frequency" is normally considered a "analog" system of measure. So you could turn a pin on and off with a "frequency" (repetition) but the wave (square) would only go from 0 to 5 volts. You cannot get a sine (frequency) wave from a digital device. Using PWM you can can a variable timing of your square wave but it still will not be a sine wave (frequency). So what exactly are you tying to do? Maybe "we" can help you once we know the details beyond trying to produce a frequency. Ralph What i am trying to do is making a music with the MCU but with the PWM. I did some research and thought why not change the OCR1a with a delay. So i calculated frequency to millisec and converted that to the value of OCR1A. But it does not work. Why? ``````//By Hariharan //Pwm music box #define F_CPU 14745600 #include #include #include #include "../libnerdkits/delay.h" int main(){ //instead of definig freq //calculated time for each freq // in http://www.sengpielaudio.com/calculator-period.htm //and calculated OCR1A // that is 1843.2 X millisec //note FREQUENCY MS OCR1a //D5 851 1.175088132 2165.922445 //E5 758 1.319261214 2431.66227 //Fsh5 675 1.481481481 2730.666666 //G5 637 1.569858713 2893.56358 //A5 568 1.76056338 3245.070422 //B5 506 1.976284585 3642.687747 //C6 477 2.096436059 3864.150944 //D6 425 2.352941176 4336.941176 unint16_t D5= 2165.922445 ; unint16_t E5= 2431.66227; unint16_t Fsh = 2730.666666; unint16_t G5= 2893.56358; unint16_t A5= 3245.070422; unint16_t B5= 3642.687747; unint16_t C6= 3864.150944; unint16_t D6= 4336.941176; } void pwm_init() { //Fast PWM pwm_set(PWM_START); TCCR1A = (1< #include #include #include "../libnerdkits/delay.h" int main(){ //instead of definig freq //calculated time for each freq // in http://www.sengpielaudio.com/calculator-period.htm //and calculated OCR1A // that is 1843.2 X millisec //note FREQUENCY MS OCR1a //D5 851 1.175088132 2165.922445 //E5 758 1.319261214 2431.66227 //Fsh5 675 1.481481481 2730.666666 //G5 637 1.569858713 2893.56358 //A5 568 1.76056338 3245.070422 //B5 506 1.976284585 3642.687747 //C6 477 2.096436059 3864.150944 //D6 425 2.352941176 4336.941176 unint16_t D5= 2165.922445 ; unint16_t E5= 2431.66227; unint16_t Fsh = 2730.666666; unint16_t G5= 2893.56358; unint16_t A5= 3245.070422; unint16_t B5= 3642.687747; unint16_t C6= 3864.150944; unint16_t D6= 4336.941176; } void pwm_init() { //Fast PWM pwm_set(PWM_START); TCCR1A = (1<