I managed to get timer1 to change its frequency to 25 kHz and I can vary the duty cycle individually for its two pins. I follow it in the scope and I can change the duty cycle (pwmA and pwmB) by changing the variable value manually and push out the new code. But i dont seem to be able to change its value in the function loop or from anywhere alse for some reason.
Btw it does only work to have the timer settings either in the void setup() or in a separate function as it is now. Doesnt work in the main loop for me, if anyone wonders about that.
Probably something easy, dunno, my eyes are tired. Appreciate more eyes.
//Dual 25kHz PWM using Timer1 Mode 10
int value = 100;
int pwmA = value; // Duty cycle (0-320 = 0-100%)
int pwmB = 300; // Duty cycle (0-320 = 0-100%)
void setup() {
pinMode(11, OUTPUT); //pwmA
pinMode(12, OUTPUT); //pwmB
pwm();
}
void loop() {
}
void pwm(){
TCCR1A = 0; //clear timer registers
TCCR1B = 0;
TCNT1 = 0;
TCCR1B |= _BV(CS10); //no prescaler
ICR1 = 320; //PWM mode counts up 320 then down 320 counts (25kHz)
OCR1A = pwmA; //0-320 = 0-100% duty cycle
TCCR1A |= _BV(COM1A1); //output A clear rising/set falling
OCR1B = pwmB; //0-320 = 0-100% duty cycle
TCCR1A |= _BV(COM1B1); //output B clear rising/set falling
TCCR1B |= _BV(WGM13); //PWM mode with ICR1 Mode 10
TCCR1A |= _BV(WGM11); //WGM13:WGM10 set 1010
value = 300;
}
I can change the duty cycle by changing the variable value manually and push out the new code. But i dont seem to be able to change its value in the function loop or from anywhere alse for some reason.
Are you expecting to see the duty cycle of pwmA change from 100 to 300 with your code?
cattledog:
Are you expecting to see the duty cycle of pwmA change from 100 to 300 with your code?
OCR1A never gets assigned the new value.
Yes that is what I am trying to do. Ie I want to be able to update the duty cycle of pwmA and B. OCR1A and B shall take its value from pwmA and B right? In the future by temp sensors, just now I am only trying to change it from somewhere by changing pwmA.
I guess now void pwm() only runs once and that is why it doesnt update. I need to loop the OCR1A/B somehow.