How to know if a 125kHz carrier is generated?

Hi,
I am trying to generat a 125kHz square wave. I am not sure how to test if pin 11 is outputing the same. How do I do that?Newbie here. Forgive the number of comments. It’s a test program.

/******* Sketch to test Input Capture interrupt handling ***********************/

#include <stdio.h>// for verbose debugging using sprintf 
#include <avr/io.h>
#include <avr/interrupt.h>
//#include "TimerOne.h"
//#include "Timer.h"
#include "PWM.h"


#define icpPin 8 //ICP input pin on arduino - DATA IN HERE.
#define pwmoutPin 11        // OCR1A pin on arduino
//#define outPin 9        // output pin that will shadow the input, can be monitored with a scope 
//#define outPin 9
                       
volatile uint8_t rising;                      

                     
unsigned long currentMicros;
unsigned long previousMicros;
unsigned long elapsedMicros;
unsigned long Period = 8000000UL; // overall period
// pick the high time, calculate the low time from what's left
unsigned long highPeriod = 4000000UL; // microseconds, x 2 = 10mS = 100 Hz
// probably best if times are increments of 4uS to match up with micros()
unsigned long lowPeriod; // microseconds,  set to = Period - highPeriod in setup()                       


byte highTime;

                       
                       
// some variables to help see that something is happening in the interrupt handlers
volatile unsigned int Value;     // this stores the current ICR1 value
//volatile unsigned int MinValue;     
//volatile unsigned int MaxValue;
volatile unsigned int Overflows;
volatile unsigned int PulseCount;


unsigned long duration;

//int32_t frequency = 125000

char in_buffer[800];
short i;

//Timer t;
/* Overflow interrupt vector */
ISR(TIMER2_OVF_vect){                 // here if no input pulse detected 
  Overflows++;                       // incriment overflow count  
}

/* ICR interrupt vector */
ISR(TIMER2_CAPT_vect){
  TCNT1 = 0;                            // reset the counter
  OCR1B = 0;                            //data
  //catchRisingEdge();
  /*if( bit_is_set(TCCR1B ,ICES1)){       // was rising edge detected ?   
       digitalWrite(outPin,HIGH );      // yes, set our output pin high to mirror the input
  }
  else {                                // falling edge was detected 
       Value = ICR1;                    // save the input capture value 
       digitalWrite(outPin,LOW );       // set our output pin low to mirror the input  
       PulseCount++;
  }/*if(Value < MinValue)             // update min or max values as appropriate   
           MinValue = Value;
       if (Value > MaxValue)
           MaxValue = Value;    
  }  */   
  TCCR1B ^= (1<<ICES1);                 // toggle bit value to trigger on the other edge

  if(bit_is_set(TCCR1B, ICES1)){
    catchRisingEdge();
    Value = OCR1B;
    digitalRead(pwmoutPin);
  }
   else{
    catchFallingEdge();
    Value = OCR1B;
    digitalRead(pwmoutPin);
    PulseCount++;
   }
         
  /*if(bit_is_set(TCCR1B, ICES1)){
    digitalWrite(OUT, HIGH);
    }
   else{
    Value = ICR1;
    digitalWrite(OUT, LOW);
    PulseCount++;
    
   }*/
 /* static byte lastEdge;
  byte thisEdge = ICR1;
  byte difference = lastEdge - thisEdge;
  if (difference
  
  lastEdge = thisEdge;  */ 
}

/*ISR(TIMER0_COMPA_vect){
  
  if(i<800){
    
    buffer[i] = digitalRead(icpPin);
    }i++;
  
  }*/
//int val = 0;
//int ledPin = 11;
//int analogPin = 3;

//int pwmPin = 9;

//const int16_t TOGGLE_TIME = 8;

void setup() {
 Serial.begin(9600);
 //pinMode(outPin, OUTPUT);               // declare the ledPin as an OUTPUT
 //pinMode(icpPin, INPUT); // ICP pin (digital pin 8 on arduino) as input
 
 //MinValue =    30000;                   // Initialise with extreme values 
 //MaxValue =    0;
 //SetPinFrequency(9, 125000);
 
 DDRB |= (1<<PINB5);//|(1<<PINB6); //OC1A and OC1B on arduino Pin 11 and Pin 12.
 
 pinMode(11, OUTPUT);//125kHz carrier generated here
 pinMode(8, INPUT_PULLUP);

 //digitalWrite(9, HIGH);
 //delayMicroseconds(8);
 //digitalWrite(9, LOW);
 //delayMicroseconds(8);
 //Timer1.initialize(8);
 //Timer1.pwm(9, 512);
 
 /*digitalWrite(9, HIGH);
 delayMicroseconds(8);
 digitalWrite(9, LOW);
 delayMicroseconds(8);*/

 

 //USE FAST MODE TO ENABLE DOUBLE BUFFERING - GIVES A GLITCH FREE OUTPUT


 //TCCR1A |= (0<<COM1A1)|(1<<COM1A0) ;        //Toggle on reaching OCRA            // this register set to 0!
 //TCCR1A |= (1<<WGM10);
 
 TCCR1A = (1<<COM1A1)|(1<<COM1A0)|(1<<COM1B1)|(1<<COM1B0)|(0<<WGM11)|(1<<WGM10);
 TCCR1B = (1<<ICNC1)|(1<<ICES1)|(0<<WGM13)|(1<<WGM12)|(0<<CS12)|(1<<CS11)|(1<<CS10);  //250kHz; prescaler 64; Set to Fast PWM Mode - 8 bit; enable noise canceller; enable input capture
 OCR1A = 127;
 OCR1B = 0;
 TCNT1H = 0;
 TCNT1L = 0;    

 TIMSK1 = (1<<ICIE1)|(1<<TOIE1);                       // enable input capture interrupt for timer// enable overflow interrupt to detect missing input pulses 

/*
 TCCR2A |= (1<<WGM21);
 TCCR2B |= (1<<CS22)|(1<<CS21);
 OCR2A = 1;

 TCCR0A |= (1<<WGM01);
 TCCR0B |= (1<<CS02) | (1<<CS00);
 OCR0A = 1;*/

 //currentMicros = micros();
 //previousMicros = currentMicros;
 //lowPeriod = Period - highPeriod;
 
 Serial.print("Finished setup\r\n");
 Serial.println(in_buffer);
 Serial.println(Value);
 
}



//long lastTime;
//int dataState = 0;

short j;

void catchRisingEdge()   {TCCR1B |= (1<<ICES1); TIFR1 |= (1<<ICF1); rising = 1;}
void catchFallingEdge()  {TCCR1B &= ~(1<<ICES1); TIFR1 |= (1<<ICF1); rising = 0;}

// this loop prints the number of pulses in the last second, showing min and max pulse widths 
void loop() {




while(1){
  
  for(i=0; i<800; i++){
    
    in_buffer[i] = digitalRead(8);
    
    }}
  //Serial.print(in_buffer);

/*for(j=0; j<800; j++){

  Serial.print(in_buffer[j]);
  
  
  
  }}*/
/*currentMicros = micros();
elapsedMicros = currentMicros - previousMicros;
if (highTime == 1 && elapsedMicros >= highPeriod) {
PINB = PINB | 0b00000011;
previousMicros = previousMicros + lowPeriod;
highTime = 0;
}
if (highTime == 0 && elapsedMicros >= lowPeriod) {
PINB = PINB | 0b00000011;
previousMicros = previousMicros + highPeriod;
highTime = 1;
}*/

  
 //char buffer[800];
 // show the captured data, divide pulse widths by 2 to display time in microseconds
 //sprintf(buffer,"Got %d pulses: min=%d, max=%d (%d timer overflows)\r\n", PulseCount, MinValue / 2, MaxValue / 2, Overflows);         

 
 //SetPinFrequency(9, 125000);
 //on();
 // digitalWrite(OUT, HIGH);
 // delay(8);
 // digitalWrite(OUT, LOW);
 // delay(8);
 //off();
 
 //int val = digitalRead(9);
/* uint8_t state = 0;
 while(1) {
    if(state == 0) {
      digitalWrite(9, LOW);
      state = 1;
      delay(8);
      catchRisingEdge();
      //Serial.println(state);
      //Serial.println(rising);
    } 
    else {
      digitalWrite(9, HIGH);
      state = 0;
      delay(8);
      catchFallingEdge();
      //Serial.println(state);
      //Serial.println(rising);
    }}*/
  //Serial.println(rising);

  //duration = pulseInLong(9, HIGH);
  //Serial.println(duration);
  //value = pulseIn(9, HIGH);

/*
  int analogValue = analogRead(9);
  int newdataState = (analogValue > 600);//need to check, hoping boolean will evaluate to 1 when true

  if(dataState == 0 && newdataState == 1){//if the led was off, but will be turned on, start the stop watch
    lastTime = millis();//store the current time
    dataState = newdataState;
  }

  if(dataState == 1 && newdataState == 0){//if the led was on, but will be turned off, get the difference between the last time we started counting time
    long difference = millis() - lastTime; //get the current time, but subtract the last stored time
    dataState = newdataState;
    Serial.println(difference);
  }

  digitalWrite(9,dataState);*/
  
/*
 while(1){
  if
  
 }*/
/*/////////////////////////////////////////////*/

 /*static uint16_t last_toggle;

    if ((uint16_t) micros() - last_toggle >= TOGGLE_TIME) {
        PINB = _BV(PB1);             // invert pin state
        last_toggle += TOGGLE_TIME;
    }

   Serial.println(last_toggle); */
/*///////////////////////////////////////////////////////*/ 
 //Serial.println(val);
 //val = analogRead(pwmPin);
 //analogWrite(ledPin, val/4);
 //Serial.println(val);
 //Serial.print(buffer);    // send the info to the serial port
 /* reset variables ready for the next reading */   
 /*PulseCount = Overflows = 0; // reset counts
 MinValue =    30000;        // set values to extremes
 MaxValue =    0;      
  
 delay(800);  */           // wait 1 second for next update  [/font]            
}


/*
void takeReading(){
  int val = digitalRead(9);
  val++;
  Serial.println(val);
}
*/

a scope ?

Is it possible to check the output without any instrumentation?

Use the capture and compare on the timers.

Does that print any values on the serial monitor? If yes, then how?

lrdazmmlk: Does that print any values on the serial monitor?

Of itself, no, the capture and compare hardware in the AVR does not print anything to the serial monitor. But it does provide data, which you could pass to serial.

Oh ok. How can I pass that data to serial?

Using Serial.print. :o

:) ..i did..it's printing trash.

#define SYNC_BITS 50 //NUMBER OF BIT PERIODS TO MEASURE
//#define MAX_MSG_BITS 1500 //STOP READING HERE
#include <util/delay.h>
const byte interrupt_pin = 2;

boolean toggle1 = 0;

int state = 0;
//int measured_periods[SYNC_BITS];
int measured_pwm;

String data;
char a[1000];

void setup() {
  // put your setup code here, to run once:
  DDRB &= ~(1<<PINB1);//pin 9 input
  PORTB |= (1<<PORTB1);//PORTB Input Pullup
  //pinMode(9, OUTPUT);//oc1a
  //DDRD &= ~(1<<PD2);//portd pin2 input
  //PORTD |= (1<<PD2);
  //pinMode(interrupt_pin,INPUT_PULLUP);
  Serial.begin(9600);
  //delayMicroseconds(1000);
  //Serial.print("AVR Initialization");
  delay(50);

  //Serial.print(buffer_in);
 
  
  //cli();

  //Timer 1 interrupt at 125kHz
  //TCCR1A = 0;
  //TCCR1B = 0;
  //TCNT1 = 0;
  
  //turn on CTC mode
  TCCR1A |= (1<<COM1A1)|(1<<COM1A0); //normal 00//toggle 01//low level 10//high level 11
  //TCCR1B |= (1<<ICNC1)|(1<<ICES1);
  TCCR1B |= (1<<WGM12);
  //Set prescaler 8 
  //-------> no prescaler - CS10 - 1; 64 prescaler - CS11, CS10 - 1 <-----//
  TCCR1B |= (1<<CS11)|(0<<CS10); //8 prescaler

  OCR1A = 63; //[(16000000/(125000*8)] - 1//top value
  OCR1B = 0;
  //enable timer compare interrupt
  //TIMSK1 |= (1<<ICIE1)|(1<<OCIE1A)|(1<<TOIE1);
  TIMSK1 |= (1<<OCIE1A);
  //sei();

  //Serial.print("Finished_Setup");
  //delay(50);
  //allow interrupts
  

}
//digitalwrite is very slow??
/*ISR(TIMER1_COMPA_Vect){//timer1 interrupt 125kHz toggles pin 13
  //generates a wave of frequency 125kHz/2 = 62.5kHz
  //int state = 0;
  if(toggle1){
    //digitalWrite(9, HIGH);
    PORTB |= (1<<PB1);
    toggle1 = 0;
    }
  else{
    PORTB &= ~(1<<PB1);
    toggle1 = 1;
    
    }

   /*TCCR1B ^= 1<<ICES1;
   static byte lastEdge;
   byte thisEdge = OCR1A;
   byte difference = lastEdge - thisEdge;

   if(difference < x ) {
    //something
    }
    else {
      //something
      }

   //lastEdge = thisEdge;
  
  delayMicroseconds(4);
  PORTB |= (1<<PB7);
  delayMicroseconds(4);
  PORTB &= ~(1<<PB7);

  
  }*/
/*
void start_sync_ISR(){


  detachInterrupt(0);
  start_sync = true;
}

void measure_middle_ISR(){
  
  detachInterrupt(0);
  start_middle = true;
  
  }
*/

byte val;
short i;
//char buffer_in[800];
//uint8_t state = 0;
void loop() {
  // put your main code here, to run repeatedly:
/*
  PORTB |= (1<<PORTB5);
  delay(8);
  PORTB &= ~(1<<PORTB5);
  delay(8);*/
 int state = 0;
 /*if(toggle1){
  PORTB |= (1<<PINB1);
  state = 0;
  Serial.println(state);
  }
 else{
  PORTB &= ~(1<<PINB1);
  state = 0;
  Serial.println(state);
  }
  */
  measured_pwm = pulseIn(PINB1, HIGH);
  if(measured_pwm>1){//why?
    read_middle();}
    for(i=0;i>1000;i++){
      Serial.println(a[i]);
      
      }
    
Serial.println("TA TA");
while(1){}/*{
  if(readState()==1){
    PORTB^=(1<<PB5);
    }
    _delay_us(8);
  
  
  
  }*/
  //measured_ = pulseIn(2, HIGH);
  /*for(i = 0; i<SYNC_BITS; i++){
    
    a[i] = pulseIn(2, HIGH);
    read_middle();
    
    }
Serial.println(measured_periods[i]);*/
    
/*while(1){
  if(i<800){
  buffer_in[i] = digitalRead(13);
  val = buffer_in[i];
  }i++;
  }
 if(toggle1 == 0){
  state = 1;
  Serial.println(state);
  }
 else{
  state = 0;
  Serial.println(state);
 }


 

 while(1){
  if(i<800){
    delayMicroseconds(4);
    buffer_in[i] = digitalRead(13);
    val = buffer_in[i];
    }i++;
 }

  
//Serial.println(val);

Serial.println(measured_periods[i]);*/



}

short j;
void read_middle(){
  
  
  for(j=0;j<1000;j++){
    a[j] = digitalRead(9);
    delayMicroseconds(8);
    
    }
  }

Could you check this code…please? :’(

It should be printing the values in the buffer a*… but it’s not…*
instead…AVR Initilization(which is commented for now)…AVAVAVAVAVAVAVAVAVAVAVAVAVAVAVAVAV… well that’s what I am getting on the serial monitor…is this some witchcraft?

Before you post code, delete all the commented out code lines and make sure the comments that remain are correct.

What you have posted is a terrible mess, and I'm not going to waste my time trying to figure it out.