SlideShare una empresa de Scribd logo
1 de 13
Introduction to AVR
Features of AVR
(ATmega16)
• Harvard architecture
• 8 bit Microcontroller
• High performance - 16MIPS @ 16MHz
• Large program memory
• EEPROM – non volatile memory
• Two 8 bit, One 16 bit timer with total 4 PWM
channels
• On chip 10 bit ADC, 8 channels
• UART, I2C, SPI protocol support
Tools
• Compiler : WinAVR/AVR Studio/CodeVision AVR
• The programmer hardware
- Simple parallel/serial port based In System
Programmer (ISP) or
- USB based ISP (recommended) : USBasp
• Bread board, wires, power supply, crystal, etc.
Points to be noted
• PORT : group of 8 pins, or set of pins used for exchanging
data with external world
• Width of almost all registers : 8 bits (some 16 bits)
• In port related registers, every bit corresponds to one pin
of the port.
Bit 0 corresponds to Pin 0
Bit 0 corresponds to Pin 1 .. Etc
• Remember direct one to one correspondence between
HEX and BINARY numbers.
0xFF = 1111 1111
0xAA = 1010 1010
0x11 = 0001 0001
Input/Output Basics
• Three registers :
- DDRx : for configuring Data Direction (input/output)
of the port pins.
- PORTx: for writing the values to the port pins in
output mode. Configuring the port pins in
input mode.
- PINx: reading data from port pins in input mode
• Where x : A,B,C,D… depending on the available ports in your
AVR.
DDR – Data Direction Register
• Configures data direction of the port - Input / Output
• DDRx.n = 0 > makes corresponding port pin as input
DDRx.n = 1 > makes corresponding port pin as output
• Examples :
• to make all pins of port A as input pins :
DDRA = 0b00000000;
• to make all pins of port A as output pins
DDRA = 0b11111111;
• to make lower nibble of port B as output and higher nibble as
input
DDRB = 0b00001111;
PIN register
• Used to read data from port pins, when port is configured as
input.
• First set DDRx to zero, then use PINx to read the value.
• If PINx is read, when port is configured as output, it will give
you data that has been outputted on port.
• There two input modes :
- Tristated input
- Pullup input
This will be explained shortly
• Example :
DDRA = 0x00; //Set PA as input
x = PINA; //Read contents of PA
PORT register
• Used for two purposes …
1) for data output, when port is configured as output:
• Writing to PORTx.n will immediately (in same clock cycle) change
state of the port pins according to given value.
• Do not forget to load DDRx with appropriate value for configuring
port pins as output.
• Examples :
• to output 0xFF data on PB
DDRB = 0b11111111; //set all pins of
port b as outputs
PORTB = 0xFF; //write data on port
• to output data in variable x on PA
DDRA = 0xFF; //make port a as output
PORTA = x; //output 8 bit variable on
port
PORT register
2) for configuring pin as tristate/pullup, when port is
configured as input) :
• When port is configures as input (i.e DDRx.n=1), then PORTx.n
controls the internal pull-up resistor.
• PORTx.n = 1 : Enables pullup for nth bit
PORTx.n = 0 : Disables pullup for nth bit, thus making it tristate
• Examples :
• to make PA as input with pull-ups enabled and read data from PA
DDRA = 0x00; //make port a as input
PORTA = 0xFF; //enable all pull-ups
y = PINA; //read data from port a pins
• to make PB as tri stated input
DDRB = 0x00; //make port b as input
PORTB = 0x00; //disable pull-ups and make it
tri state
What is pull-up ?
• Pull-up resistor is used to ensure that tri-stated input always
reads HIGH (1) when it is not driven by any external entity.
• Pull-up is very important when you are using tri-stated input
buffers.
• Tri-state input pin offers very high impedance and thus can read
as logic 1/ logic 0 because of minute static charges on nearby
objects.
• Pin state changes rapidly and this change is unpredictable.
• This may cause your program to go haywire if it depends on input
from such tri-state pin.
Input/Output Basics
Summary
register bits →
pin function↓
DDRx.n PORTx.n PINx.n
tri stated input 0 0 read data bit(s)
x = PINx.n;
y = PINx;
pull-up input 0 1 read data bit(s)
x = PINx.n;
y = PINx;
output 1 write data bit(s)
PORTx.n = x;
PORTx = y;
n/a
• Following table lists register bit settings and resulting
function of port pins
I/O Basics – Exercise
1. Configure PB as output port
2. Configure PC as tri-stated input and read value into variable x.
3. Configure PA as pullup input and read lower nibble into
variable y and higher nibble into variable x.
4. Make higher nibble of PA as pullup inputs and lower nibble as
output.
5. Read, only input pins of above mentioned port and get the
binary number present on those pins into variable x.
6. Write four bit number 0x5 onto output pins of above mentioned
port
Answers to Exercise
1. DDRB = 0xFF (or) 0b11111111 (or) 255;
2. DDRC = 0xFF; PORTC = 0x00; x = PINC;
3. DDRA = 0x00; PORTA = 0xFF; y = PINA & 0b00001111;
x = (PINA & 0b11110000) / 24
4. DDRA = 0x0F; PORTA = 0xF0;
5. x = (PINA & 0b11110000) / 24
6. PORTA = PORTA | 0x05;

Más contenido relacionado

La actualidad más candente

Chapter 2 The 8088 Microprocessor
Chapter 2   The 8088 MicroprocessorChapter 2   The 8088 Microprocessor
Chapter 2 The 8088 MicroprocessorDwight Sabio
 
LPC 2148 ARM MICROCONTROLLER
LPC 2148 ARM MICROCONTROLLERLPC 2148 ARM MICROCONTROLLER
LPC 2148 ARM MICROCONTROLLERsravannunna24
 
Introduction to ARM LPC2148
Introduction to ARM LPC2148Introduction to ARM LPC2148
Introduction to ARM LPC2148Veera Kumar
 
Pwm(Pulse Width Modulation) presentation
Pwm(Pulse Width Modulation) presentationPwm(Pulse Width Modulation) presentation
Pwm(Pulse Width Modulation) presentationHabitamuAsimare
 
Memory organization of 8051
Memory organization of 8051Memory organization of 8051
Memory organization of 8051Muthu Manickam
 
Digital to Analog Converter by LDCE students
Digital to Analog Converter by LDCE studentsDigital to Analog Converter by LDCE students
Digital to Analog Converter by LDCE studentsMitul Lakhani
 
Microchip's PIC Micro Controller
Microchip's PIC Micro ControllerMicrochip's PIC Micro Controller
Microchip's PIC Micro ControllerMidhu S V Unnithan
 
AVR Microcontroller
AVR MicrocontrollerAVR Microcontroller
AVR MicrocontrollerÖzcan Acar
 
Study on 32-bit Cortex - M3 Powered MCU: STM32F101
Study on 32-bit Cortex - M3 Powered MCU: STM32F101Study on 32-bit Cortex - M3 Powered MCU: STM32F101
Study on 32-bit Cortex - M3 Powered MCU: STM32F101Premier Farnell
 
Verilog presentation final
Verilog presentation finalVerilog presentation final
Verilog presentation finalAnkur Gupta
 
Arm assembly language programming
Arm assembly language programmingArm assembly language programming
Arm assembly language programmingv Kalairajan
 

La actualidad más candente (20)

Chapter 2 The 8088 Microprocessor
Chapter 2   The 8088 MicroprocessorChapter 2   The 8088 Microprocessor
Chapter 2 The 8088 Microprocessor
 
UART
UART UART
UART
 
LPC 2148 ARM MICROCONTROLLER
LPC 2148 ARM MICROCONTROLLERLPC 2148 ARM MICROCONTROLLER
LPC 2148 ARM MICROCONTROLLER
 
Embedded _c_
Embedded  _c_Embedded  _c_
Embedded _c_
 
Introduction to ARM LPC2148
Introduction to ARM LPC2148Introduction to ARM LPC2148
Introduction to ARM LPC2148
 
ARM Processor
ARM ProcessorARM Processor
ARM Processor
 
Pwm(Pulse Width Modulation) presentation
Pwm(Pulse Width Modulation) presentationPwm(Pulse Width Modulation) presentation
Pwm(Pulse Width Modulation) presentation
 
Memory organization of 8051
Memory organization of 8051Memory organization of 8051
Memory organization of 8051
 
Digital to Analog Converter by LDCE students
Digital to Analog Converter by LDCE studentsDigital to Analog Converter by LDCE students
Digital to Analog Converter by LDCE students
 
PIC Microcontrollers
PIC MicrocontrollersPIC Microcontrollers
PIC Microcontrollers
 
Microchip's PIC Micro Controller
Microchip's PIC Micro ControllerMicrochip's PIC Micro Controller
Microchip's PIC Micro Controller
 
AVR Microcontroller
AVR MicrocontrollerAVR Microcontroller
AVR Microcontroller
 
Study on 32-bit Cortex - M3 Powered MCU: STM32F101
Study on 32-bit Cortex - M3 Powered MCU: STM32F101Study on 32-bit Cortex - M3 Powered MCU: STM32F101
Study on 32-bit Cortex - M3 Powered MCU: STM32F101
 
Verilog presentation final
Verilog presentation finalVerilog presentation final
Verilog presentation final
 
Embedded C - Lecture 1
Embedded C - Lecture 1Embedded C - Lecture 1
Embedded C - Lecture 1
 
STM32 MCU Family
STM32 MCU FamilySTM32 MCU Family
STM32 MCU Family
 
Arm assembly language programming
Arm assembly language programmingArm assembly language programming
Arm assembly language programming
 
Lecture 2 timers, pwm, state machine IN PIC
Lecture 2   timers, pwm, state machine IN PIC Lecture 2   timers, pwm, state machine IN PIC
Lecture 2 timers, pwm, state machine IN PIC
 
ARM Processors
ARM ProcessorsARM Processors
ARM Processors
 
Arm instruction set
Arm instruction setArm instruction set
Arm instruction set
 

Similar a AVR programming - BASICS

Microcontroller avr
Microcontroller avrMicrocontroller avr
Microcontroller avrMahmoud Amr
 
Hardware interfacing basics using AVR
Hardware interfacing basics using AVRHardware interfacing basics using AVR
Hardware interfacing basics using AVRMohamed Abdallah
 
Programming A Robot Using
Programming A Robot UsingProgramming A Robot Using
Programming A Robot UsingSpitiq
 
Motorola parallel port
Motorola parallel portMotorola parallel port
Motorola parallel portBkannan2
 
MICROCONTROLLER.pptx
MICROCONTROLLER.pptxMICROCONTROLLER.pptx
MICROCONTROLLER.pptxfiqrie mohd
 
Microcontroller
MicrocontrollerMicrocontroller
MicrocontrollerSpitiq
 
An Overview Study on I/O Expander with I2C and SMBus Interface
An Overview Study on I/O Expander with I2C and SMBus InterfaceAn Overview Study on I/O Expander with I2C and SMBus Interface
An Overview Study on I/O Expander with I2C and SMBus InterfacePremier Farnell
 
Universal synchronous asynchronous receiver transmitter(usart) and AtoD Coverter
Universal synchronous asynchronous receiver transmitter(usart) and AtoD CoverterUniversal synchronous asynchronous receiver transmitter(usart) and AtoD Coverter
Universal synchronous asynchronous receiver transmitter(usart) and AtoD CoverterTejas Shetye
 
AN INTRODUCTION TO SERIAL PORT INTERFACING
AN INTRODUCTION TO SERIAL PORT INTERFACINGAN INTRODUCTION TO SERIAL PORT INTERFACING
AN INTRODUCTION TO SERIAL PORT INTERFACINGTotal Project Solutions
 
Port Organization of 8051 .pdf
Port Organization of 8051 .pdfPort Organization of 8051 .pdf
Port Organization of 8051 .pdfSrikrishna Thota
 
Unit2.2 8051
Unit2.2 8051Unit2.2 8051
Unit2.2 8051sjajsj
 

Similar a AVR programming - BASICS (20)

Avr report
Avr reportAvr report
Avr report
 
Microcontroller avr
Microcontroller avrMicrocontroller avr
Microcontroller avr
 
Hardware interfacing basics using AVR
Hardware interfacing basics using AVRHardware interfacing basics using AVR
Hardware interfacing basics using AVR
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontroller
 
Lecture6
Lecture6Lecture6
Lecture6
 
Lecture6.pptx
Lecture6.pptxLecture6.pptx
Lecture6.pptx
 
Programming A Robot Using
Programming A Robot UsingProgramming A Robot Using
Programming A Robot Using
 
Motorola parallel port
Motorola parallel portMotorola parallel port
Motorola parallel port
 
Presentation
PresentationPresentation
Presentation
 
MICROCONTROLLER.pptx
MICROCONTROLLER.pptxMICROCONTROLLER.pptx
MICROCONTROLLER.pptx
 
Microcontroller
MicrocontrollerMicrocontroller
Microcontroller
 
UNIT 3 mp (1).ppt
UNIT 3 mp (1).pptUNIT 3 mp (1).ppt
UNIT 3 mp (1).ppt
 
8051 full ppt
8051 full ppt8051 full ppt
8051 full ppt
 
An Overview Study on I/O Expander with I2C and SMBus Interface
An Overview Study on I/O Expander with I2C and SMBus InterfaceAn Overview Study on I/O Expander with I2C and SMBus Interface
An Overview Study on I/O Expander with I2C and SMBus Interface
 
Universal synchronous asynchronous receiver transmitter(usart) and AtoD Coverter
Universal synchronous asynchronous receiver transmitter(usart) and AtoD CoverterUniversal synchronous asynchronous receiver transmitter(usart) and AtoD Coverter
Universal synchronous asynchronous receiver transmitter(usart) and AtoD Coverter
 
AN INTRODUCTION TO SERIAL PORT INTERFACING
AN INTRODUCTION TO SERIAL PORT INTERFACINGAN INTRODUCTION TO SERIAL PORT INTERFACING
AN INTRODUCTION TO SERIAL PORT INTERFACING
 
Port Organization of 8051 .pdf
Port Organization of 8051 .pdfPort Organization of 8051 .pdf
Port Organization of 8051 .pdf
 
Unit2.2 8051
Unit2.2 8051Unit2.2 8051
Unit2.2 8051
 
amba.ppt
amba.pptamba.ppt
amba.ppt
 
amba.ppt
amba.pptamba.ppt
amba.ppt
 

Último

BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 

Último (20)

BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 

AVR programming - BASICS

  • 2. Features of AVR (ATmega16) • Harvard architecture • 8 bit Microcontroller • High performance - 16MIPS @ 16MHz • Large program memory • EEPROM – non volatile memory • Two 8 bit, One 16 bit timer with total 4 PWM channels • On chip 10 bit ADC, 8 channels • UART, I2C, SPI protocol support
  • 3. Tools • Compiler : WinAVR/AVR Studio/CodeVision AVR • The programmer hardware - Simple parallel/serial port based In System Programmer (ISP) or - USB based ISP (recommended) : USBasp • Bread board, wires, power supply, crystal, etc.
  • 4. Points to be noted • PORT : group of 8 pins, or set of pins used for exchanging data with external world • Width of almost all registers : 8 bits (some 16 bits) • In port related registers, every bit corresponds to one pin of the port. Bit 0 corresponds to Pin 0 Bit 0 corresponds to Pin 1 .. Etc • Remember direct one to one correspondence between HEX and BINARY numbers. 0xFF = 1111 1111 0xAA = 1010 1010 0x11 = 0001 0001
  • 5. Input/Output Basics • Three registers : - DDRx : for configuring Data Direction (input/output) of the port pins. - PORTx: for writing the values to the port pins in output mode. Configuring the port pins in input mode. - PINx: reading data from port pins in input mode • Where x : A,B,C,D… depending on the available ports in your AVR.
  • 6. DDR – Data Direction Register • Configures data direction of the port - Input / Output • DDRx.n = 0 > makes corresponding port pin as input DDRx.n = 1 > makes corresponding port pin as output • Examples : • to make all pins of port A as input pins : DDRA = 0b00000000; • to make all pins of port A as output pins DDRA = 0b11111111; • to make lower nibble of port B as output and higher nibble as input DDRB = 0b00001111;
  • 7. PIN register • Used to read data from port pins, when port is configured as input. • First set DDRx to zero, then use PINx to read the value. • If PINx is read, when port is configured as output, it will give you data that has been outputted on port. • There two input modes : - Tristated input - Pullup input This will be explained shortly • Example : DDRA = 0x00; //Set PA as input x = PINA; //Read contents of PA
  • 8. PORT register • Used for two purposes … 1) for data output, when port is configured as output: • Writing to PORTx.n will immediately (in same clock cycle) change state of the port pins according to given value. • Do not forget to load DDRx with appropriate value for configuring port pins as output. • Examples : • to output 0xFF data on PB DDRB = 0b11111111; //set all pins of port b as outputs PORTB = 0xFF; //write data on port • to output data in variable x on PA DDRA = 0xFF; //make port a as output PORTA = x; //output 8 bit variable on port
  • 9. PORT register 2) for configuring pin as tristate/pullup, when port is configured as input) : • When port is configures as input (i.e DDRx.n=1), then PORTx.n controls the internal pull-up resistor. • PORTx.n = 1 : Enables pullup for nth bit PORTx.n = 0 : Disables pullup for nth bit, thus making it tristate • Examples : • to make PA as input with pull-ups enabled and read data from PA DDRA = 0x00; //make port a as input PORTA = 0xFF; //enable all pull-ups y = PINA; //read data from port a pins • to make PB as tri stated input DDRB = 0x00; //make port b as input PORTB = 0x00; //disable pull-ups and make it tri state
  • 10. What is pull-up ? • Pull-up resistor is used to ensure that tri-stated input always reads HIGH (1) when it is not driven by any external entity. • Pull-up is very important when you are using tri-stated input buffers. • Tri-state input pin offers very high impedance and thus can read as logic 1/ logic 0 because of minute static charges on nearby objects. • Pin state changes rapidly and this change is unpredictable. • This may cause your program to go haywire if it depends on input from such tri-state pin.
  • 11. Input/Output Basics Summary register bits → pin function↓ DDRx.n PORTx.n PINx.n tri stated input 0 0 read data bit(s) x = PINx.n; y = PINx; pull-up input 0 1 read data bit(s) x = PINx.n; y = PINx; output 1 write data bit(s) PORTx.n = x; PORTx = y; n/a • Following table lists register bit settings and resulting function of port pins
  • 12. I/O Basics – Exercise 1. Configure PB as output port 2. Configure PC as tri-stated input and read value into variable x. 3. Configure PA as pullup input and read lower nibble into variable y and higher nibble into variable x. 4. Make higher nibble of PA as pullup inputs and lower nibble as output. 5. Read, only input pins of above mentioned port and get the binary number present on those pins into variable x. 6. Write four bit number 0x5 onto output pins of above mentioned port
  • 13. Answers to Exercise 1. DDRB = 0xFF (or) 0b11111111 (or) 255; 2. DDRC = 0xFF; PORTC = 0x00; x = PINC; 3. DDRA = 0x00; PORTA = 0xFF; y = PINA & 0b00001111; x = (PINA & 0b11110000) / 24 4. DDRA = 0x0F; PORTA = 0xF0; 5. x = (PINA & 0b11110000) / 24 6. PORTA = PORTA | 0x05;