# 40 kHz square wave with timer1

hello, I need to create a wave of 40 kHz and 10 in pin9 timer1, but when it generated in the generated pin9 20 and pin10 to 40 khz, does anyone could help me ?. Thank you. This is my code:

``````const long  frecuencia = 40000L;

void setup()
{

pinMode(9, OUTPUT);
pinMode(10, OUTPUT);

// ACTIVAMOS PIN 9 CON COM1A0 Y PIN10 CON COM1B1
TCCR1A = _BV(WGM11) | _BV(WGM10) | _BV(COM1A0) | _BV(COM1B1) ;
//TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // No prescalar
TCCR1B = _BV (WGM12) | _BV (CS11) | _BV(WGM13);

OCR1A = ((F_CPU / 8) / frecuencia) - 1;
OCR1B = (((OCR1A + 1) / 2) - 1);
}

void loop()
{}
``````

any ideas?
thank you..

any ideas?

Yes. Look at your code. Did you put the smiley in it? No.

Read the how to use this forum sticky to find out how to post your code correctly.

Why do you want to use timer 1? Why not another timer?

Thanks for answering the first, I did not notice the smiley, sorry. At first I would give just use the timer0, timer1 or the timer2, but only one of them, what I need is to set the output of two pins 40 khz, are could do with just a timer ?.

Thanks and regards

hello, I need to create a wave of 40 kHz and 10 in pin9 timer1, but when it generated in the generated pin9 20 and pin10 to 40 khz, does anyone could help me ?. Thank you. This is my code:

1st, CODE TAGs around your code.

Second, from your post, I am under the assumption that you want to create 40 kHz on pin 10, and 20 kHz on pin 9.

If you just want to create a square wave, you should be using the first CTC mode ( set WGM12, clear the other WGM bits), not fastPWM mode. You should also be using a the same compare mode for A and B, setting the COMP1_0 bits and leaving the other one clear. This will toggle the bit on match.

The other problem is that the timer interval describes one half of the timer period, not a whole period. You need to divide by two again before subtracting one.

And when calculating OCR1B, you should be multiplying by 4, not dividing by 2.

Thanks for answering Jiggy-Ninja, I would like the two output pins 40 khz, in this case on pin 9 and pin 10, no one would like half of them.

I am a beginner. Thanks for everything.

Why have you not corrected that first post?

Having two outputs produce exactly the same signal is pointless, just wire the one output to the two of what ever they are.

I just correct the first post.
I need two outputs because they are two ultrasonic transmitters with two receivers and have to go to 40 khz.

Thank you.

Is it not possible that the same timer have the same frequency in two pins?

Wrong mode, you can't use mode 15 and drive both pins properly,
use mode 14 (ICR1 sets the TOP value)

``````const long  frecuencia = 40000L;

void setup()
{

pinMode(9, OUTPUT);
pinMode(10, OUTPUT);

TCCR1A = _BV(WGM11)  | _BV(COM1A1) | _BV(COM1B1) ; // both in clear-on-match
TCCR1B = _BV (WGM12) | _BV (CS11) | _BV(WGM13);  // divide-by-8

ICR1 = ((F_CPU / 8) / frecuencia) - 1;
OCR1A = ICR1/2 ;
OCR1B = ICR1/2 ;
}

void loop()
{}
``````

Before you were toggling one pin and not the other, but since OCR1A was setting
TOP toggling was the only mode that could work for pin 9. Using mode 14 allows
both pins complete freedom.

I just correct the first post.

No you haven't, it is not in code tags.

I need two outputs because they are two ultrasonic transmitters with two receivers and have to go to 40 khz.

Why can you not wire both transmitters to the same pin?

Thanks MarkT, I understood many things I had wrong and not know why.

Thank you.

Grumpy_Mike now if I modified the post and Need two to differentiate your emit wave with which you receive.

Greetings