SlideShare ist ein Scribd-Unternehmen logo
1 von 3
Programming AVR Microcontroller Digital I/O in C
Introduction to AVR Digital I/O CProgramming
ThisAVRtutorial looksat AVRprogrammingfordigital I/OinC.Before we start lookingatactual
programmingAVRmicrocontrollersDigital Input/Output(I/O)recall thateachAVRDigital I/Oportis
associatedwithI/Oregisters.Namelythe DDRx,PINx andPORTx registers,wherex representthe port:
A,B, C .....
The DDRx registerisuse to configure the pinsonportx as inputor outputpins.Each pinona port is
independentandthusthe entire portdoesnothave tobe configuredtotallyasaninputor outputport.
Writinga 1 inthe pin positioninthe DDRx will configurethatpinasan outputpin andwritinga 0 will
configure the pinasan inputpin.
Logic valueswrittentothe PORTx registerisoutputtedtothe pinsonPortx thatare configuredas
outputpins.
Logic valuesreadfromthe PINx registerisequivalenttothe valuespresentlyonthe pinsof Portx which
are configuredasinputpins.
Programmingthe Digital I/Opinsof an AVRinC
The AVR C code belowshowshowtoconfigure the pinsona port
DDRA = 0xFF; //Configure PortA asanOutputport
DDRB = 0x00; //Configure PortBasan Inputport
DDRC = 0xF0; //Configure firstfourpinsonPortCas
//Inputpinsandthe othersas output
The AVR C code belowshowshowtowrite toor readfromthe pinsof a port once theyare configured.
Assume here the configurationsfromthe Ccode above.
PORTA = 0xFF; //Write all 1's to the pinsof PortA
PORTA = PINB; //Readvaluesfrompinsof PortBand
//write topinsof PortA
Example
Write a program to be downloadedtothe Atmel AVRATMega32microcontrollerwhichcontinuously
readthe logicvaluesonportBand write themtoportC.
Solution
The program below,writteninAVRStudio5andAVRStudio6, accomplishthe taskthat was asked
above.There are several thingstonote here.
The headerfile avr/io.hmustbe includedinorderforusto use the name of the ports.
The ports mustbe configuredbeforetheyare used.The firsttwo(2) linesinthe main() functionof the
program are for configuration.
Once the ports are configuredyoucanthenwrite toor readfromthem, were applicable.
The use of the while(1) loopallowsforthe continuousreadandwrite operation.
/*
* WritteninAVRStudio5 / AVRStudio6
* Compiler:AVRGNUC Compiler(GCC)
*
* Author:AVRTutorials
* Website:www.AVR-Tutorials.com
*/
#include<avr/io.h>
int main()
{
DDRB = 0x00; //configure portBasinput
DDRC = 0xFF; //configure portCasoutput
while(1)
{
PORTC = PINB;
}
return0;
}
Interfacing a LED with a Microcontroller
AVRmicrocontrollerssuchasthe ATMega8515 onlysupplya currentof about20mA and so we can drive
an LED directlyfromthe microcontrollerporteliminatingthe resistor.Infactif a resistorisaddedthe
intensityof the LEDwill be low.
The figure belowshows8-LEDsconnectedtoan ATMega8515 microcontroller.The code thatfollowsif
downloadedtothe microcontrollerwill blinkthe LEDscontinuously.Note thatthiscode couldworkwith
otherAVRmicrocontrollerssuch asthe ATMega16, ATmega32, ATTiny2313, etc.
LED ATMega8515 MicrocontrollerSchematic
Note:Inorder forthe circuitto operate asdescribe the internal oscillatorof the ATMega8515
microcontrollermustbe programtorun at 4MHz.
/*
* WritteninAVRStudio5
* Compiler:AVRGNUC Compiler(GCC)
* Author:AVRTutorials
* Website:www.AVR-Tutorials.com
*/
#include <avr/io.h>
#define F_CPU4000000UL
#include <util/delay.h>
int main()
{
DDRC = 0xFF; // Configure portCas output
while(1)
{
PORTC = 0xFF; // Turn ON LEDs
_delay_ms(250);//Wait 250ms
PORTC = 0x00; // Turn OFFLEDs
_delay_ms(250); // Wail 250ms
}
return0;
}

Weitere ähnliche Inhalte

Was ist angesagt?

Programming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerProgramming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontroller
Corrado Santoro
 
Io (2)
Io (2)Io (2)
Io (2)
Aisu
 
1.arm tutorial(gpio)
1.arm tutorial(gpio)1.arm tutorial(gpio)
1.arm tutorial(gpio)
Ram Mohan
 
Solution manual 8051 microcontroller by mazidi
Solution manual 8051 microcontroller by mazidiSolution manual 8051 microcontroller by mazidi
Solution manual 8051 microcontroller by mazidi
Muhammad Abdullah
 

Was ist angesagt? (20)

Picmico
PicmicoPicmico
Picmico
 
Intro2 Robotic With Pic18
Intro2 Robotic With Pic18Intro2 Robotic With Pic18
Intro2 Robotic With Pic18
 
Atmega16
Atmega16Atmega16
Atmega16
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontroller
 
8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C
 
Programming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontrollerProgramming the Digital I/O Interface of a PIC microcontroller
Programming the Digital I/O Interface of a PIC microcontroller
 
PIC and LCD
PIC and LCDPIC and LCD
PIC and LCD
 
AT mega8 basics
AT mega8 basicsAT mega8 basics
AT mega8 basics
 
Lec12
Lec12Lec12
Lec12
 
Chapter 7 8051 programming in c
Chapter 7  8051 programming in cChapter 7  8051 programming in c
Chapter 7 8051 programming in c
 
Class7
Class7Class7
Class7
 
Chapter5 dek3133
Chapter5 dek3133Chapter5 dek3133
Chapter5 dek3133
 
Io (2)
Io (2)Io (2)
Io (2)
 
Modules and ports in Verilog HDL
Modules and ports in Verilog HDLModules and ports in Verilog HDL
Modules and ports in Verilog HDL
 
Micro
MicroMicro
Micro
 
Controlling Motors for Robot
Controlling Motors for RobotControlling Motors for Robot
Controlling Motors for Robot
 
A tmega8 basics
A tmega8 basicsA tmega8 basics
A tmega8 basics
 
1.arm tutorial(gpio)
1.arm tutorial(gpio)1.arm tutorial(gpio)
1.arm tutorial(gpio)
 
Solution manual 8051 microcontroller by mazidi
Solution manual 8051 microcontroller by mazidiSolution manual 8051 microcontroller by mazidi
Solution manual 8051 microcontroller by mazidi
 
Soc
SocSoc
Soc
 

Andere mochten auch (6)

ADC - Analog to Digital Conversion on AVR microcontroller Atmega16
ADC - Analog to Digital  Conversion on AVR microcontroller Atmega16ADC - Analog to Digital  Conversion on AVR microcontroller Atmega16
ADC - Analog to Digital Conversion on AVR microcontroller Atmega16
 
7 Analog Digital Converter
7 Analog Digital Converter7 Analog Digital Converter
7 Analog Digital Converter
 
AVR Microcontroller
AVR MicrocontrollerAVR Microcontroller
AVR Microcontroller
 
Analog to digital converter
Analog to digital converterAnalog to digital converter
Analog to digital converter
 
Advance Microcontroller AVR
Advance Microcontroller AVRAdvance Microcontroller AVR
Advance Microcontroller AVR
 
Introduction to AVR Microcontroller
Introduction to AVR Microcontroller Introduction to AVR Microcontroller
Introduction to AVR Microcontroller
 

Ähnlich wie Programming avr microcontroller digital i

Microcontroller
MicrocontrollerMicrocontroller
Microcontroller
Spitiq
 
Embedded c programming22 for fdp
Embedded c programming22 for fdpEmbedded c programming22 for fdp
Embedded c programming22 for fdp
Pradeep Kumar TS
 
Chp4 introduction to the pic microcontroller copy
Chp4 introduction to the pic microcontroller   copyChp4 introduction to the pic microcontroller   copy
Chp4 introduction to the pic microcontroller copy
mkazree
 

Ähnlich wie Programming avr microcontroller digital i (20)

Hardware interfacing basics using AVR
Hardware interfacing basics using AVRHardware interfacing basics using AVR
Hardware interfacing basics using AVR
 
Basics Of Embedded Systems
Basics Of Embedded SystemsBasics Of Embedded Systems
Basics Of Embedded Systems
 
Microcontroller
MicrocontrollerMicrocontroller
Microcontroller
 
embedded system and AVR
embedded system and AVRembedded system and AVR
embedded system and AVR
 
Embedded System Practical manual (1)
Embedded System Practical manual (1)Embedded System Practical manual (1)
Embedded System Practical manual (1)
 
Arduino Programming Basic
Arduino Programming BasicArduino Programming Basic
Arduino Programming Basic
 
ECET 330 Massive Success--snaptutorial.com
ECET 330 Massive Success--snaptutorial.comECET 330 Massive Success--snaptutorial.com
ECET 330 Massive Success--snaptutorial.com
 
Ecet 330 Success Begins / snaptutorial.com
Ecet 330 Success Begins / snaptutorial.comEcet 330 Success Begins / snaptutorial.com
Ecet 330 Success Begins / snaptutorial.com
 
Ecet 330 Enthusiastic Study / snaptutorial.com
Ecet 330 Enthusiastic Study / snaptutorial.comEcet 330 Enthusiastic Study / snaptutorial.com
Ecet 330 Enthusiastic Study / snaptutorial.com
 
ECET 330 Technology levels--snaptutorial.com
ECET 330 Technology levels--snaptutorial.comECET 330 Technology levels--snaptutorial.com
ECET 330 Technology levels--snaptutorial.com
 
Embedded c programming22 for fdp
Embedded c programming22 for fdpEmbedded c programming22 for fdp
Embedded c programming22 for fdp
 
Embedded C programming session10
Embedded C programming  session10Embedded C programming  session10
Embedded C programming session10
 
Presentation
PresentationPresentation
Presentation
 
C programming session10
C programming  session10C programming  session10
C programming session10
 
microcontroller basics
microcontroller basicsmicrocontroller basics
microcontroller basics
 
Chp4 introduction to the pic microcontroller copy
Chp4 introduction to the pic microcontroller   copyChp4 introduction to the pic microcontroller   copy
Chp4 introduction to the pic microcontroller copy
 
Basics of arduino uno
Basics of arduino unoBasics of arduino uno
Basics of arduino uno
 
Avr book
Avr bookAvr book
Avr book
 
Microcontroladores: programación con microcontrolador AVR
Microcontroladores: programación con microcontrolador AVRMicrocontroladores: programación con microcontrolador AVR
Microcontroladores: programación con microcontrolador AVR
 
AVR Micro controller Interfacing
AVR Micro controller Interfacing AVR Micro controller Interfacing
AVR Micro controller Interfacing
 

Mehr von Manas Mantri (7)

Raman Spectroscopy
Raman SpectroscopyRaman Spectroscopy
Raman Spectroscopy
 
2nd sem mechanical syllabus
2nd sem mechanical syllabus2nd sem mechanical syllabus
2nd sem mechanical syllabus
 
semiconductor electronics
semiconductor electronicssemiconductor electronics
semiconductor electronics
 
Digital image processing questions
Digital  image processing questionsDigital  image processing questions
Digital image processing questions
 
Dip 5 mathematical preliminaries
Dip 5 mathematical preliminariesDip 5 mathematical preliminaries
Dip 5 mathematical preliminaries
 
Dip 1 introduction
Dip 1 introductionDip 1 introduction
Dip 1 introduction
 
Connected component labeling algorithm
Connected component labeling algorithmConnected component labeling algorithm
Connected component labeling algorithm
 

Kürzlich hochgeladen

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Kürzlich hochgeladen (20)

microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 

Programming avr microcontroller digital i

  • 1. Programming AVR Microcontroller Digital I/O in C Introduction to AVR Digital I/O CProgramming ThisAVRtutorial looksat AVRprogrammingfordigital I/OinC.Before we start lookingatactual programmingAVRmicrocontrollersDigital Input/Output(I/O)recall thateachAVRDigital I/Oportis associatedwithI/Oregisters.Namelythe DDRx,PINx andPORTx registers,wherex representthe port: A,B, C ..... The DDRx registerisuse to configure the pinsonportx as inputor outputpins.Each pinona port is independentandthusthe entire portdoesnothave tobe configuredtotallyasaninputor outputport. Writinga 1 inthe pin positioninthe DDRx will configurethatpinasan outputpin andwritinga 0 will configure the pinasan inputpin. Logic valueswrittentothe PORTx registerisoutputtedtothe pinsonPortx thatare configuredas outputpins. Logic valuesreadfromthe PINx registerisequivalenttothe valuespresentlyonthe pinsof Portx which are configuredasinputpins. Programmingthe Digital I/Opinsof an AVRinC The AVR C code belowshowshowtoconfigure the pinsona port DDRA = 0xFF; //Configure PortA asanOutputport DDRB = 0x00; //Configure PortBasan Inputport DDRC = 0xF0; //Configure firstfourpinsonPortCas //Inputpinsandthe othersas output The AVR C code belowshowshowtowrite toor readfromthe pinsof a port once theyare configured. Assume here the configurationsfromthe Ccode above. PORTA = 0xFF; //Write all 1's to the pinsof PortA PORTA = PINB; //Readvaluesfrompinsof PortBand //write topinsof PortA
  • 2. Example Write a program to be downloadedtothe Atmel AVRATMega32microcontrollerwhichcontinuously readthe logicvaluesonportBand write themtoportC. Solution The program below,writteninAVRStudio5andAVRStudio6, accomplishthe taskthat was asked above.There are several thingstonote here. The headerfile avr/io.hmustbe includedinorderforusto use the name of the ports. The ports mustbe configuredbeforetheyare used.The firsttwo(2) linesinthe main() functionof the program are for configuration. Once the ports are configuredyoucanthenwrite toor readfromthem, were applicable. The use of the while(1) loopallowsforthe continuousreadandwrite operation. /* * WritteninAVRStudio5 / AVRStudio6 * Compiler:AVRGNUC Compiler(GCC) * * Author:AVRTutorials * Website:www.AVR-Tutorials.com */ #include<avr/io.h> int main() { DDRB = 0x00; //configure portBasinput DDRC = 0xFF; //configure portCasoutput while(1) { PORTC = PINB; } return0; }
  • 3. Interfacing a LED with a Microcontroller AVRmicrocontrollerssuchasthe ATMega8515 onlysupplya currentof about20mA and so we can drive an LED directlyfromthe microcontrollerporteliminatingthe resistor.Infactif a resistorisaddedthe intensityof the LEDwill be low. The figure belowshows8-LEDsconnectedtoan ATMega8515 microcontroller.The code thatfollowsif downloadedtothe microcontrollerwill blinkthe LEDscontinuously.Note thatthiscode couldworkwith otherAVRmicrocontrollerssuch asthe ATMega16, ATmega32, ATTiny2313, etc. LED ATMega8515 MicrocontrollerSchematic Note:Inorder forthe circuitto operate asdescribe the internal oscillatorof the ATMega8515 microcontrollermustbe programtorun at 4MHz. /* * WritteninAVRStudio5 * Compiler:AVRGNUC Compiler(GCC) * Author:AVRTutorials * Website:www.AVR-Tutorials.com */ #include <avr/io.h> #define F_CPU4000000UL #include <util/delay.h> int main() { DDRC = 0xFF; // Configure portCas output while(1) { PORTC = 0xFF; // Turn ON LEDs _delay_ms(250);//Wait 250ms PORTC = 0x00; // Turn OFFLEDs _delay_ms(250); // Wail 250ms } return0; }