Thread Tools
This thread is privately moderated by Jack Crossfire, who may elect to delete unwanted replies.
Feb 28, 2015, 05:59 PM
Registered User
Jack Crossfire's Avatar
Thread OP

Arduino notes

It's 10 years late in the making, but now the Arduino nuggets not available anywhere else because no-one talks about the flaws in their own investment or they just don't know. The ATMega328 has a lot of performance not available in the default Arduino load.

The Arduino sketch is preprocessed into a C++ source file of some kind, which is linked into a main program in hardware/arduino/avr/cores/arduino. There are convenience macros named sbi in the main program but named setBit in the sketch.

The Arduino IDE runs a bunch of commands to preprocess, compile & flash your program. It would be easier to run a makefile from the command line, but there's no easy way to extract the commands from the IDE. Running a program requires uploading then clicking through to enable the serial monitor every time. The serial monitor resets it again.

There is no bit toggling opcode like there is on a PIC18. Toggle a bit with

bitWrite(PORTD, 2, !bitRead(PIND, 2));

Any project is going to finish as a standalone ATMega using its internal oscillator. The Arduino UNO is fixed to use a 16Mhz crystal & doesn't allow changing the clock source to the internal oscillator. This requires building up a 2nd board, using the UNO board to write a bootloader on the 2nd board. The clock source can only be set with the UNO board programmed with an ArduinoISP sketch.

Fortunately, the internal oscillator's 8Mhz speed can be simulated by setting the CLKPR register.

CLKPR = (1 << CLKPCE);
CLKPR = (0 << CLKPS1) |
(1 << CLKPS0);

The Arduino UNO spends a lot of clockcycles servicing a timer interrupt. Disable the timer interrupts.

bitClear(TIMSK0, TOIE0);
bitClear(TIMSK1, TOIE1);
bitClear(TIMSK2, TOIE2);

Indirection on the ATMega328 is as bad as any other microcontroller. You'll be using inline macros. The Arduino spends a lot of clockcycles calling the loop() function & servicing the serial port. Put a while loop in your loop() function & service the serial port using inline macros. The setup() & loop() functions are called in hardware/arduino/avr/cores/arduino/main.cpp

An interrupt is enabled by setting an IE bit in a register related to the peripheral, then setting the global interrupt using sei(), then defining an interrupt handler function using the ISR macro.


The interrupt names are defined in hardware/tools/avr/avr/include/avr/iom328p.h There is no clear convention for naming the header file or the interrupt vector.

There is no need to clear a flag bit or handle multiplexed interrupts like there is on a PIC18. Context switching for an interrupt is as expensive as any other microcontroller, so only use them for precise timing.

A mane loop doing absolutely nothing but toggling a pin goes at 625khz, using the internal oscillator.
Last edited by Jack Crossfire; Mar 01, 2015 at 12:53 AM.
Sign up now
to remove ads between posts

Quick Reply
Thread Tools

Similar Threads
Category Thread Thread Starter Forum Replies Last Post
Discussion Note on ESC Inputs (Using Arduino) lordvon Power Systems 2 May 22, 2016 10:51 PM