Ports and Flags in Atmega32 Microcontroller using Atmel Studio

Ports and Flags in Atmega32 Microcontroller

In this article, I have explained Ports and flags in Atmega32 AVR Microcontroller using Atmel Studio 7 Assembly code and in that explained how to Write an assembly code to output data at Port B and Port C in such a way that Port B becomes ON then Port C becomes Off and vice versa. Use the simulator to single-step and examine the flags and I/O Ports after the execution of each instruction.

LDI R16, 0XFF

In R16 hex value FF is stored and in binary 11111111 value is stored.

OUT DDRB, R16

Now, in the data direction register these hex or binary values are output.  If we give 1 value to our data register, that value is configured as an output on that port. Here all 11111111 pins are configured as an output.  So, PORTB and PORTC both are configured for output here.

OUT DDRC, R16

TOGGLE: Here we use a loop.

OUT PORTB, R16

The r16 value is output on PORTB. It means our PORTB has eight ones (11111111), if we attach eight LEDs to PORTB, all will be switched on.

COM R16

Here we are taking 1’s complement. For that COM command is used. Due to complement all 11111111 are converted into 00000000.

OUT PORTC, R16

Now you can see, we are giving an r16 value to PORTC, so here 00000000 will switch off all the attached LEDs.

Video:

Moreover, If you want to perform and understand ports and flags in atmega32. Then, please watch this Video given below

EXPLANATION OF PORTS AND FLAGS IN ATMEGA32

JMP TOGGLE

As this will toggle, all values on PORTB will be 00000000 when it returns because r16 now has 00000000. Therefore, all LEDs will be turned off.

Additionally, when moving on to the following compliment command, the PORTC values change to 11111111 and LEDs turn on.

Moreover, by pressing the F7 key, we are creating a solution.

Click the F10 key to see the solution in detail.

Here, we can see that R16 is set to 0xFF. In essence, we must determine whether the data direction registers are set up for output or not.

ports and flags in atmega32 understanding

Go to Windows, then the I/O option, then the debug option.

how to check ports and flags

The ports that you have chosen for configuration must then be checked.

The value is saved on PORTB, which is our first port.

data direction register in ports and flags in atmega32

Three different register types fit onto one port. The data direction register, which is utilized for input-output, is one of them. For input, a PIN register is used, and for output, a PORT register.

When we check PORTC now, all the pins have the same value

ports become on

Step over by pressing the F10 key once again. On PORTB, it will produce r16 values. You can see that because r16 has 11111111 values, all these values will be output on PORTB.

portb

You can see that r16 will be set to 0 after the complement instruction.

complement register value in port

So, you will see now PORTC which is the output port will have zero values.

port have zero values

Now we will check again by step over, PORTB will have zero values.

complement instructions

Again, after running the complement instruction, we check PORTC, all pins have 1 value, and all LEDs will switch on.

pinc ports and flags in atmega32

PROTEUS OUTPUT OF PORTS AND FLAGS IN ATMEGA32

The figure below shows how PORTB and PORTC are connected to eight LEDs. It would not be feasible to attach LEDs to them if we had set both ports to be input.

ports and flags in atmega32 circuit diagram

Go to your stored location and open the hex file there.

LEDs will be off on PORTB when they turn on PORTC, and vice versa.

ports and flags in atmega32 output

Code:

LDI R16,0XFF
OUT DDRB,R16
OUT DDRC,R16
 
TOGGLE:
 
  OUT PORTB,R16
  COM R16
  OUT PORTC,R16
 
  JMP TOGGLE

as an illustration I hope you have understood the working of the Ports and flags in Atmega32. Additionally, If you want to see the project  Automatic Car Parking System in Atmega32 then visit our website. Further, If you have any queries, you can comment below. Thanks

Leave a Comment

Your email address will not be published. Required fields are marked *