So I figure most people in our merry little band have an idea what the Arduino is. But to clarify for those who may not be so sure, It is a combination of three things:
A hardware platform which uses Atmel AVR microcontrollers alongside a hardware USB interface controller which allows the thing to easily connect to a PC's UB port.
A The Arduino Bootloader firmware on the Microcontroller that allows the chip to be easily programmed via the USB interface.
And the ArduinoIDE software platform that makes creating and editing code much easier than it would be.
The Arduino has seen major success over the years with many MANY people making a vast array of wonderful things. However most people seem unaware that the Microcontroller at the heart of the Arduino can be used on its own without the Arduino Firmware or the Software.
It has been my desire for a while now to progress into being able to program the AVR microcontrollers without using Arduino. The great thing about the way it was developed is that you can actually write generic AVR C/C++ code right into the ArduinoIDE alongside the usuall Arduino commands and functions.
This allows a sort of Hybrid code to be developed that shares the ease of Arduino along with the efficiency of C/C++
One example of this that is probably the most well known, The Arduino digitalWrite() function is slow....Very slow infact, The reason is because the digitalWrite() actually does a lot of checking and calculation in the background before it actually writes the pin. these overheads are there to allow the same language to be portable across the wide range of different Arduino products many of which use different Microcontrollers on them.
However if you know what chip you are using, And are willing to put a little effort into reading datasheets and are willing to dabble in lower level code then you can perform the same function as a digitalWrite() on up to 8 pins in a fraction of the time it takes with digitalWrite().
Most if not all Arduino functions sacrifice speed for portability and efficiency for ease for the beginner.
So how do I create a hybrid program? Simple, Just write it straight in there! because the ArduinoIDE uses the same gcc compiler provided my Atmel for their own products the generic code is interpreted automatically. An example of this hybrid code is below:
//LED sequence for ArduinoMEGA with ATMEGA2560 chip
void setup() {
DDRB = 0xff; //sets all of portB as outputs (Port b = digital pins 53, 52, 51, 50, 10, 11, 12, 13)
DDRA = 0xff; //sets all of portA as outputs (port a = digital pins 22, 23, 24, 25, 26, 27, 28, 29)
}
void loop() {
for (int i = 0 ; i <= 7 ; i++)
{
PORTB |= (1<<i); //turns on pin i in port B
PORTA &= ~(1<<i); //turns off pin i in port A
delay(128);
}
for (int i = 0 ; i <= 7 ; i++ )
{
PORTB &= ~(1<<i); //turns on pin i in port A
PORTA |= (1<<i); // turns off pin i in port B
delay(128);
}
}
You can see the DDRx = 0xff is the same saying pinMode, but the AVR C way allows all eight pins on the port to be changed to outputs in a single operation,
The PORTx bits are no more compact then you could make them with Arduino using a very similar loop, but they are significantly faster in execution. In this particular example the speed isn't so much of an issue as we are adding a 128ms delay anyway. However it is quite likely that some day you will want to interface with an external controller of some kind which will usually need a sequence of control and data signals to operate and the overheads of the Arduino function will greatly reduce the speed at which you can communicate.
To understand this further I suggest you take some time to read and understand
http://www.micahcarrick.com/tutorials/avr-microcontroller-tutorial/avr-c-programming.html I'm learning more and more myself and some time very soon I hope to not have to use Arduino at all and just be able to program the microcontroller directly.
It is important also to realise that AVR C isnt specific to a particular chip, Infact any 8-bit AVR chip uses the same language. However you do need to familiarise yourself with the pin to port mapping for each chip, And also be familiar with the hardware peripherals on each chip you intend to play with.
Further updates will be provided as I develop my own skills, Comments, suggestions and questions are always welcome :)