Pages: [1]   Go Down
Author Topic: How often will this program toggle the digital pin  (Read 1813 times)
0 Members and 1 Guest are viewing this topic.
Washington
Offline Offline
God Member
*****
Karma: 30
Posts: 780
Firefox & Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
for(int i=0;i<10000;i++){
    PORTD |= 0x8;
    PORTD &= ~0x8;
  }  
How fast will this code toggle the pin?
Logged

Avoid throwing electronics out as you or someone else might need them for parts or use.
Solid state rectifiers are the only REAL rectifiers.
Resistors for LEDS!

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

Depends on the clock speed of the processor
Logged

Washington
Offline Offline
God Member
*****
Karma: 30
Posts: 780
Firefox & Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

16mhz
Logged

Avoid throwing electronics out as you or someone else might need them for parts or use.
Solid state rectifiers are the only REAL rectifiers.
Resistors for LEDS!

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 514
Posts: 31558
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can't tell from the C code, you need to look at the machine code that's generated by the compiler.
Logged

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

16mHz is very slow, and 16MHz is the wrong answer too.

About 1.45MHz, with an 18% duty cycle on a Nano, according to my scope.

If you take out the loop count, about 2.63MHz, and the duty cycle goes to 33%.
« Last Edit: January 22, 2011, 05:41:53 am by AWOL » 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.

0
Offline Offline
Newbie
*
Karma: 1
Posts: 38
Turn it off!!!!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And if you want the duty cycle to be 50%, toggle the pin instead of setting/resetting:

Code:
for(int i=0;i<10000;i++){
    PORTD ^= 0x8;  //This is Exclusive OR equals.
  }

It will also run one instruction cycle faster.
Logged

Washington
Offline Offline
God Member
*****
Karma: 30
Posts: 780
Firefox & Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks  its working now.
BTW I do not know how to look at the assembly code.
Logged

Avoid throwing electronics out as you or someone else might need them for parts or use.
Solid state rectifiers are the only REAL rectifiers.
Resistors for LEDS!

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 178
Posts: 12288
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


Something to consider... Approximately every millisecond the timer 0 interrupt service routine runs.  The loop is suspended for that brief moment.  There will be a "hiccup" in the output.
Logged

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

Quote
Something to consider... Approximately every millisecond the timer 0 interrupt service routine runs.  The loop is suspended for that brief moment.  There will be a "hiccup" in the output.

Or disable interrupts before calling the function and reenable them after it returns?

Lefty
Logged

Offline Offline
God Member
*****
Karma: 32
Posts: 506
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
asm(" ldi r16,8 \n\t out 0x0a,r16 \n\t .rept 10000 \n\t out 0x09,r16 \n\t .endr \n\t");

Should be 8MHz minus a bit for interrupts and loop overhead. Unfortunately I don't have a scope or frequency meter to test it.

I'm quite impressed by the 2.63MHz just from plain C code.
Logged


0
Offline Offline
Sr. Member
****
Karma: 5
Posts: 476
what?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Something i was playing with a few months ago
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1279055639/12
Logged

Pages: [1]   Go Up
Jump to: