Arduino Fast PWM with other code fail

it's my first time to code Fast PWM.... I copy code from other web.
I intend to generate 40Khz square waveform out eight times, and with other functions.
my code is below. i can successful output 40Khz by pin3, but fail to control pin13 for blinking function.

void setup() {
pinMode(5, OUTPUT);
pinMode(6,OUTPUT);
TCCR0A=0;//reset the register
TCCR0B=0;//reset the register
TCCR0A=0b01010011;// fast pwm mode
TCCR0B=0b00001001;// no prescaler and WGM02 is 1
OCR0A=199;//control value from the formula
pinMode(13, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly
 digitalWrite(13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);                       // wait for a second
  digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
  delay(100);
}

1.png

2.png

1.png

2.png

generate 40Khz square waveform out eight times

I don't understand what you mean by "out eight times".

can successful output 40Khz by pin3

Your 'scope shows pin 6, not pin 3.

fail to control pin13 for blinking function

"Fail"?

Why does your 'scope shows 40Hz for pin 13? It should show only 5Hz I think. Do some of those register settings affect the output of pin 13 in some way?

sorry! I may not describe clear.
I mean I want to output (character 40KHz) square repeat eight times.
From my code, it really can output 40Khz square waveform. <-- sorry, I don't understand how to calculate the frequency. I think it related TCCR0A、TCCR0B、OCR0A.

PaulRB:
I don't understand what you mean by "out eight times".
Your 'scope shows pin 6, not pin 3.
"Fail"?

Why does your 'scope shows 40Hz for pin 13? It should show only 5Hz I think. Do some of those register settings affect the output of pin 13 in some way?

Sorry, but I don't think we understand each other. You have not understood my questions. I do not think I understand what you want to achieve. Is there a forum section for your native language?

Hi,
Do you want to output, 8 square waves only, with a frequency of 40kHz?

What is the application that needs this signal?

Thanks.. Tom.. :slight_smile:

TomGeorge:
Hi,
Do you want to output, 8 square waves only, with a frequency of 40kHz?

What is the application that needs this signal?

Thanks.. Tom.. :slight_smile:

Yes, only you know me. you are a ray of my hope.
I want to generate 8 square waves only, with a frequency of 40kHz. --> output to ultrasonic sensor to realize backup parking sensor system.

Hi,
If you are measuring distance, why aren't you using the NewPing Library to do all the work for you?

https://playground.arduino.cc/Code/NewPing/

You can then just request a measurement when ever you need one.

Tom.. :slight_smile:

i have tried your lib. and get start with example named NewPingExample.uno, i found this lib will generate pulse which is 10us, not 40KHz.
i think this lib is for ultrasonic sensor module, like below image.

but i am using is in attached.

擷取a.PNG

aa.png

Hi,
If you measured that at the input to the ultrasonic PCB, then that is the Tx command, the PCB itself will be generating the 40kHz signal.

Can you post a picture and schematic of what you are trying to drive to get a distance measurement?

What do you need that the NewPing and HC-SR04 cannot do for you?

Tom... :slight_smile:

sorry, i have another question.
who knows 40Khz how to calculate by setting registers?

TCCR0A=0b01010011;// fast pwm mode
TCCR0B=0b00001001;// no prescaler and WGM02 is 1
OCR0A=199;//control value from the formula

i bought a parking sensor system from the market.
someday I connected wrong way,causing MCU is broken.
i tried to understand how it works so I buy a new one.
MCU module can't be recognizable....so sad.
So I measured health one and want to copy action by Arduno. <-- this is my main idea.

Hi,
Ops picture;


Thanks for the explanation.

Tom... :slight_smile:

TCCR0A=0b01010011;// fast pwm mode
TCCR0B=0b00001001;// no prescaler and WGM02 is 1
OCR0A=199;//control value from the formula

This looks correct to me. Fast PWM to OCR0A with the output toggled on compare match.

200 counts at .0625us per count is 12.5us, or 25us for the complete cycle = 40KHz square wave

Why do you think you are not seeing 40KHz output?

sorry! i still have question.
you mean, my code "199" refer to your 200 count?

and why .0625us for each count? and why it toggle?

cattledog:

TCCR0A=0b01010011;// fast pwm mode

TCCR0B=0b00001001;// no prescaler and WGM02 is 1
OCR0A=199;//control value from the formula




This looks correct to me. Fast PWM to OCR0A with the output toggled on compare match.

200 counts at .0625us per count is 12.5us, or 25us for the complete cycle = 40KHz square wave

Why do you think you are not seeing 40KHz output?

from your description, my understand is that 1 count equal .625us.
so i code below

void setup() {
pinMode(3, OUTPUT);
pinMode(11,OUTPUT);
TCCR2A=0;//reset the register
TCCR2B=0;//reset the register
TCCR2A=0b01010011;// fast pwm mode
TCCR2B=0b00001001;// no prescaler and WGM02 is 1
OCR2A=1;//control value from the formula

}
void loop() {

}

but i get about 3.9Mhz square, not 16Mhz. <-- why?

acc.png

acc.png

Timer counts are zero indexed so you have two ticks 0,1 before the timer resets. You toggle the output pin on count 1.

You have four counts for the total cycle = 4 MHz.

You would benefit from a study of Nick Gammons Timer/Counter tutorial