Open a new sketch and save as blinC.ino. Enter this code.
// blink LED on PORTB pin 5
#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>
int main (void) { DDRB=0b00100000;
while(1) { PORTB=0b00000000; _delay_ms(500);
PORTB=0b00100000; _delay_ms(500); }}
Upload
this sketch to an Uno board and watch the LED blink.
#include <avr/io.h> and <util/delay.h>
are header files located in your computer. The sketch tells the compiler to include files from the AVR-GCC toolchain which are part of the Arduino IDE product.
The avr/io library knows what ports are and util/delay can calculate milliseconds with F_CPU speed. You may see warning messages followed by successful compilation. Includes with "file.h" are in the same folder as our sketch, <file.h> are in Arduino.
Header files say to include other pages of code that have details how to define things and calculate functions. You can open, read and edit these files. Arduino libraries work this way.
Embedded software has a lot of files that present maps from names into numbers that describe the machine to the compiler.
CompilerJust like standing at the photocopier machine printing off chapters on paper and trying to get double sided and collated. The compiler program sorts and sifts through all the text in these files. It finds the code and keeps just the functions you use in your program.
Ports as circuitsInside the Arduino board we push the binary number 0b0010000 into PORTB and a voltage lights the LED. Push 0b00000000 and the light goes off. The wire going to the LED toggles +5Volts and carries a tiny current.
Look at the circuit, below. B5 is the pin on the chip and wires connect it to pin13 on the board plus the LED circuit. B0 is uno pin8 and the other pins are in the image. PORTB is a RAM memory location at address hex 0x25 or 37 in decimal. u8 means unsigned integer of 8 bits.
Ports are of sets of 8 latching flip-flop circuits that can be input or output, voltage on or off. DDRB is the data direction register for port B so it turns the pin into an output by pushing 0b00100000 into the register.
C is a functional programming language in the way that algebra works with mathematical functions. Start with addition, subtraction and the usual mathematical functions. Add to that bit operations, masking or shift-right/shift-left. Then add decision making and electricity.
The assignment function is easy and powerful #define LED_BUILTIN 13 and we can digitalWrite(LED_BUILTIN, HIGH). Arduino sketches define it as 13 because of wiring. Much of C code is telling the compiler things like PB5 and PORTB5 are reserved mnemonic keywords to be substituted with digits.
Ports as variablesOur C program uses names like PORTB and DDRB as variable identifiers. The header files or the code added by the headers tell the compiler where they are located and how to treat them.
The compiler needs to handle these variables and give them a specific address. PORTB address is at hexadecimal 0x25 or 37 decimal. Only 8 bits long we can treat them as 8bit integers or char characters. We can set them to numbers from 0=0x00 to 255=0xFF and then restart.
You may not be used to switching back and forth between binary, octal, decimal and hexadecimal. C allows us to write our program with binary 0b00100000, decimal 32 or 0x20 in hexadecimal.
The LED is wired to the sixth pin on PORTB of the integrated circuit chip. But the circuit calls it B5. Lots of documentation calls it pin 5 because we start our numbering at B0. Bit operations in C allow you to say (1<<PB5) because 0b00100000 = (0b00000001 << 5).
main( ) FunctionC is a functional programming language. Like an algebraic equation our main() function has an answer. The answer is that main( ) = a 16bit integer located in RAM memory. main(void) because no parameters come from outside of main( ) to calculate.
I think it is a pointer to the integer value of an address in flash, the instruction being executed or the data being loaded. Tell me in the comments if this is correct.
We just took C code and dropped it into an Arduino sketch. Doing this with a larger project can take a lot of work. But imagine just porting code and using the Arduino ecosystem of hardware and software.
The Arduino programming language is a dialect of C++. ArduinoCorp added some programs and put in descriptor files for their products. Libraries are provided to get you going. C++ was designed to contain the C programming language.
The Arduino IDE contains the AVR-GCC toolchain. Most of what you do on Atmel Studio or MPLab can be done on Arduino. It may even be easier to program.
Comments