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);
}
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?
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..
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.
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.
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.
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?