Prescaling/PWM settings for Atmega328p (Arduino UNO)

Hi,
I have generated a 125kHz square wave using the code attached. I need to read mid bit transitions and timer value at say every 4us. I just started understanding the pwm and prescaling concepts associated with timer and counter settings. Upon calcuation using the formula - Timer Count = (Required Delay/Clock Time Period) - 1, I get 15 as the timer count for a prescaled frequency of 2 MHz. Is that the TOP value (OCR1A) ?

I am using the normal mode; but would appreciate if someone suggested as to what mode should I use for reading the mid bit transitions (only for logic 0) of a BPSK signal?

#include "TimerOne.h"
int PIN = 6;
int OUT = 2;
int count = 0;

void setup() {
  Serial.begin(9600);
  pinMode(PIN, OUTPUT);
  pinMode(OUT, INPUT);

  noInterrupts();
  TCCR1A = 0;
  TCCR1B = 0;
  TCNT1 = 0;

  TCCR1A = 
  TCCR1B = 
  OCR1A = 
  
}

void loop() {
  int state = 0;
//conditional loop for 125kHz square wave generation
  while(1) {
    if(state == 0) {
      digitalWrite(PIN, LOW);
      state = 1;
      //delayMicroseconds(8);
      Serial.println(state);
    } 
    else {
      digitalWrite(PIN, HIGH);
      state = 0;
      //delayMicroseconds(8);
      Serial.println(state);
    }
    delayMicroseconds(8);
  }
}

I'm puzzled by this code

TCCR1A = 
  TCCR1B = 
  OCR1A =

I did not set the bits as I am not sure which mode to use. :confused:

So you didn't generate anything with that code, because it doesn't compile.
Is that correct?

Yeah. :-\

What exactly are you trying to achieve? Generating or reading the signal? If reading where it comes from?

So, I am trying to demodulate an incoming BPSK signal; the signal will be superimposed on a 125kHz square wave. In order to do that first I need to generate a 125kHz square wave carrier which will be outputted at the pin of an analog front end(which is suppose to generate it's own 125kHz wave once initialized, but at this point it's not doing :frowning: ). The signal is biphase encoded for which I believe a timer/counter is required in order to locate the value at every rising edge so as to keep track of the mid bit transitions in a biphase encoded signal.

For the above, one has to set the TCCR1A and TCCR1B for PWM generation and prescaling(right?). I am not sure what mode to use - Normal, CTC, Fast, Phase Correct or Phase and Frequency Correct?. I haven't properly understood the same.
Also, can anyone tell me the difference between inverting and non-inverting in modes?

In order to do that first I need to generate a 125kHz square wave carrier which will be outputted at the pin of an analog front end

Most simple will be to use the Timer One library TimerOne & TimerThree Arduino Libraries