0% found this document useful (0 votes)
2 views

I-O Port Programming in AVR

The ATmega32 microcontroller features 40 pins, including three for Vcc and GND, and 23 I/O port pins across four ports (A, B, C, D) with specific pin configurations. The document explains how to configure pin directions and output levels using `DDRx` and `PORTx`, along with example code for setting up and manipulating these ports. Additionally, it provides details on the addresses for the Data Direction Registers for each port.

Uploaded by

Hiếu Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

I-O Port Programming in AVR

The ATmega32 microcontroller features 40 pins, including three for Vcc and GND, and 23 I/O port pins across four ports (A, B, C, D) with specific pin configurations. The document explains how to configure pin directions and output levels using `DDRx` and `PORTx`, along with example code for setting up and manipulating these ports. Additionally, it provides details on the addresses for the Data Direction Registers for each port.

Uploaded by

Hiếu Nguyễn
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

KEYS:

1. 32

2. In the ATmega32, there are three pins assigned to Vcc (power supply) and
three pins assigned to GND (ground).

3. In the ATmega32, there are 23 pins designated as I/O port pins.

4. In the ATmega32 40-pin DIP package, PORTA consists of 8 pins, numbered


PA0 to PA7. These correspond to the following pin numbers on the DIP
package:

- PA0: Pin 40

- PA1: Pin 39

- PA2: Pin 38

- PA3: Pin 37

- PA4: Pin 36
- PA5: Pin 35

- PA6: Pin 34

- PA7: Pin 33

These pins can be used for various I/O functions as configured in the
microcontroller.

5. In the ATmega32 40-pin DIP package, PORTB consists of 8 pins, numbered


PB0 to PB7. These correspond to the following pin numbers on the DIP
package:

- PB0: Pin 16

- PB1: Pin 15

- PB2: Pin 14

- PB3: Pin 13

- PB4: Pin 12

- PB5: Pin 11

- PB6: Pin 10

- PB7: Pin 9

These pins can be configured for various I/O functions.

6. In the ATmega32 40-pin DIP package, PORTC consists of 7 pins, numbered


PC0 to PC6. These correspond to the following pin numbers on the DIP
package:

- PC0: Pin 23

- PC1: Pin 22

- PC2: Pin 21

- PC3: Pin 20

- PC4: Pin 19

- PC5: Pin 18

- PC6: Pin 17
These pins can be used for various I/O functions as configured in the
microcontroller.

7. In the ATmega32 40-pin DIP package, PORTD consists of 8 pins, numbered


PD0 to PD7. These correspond to the following pin numbers on the DIP
package:

- PD0: Pin 2

- PD1: Pin 3

- PD2: Pin 4

- PD3: Pin 5

- PD4: Pin 6

- PD5: Pin 7

- PD6: Pin 8

- PD7: Pin 9

These pins can be configured for various I/O functions, including external
interrupts.

8. Input

9. In microcontrollers, particularly in AVR architecture, `DDRx` and `PORTx`


are crucial for managing I/O operations.

* DDRx (Data Direction Register)

- Function: It determines the direction of the pins associated with a specific


port (x).

- Configuration:

- A bit set to `1` makes the corresponding pin an **output**.

- A bit set to `0` makes the corresponding pin an **input**.

* PORTx (Data Register)

- Function: It controls the output level of the pins configured as outputs and
influences the behavior of pins configured as inputs (when using pull-up
resistors).

- Behavior:
- If a bit is set to `1`, the corresponding output pin is driven high (logic level
‘1’).

- If a bit is set to `0`, the corresponding output pin is driven low (logic level
‘0’).

- For input pins, setting the bit in `PORTx` enables the internal pull-up
resistor for that pin, keeping it at a high level unless driven low externally.

Summary

Configure Direction: Use `DDRx` to set pin directions (input or output).

Set Output Levels: Use `PORTx` to drive output pins high or low, or to
enable pull-ups for input pins.

11. #include <avr/io.h>

Int main(void) {

// Set PORTB and PORTC as output

DDRB = 0xFF; // Set all bits of PORTB as output

DDRC = 0xFF; // Set all bits of PORTC as output

DDRD = 0x00; // Set all bits of PORTD as input

While (1) {

// Read data from PORTD

Uint8_t data = PIND; // PIND is the input register for PORTD

// Send data to PORTB and PORTC

PORTB = data; // Write data to PORTB

PORTC = data; // Write data to PORTC

Return 0;

10. #include <avr/io.h>


Int main(void) {

// Set PORTB and PORTD as output

DDRB = 0xFF; // Set all bits of PORTB as output

DDRD = 0xFF; // Set all bits of PORTD as output

DDRC = 0x00; // Set all bits of PORTC as input

While (1) {

// Read data from PORTC

Uint8_t data = PINC; // PINC is the input register for PORTC

// Send data to PORTB and PORTD

PORTB = data; // Write data to PORTB

PORTD = data; // Write data to PORTD

Return 0;

12. RxD: Typically PD0 (Pin 0 of PORTD)

TxD: Typically PD1 (Pin 1 of PORTD)

13. DDRA (Data Direction Register for Port A):

Address: 0x3A

DDRB (Data Direction Register for Port B):

Address: 0x37

DDRC (Data Direction Register for Port C):

Address: 0x34

14.

a) #include <avr/io.h>
#include <util/delay.h>

Int main(void) {

// Set PORTB and PORTC as output

DDRB = 0xFF; // Set all bits of PORTB as output

DDRC = 0xFF; // Set all bits of PORTC as output

While (1) {

// Toggle PORTB and PORTC using 0xAA and 0x55

PORTB = 0xAA; // Set PORTB to 0b10101010

PORTC = 0xAA; // Set PORTC to 0b10101010

_delay_ms(500); // Delay for visibility

PORTB = 0x55; // Set PORTB to 0b01010101

PORTC = 0x55; // Set PORTC to 0b01010101

_delay_ms(500); // Delay for visibility

Return 0;

b) #include <avr/io.h>

#include <util/delay.h>

Int main(void) {

// Set PORTB and PORTC as output

DDRB = 0xFF; // Set all bits of PORTB as output

DDRC = 0xFF; // Set all bits of PORTC as output

While (1) {

// Toggle PORTB and PORTC using COM instruction


Asm volatile (

“com %0” // Complement PORTB

: “=r” (PORTB)

: “0” (PORTB)

);

Asm volatile (

“com %0” // Complement PORTC

: “=r” (PORTC)

: “0” (PORTC)

);

_delay_ms(500); // Delay for visibility

Return 0;

You might also like