# GBF( générateur de basse fréquence) with ARDUINO PWM problem frequency

Hey have a Good Day!
I am working on a project of a frequency probe and I need to create a GBF goes from ]0-10KHz]
I am using an Arduino Uno board to generate a PWM to get frequency.
I’am using Potentiometr to change the frequency on A0 pin of my ARDUINO baord .
I need help to fix my Program to provide at least from 1 HZ to 11khz frequency of pwm.
my out put is on pin 9.
this is my program:

``````#include "Arduino.h"
#define NECHANT 128
#define SHIFT_ACCUM 25

uint32_t icr;
uint32_t table_onde[NECHANT];
uint32_t accum1,accum2,accum3,increm;
uint16_t diviseur[6] = {0,1,8,64,256,1024};

void init_pwm_timer1(uint32_t period) {
char clockBits;
TCCR1A = 0;
TCCR1A |= (1 << COM1A1); //Clear OC1A on compare match when upcounting, set OC1A on compare match when downcounting
TCCR1A |= (1 << COM1B1);
#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__)
TCCR1A |= (1 << COM1C1);
#endif
TCCR1B = 1 << WGM13; // phase and frequency correct pwm mode, top = ICR1
int d = 1;
icr = (F_CPU/1000000*period/2);
while ((icr>0xFFFF)&&(d<6)) { // choix du diviseur d'horloge
d++;
icr = (F_CPU/1000000*period/2/diviseur[d]);
}
clockBits = d;
ICR1 = icr; // valeur maximale du compteur
TIMSK1 = 1 << TOIE1; // overflow interrupt enable
sei(); // activation des interruptions
TCNT1 = 0; // mise à zéro du compteur
TCCR1B |= clockBits; // déclenchement du compteur
}

ISR(TIMER1_OVF_vect) { // Timer 1 Overflow interrupt
accum1 += increm;
accum2 += increm;
#if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__)
accum3 += increm;
OCR1C = table_onde[accum3 >> SHIFT_ACCUM];
#endif
OCR1A = table_onde[accum1 >> SHIFT_ACCUM];
OCR1B = table_onde[accum2 >> SHIFT_ACCUM];

}

void set_sinus_table(float amp) {
int i;
float dt = 2*3.1415926/NECHANT;
for(i=0; i<NECHANT; i++) {
table_onde = icr*0.5*(1.0+amp*sin(i*dt));
}
}
int p;

void setup() {
pinMode(9,OUTPUT);
pinMode(A0,INPUT);
Serial.begin(19200);

}

void loop() {
p=map(p,0,1023,0,255);
Serial.println(p);
analogWrite(9,p);

uint32_t period_pwm = 1.5*p; // en microsecondes
uint32_t frequence = 40*p; // en Hz
accum1 = 0;
accum2 = ((uint32_t)(NECHANT * 0.25)) << SHIFT_ACCUM;
accum3 = 0;
increm = (uint32_t) (((float)(0xFFFFFFFF))*((float)(frequence)*1e-6*(float)(period_pwm))); // incrément de l'accumulateur de phase
init_pwm_timer1(period_pwm);
set_sinus_table(1.0);

}
``````

Did you write that code? What do you expect from`analogWrite(9,p);`