0
Offline
Newbie
Karma: 0
Posts: 12
Arduino rocks
|
 |
« on: September 20, 2009, 07:59:14 pm » |
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
Full Member
Karma: 0
Posts: 213
sudo make me a (bread+ham+lettuce+cheese+bread)
|
 |
« Reply #1 on: September 20, 2009, 08:27:53 pm » |
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
Brattain Member
Karma: 279
Posts: 15312
Measurement changes behavior
|
 |
« Reply #2 on: September 20, 2009, 09:59:01 pm » |
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
Brattain Member
Karma: 137
Posts: 19008
I don't think you connected the grounds, Dave.
|
 |
« Reply #3 on: September 21, 2009, 01:44:21 am » |
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.
|
|
|
|
Toronto, ON
Offline
Full Member
Karma: 9
Posts: 233
|
 |
« Reply #4 on: September 21, 2009, 02:53:02 am » |
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!  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. 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.  )
|
|
|
|
|
Logged
|
|
|
|
|
London
Offline
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #5 on: September 21, 2009, 04:23:36 am » |
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
Newbie
Karma: 0
Posts: 12
Arduino rocks
|
 |
« Reply #6 on: October 06, 2009, 04:11:32 pm » |
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
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #7 on: October 07, 2009, 03:19:08 am » |
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
Newbie
Karma: 0
Posts: 12
Arduino rocks
|
 |
« Reply #8 on: October 07, 2009, 08:27:39 am » |
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
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #9 on: October 07, 2009, 08:41:18 am » |
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: #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
|
|
|
|
|
|
|
0
Offline
Newbie
Karma: 0
Posts: 12
Arduino rocks
|
 |
« Reply #11 on: October 07, 2009, 04:58:38 pm » |
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
Faraday Member
Karma: 6
Posts: 6226
Have fun!
|
 |
« Reply #12 on: October 07, 2009, 05:08:14 pm » |
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
Newbie
Karma: 0
Posts: 12
Arduino rocks
|
 |
« Reply #13 on: October 07, 2009, 06:07:46 pm » |
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
God Member
Karma: 0
Posts: 572
|
 |
« Reply #14 on: October 07, 2009, 10:16:08 pm » |
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
|
|
|
|
|
|