Variable frequency and 50% duty cycle with Timer 1 (arduino UNO)

Go To Last Post
16 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello my friends, I am doing a project for my college and I need to vary the frequency between 100Hz and 3Khz with fixed duty cycle of 50%. I researched here in the forum some articles but not much experience yet someone could help me?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Greetings, and Welcome to AVR Freaks. There do seem to be quite a few newbies for one day!

 

You project is, at once, simple and challenging.

 

You have one question that you need to consider, up front. How are you going to change the frequency? There are quite a few options but you need to choose something. It is a task for the student to figure out different ways that this might be done (I will NOT list anything because it is something YOU need to do). Hint: look at devices around you and see how they change such a thing. You might look at a radio, or things that go faster and slower. Maybe look at basic electronic instruments that you might find on-line.

 

Another important question is how finely the frequency needs to be adjustable. And, does that fineness (resolution) need to be constant through the adjustment range. This will have a big impact on the techniques that have to be used.

 

Next consider the following technical details:

 

1. The prescaler of the timer allows you to change the period of the timer by changing the clock rate. Duty cycle does not change when you change the prescaler. The big problem: the very finest increment of the prescaler value is 2, but some possible values are skipped. 

 

2. You can change the period by changing the TOP value in certain timer modes. BUT, every time you change top, you need to change the compare capture value to be exactly 1/2 of top. Note that if you have an odd value for top, the duty cycle cannot be exactly 50%. You also need to be careful about WHEN you change this, to avoid anomalous cycles.

 

3. You will probably want to look at "PWM" modes of the timer, even though you are not modulating the pulse width. That is because what you are talking about is PWM. You might also consider "phase correct" modes, as (I think) that will avoid the even-odd issue at the expense of maximum output frequency. 

 

Have fun with your project!

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you need to change the frequency while the program is running?  

 

If yes, then how do you indicate to the program what the new frequency will be?  Do you use an  rotary-position-sensor (a potentiometer or volume knob)?  or does the program have a serial port interface with a PC and you send to the AVR the new frequency in ASCII digits like "1" "2" "3" "4" "5" "newline" for 12345 Hz?  

 

How accurate is the new frequency supposed to be?  10%, 1%, 0.1%?

 

Does the output have to be on a specific pin?  

 

What is your AVR device?

 

Is there a main program that is also running, or is the creation of this waveform the only task that the AVR is doing?

 

How much time does the main program allow for this task to complete?  What percentage of the time spent running main code can this waveform generator consume?

 

It's easy to a 50% duty cycle.  Simply take the frequency and double it. Then take the period of this doubled frequency.  Set one of the timers to this interval. 

 

Say the frequency requested is 1 KHz.  You want for 50% duty cycle the square wave to be high for 500 microseconds and low for 500 microseconds. Set the timer to the mode where it counts to this 500 microsecond interval and then toggles the output pin, resets the timer value to zero, and begins counting again, all in the background.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:
How accurate is the new frequency supposed to be?  10%, 1%, 0.1%?

And what resolution is needed? I.e. in what steps do you need to alter the frequency? (10 Hz, 1 Hz, some fraction of a Hertz ,or...?)

 

(Yes, one of my pet nags is to point out the difference between accuracy and resolution..)

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Of  course, Johan. I think I was pretty explicit about resolution. But, I did forget to address absolute accuracy.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, Jim. I missed that in your post. I just saw Simonetta refer to accuracy, and thought I'd toss in the resolution.

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am using an atmega 328p, the frequency needs to vary while the program is working, it needs to be at pin 9, the frequency must have a range of 100hz ~ 3khz for a change I will use two buttons one to increment and another for decrement of frequency and duty should be set at 50%.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Again:

 

Weve asked about resolution and accuracy. 

 

When the increase button is pressed, how many Hertz should the signal increase for one press?

 

If the device is set for, say, 1000 Hz. What is the acceptable actual frequency limits?

 

We ask these questions not to just tease you but because they are important for deciding on how to solve your problem.

 

Also:

 

How is the user (the one who presses the buttons) to know the currently set frequency?

 

And what if the device is at 10 Hz and the user wants to set it to 500 Hz? Must he press the increase button several hundred times? 

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

adriel wrote:
I am using an atmega 328p [...] it needs to be at pin 9

Inadequate information. Depending on the package that could be port B pin 6, or port D pin 5 or port D pin 7. So: What package? 

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Johan,

 

The OP has stated, albeit in the title only, that he is using an Arduino Uno... so pin 9 is likely to be the "Arduino 9"

 

Cheers,

 

Ross

 

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah, I just noticed that..

 

Hard specification to nail down, this one..

"He used to carry his guitar in a gunny sack, or sit beneath the tree by the railroad track. Oh the engineers would see him sitting in the shade, Strumming with the rhythm that the drivers made. People passing by, they would stop and say, "Oh, my, what that little country boy could play!" [Chuck Berry]

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah, number nine....number nine....number nine....number nine....number nine....

 

Is this pin 9 on the AVR IC (PD5) or Arduino D9 (which is PB1?).  PD5 has an alternative function that is Output_Compare_B_of Timer_Zero [OC0B]. 

 

Are you being asked to make this frequency appear on OC0B (pin 9) under the control of the Timer0 or under the control of the main program?  Check section 19.5 (page 128) of the Mega328P data sheet for the timer mode that toggles a port pin on an output compare match.     Then each time that either button is pressed, change the value in the Output Compare B register to get the frequency change on the OC0B pin.  See table 19-6 on page 139 of the Mega328P data sheet.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is the pb1, oc1a with timer 1, can be with an accuracy of 1%, when the button is pressed and held the frequency increases faster the frequency will be displayed in a lcd display, the button is pressed the frequency will increase, however it must have a range between 100hz ~ 3khz, I need it vary in this range

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello Simonetta, my question is which way should I configure the TCCR1A and TCCR2B? fast pwm? phase and correct frequency? so I can vary this frequency range using two buttons

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hint: Have a variable that represents frequency. When you press the "increase button", step the variable up by 1. Ditto "decrease", but step down. If the button is down for a long time, start a timer that does auto increment for as long as the button is pressed. You would need to implement limits so that the variable is never incremented above the maximum nor decremented below the minimum.

 

You have a couple of choices: (1) have this variable represent actual frequency for display in the LCD and translate the value into a TOP number for the timer, or (2) have this variable represent the TOP value and translate that to the actual frequency for LCD display. Remember that the TOP value is proportional to period rather than frequency so the second will give you linear period and the first will give you linear frequency.

 

That is pretty much the whole enchilada. The details are left for implementation by the student.

 

As far as configuring the timer, the spec sheet tells you what you need to know.

 

West Side Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sun. Sep 17, 2017 - 03:03 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As others have said, Welcome to the Freak Show.

 

If you are using the Arduino environment, one mu  tbe careful which timers are used as they are also used in the background for other timing features/functions.

 

If you are using the Arduino strictly for hardware and programming in Stdio/GCC for example then you could simply run the Timer in CTC mode with an ISR that toggles the pin you want every time a compare is hit.  Your buttons decrease/increase the value of OCxA for that timer and with Timer1 for example you would have a rather high resolution unit.  If you need to switch prescalers during your sweep, your code can do this automatically when a certain OCRxA value is hit.

 

So, which IDE are you using?

 

East Side Jim

 

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user