I use an ATmega328p Xplained mini board with a potentiometer at the ADC5 input. PWM off is PB1 (OC1A).
source snippet;
#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
double dutyCycle;
int main(void)
{
DDRB = (1 << PORTB1);
TCCR1A |= (1 << COM1A1) | (1 << COM1A0) | (1 << WGM11); // fast PWM mode 14 en geinverteerd, set OC1A/OC1B on compare match
TCCR1B |= (1<<WGM13) | (1 << WGM12) | (1 << CS11); // delen door 8
ICR1 = 50; // Frequentie
setupADC();
sei();
while(1)
{
}
}
void setupADC()
{
ADMUX = (1 << REFS0) | (1 << MUX0) | (1 << MUX2);
ADCSRA = (1 << ADEN) | (1 << ADIE) | (1 << ADPS0) | (1 << ADPS1);// | (1 << ADPS2); // 128 prescaler
DIDR0 = (1 << ADC5D);
startConversion();
}
void startConversion()
{
ADCSRA |= (1 << ADSC); // ADSC is start conversie
}
ISR(TIMER1_OVF_vect)
{
OCR1A = dutyCycle;
}
ISR(ADC_vect)
{
dutyCycle = ADC;
startConversion();
}
With this source I'll get a constant signal as you can see in the picture, the position of the potentiometer does not matter.
Does anyone know how I can adjust the source so that I can use the potentiometer to control the duty cula of e.g. ~ 3% to 98%?