Go Down

### Topic: How i make delay .5 microseconds? (Read 1 time)previous topic - next topic

#### antkan

##### Jan 23, 2012, 08:41 am
Hello i want make delay .5 microseconds(500 nanoseconds).I must use NOP() from assembly?I have ATmega328 16MHZ....nop for me is 62.5ns...ok i want 8 nop but i could use assebly code into my sketch?I must use library first?How i doing this?A small example please...

#### nickgammon

#1
##### Jan 23, 2012, 08:49 am
Why do you want to do this?
Please post technical questions on the forum, not by personal message. Thanks!

#### antkan

#2
##### Jan 23, 2012, 09:01 am
I want make IR pulse with accuracy 95KHz...i think i must use __asm__("nop\n\t"); .....

#### nickgammon

#3
##### Jan 23, 2012, 11:44 am
1/95000 is 10.5 uS.

Anyway, you want PWM not NOPs.
Please post technical questions on the forum, not by personal message. Thanks!

#### antkan

#4
##### Jan 23, 2012, 01:06 pm
How i use PWM for doing this?

#### mromani

#5
##### Jan 23, 2012, 01:47 pm
This question reminded me I've been curious about how to change the Arduino PWM frequency, but never bothered to look hard enough... here's what I found:

http://www.arduino.cc/playground/Code/PwmFrequency

#### nickgammon

#6
##### Jan 23, 2012, 10:02 pm
This sketch will output 95.23 kHz on pin D9. The period is actually 5.25 uS (ie. for each toggle, being 84 * 62.5) which is 10.5 uS between each leading edge. The inverse of that is 95.238 kHz.

Code: [Select]
`const byte LED = 9;void setup() {  pinMode (LED, OUTPUT);    // set up Timer 1  TCCR1A = _BV (COM1A0);  // toggle OC1A on Compare Match  TCCR1B = _BV(WGM12) | _BV(CS10);   // CTC, no prescaler  OCR1A =  83;       // compare A register value to 84 (zero relative)}  // end of setupvoid loop() { }`

You can't get any closer than that, the system clock needs to pulse twice per square wave (once up, once down) and thus the resolution is 125 nS.

http://www.gammon.com.au/forum/?id=11488
Please post technical questions on the forum, not by personal message. Thanks!

#### mkwired

#7
##### Jan 23, 2012, 10:18 pm
If you are going to control a commercial device like a TV, then it's a little more complicated then PWM driving a LED.  You may want to check out the TV-B-Gone code to get some ideas if this is what you're trying to do.

#### antkan

#8
##### Jan 24, 2012, 06:36 am
I want control my air condition United...i have another A/C Toyotomi i decode ok work to 38KHz i make a library...but the United i think no have the standard frequency 38KHz i decode the pulses but no work and i must test it with little different frequency 32,33,34....40,56 KHz....i have not osciloscope....

#### nickgammon

#9
##### Jan 24, 2012, 06:40 am
Quote
I want make IR pulse with accuracy 95KHz...

...

I want control my air condition United...i have another A/C Toyotomi i decode ok work to 38KHz...

Well which is it? 38 KHz or 95 KHz?
Please post technical questions on the forum, not by personal message. Thanks!

#### antkan

#10
##### Jan 24, 2012, 07:09 am
Finaly 95 KHz is wrong i think i want a bandwidth 30-56KHz ....possible frequencies....

#### antkan

#11
##### Jan 24, 2012, 07:22 am
Code: [Select]
`void pulseIR(long microsecs) {  // we'll count down from the number of microseconds we are told to wait   cli();  // this turns off any background interrupts   while (microsecs > 0) {    // 38 kHz is about 13 microseconds high and 13 microseconds low   digitalWrite(IR_PIN4, HIGH);  // this takes about 3 microseconds to happen   delayMicroseconds(10);         // hang out for 10 microseconds   digitalWrite(IR_PIN4, LOW);   // this also takes about 3 microseconds   delayMicroseconds(10);         // hang out for 10 microseconds   // so 26 microseconds altogether   microsecs -= 26;  }   sei();  // this turns them back on}void SendACCodeOn() {  // This is the code for my particular Toyotomi on button   pulseIR(9000);   delayMicroseconds(4340);  pulseIR(840);   delayMicroseconds(320);  pulseIR(740);   delayMicroseconds(400);  pulseIR(700);   delayMicroseconds(1440);  pulseIR(740);  ........}`

Go Up