Ep.1: Adventures With Embedded C and Arduino

by Electronhub in Circuits > Arduino

414 Views, 0 Favorites, 0 Comments

Ep.1: Adventures With Embedded C and Arduino

EP.1  Adventures In Embedded C and Arduino.png
Ep1: Adventures in embedded C and Arduino Demo Video

We all know how good Arduino is, right? But on the other hand, we all know there is so much background work that the Arduino's programming language has abstracted from us. In this and the upcoming series of 5-10 tutorial posts will be all about how actual embedded C works. We will start from the ground up. The classic LED blink (the hello world of electronics!) to interfacing with various sensors to serial communication everything will be implemented using Embedded C. The industry uses embedded C, other microcontrollers available in the market deliver and work with embedded C. There is some amount of learning curve involved in this but with some amount of effort I think everyone can easily get aquainted with embedded C.

In this particular tutorial we will start with blinking an led on different ports.

Outcome of today's tutorial:

  1. How the ATMega328P microcontroller works (The microcontroller used in Arduino's Uno and Nano boards).
  2. How does embedded C work.
  3. All about Registers and how to configure.
  4. All about the microcontroller's I/O Ports.
  5. Comparision between Arduino and embedded C.

Prerequisites:

  1. Basic Knowledge of Arduino
  2. Basic Knowledge of C programming
  3. Basic understanding of Binary Operations

Supplies

embedc_supplies[1].jpg

1 x Breadboard

1 x Arduino Nano / Arduino Uno

1 x 2.5mm LED

1 x 1KOhm Current Limiting Resistor

5-10 x Jumper Wires


Electron Hub is an Electronics Company. We sell Custom designed and made products catering to DIY enthusiasts, Engineering Students, Hobbyists, Makers, etc. We also help people by posting tutorials, How Tos and guide videos.

You can follow Electron Hub on Instagram: @electronhub_

What Are Registers?

Imagine a microcontroller as a tiny, super-smart brain inside your electronic devices, like a robot or a microwave. Now, think of registers as the microcontroller's sticky notes or small storage spaces. Each sticky note (register) can hold a small piece of information, like a number or a special code.

These sticky notes (registers) help the microcontroller keep track of things and perform tasks. For example, one sticky note might hold a number representing how many times you want your robot to move forward. Another sticky note might keep track of whether the microwave door is open or closed.

When the microcontroller needs to do something, it looks at these sticky notes (registers), reads the information on them, and follows the instructions accordingly. It's like the brain checking its notes to know what to do next.

Registers in microcontrollers are like the memory spaces that store essential information, making sure everything runs smoothly and your electronic gadgets behave just the way you want them to.

Remember, Registers are your best friends.

Refersher on Binary Operations

truth_table.png
leftandrightshift.png

(Please click on the images to view the full image)

AND operation: The AND operation states that if 2 bits are given as an input then the output will be '1' only if both the inputs are '1'. Otherwise, the output will be '0'. So, in other words, AND will act like binary multiplication, easy!

OR operation: In OR operation the output will be '1' if either of the two inputs will be a '1'. Simple!

XOR operation: XOR says the output will be a '1' if and only if either of the two inputs are a '1'. Here "if and only if" is the key. So if both the inputs are a '0' or '1' the output will result in a '0'.

NOT operation: This is a single bit operator. Here the output is the complement to the input. so if input is 0 the output is 1 and vice versa.

LEFT and RIGHT SHIFT: The name says what it does in this case. Consider a 4 bit binary number '1010' if you left shit this your output would be '0100'. In Left shift the bits are moved one place to the left. The right shift is exactly opposite to the left shift. Here the bits are shifted one place to the right. Consider the same example of '1010'. When right shifted 1 bit the output would be '0101'.

Configure the Arduino IDE

Configuring your Arduino IDE

  1. Open up your Arduino IDE and create a blank project.
  2. Save it with a desired name. (File extension remains the same i.e. filename.uno)
  3. Configure your upload settings (Find them under tools menu)

Programming

atmegaportandddrbregister.png

Okay, now that your Arduino IDE is ready copy and paste the code below into the project file you made:

#include <avr/io.h>
#include <util/delay.h>
int main(void) {
    DDRB = DDRB | (1 << DDB5);
    while (1) {
        PORTB = (1 << PORTB5);
        _delay_ms(1000);
        PORTB = PORTB & ~(1 << PORTB5);
        _delay_ms(200); 
    }
    return 0;
}


Explanation of the above code:

Okay as said before registers are your best friend, for controlling the Input/Output ports of the ATMega328P microcontroller, it provides us with 2 registers named the Data Direction Register and the Port Data Register.

The names of the registers are complicated, but their functioning and working is very easy.

In order to turn off and turn on a port you need to tell a few things to your microcontroller:

  1. What Port you will be using.
  2. What Pin of that port will you be controlling.
  3. Is that particular pin an input or output (in our case it's output)
  4. Sending the actual command to the microcontroller.

In order to achieve the above mentioned tasks we will use the DDR(Data Direction Register) and the Port Register(Port Data Register)

DDR Register: This register on the ATMega328P is used to configure pins of the controller as an output or input. So basically, we are telling what port and what pin we will use and also set the Pin as an output.

while programming the DDR register it would be in the form of DDRx where x stands for the port name (in our case Port 'B').

Explanation of line 4:

the code on line number 4 is: DDRB = DDRB | (1 << DDB5);

here DDRB stands for Data Direction Register for Port B. And we need to set DDRB to ' 00100000' which means Pin 5 of Port B will act as an output.

The Binary Math:

DDRB is 'OR'ed with (1<<DDB5). reading from right to left '(1<<DDB5)' means '0010000' that is what we want, and then we perform an OR operation with DDRB. This is done to ensure that the remaining bits do not change their state.

so if you look at the DDRB register it would look like: '00000000'(Considering that initially DDRB is set to all zeros) | '00100000' = '00100000'

So we have successfully told the microcontroller to set pin 5 of port B as output.


The 2nd step is to Control the Pin 5 of PORT B, to do this we use the PORTB register:

line 6 of the code : PORTB = PORTB | (1 << PORTB5);

Here the same logic is applied.

for line 8 there is the use of two binary operations '~' which is the not operations which basically flips the bit so

~(1<<PORTB5) means setting PORTB5 to '0' and the '&' operation basically resists all the other bits from changing.



Hardware

Ep.1 Adventures in Embedded C and Arduino - final video

As far the hardware goes you do not need to connect anything externally as the code will use the on board LED on Arduino's Pin 13. If you want to still use an external LED you can go ahead and use it with a current limiting resistor.


So, that's it for today's tutorial. We have successfully taken our first steps into learning embedded C using Arduino. In the upcoming tutorials we will be implementing inputs, PWM, Timers and also interfacing with sensors and serial communication.

Stay tuned for upcoming tutorials and do follow @electronhub_ on instagram.

Please feel free to give a feedback, suggestions,etc in the comments below.