Go Down

Topic: 120 khz with arduino (Read 1 time) previous topic - next topic

robotkid249

How do i generate a 120 khz square wave frequency using arduino. Please help i know that there is someway to do this in software.

bohne

Quote
Please help i know that there is someway to do this in software.

in software means that you don't want to use the hardware timers?

Have a look at http://arduino.cc/en/Reference/DelayMicroseconds
replace the value 50 in the two lines delayMicroseconds(50); by a value that fits your needs, e.g.: delayMicroseconds(1);
and measure the frequency on the output pin... modify the value until you read 120kHz. On my Arduino I can't get better than 108kHz with this software solution.

Maybe you consider using a hardware timer (e.g. timer1) for your problem?
http://www.dorkbot.de
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1236434254
http://www.luminet.cc

robotkid249

Ya, is their any delay that is smaller than "delaymicroseconds"?

bohne

There is no function that waits shorter than microseconds, sorry.

But you can wait with
asm volatile("nop");

and with my Arduino Diecimila (atmega168 at 16MHz)
the following code is gets very close to 120kHz:

Code: [Select]

uint8_t outPin = 2;

void setup()
{
 pinMode(outPin, OUTPUT);
 digitalWrite(outPin, LOW);
}

void loop()
{
 digitalWrite(outPin, HIGH);   // sets the pin on
 asm volatile("nop");
 asm volatile("nop");
 asm volatile("nop");
 asm volatile("nop");
 asm volatile("nop");
 asm volatile("nop");
 asm volatile("nop");
 digitalWrite(outPin, LOW);    // sets the pin off
 asm volatile("nop");
 asm volatile("nop");
 asm volatile("nop");
 asm volatile("nop");
 asm volatile("nop");
 asm volatile("nop");
 asm volatile("nop");
}


maybe on your hardware you must try more or less of the nop expressions... and I do not recommend this solution. A timer would be the better choice, but you asked for a software solution.

Oh and your Arduino should do nothing else while he is running this code... why do you need this 120kHz?
http://www.dorkbot.de
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1236434254
http://www.luminet.cc

robotkid249

Great, Thanks for the code it works perfectly.
Is this a square wave frquency?
One other question, how to i turn this output into dc, into ac power with a dc source, arduino.

robotkid249

how to i turn this output, dc, into ac power with a dc source,  which is arduino.****
sorry for the misinterpretation...

Go Up