Sunday, August 1, 2021

ATMega16 Digital Port Reading And Writing

 

Digital I/O

The ATMega16 AVR has up to four digital Input/Output ports. Those ports are bidirectional read/write port.

ATMega16 Digital Port Reading And Writing
ATMega16 40-pin DIP pin diagram

These four digital I/O ports are,

  • Port A – PA
  • Port B – PB
  • Port C – PC
  • Port D – PD

Each port is 8-bit wide. Beside the digital I/O function, these ports have other purposes,

  • external interrupt
  • counter inputs
  • PWM outputs
  • SPI/TWI communication
  • USART communication
  • Analog inputs, etc.

PORT Register And Direction Control

Output Direction

Each port is configurable as a digital input, or a digital output. The PORTx is an output register. To configure the any port as an output port, its corresponding data direction register, DDRx register must set.

As an instance, the programmer needs PA to output its digital data. The assignment in a C code must be set as follow,

DDRA=0xFF;   //PORTA is a fully output port
PORTA=0xAA;  //Output the hex value AA from PORTA

Let see another example, the lower nibble of PB is the output.

DDRB=0x0F;   //PORTB3.0 are the outputs
PORTB=0x02   //Set the output bit 1 of PORTB

Input Direction

A DDRx register must be cleared to configure the corresponding port as a digital input. PINx is its corresponding input buffer. Reading from the PINx register to get the logic state of the digital inputs.



For example, all bits of PA are the digital input.

DDRA=0x00;     //Configure PA as digital input
myState=PINA;  //Read the digital input logic state from PA

As in this instance, the lower nibble of PB is a digital input part.

DDRB=0xF0;         //PB3.0 are the digital input bits
myState=0x0F&PINB  //Read the digital input at the lower nibble

Digital Port Internal Weak Pull Up Resistors

For most of PIC microcontroller users, they usually think about the PORTB internal weak input pull up resistor. However, in the ATMega16 microcontroller every ports have their own internal weak pull up resistors. Each weak pull up resistor of one port is individually configurable.

This feature is commonly used in the input direction. To turn on the internal weak pull up resistors,

  • Set the data direction to the input
  • Write a preferred value to the PORTx register

For example, a lower nibble of PD is a digital input. The programmer turn on the internal weak pull up resistor of this lower nibble.

DDRD=0xF0;   //PIND3.0 are the digital inputs
PORTD=0x0F;  //Turn on the weak pull up resistors of the PINC3.0

Atmel Studio 7 C Programming

Overview

Within this introductory example, I select port A as a digital input port. PB is configured as a digital output port. The main program loop reads the digital data from PINA, while port b output this digital data. We don’t need to add any external resistors to the digital input pins since the programmer just need to write 0xFF to PORTA.



We need to know about port A register associations.

ATMega16 Digital Port Reading And Writing
Port A Data Register – PORTA

ATMega16 Digital Port Reading And Writing
Port A Data Direction Register – DDRA

ATMega16 Digital Port Reading And Writing
Port A Inputs Pin Address – PINA


Finally, the registers associate to port B list below.

ATMega16 Digital Port Reading And Writing
Port B Data Register – PORTB

 
ATMega16 Digital Port Reading And Writing
Port B Data Direction Register – DDRB

Schematic Diagram

I draw the schematic diagram of this example using a free EDA software. It follow the hardware configuration on my development board I designed.

ATMega16 Digital Port Reading And Writing
Schematic Diagram

The supply voltage is +5 V DC to clock the CPU to its maximum 16 MHz frequency.

Atmel Studio 7 C Code



I migrated my code writing from the AVR Studio to Atmel Studio for a few years now. I started with the earlier version Atmel Studio 6 in 2017.

  1. /*
  2.  * portAportB.c
  3.  *
  4.  * Created: 11/12/2020 7:46:41 PM
  5.  * Author : Admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9.  
  10. int main(void)
  11. {
  12. //Port A Input
  13. DDRA=0x00;
  14. //Turn Port A High
  15. PORTA=0xFF;
  16. //Port B Output
  17. DDRB=0xFF;
  18. while (1)
  19. {
  20. //Read PINA
  21. PORTB=PINA;
  22. //Make A Little Delay
  23. for(int i=0;i<25;i++);
  24. }
  25. }

 

Click here to download the zip file of this example.

ATMega16 Digital Port Reading And Writing
A program testing on the development board




No comments:

Post a Comment

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)