The code attached is vary the PWM frequency by change the ICR1, my question is: can set the ICR1 = xx; int xx based on the Analog Read of a pot? I tested this way, no output.
If directly: ICR1 = 320; the PWM frequency changes.
// PWM output @ 25 kHz, only on pins 9 and 10.
// Output value should be between 0 and 320, inclusive.
int xx=0;
int potpin=A2;
void analogWrite25k(int pin, int value)
{
switch (pin) {
case 9:
OCR1A = value;
break;
case 10:
OCR1B = value;
break;
default:
// no other pin will work
break;
}
}
void setup()
{
// Configure Timer 1 for PWM @ 25 kHz.
TCCR1A = 0; // undo the configuration done by...
TCCR1B = 0; // ...the Arduino core library
TCNT1 = 0; // reset timer
TCCR1A = _BV(COM1A1) // non-inverted PWM on ch. A
| _BV(COM1B1) // same on ch; B
| _BV(WGM11); // mode 10: ph. correct PWM, TOP = ICR1
TCCR1B = _BV(WGM13) // ditto
| _BV(CS10); // prescaler = 1
// ICR1 = 160; // TOP = 320
//ICR1 = xx;
// Set the PWM pins as output.
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop()
{
xx = analogRead(potpin);
if(xx<=100){
ICR1==320;
// Just an example:
analogWrite25k(9, 110);
analogWrite25k(10, 210);
for (;;) ; // infinite loop
}
if(xx>100){
ICR1==160;
// Just an example:
analogWrite25k(9, 200);
analogWrite25k(10, 60);
for (;;) ; // infinite loop
}
}
My few observations/comments on the following codes of your Post#3:
void analogWrite25k(byte pin, byte percent)
{
int value = (ICR1 * 100UL) / percent;
switch (pin) {
case 9:
OCR1A = value;
break;
case 10:
OCR1B = value;
break;
default:
// no other pin will work
break;
}
}
void setup()
{
// Configure Timer 1 for PWM @ 25 kHz.
//TCCR1A = 0; // undo the configuration done by...
//TCCR1B = 0; // ...the Arduino core library
//TCNT1 = 0; // reset timer
TCCR1A = _BV(COM1A1) // non-inverted PWM on ch. A
| _BV(COM1B1) // same on ch; B
| _BV(WGM11); // mode 10: ph. correct PWM, TOP = ICR1
TCCR1B = _BV(WGM13) // ditto
| _BV(CS10); // prescaler = 1
ICR1 = 640; // TOP = 320
// Set the PWM pins as output.
pinMode( 9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop()
{
ICR1 = map(analogRead(2), 0, 1023, 0xFFFFU, 0x000F);
// Just an example:
analogWrite25k( 9, 50); // 50% duty cycle
analogWrite25k(10, 75); // 75% duty cycle
delay(100);
}
1. When A2-pin is connected with 5V, there is no PWM signal at DPin-9 to appear on the oscilloscope. As a part of trouble shooting, I calculated the values of ICR1 and OCR1A and found them as 15 (0x000F) and 30 (0x001E) respectively. The value of OCR1A is greater than the value of ICR1 -- a situation that can not generate PWM signal in Mode-10 operation of TC1. The value of OCR1A register must be lower than ICR1 Register to produce acceptable ON/OFF-period of the PWM signal.
Fgure-1: PWM timing diagram for Mode-10 operation of TC1
2. Similarly, there are ICR1 = 0xFFFF and OCR1A = 0xFFFE when A2-pin is connected with 0V. There is only one count difference between ICR1 and OCR1A; the resultant PWM signal is almost stuck at HIGH level with a sudden drop to logic-low level.
I would like to see a modified 'argument list' in the map() function so that the value of OCR1A always remains below ICR1 as the voltage at A2-pin changes.