I've looked at PWM modes and it is quite easy to change the duty cycle using an analog input.
My problem is the pwm pins are at specific frequencies. I have a 10hz square wave and I wanto change the duty cycle depending on the input to an analog input on the arduino.
basiclly I want to monitor the input say from 10v as the volage increases the duty cycle is reduced .
ideas appreciated
alanj100:
I've looked at PWM modes and it is quite easy to change the duty cycle using an analog input.
My problem is the pwm pins are at specific frequencies. I have a 10hz square wave and I wanto change the duty cycle depending on the input to an analog input on the arduino.
basiclly I want to monitor the input say from 10v as the volage increases the duty cycle is reduced .
ideas appreciated
since the frequency is quite low, if it was me I would study the 'blink without delay' example and modify it accordingly allow me to control the 'blinking' duty cycle.
hope that helps
What device or code is generating the 10Hz square wave?
I think you will need a 16-bit timer to get down to 10hz. The prescaler only goes to 1024.
16 MHz / 10 Hz = 1.6 million counts per cycle.
1.6M / 1024 = 1562.5 counts per cycle for FastPWM
1562 * 1024 = 1599488 counts per cycle = 10.0032... Hz. Hope that is close enough.
// Stop Timer/Counter1
TCCR1A = 0; // Timer/Counter1 Control Register A
TCCR1B = 0; // Timer/Counter1 Control Register B
TIMSK1 = 0; // Timer/Counter1 Interrupt Mask Register
TIFR1 = 0; // Timer/Counter1 Interrupt Flag Register
ICR1 = 1561; // 16 MHz (clock) / 1024 (prescale) / 1562 (TOP+1) == 10Hz
OCR1A = 0; // Default to 0% PWM
OCR1B = 0; // Default to 0% PWM
// Set clock prescale to 1024 for minimum PWM frequency
TCCR1B |= (1 << CS12) | (1 << CS10);
// Set to Timer/Counter1 to Waveform Generation Mode 14: Fast PWM with TOP set by ICR1
TCCR1A |= (1 << WGM11);
TCCR1B |= (1 << WGM13) | (1 << WGM12) ;
// Enable Fast PWM on Pin 9: Set OC1A at BOTTOM and clear OC1A on OCR1A compare
TCCR1A |= (1 << COM1A1);
pinMode(9, OUTPUT);
// Enable Fast PWM on Pin 10: Set OC1B at BOTTOM and clear OC1B on OCR1B compare
TCCR1A |= (1 << COM1B1);
pinMode(10, OUTPUT);
Then just set OCR1A or OCR1B to any value from 0 to 1561 to set the duty cycle from 0% to 100%.