Control PWM Freq and phase correct timer 1 with no shoot through

Hello and I have this code that i am using that has a freq of 15k

It says i can control Frequency and it has delay between pulses but I dont know what to change to change the freq and the delays between pulses… ???

//this code fixes all issues with the analogwrite. it has delays between pulses and can control frquency…

void setup() {

TCCR1A = 0XB2;
TCCR1B = 0X11; // 11 fpr phase correction 19 for fast pwm
ICR1 = 0X01FF;
DDRB = 0X06;
}

void loop() {
// put your main code here, to run repeatedly:
int pot = analogRead (A0) / 2;
OCR1A = pot;
OCR1B = pot;
}

You need to understand the hardware - the datasheet for the ATmega328 explains timer1 registers and modes of operation. The top 4 bits of TCCR1A define the polarity and mode of output pins, here they are in antiphase.

The values written to OCR1A and OCR1B need to be different to have dead-time, otherwise they will simply change perfectly in anti-phase.

The code must synchronize with timer1 so that the OCR1A and OCR1B change together, which means using the timer1 overflow interrupt to update them. Otherwise you could get occasion shoot-through.

I am new to all this so i am sorry if something is obvious Ok I tried this code below and it works to have a slight delay between pulses my Freq is 15 k how can i change the freq to 31k or can I also make it any number ?

void setup() { // put your setup code here, to run once: TCCR1A = 0XB2; TCCR1B = 0X11; ICR1 = 0X01FF; DDRB = 0X06;

}

void loop() {

OCR1A = (10, (analogRead (A0) / 5)); OCR1B = (9, analogRead(A0) / 4); }

Do you understand the C comma operator?

  OCR1A = (10, (analogRead (A0) / 5));
  OCR1B = (9, analogRead(A0) / 4);

Anyway the difference in values written to OCR1A and B should be constant, you are scaling.

Whats wrong with

#define DEADTIME 2  // or whatever number of clocks is right

volatile int level = 0 ;  // probably not zero, need some minimum pulse width

void loop ()
{
  level = analogRead(A0) / 4;  // probably should use map() and and constrain() here.
}

ISR (TIMER1_OVF_vect)   // runs synchronized with timer so both registers update in lockstep
{
  OCR1A = level ;
  OCR1B = level + DEADTIME ;
}

Note the ISR for register update, meaning that the level can't change between setting each register and both registers are updated at a fixed point in the cycle. Lookup map() and constrain(), they are really useful for this kind of thing.