Offline
Newbie
Karma: 0
Posts: 36
|
 |
« Reply #15 on: April 22, 2012, 06:21:33 pm » |
Thanks for the explanation. This method it is, then!
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 36
|
 |
« Reply #16 on: April 24, 2012, 03:41:08 am » |
I used the following 2 fxn to measure frequency using your (nick gammon's timer method) code.
long getFrequencySampled() { #define SAMPLES 4096 long freq = 0; for(unsigned int j=0; j<SAMPLES; j++) freq+= 500000/pulseIn(pin, HIGH, 250000); return freq / SAMPLES; }
long getFrequency() { long duration=pulseIn(pin, HIGH, 250000); Serial.print("duration of pulse is: "); Serial.println(duration); //in microseconds duration=duration*2; Serial.print("duration of pulse is: "); Serial.println(duration); //in microseconds freq = 1000000/duration; Serial.print("Frequency is: "); Serial.println(freq); return freq; }
getFrequencySampled() returned a value of 40.5-41 Khz. getFrequency() returns a value that alternates randomly between 35 Khz and 41 Khz. (for pulse durations of 12 & 14 microseconds)
Any idea why the frequency doesn't equal the theoretical value?
|
|
|
|
« Last Edit: April 24, 2012, 03:43:57 am by praky »
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #17 on: April 24, 2012, 04:22:54 am » |
long duration=pulseIn(pin, HIGH, 250000);
My code doesn't use pulseIn. Can you post the whole sketch?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 36
|
 |
« Reply #18 on: April 24, 2012, 04:31:34 am » |
I used pulseIn to measure the freq. Here is the whole code... const int pin = 7; unsigned long duration; long x; long freq; const byte LED = 10; // Timer 2 "A" output: OC2A void setup() { pinMode(pin, INPUT); Serial.begin(9600); // pinMode (LED, OUTPUT); // set up Timer 2 TCCR2A = _BV (COM2A0) | _BV(WGM21); // CTC, toggle OC2A on Compare Match TCCR2B = _BV (CS20); // No prescaler OCR2A = 209; // compare A register value (210 * clock speed) // = 13.125 nS , so frequency is 1 / (2 * 13.125) = 38095 // tone (10, 38000); } long getFrequency() { long duration=pulseIn(pin, HIGH, 250000); Serial.print("duration of pulse is: "); Serial.println(duration); //in microseconds duration=duration*2; Serial.print("duration of pulse is: "); Serial.println(duration); //in microseconds freq = 1000000/duration; Serial.print("Frequency is: "); Serial.println(freq); return freq; } void loop() { x=getFrequency(); Serial.print("Frequency is: "); Serial.println(x); } long getFrequencySampled() { #define SAMPLES 4096 long freq = 0; for(unsigned int j=0; j<SAMPLES; j++) freq+= 500000/pulseIn(pin, HIGH, 250000); return freq / SAMPLES; }
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 36
|
 |
« Reply #19 on: April 24, 2012, 05:23:01 am » |
Is it possible to create a 38 khz modulated wave by the same method?
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Offline
Brattain Member
Karma: 137
Posts: 19006
I don't think you connected the grounds, Dave.
|
 |
« Reply #20 on: April 24, 2012, 05:45:08 am » |
If you're going to use it for modulated IR, just connect one end of your LED to your constant 38kHz source, and the other end to another output pin. Waggling the other output pin at your desired (<38kHz!) rate produces a modulated 38kHz IR beam.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Manchester (England England)
Offline
Brattain Member
Karma: 271
Posts: 25422
Solder is electric glue
|
 |
« Reply #21 on: April 24, 2012, 07:55:05 am » |
If you want to modulate the IR then that is what the interrupted service routine was to do in the code I originally posted. It is best to do this since the modulation can be synchronised with the carrier.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #22 on: April 24, 2012, 06:08:14 pm » |
PulseIn isn't particularly accurate. From the documentation: The timing of this function has been determined empirically and will probably show errors in longer pulses I checked the timer output on the oscilloscope. Plus, assuming your clock is accurate, it matches the theory. PulseIn just works by counting in a loop. It looks like Mike's solution incorporates an ISR to turn the output of the timer on or off, exactly at the start of each pulse, which, as he says, would synchronize with the carrier.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #23 on: April 24, 2012, 06:12:46 pm » |
void setIrModOutput(){ // sets pin 3 going at the IR modulation rate pinMode(3, OUTPUT); TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); // Just enable output on Pin 3 and disable it on Pin 11 TCCR2B = _BV(WGM22) | _BV(CS22); OCR2A = 51; // defines the frequency 51 = 38.4 KHz, 54 = 36.2 KHz, 58 = 34 KHz, 62 = 32 KHz OCR2B = 26; // deines the duty cycle - Half the OCR2A value for 50% TCCR2B = TCCR2B & 0b00111000 | 0x2; // select a prescale value of 8:1 of the system clock }
He's using a different mode (PWM) and a different prescaler, but you can work out the maths: 1 / ((1 / 16e6) * 8 * 52) = 38461.538 So, his timer will generate 38.46 KHz, as advertised.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 36
|
 |
« Reply #24 on: April 24, 2012, 08:08:36 pm » |
I am a newbie with assembly with avr. can you please post the code of 38 Khz wave modulated with 1 Khz wave to be used with TSOP1738. Will be a life saver. Thanks.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Melbourne, Australia
Offline
Shannon Member
Karma: 218
Posts: 13896
Lua rocks!
|
 |
« Reply #25 on: April 24, 2012, 08:22:18 pm » |
What was wrong with Claghorn's suggestion on page 1?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 36
|
 |
« Reply #26 on: April 24, 2012, 10:24:17 pm » |
i don't know a lot about modulation and synchronization. I get the idea that grumpy_mike's code with little modification will produce a modulated 38 Khz wave with 1 Khz. Any idea what to modify in that code?
|
|
|
|
|
Logged
|
|
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 36
|
 |
« Reply #28 on: April 26, 2012, 12:05:08 am » |
As I have said before, I really don't want to do that unless I can't do it with software.
I am successful in generating 38 Khz thanks to you guys, now I just need it modulated with 1Khz
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 36
|
 |
« Reply #29 on: April 26, 2012, 12:33:26 am » |
@nick Your code produces output at pin 10 using timer 2. Can't I similarly produce output of 1 KHz using Timer1 at some other pin? Then connect this pin at the other end of led?
|
|
|
|
|
Logged
|
|
|
|
|
|