FYI⌠Regarding the 328P chips and Speed
I like speed, too. And, once upon a time I thought I was getting the Max speed by using external XTL. Then, I did some testing and learned just how foolish my âthinkingâ was.
Realizing that Arduino loads the chip with stuff specific the Arduinoâs syntax and, being a âCâ programmer, I coded several approaches for spitting out High/Low on a Port without using Arduino syntax.
I used various âCâ and Bit-Bang approaches and a Oâscope. So as to not cause extra delay, I added a 1 mico-second delay (not milli-second). {could have used a âNOPâ, instead).
Also, I use Main/While, instead of Setup/Loop for max speed increase and avoiding Arduino bloatingâŚ
Summary of what I learned:
⢠The fastest the chip will pump out via a Port is 4Mhz. This is consistent with testing many Geeks discover. Itâs well documented in Books.
Since itâs been a few years, I loaded a 328p chip (not an Arduino UNO) with examples of code from my Speed-Code and hooked up the OâscopeâŚ
Attached is my code - fairly self explanatory: just Uncomment one section at a time and program the chipâŚ
The .INO file attached and Text of the Code (for amusement of others not wanting to bother with Arduino IDE)
Speed_wo_delay_ARD.ino (2.6 KB)
EDIT - Comment out the Delay to achieve 4MHz
The Code
/* Author: B.T.
- Port speed testing w/o delay. SUMMARY: Binary and Hex are fastest at 3.98MHz
_BV at 3.98 MHz. Shift is 1.1 MHz. sbi/cbi is 414 kHz.
RESULTS: At delay=1us, Binary, Hex and BV are the fastest. */
//-------------------------- DECLARATIONS ------------------------------------
#include <stdio.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define DELAYTIME 1 // each on, off
#define LED_PORT PORTC
#define LED_PIN PINC
#define LED_DDR DDRC
// sbi / cbi function (actuualâs are depricated in C )
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
//------------------------------- MAIN ---------------------------------------
int main(void) {
LED_DDR = 0x09; // setup output A0, A3 (=C0, C3)
// 0000 1001 = 0 9
while (1) {
// Group 1, binary set/clear Speed Result = 3.95 MHz
LED_PORT = 0b00001001;
_delay_us(DELAYTIME);
LED_PORT = 0b00000000;
_delay_us(DELAYTIME);
//---------------------------------------------------------------------------
// Group 2, hex set/clear Speed Result = 3.95 MHz
// LED_PORT = 0x09;
// _delay_us(DELAYTIME);
//
// LED_PORT = 0x00;
// _delay_us(DELAYTIME);
//---------------------------------------------------------------------------
// Group 3, shift set/clear Speed Result = 1.1 MHz
// LED_PORT ^= ((1 << 0) | (1 << 3)); // A0, A3 (=C0, C3)
// _delay_us(DELAYTIME);
// LED_PORT &= ~((0 << 0) | (0 << 3));
// _delay_us(DELAYTIME);
//---------------------------------------------------------------------------
// Group 4, sbi/cbi set/clear Speed Result = 260 kHz
// sbi(LED_PORT, 3); // A3 (=C3), â multiples
// _delay_us(DELAYTIME);
// cbi(LED_PORT, 3);
// _delay_us(DELAYTIME);
//---------------------------------------------------------------------------
/* Group 5, BV bit set/clear Speed Result:
with &= ~ off: 2.6 MHz, with = ~ off: 3.98 MHz */
// LED_PORT = _BV(0) | _BV(3); // turn on #0 and #3
// _delay_us(DELAYTIME);
// LED_PORT = ~(_BV(0) | _BV(3)); // turn off #0 and #3
// _delay_us(DELAYTIME);
//---------------------------------------------------------------------------
}
return (0); // never reached
}//end main