Pages: [1] 2   Go Down
Author Topic: Square or Sinewave  (Read 1782 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I just recieved my Arduino Board and I am ready to try a project. I want to create a continuous squarewave or sinewave (<= 1.2 mhz) in order to excite an RLC circuit. Is that possible? I am a beginner with this system. Can someone lead me in the right direction or tell me if there is a sample?
Thanks,
Curtis
Logged

From:0x000000
Offline Offline
Full Member
***
Karma: 0
Posts: 213
sudo make me a (bread+ham+lettuce+cheese+bread)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The square wave should be easy, but the sine wave will not be easy.
Though i don't know how easy it will be for the arduino to switch on a pin and off 1,200,000 times a second...
Logged

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16540
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sine wave, no way without external filtering circuit. However to just excite a RLC circuit a square wave should be fine. I don't know the maximum frequency the timers can be operated on but I think that it might be possible to output a 1.2Mhz digital signal. You will have to wait for one of the software gurus around here to verify if and how it would be coded.

Lefty
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24424
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I assume the OP meant "MHz" and not "mHz" - 1.2 mHz is easy !  ;D
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Toronto, ON
Offline Offline
Full Member
***
Karma: 10
Posts: 233
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Guru? No.  Datasheet reader?  Yes.

Here's one way to do it.  It's a clock divider using Timer 2.  Frequency is calculated as follows:

Freq = 16000000/(2 * (OCR2A + 1))

This means that you can only get 1.3333MHz or 1.1429Mhz.  Sorry, no 1.2MHz for you!  smiley  On the other hand, you can generate a nice 1.6000MHz signal really easy.

Because it uses Timer 2 in CTC Mode (Clear Timer on Compare Match Mode), it doesn't create interrupts, and you can run anything else you like at the same time.

Warning:  this is not for the feint of heart.  It's a bit technical.  Not too technical though; you just have to read a 448+ page datasheet.  Nice light reading.  Cures your insomnia.

Code:
void setup()
{
  // OCR2A is on "Pin 10" on the Arduino Mega
  //             "Pin 11" on the Arduino 2009 ('328p)
  
  // Set OCR2A as an output
  DDRB |= (1 << PB3);  // for Arduino Mega, change PB3 to PB4

  // Set Timer 2 to CTC Toggle mode (see datasheet)
  TCCR2A = (1 << COM2A0) | (1 << WGM21);

  // No prescalar
  TCCR2B = (1 << CS20);

  // divider:
  // Freq = 16MHz / (2 * (OCR2A + 1))
  // e.g. OCR2A = 4, Freq = 1.6000MHz
  //      OCR2A = 5, Freq = 1.3333MHz
  OCR2A = 5;
}

void loop()
{
}

b

(P.S. 1.2milliHertz: while(1) { digitalWrite(10, 1); delay(416666); digitalWrite(10, 0); delay(416666); } - not that great for experimenting with frequency response though.  smiley-wink )
Logged


London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you can manage with an upper bound of 1MHz then you can use the FrequencyTimer2 library to simplify access to the timer.

Usage:
#include <FrequencyTimer2.h>

  FrequencyTimer2::setPeriod(period); // period in microseconds
  FrequencyTimer2::enable();  // start pulsing on pin 11 (set this to output mode in setup)


You can find the library here: http://www.arduino.cc/playground/Code/FrequencyTimer2
« Last Edit: September 21, 2009, 04:24:03 am by mem » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Hello,
I tried the code below and I get many errors coming from the "FrequencyTimer" library. Where do I put the downloaded library files? I changed my hardware to accept a square wave from 100 khz to 900 khz. Thanks, Curtis
// SqrWave
#include <FrequencyTimer2.h>

void setup() {
  pinMode(11,OUTPUT);
    
  FrequencyTimer2::setPeriod(10000);
  FrequencyTimer2::enable();
}

void loop()
{
}
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are using arduino 0017 you can follow the information on the Arduino reference page:  

To install, unzip the library to a sub-directory of the libraries sub-directory of your Arduino sketchbook directory (shown in the Arduino preferences dialog). If this is the first library you've installed, you'll need to create the libraries directory. After unzipping the library, (re-)launch the Arduino environment; you should see the library in the Import Library menu.

If you are still getting errors after copying the library into the correct place, please say what Arduino version you are running, your operating system and post the first few lines of the error message.
« Last Edit: October 07, 2009, 03:19:27 am by mem » Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
Thanks for the reply. Here are the errors:
This is one of the errors, they are all the same from different files in FrequencyTimer2:
C:\Documents and Settingings\Intern10\Desktop\Arduino-0017\Hardware\Libraries\FrequencyTimer2\FrequencyTimer2.cpp: In 'Static Member Function Void FrequencyTimer2::SetOnOverFlow(Void(*)())':

There was also the same type of error in that file and the other 'TIMSK' was not declared in this scope.

I am using Windows XP and Arduino 0017.
Thanks,
Curtis
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are using a standard Arduino bard then check to make sure you have the latest version, you should see this code at the beginning of the FrequencyTimer2.cpp file:
Code:
#if defined(__AVR_ATmega328P__)
// A mega328 is mostly like an mega168, in terms of peripherals
#define __AVR_ATmega168__
#endif
Download the latest version if yours does not have that code at the beginning.

If you are running the latest code, can you say what board are you using?
Logged

Wuppertal/Germany
Offline Offline
God Member
*****
Karma: 1
Posts: 895
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
just found this here
http://www.sparkfun.com/commerce/product_info.php?products_id=9116
even though it belongs into the hardware-section it matches the topic very well.

Eberhard
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you Wayoda,
I already ordered it today. I cannot get the FrequencyTime2 library to work. This product is just what I want.
Thanks everyone and I will let you know if it works.
Curtis
Logged

London
Offline Offline
Faraday Member
**
Karma: 8
Posts: 6240
Have fun!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That board wayoda found looks good, interested to hear how you get on with it.

I am also interested in understanding why you had trouble with FrequencTimer2.
Did you check it see if the code you are running is the same as I posted above?

Also, what kind of arduino board are you using  (does it have a 168 or 328 chip?)
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I tried all the code in the post and solved some errors but the library still doesn't work. I keep trying. My board is a 2009 on with the 328 chip.
Thanks,
Curtis
Logged

0
Offline Offline
God Member
*****
Karma: 0
Posts: 572
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You didn't put it in the right place (I think)

You put it in the /hardware/libraries folder yeah?

That was right for previous versions of the IDE, for 0017 read mem's post above.

It should go in the "My Documents" area, under /Arduino/libraries
Logged

Pages: [1] 2   Go Up
Jump to: