Microcontroller Register Basic Explanation

Posted on

Explanation Register in a Microcontroller

Explanation of basic microcontroller register – This article will discuss what it is registered and accompanied by images. However, before we started, let’s give a little limit about what will be discussed.

The microcontroller and microprocessor are very many types, ranging from producers like:

  • NXP
  • Infineon
  • Panasonic
  • On Semiconductor
  • Microchip
  • Atmel
  • STMicroelectronics
  • Texas Instruments
  • 3PEAK
  • AIC(Analog Integrations)
  • AME
  • Allianc
  • AnaSem
  • Anachip
  • Analog Devices
  • Axelite TechBL(Shanghai Belling)
  • Corebai Microelectronics
  • DIOO
  • Daily Silver Imp Microelectronics
  • Diodes Incorporated
  • FUJITSU
  • HK
  • HTC Korea TAEJIN Tech
  • HX(hengjiaxing)
  • Holtek Semicon
  • Intersil(Renesas Electronics)
  • KEC Semicon
  • Kodenshi AUK
  • LOWPOWER
  • LRC
  • MICRONE(Nanjing Micro One Elec)
  • MaxLinear
  • etc

There are 2 most commonly used architecture:

  1. CISC
  2. RISC

This article will focus on learning microcontroller register atmega328 only for the basics.

A. What is Register?

Register is a container that can store binary data (0 and 1). To give an understanding of the container, I illustrated that 1 container was the same as a cardboard and data was a ball.

Look at to this picture.

Microcontroller Register Basic Explanation

1 cardboard if we simp into the memory system is 1 bit.

If 1 cardboard contains the ball, the bit value is 1.

If 1 cardboard does not contain balls, then the bit value is 0.

In the picture above, it consists of 8 cardboard, starting from to 0 to 7. Because the number of cardboard is 8, then this is called 8-bit.

8-bit is equal to 1 byte. This means 1 row registers contain 1 byte.

If 3 balls are inserted into the order of 1, 2 and 5. Then, the binary value is 00100110 or in decimal value is 38 results from 32 + 4 + 2.

Each 1 register has an address and is often written with hexadecimal numbers such as 0x00, 0x1A and so on.

In a microcontroller, it contains a register-register-number.

Each register connected into the same bus line to be able to store data at a particular address.

If in “illustrated”, the register-register in a microcontoller is like the following:

Microcontroller Register Basic Explanation

Because this register can store data, this register is also called memory.

Registers, which are special memory parts of this microcontroller are stored into their own memory blocks.

Inside atmega328, there are 3 memory types or 3 main memory blocks:

  • Progam Memory (Flash)
  • Data Memory(SRAM)
  • EEPROM

Well, the register is in data memory (SRAM). Look at the following picture:

Microcontroller Register Basic Explanation

Because the register is in the SRAM block, then if the voltage source is disconnected from the voltage source, the data inside the register will be deleted and become 0 again.

B. How to access microcontroller registers

Because there are 3 registers in the ATmega328, then we will learn how to access I / O registers only to access I / O port, because this will be used in the project later.

Every microcontroller definitely has a pin. Each pin has its own function. One of them is as input and output (I / O).

Example of Input:

  • Read button
  • Read voltage
  • Read ADC
  • etc

Example of Output:

  • Turning LED
  • Give a PWM signal
  • Send serial communication signals, i2C
  • etc

Then, how can we access or in other words “manipulate” the port on the microcontroller as input or output?

On the Atmega328 datasheet sheet, page 59 mentioned, there are 3 registers (from the I / O registers) to be able to manipulate the port, namely by the name DDXN, PortXN, and PinXN.

Look at to the following picture:

Microcontroller Register Basic Explanation
  • DDxn is a data direction register. The function is to determine the direction of the data in or out.
  • PORTxn, is a port register. The function is to determine which port you want to use (B, C, or D).
  • PINxn, is a register PIN. The function is to determine the PIN (0, 1, 2, 3, 4, 5, 6, 7 or 8) from the specific port to use.

    *xn shows that “x is the port name” and “n is a bit sequence”.
    For example, DDD1. Read is direct data on the port d bit 1.

Look at the following picture to provide a block understanding of the I / O registers.

Microcontroller Register Basic Explanation

For the next example, we try to focus on PORTs. For explanation set the port and direction of the data in or out, look at to the following image.

Microcontroller Register Basic Explanation

C. Examples of I / O access programs atmega328 microcontroller ports

Now let’s practice the tutorial above. First, we will make the program turn a LED on PIN 0 on Portd. Second, we will make the program read a button on PIN 1 on Portd. Third, we will create a progam that will combine the two programs above. So, when the button is pressed, the LED will live and vice versa.

1. Turn On LED

Now, we already know to be able to make the PIN into an output/input is to change the data bits 1 or 0.

Because the LED is a component that must be turned on using the voltage of the microcontroller, then we must set the 0 pin on the port to the output.

The trick is:

>> Set biner DDRD menjadi B00000001

Then, to be able to make a PIN a 5V (high) voltage that will turn on the LED, then we have to set the Bit PORD on the 0 pin to be worth 1.

>> Set biner PORTD menjadi B00000001

To give a voltage of 0V (Low) to turn off the LED, then we must set the portd bit on pin 0 to be worth 0 again.

>> Set biner PORTD menjadi B00000000

Arduino Code:

void setup() {
  DDRD = B00000001;   //set pin 0 of PORTD to OUTPUT
}

void loop() {
  PORTD = B00000001;  // turn the LED on (5V)
  delay(1000);        // wait for a second
  PORTD = B0000000;   // turn the LED off (0V)
  delay(1000);        // wait for a second
}

2. Read button

Now we will read the button on PIN 1 from the port. The steps taken are set DDRD as input.

>> Set biner DDRD menjadi B00000000

In this example, we don’t set up PORTD, because we will use an external pull-down resistor.

To get a high or low value on PIN 1 from Portd, we must read so that the binary value to B000010 and how to read it is adding commands and on the program as:

>> PIND & (1 << PIND1);

Here, we will get high or low when the button is pressed or not pressed.

However, here the problem is the microcontroller does not provide high logic with a value of 1 or low with a value of 0, but with a decimal value of the sequence of the bit.

  • If the pin used is PIN-0, then the high value is 0.
  • If the pin used is PIN-1, then the High value is 2.
  • If the pin used is PIN-2, then the High value is 4.
  • If the pin used is PIN-3, then the High value is 8.
  • If the pin used is PIN-4, then the high value is 16.
  • If the pin used is PIN-5, then the High value is 32.
  • If the pin used is PIN-6, then the high value is 64.
  • If the pin used is PIN-7, then the high value is 128.

In our program or generally, we only want these values to logically high (1) and low (0), because with these two logic we can easily compare a condition.

So that these conditions can be read high is 1 and Low is 0, then we need to add the Binary Right Shift operator “>>” as follows:

>> (PIND & (1 << PIND1)) >> PIND1;

Explanation:

(PIND & (1 << PIND1)), When the pressed button will produce a value of 2 and P 1 has a value of 1.

If (00000010) >> 1, read “move each bit data to the right 1 step”. So the final binary is 0000,0001.

Well, here we have obtained a condition where if the button is pressed, it will be high with a value of 1 and the button is not pressed, the value will be logged in low value 0.

For the program are as follows. Use the monitor serial to view the resulting data when the button is pressed or removed.

void setup() {
     Serial.begin(9600);
     DDRD = B00000000;   //set pin 0 of PORTD to INPUT
}

void loop() {
     uint8_t con = (PIND & (1<<PIND1)) >> PIND1;
     Serial.println(con);
}

3. Read the button to turn on the LED

Now, let’s join the two programs above. When the button is pressed, the LED will live and when the button is removed, the LED will die.

The program code is as follows:

void setup() {
     Serial.begin(9600);
     DDRD = B00000001;   //set pin 0 of PORTD to INPUT
}

void loop() {
     uint8_t con = (PIND & (1<<PIND1)) >> PIND1;
     Serial.println(con);

     if (con == 1){
       PORTD = B00000001;
      }
      else {
        PORTD = B00000000;
      }  
}

The results are as follows:

Microcontroller Register Basic Explanation

If you feel this microcontroller register article is useful, please share this article with the sharing button. May be useful.

Source:

  1. Sparkfun (YT), Level Up Your Arduino Code: Registers
  2. AVR basics: reading and writing GPIO pins