Pages: [1]   Go Down
Author Topic: 120 khz with arduino  (Read 820 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 250
Arduino is the brain of the robot.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 588
LumiNet rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged


0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 250
Arduino is the brain of the robot.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
God Member
*****
Karma: 0
Posts: 588
LumiNet rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?
Logged


0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 250
Arduino is the brain of the robot.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 250
Arduino is the brain of the robot.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: