Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Arduino CTC mode output not as expected

Status
Not open for further replies.

fm101

Member level 5
Joined
Apr 13, 2020
Messages
81
Helped
1
Reputation
2
Reaction score
4
Trophy points
18
Activity points
591
I have the following CTC mode arduino code to generate 100khz square wave at pin 6.
C++:
void setup () {
  //Pin 6 (OC0A) is output
  pinMode(6, OUTPUT);   
   // Load 79 to generate 100 kHz sq.wave
  OCR0A = 79;
   // Toggle OC0A on compare match, WGM 2 (CTC mode)
  TCCR0A = (1 << COM0A0) | (1 << WGM01);
  // Start the timer, no prescalar
  TCCR0B = (1 << CS00);
}

void loop() {

}

But the oscilloscope shows peaked square wave at frequency 7.99MHz and the amplitude is just 765.44mV as shown below.
20220215_015832.jpg

Using delay and microseconds function I was able to generate expected square wave with frequency. But with the above CTC mode code I am not able to generate expected square wave. What could be the problem?
 

Hi,

What hardware do you use? (Microcontroller, module ...)
What's the system clock frequency?

Klaus
 

Try setting TCCR0A and TCCR0B to zero so you have everything at 0 to begin with.
Code:
void setup () {
  //Pin 6 (OC0A) is output
  pinMode(6, OUTPUT);
 
TCCR0B = 0;   // write 0 to timer 0 control registers
 TCCR0A = 0;
 
   // Load 79 to generate 100 kHz sq.wave
  OCR0A = 79;
   // Toggle OC0A on compare match, WGM 2 (CTC mode)
  TCCR0A = (1 << COM0A0) | (1 << WGM01);
  // Start the timer, no prescalar
  TCCR0B = (1 << CS00);
}

void loop() {

}
 
Last edited:

Hi,

What hardware do you use? (Microcontroller, module ...)
What's the system clock frequency?

Klaus
Sorry for late reply, I was out. I am using Arduino Uno which is board based on ATmega328p. Clock frequency is 16MHz. Output frequency desired is 100KHz. Using Timer 0 in CTC mode.
Try setting TCCR0A and TCCR0B to zero so you have everything at 0 to begin with.
Code:
void setup () {
  //Pin 6 (OC0A) is output
  pinMode(6, OUTPUT);
 
TCCR0B = 0;   // write 0 to timer 0 control registers
 TCCR0A = 0;
 
   // Load 79 to generate 100 kHz sq.wave
  OCR0A = 79;
   // Toggle OC0A on compare match, WGM 2 (CTC mode)
  TCCR0A = (1 << COM0A0) | (1 << WGM01);
  // Start the timer, no prescalar
  TCCR0B = (1 << CS00);
}

void loop() {

}
Yes with the initialization I get closer waveform which is at 34.3KHz but still not getting the calculated 100KHz and the amplitude is just around 590mV.

20220216_071115.jpg



Any further advice? could this be due to the DSO because this is new one?
 
Last edited:

Before my previous post, I ran your code on my Arduino and received the same 8 Mhz as you experienced. Starting with TCCR0A and TCCR0B zeroed, your code ran with the correct 100khz on pin 6. I suspect that the bootloader may cause the registers to be non zero to start with.
The low voltage you are experiencing and incorrect frequency, Is a separate issue. My arduino, is showing a 10 microsecond waveform with an amplitude of 4.8 volts at pin 6 on the scope.
Check for loading of the signal, probe settings (10x or 1x), and double check scope settings.
Maybe measure frequency with a mutimeter if you have one that will measure the expected signal.
 

    fm101

    Points: 2
    Helpful Answer Positive Rating
Before my previous post, I ran your code on my Arduino and received the same 8 Mhz as you experienced. Starting with TCCR0A and TCCR0B zeroed, your code ran with the correct 100khz on pin 6. I suspect that the bootloader may cause the registers to be non zero to start with.
The low voltage you are experiencing and incorrect frequency, Is a separate issue. My arduino, is showing a 10 microsecond waveform with an amplitude of 4.8 volts at pin 6 on the scope.
Check for loading of the signal, probe settings (10x or 1x), and double check scope settings.
Maybe measure frequency with a mutimeter if you have one that will measure the expected signal.
Thank you for testing and confirmation.
I now got the correct frequency and amplitude. The attenuation probe setting led to amplitude misread and changes in the OCR value led to the frequency mistake.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top