SlideShare ist ein Scribd-Unternehmen logo
1 von 13
Downloaden Sie, um offline zu lesen
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;

Weitere ähnliche Inhalte

Was ist angesagt?

8051 Microcontroller
8051 Microcontroller8051 Microcontroller
8051 Microcontrollerthokalpv
 
SRAM read and write and sense amplifier
SRAM read and write and sense amplifierSRAM read and write and sense amplifier
SRAM read and write and sense amplifierSoumyajit Langal
 
Arduino Workshop Day 2 - Advance Arduino & DIY
Arduino Workshop Day 2 - Advance Arduino & DIYArduino Workshop Day 2 - Advance Arduino & DIY
Arduino Workshop Day 2 - Advance Arduino & DIYVishnu
 
8086 microprocessor
8086 microprocessor8086 microprocessor
8086 microprocessorsavitamhaske
 
Arduino Microcontroller
Arduino MicrocontrollerArduino Microcontroller
Arduino MicrocontrollerShyam Mohan
 
8096 microcontrollers notes
8096 microcontrollers notes8096 microcontrollers notes
8096 microcontrollers notesDr.YNM
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programmingClaus Wu
 
301378156 design-of-sram-in-verilog
301378156 design-of-sram-in-verilog301378156 design-of-sram-in-verilog
301378156 design-of-sram-in-verilogSrinivas Naidu
 
MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration AKHIL MADANKAR
 
Microprocessors evolution introduction to microprocessor
Microprocessors  evolution introduction to microprocessorMicroprocessors  evolution introduction to microprocessor
Microprocessors evolution introduction to microprocessorVijay Kumar
 
microcontroller basics
microcontroller basicsmicrocontroller basics
microcontroller basicssagar Ramdev
 
Evolution Of Microprocessors
Evolution Of MicroprocessorsEvolution Of Microprocessors
Evolution Of Microprocessorsharinder
 
Logic System Design KTU Chapter-4.ppt
Logic System Design KTU Chapter-4.pptLogic System Design KTU Chapter-4.ppt
Logic System Design KTU Chapter-4.pptAlbin562191
 
Serial Io
Serial IoSerial Io
Serial IoAisu
 

Was ist angesagt? (20)

8051 Microcontroller
8051 Microcontroller8051 Microcontroller
8051 Microcontroller
 
SRAM read and write and sense amplifier
SRAM read and write and sense amplifierSRAM read and write and sense amplifier
SRAM read and write and sense amplifier
 
Mips
MipsMips
Mips
 
Arduino- Serial communication
Arduino-  Serial communicationArduino-  Serial communication
Arduino- Serial communication
 
Arduino Workshop Day 2 - Advance Arduino & DIY
Arduino Workshop Day 2 - Advance Arduino & DIYArduino Workshop Day 2 - Advance Arduino & DIY
Arduino Workshop Day 2 - Advance Arduino & DIY
 
8086 microprocessor
8086 microprocessor8086 microprocessor
8086 microprocessor
 
Arduino Microcontroller
Arduino MicrocontrollerArduino Microcontroller
Arduino Microcontroller
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
 
8096 microcontrollers notes
8096 microcontrollers notes8096 microcontrollers notes
8096 microcontrollers notes
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programming
 
Evolution of Microprocessor
Evolution of MicroprocessorEvolution of Microprocessor
Evolution of Microprocessor
 
301378156 design-of-sram-in-verilog
301378156 design-of-sram-in-verilog301378156 design-of-sram-in-verilog
301378156 design-of-sram-in-verilog
 
MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration MICROCONTROLLER 8051- Architecture & Pin Configuration
MICROCONTROLLER 8051- Architecture & Pin Configuration
 
Microprocessors evolution introduction to microprocessor
Microprocessors  evolution introduction to microprocessorMicroprocessors  evolution introduction to microprocessor
Microprocessors evolution introduction to microprocessor
 
microcontroller basics
microcontroller basicsmicrocontroller basics
microcontroller basics
 
Evolution Of Microprocessors
Evolution Of MicroprocessorsEvolution Of Microprocessors
Evolution Of Microprocessors
 
Embedded C - Day 1
Embedded C - Day 1Embedded C - Day 1
Embedded C - Day 1
 
Microprocessor
MicroprocessorMicroprocessor
Microprocessor
 
Logic System Design KTU Chapter-4.ppt
Logic System Design KTU Chapter-4.pptLogic System Design KTU Chapter-4.ppt
Logic System Design KTU Chapter-4.ppt
 
Serial Io
Serial IoSerial Io
Serial Io
 

Ähnlich wie 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-8051-1234612612414791-3-151012140110-lva1-app6891 (1).pdf
microcontroller-8051-1234612612414791-3-151012140110-lva1-app6891 (1).pdfmicrocontroller-8051-1234612612414791-3-151012140110-lva1-app6891 (1).pdf
microcontroller-8051-1234612612414791-3-151012140110-lva1-app6891 (1).pdfSanjjaayyy
 
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
 

Ähnlich wie 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-8051-1234612612414791-3-151012140110-lva1-app6891 (1).pdf
microcontroller-8051-1234612612414791-3-151012140110-lva1-app6891 (1).pdfmicrocontroller-8051-1234612612414791-3-151012140110-lva1-app6891 (1).pdf
microcontroller-8051-1234612612414791-3-151012140110-lva1-app6891 (1).pdf
 
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
 

Kürzlich hochgeladen

Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.EnglishCEIPdeSigeiro
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17Celine George
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17Celine George
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?TechSoup
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.raviapr7
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17Celine George
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17Celine George
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesMohammad Hassany
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice documentXsasf Sfdfasd
 
Quality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICEQuality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICESayali Powar
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxiammrhaywood
 
Practical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxPractical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxKatherine Villaluna
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfMohonDas
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and stepobaje godwin sunday
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxEduSkills OECD
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxSaurabhParmar42
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxraviapr7
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxMYDA ANGELICA SUAN
 

Kürzlich hochgeladen (20)

Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.Easter in the USA presentation by Chloe.
Easter in the USA presentation by Chloe.
 
How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17How to Add a New Field in Existing Kanban View in Odoo 17
How to Add a New Field in Existing Kanban View in Odoo 17
 
How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17How to Make a Field read-only in Odoo 17
How to Make a Field read-only in Odoo 17
 
What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?What is the Future of QuickBooks DeskTop?
What is the Future of QuickBooks DeskTop?
 
Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.Drug Information Services- DIC and Sources.
Drug Information Services- DIC and Sources.
 
How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17How to Add Existing Field in One2Many Tree View in Odoo 17
How to Add Existing Field in One2Many Tree View in Odoo 17
 
How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17How to Add a many2many Relational Field in Odoo 17
How to Add a many2many Relational Field in Odoo 17
 
Human-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming ClassesHuman-AI Co-Creation of Worked Examples for Programming Classes
Human-AI Co-Creation of Worked Examples for Programming Classes
 
The Singapore Teaching Practice document
The Singapore Teaching Practice documentThe Singapore Teaching Practice document
The Singapore Teaching Practice document
 
Quality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICEQuality Assurance_GOOD LABORATORY PRACTICE
Quality Assurance_GOOD LABORATORY PRACTICE
 
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptxAUDIENCE THEORY -- FANDOM -- JENKINS.pptx
AUDIENCE THEORY -- FANDOM -- JENKINS.pptx
 
Practical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptxPractical Research 1 Lesson 9 Scope and delimitation.pptx
Practical Research 1 Lesson 9 Scope and delimitation.pptx
 
Diploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdfDiploma in Nursing Admission Test Question Solution 2023.pdf
Diploma in Nursing Admission Test Question Solution 2023.pdf
 
General views of Histopathology and step
General views of Histopathology and stepGeneral views of Histopathology and step
General views of Histopathology and step
 
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptxPISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
PISA-VET launch_El Iza Mohamedou_19 March 2024.pptx
 
CAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptxCAULIFLOWER BREEDING 1 Parmar pptx
CAULIFLOWER BREEDING 1 Parmar pptx
 
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdfPersonal Resilience in Project Management 2 - TV Edit 1a.pdf
Personal Resilience in Project Management 2 - TV Edit 1a.pdf
 
Prescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptxPrescribed medication order and communication skills.pptx
Prescribed medication order and communication skills.pptx
 
Prelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quizPrelims of Kant get Marx 2.0: a general politics quiz
Prelims of Kant get Marx 2.0: a general politics quiz
 
Patterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptxPatterns of Written Texts Across Disciplines.pptx
Patterns of Written Texts Across Disciplines.pptx
 

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;