I've been trying to control timer1 on the Arduino UNO to output at 25kHz to control some fans with some interesting and unexpected results. Here is the code I am using:
int controlPin = 10;
void setup()
{
Serial.begin(9600);
TCCR1A = 0x23;
TCCR1B = 0x02; // select clock
ICR1 = 639; // aiming for 25kHz
pinMode(controlPin, OUTPUT); // enable the PWM output (you now have a PWM signal on digital pin 3)
OCR1B = 320; // set the PWM duty cycle
}
void loop()
{
OCR1B = 160;
Serial.println(OCR1B);
delay(10000);
OCR1B = 639;
Serial.println(OCR1B);
delay(10000);
}
This code is sort of working to control the PWM on pin 10 (goes from around 0.7 V to 3 V). The problem in OCR1B should correspond to pin 9. Pin 9 does not work for either OCR2B or OCR2A. Any thoughts or suggestions would be greatly appreciated! Thanks in advance!
Typo, my bad. Yes, I am using OCR1A and OCR1B. I guess I could have been misinformed as to which one corresponds to which pin, but pin 9 will not work for either OCR1A or B. Any ideas as to why?
Because you have set TCCR1A to 0x23 you are not connecting OCR1A to the hardware output on pin 9. You have only connected OCR1B to the hardware output. You need to set the upper 2 bits of TCCR1A according to Section 16.11 of the ATmega328P datasheet.
You also have not used pinMode() to make pin 9 an output pin.
--
The QuadRAM shield: add 512 kilobytes of external RAM to your Arduino Mega/Mega2560
Thanks a lot! I looked at the datasheet, though, and that stuff is all pretty far over my head. I just want to be able to control the both pins 9 and 10 at the same time using ICR1 as top. Here is the code that I would like to use: