I have advanced with the help of this forum.
Particularly valerio_sperati illustrated the Prescaler. http://forum.arduino.cc/index.php?topic=72092.0
(I using Mega2560 and Timer3)
int myEraser = 7; // this is 111 in binary and is used as an eraser TCCR3B &= ~myEraser; // this operation (AND plus NOT), set the three bits in TCCR3B to 0
now that CS02, CS01, CS00 are clear, we write on them a new value:
int myPrescaler = 1; // this could be a number in [1 , 6]. In this case, 1 corresponds in binary to 001. TCCR3B |= myPrescaler; //this operation (OR), replaces the last three bits in TCCR3B with our new value 001
now we have a new PWM frequency at 1/16000000 = 62.5ns this is great and very clear for me. Thank you very much valerio_sperati.
But I have not finished my project, and this big step, is not enough.
I need to control the width of the period. (realtime)
The above example works only 8 bits. 1/16000000*(2^8)= 0.000016 seconds (width of the period at 8-bits) = 1/0.000016 = 62500 Hz. I repeat, It's great.
But I also need:
1/16000000*(2^6)= 0.000004 seconds (width of the period at 6-bits) = 1/0.000004 = 250000 Hz. 1/16000000*(2^7)= 0.000008 seconds (width of the period at 7-bits) = 1/0.000008 = 125000 Hz. 1/16000000*(2^9)= 0.000032 seconds (width of the period at 9-bits) = 1/0.000032 = 31250 Hz.
I have no clear definitions of timer modes. I'm lost between the 15 modes of the timers.
I want to swing between 6-bit and 9-bit.
These are the questions:
1.- What are all the mode-timer in to do this?
2.- The main thing: How to do it?
as far as I understand (with doc2549.pdf). Mode 8 PWM, Phase and Frequency Correct, seems best for this.
TCCR3A=B00000000; // TCCR3B=B00010001; // The first three bits (from right to left!) are Prescaler =00 1 equal time= (1/16000000 = 62.5 nS) // The other (bit 4) is WGM33=1 to set Mode 8 PWM, Phase and Frequency Correct.
// as ICR3 can set the top (2^bits / 2)-1
ICR3=65535; // (the maximum) ICR3=3; // (the minimum)
OCR3A= 0; // The duty is ON (100% pwm) OCR3A= ICR3A; // The duty cicle is OFF 0(0% pwm)
// Then can work two variables register
cli(); // Disable interrupts for 16 bit register access ICR3 = (2^bits / 2)-1; // ICR1 is TOP in p & f correct pwm mode OCR3A = someduty; sei(); // Enable interrupts for 16 bit register access
if this works properly, I can do a lot.
I have no oscilloscope to check, so again I ask your comment.
I appreciate your time and attention.