Fundamentals of AVR microcontroller with ATmega32

Navod Dilshan
5 min readOct 20, 2020

--

In this article series, I would like to share some fundamental knowledge about AVR microcontrollers under these points.

01)How to define INPUT pins with blinking LED bulbs

02)How to define OUTPUT pins with the interface of an IR sensor.

03)How to develop the demonstration hardware module easily with some special components.

In this article, I am going to focus on how to define OUTPUT pins with blinking LED bulbs.

How to define a pin

There are 3 registers in IO port in an IC. These names are using as a keyword when defining a pin.

  • DDRx — Data Direction Register for port x(An input or an output port)
  • PORTx — Write to Port x(when we dealing with output pins)
  • PINx — Read from Port x(when we dealing with input pins)

When we want to program the microcontroller, we should identify, what is the component we wish to connect with the IC. For example, we have to identify whether the component you wish to connect is analog or digital. Then we have to connect to the relevant port. You can refer to my first article to refresh your knowledge about these things(LINK).

The next step is to define the pin as an input pin or output pin. Here i have discussed 2 methods to accomplish that.

The first method is to define pins in binary representation. There are 8 bits to present the pins of the IC from any PIN0 to PIN7. That 8 bits are in binary (“1” and “0”).

If we want to interface an input component, we should have to place that relevant bit as 0. In contrast, if we want to interface an output component, we should have to place the relevant bit as 1 in the binary representation;

As an example, we want to interface LED bulbs to the 3rd and 6th pins of the ATmega32 IC. Therefore we should define those 3rd and 6th pins as output pins.

Suitable 8 bits are 00100100, Relevant to the example. We write the binary digits starting from pin7 to pin0 in the code. When we are programming, we should have to code these 8 bits with the DDRX keyword. (DDRX could be DDRA, DDRB, DDRC, DDRD).

0b” means, this a binary representation.

We can read the above code as “There are pins defined 3rd and 6th in pin B as output pins.” and that code tells what are the data directions of every and each pin.

Another method is to define pins in hexadecimal representation .we can define pins in hexadecimal. In this method, we have to realize what is the binary code firstly. Then we can convert to the hexadecimal easily. For this method, you should know the process of converting binary to decimal.I take my above example again and converting it to hexadecimal. This is the hexadecimal code I get after converting.

0x” represents this is in hexadecimal representation. This method is similar to the first method and gives the same meaning.

Just like that, we can define “0” as if you want interface input components.

According to this example, 3rd and 6th pins are defined as outputs. At the moment we only consider the pins we need but not others.

How to control output pins.

If we define a port as an output port, we use PORTX (PINX=PORTA, PORTB, PORTC, PORTD) keyword to write a port digitally(0 or 1). I get the above-mentioned example to clarify this.

We have already defined 3rd and 6th pins as output ports to connect the LED bulbs. But we don't know about the status of the LED bulb. Therefore we have to define the status of these pins. using the PORTX keyword. If we want to switch ON the LED, we write that port digitally ON as “1” or we want to switch OFF the LED, we write that port digitally OFF as “0”.

This is a way to define a port as an output port.

If you want to change one of the bit or if you want to turn on or turn off a pin, this method is not suitable. Therefore there is a method, we can do these changes using logic operators. I give a rough idea about that method .you can calculate and prove that with logical operators.

PORTB | = (0b00000001); Turns on bit 0
or
PORTB | = (1 << n); Turns on bit n where n = 0 to 7

PORTB & =~(0b00000001); Turns off bit 0
or
PORTB & =~(1 << n); Turns off bit n where n = 0 to 7

this is the method to turn ON/OFF a port using logical operators.

There is the source code for blinking 2 LEDs.

The following are examples of all the methods for pin representation in binary, hexadecimal, and controlling pins with logical operators.

#define F_CPU 1000000UL

#include <avr/io.h>

#include <util/delay.h>//header file for delaying LED bulbs

int main(void)

{

DDRB =0b00100100;//In binary

//DDRB =0x24;//In hexadecimal

while (1)

{

PORTB = 0b00000100;//turn off 1st led in 6th pin and turn on 2nd led in 3rd pin

//PORTB |=(1<<2); //turn on 2nd led in 3rd pin using binary operators

//PORTB &=~(1<<5); //turn off 1st led in 6th pin using binary operators

_delay_ms(1000);

PORTB = 0b00100000; //turn on 1st led in 6t pin and turn off 2nd LED in 3rd pin

//PORTB |=(1<<5); //turn on 1st led in 6th pin using binary operators

//PORTB &=~(1<<2); //turn off 2nd led in 3rd pin using binary operators

_delay_ms(1000);

}

}

You can download the source code at this link. (LINK). Then you can upload the hex file into the IC. In my next article, I hope to discuss how to control input pins and how to develop the demonstration hardware module very easily. Happy learning :).

--

--