Go Down

Topic: varying frecuency of a PWM (Read 693 times) previous topic - next topic

jongar

Hi everybody!
I´m tryinig to generate a PWM signal in which you can change the width and the frecuency, each one with one potentiometer.
The first of these is done but I´m not able to control the frecuency. I´m using timer 1.
If I want to get a phase correct PWM with the register OCR1A as limit (which is the one i will vary with the potentiometer) how should i configure TCCR1A and TCCR1B registers?
Is it correct to set them like this?:
TCCR1A=B10100011
TCCR1B=B00010100
I´ve tried writteing this but it doesen´t work.
Thanks in advance.

James C4S

You might want to read this article, which goes into depth about how the registers work:
http://www.arcfn.com/2009/07/secrets-of-arduino-pwm.html
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

jongar

I´ve already knew about that page but I tried using the code that appears in it and it stills doesen´t work...

Einherjer-Joe

#3
Jun 16, 2012, 05:27 pm Last Edit: Jun 16, 2012, 05:30 pm by Einherjer-Joe Reason: 1
I am on very thin ice here, but the "Arduino Cookbook" by Michael Margolis includes some sub chapters in chapter 18 about changing the PWM frequency. I just bought the Kindle Edition, and combined with my gigantic lack of knowledge regarding the Arduino I don't know if this  make any sense but:

In the mentioned chapters he use the term "prescale factor", accoding to the book TCCR1B ( timer 1) have a table showing the following columns, TCCR1B Prescale Value, Prescale factor (divider), Frequency:
1  1    312500

2   8    3906.25

3   64    488.28125

4   256    122.070......

5   1024    30.5175.....

The sketch in the example use the Serial Monitor so you can type in a digit from 1 to 7 to choose the frequency. The TCCR1B is the only register mentioned for timer 1.

And that is all I understand, sorry if I only added to the confusion!   :)
Einherjer-Joe
---------------------
www.arduinonorway.org
www.arduinonorge.org
www.bitraf.no

James C4S


I´ve already knew about that page but I tried using the code that appears in it and it stills doesen´t work...

That doesn't give much to go on.  Instead of saying "it doesn't work" tell what did happen.

You say you used Ken's examples but your registers don't match his:
TCCR1A should be B10100001
TCCR1B should be B00000100
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

jongar

Finally I found how to do it work!
The key is how to set WGM bits depending on what tipe of PWM you want (phase correct, phase and frecuency correct or fast PWM)
In my case I want a phase an frecuency correct PWM so the configurantion is:

// TCCR1B
  // Bit        7   6   5            4         3       2      1    0
  // Bit Name   COM1A1 COM1A0 COM1B1 COM1B0 -----  -----  WGM11   WGM10
 
TCCR1A = B11110001;

  // TCCR1B
  // Bit               7     6      5     4     3     2     1     0
  // Bit Name        ICNC1  ICES1      -----   WGM13   WGM12   CS12   CS11   CS10

TCCR1B = B00010100;

I found how to do it in this page. Here you have a detailed explanation of how AVR and PIC registers work.
The problems is that it´s in spanish but I hope you will be able to translate it because it was really useful for me.
Thanks every body.

jongar


Go Up