SlideShare une entreprise Scribd logo
1  sur  3
Télécharger pour lire hors ligne
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;
}

Contenu connexe

Tendances

Intro2 Robotic With Pic18
Intro2 Robotic With Pic18Intro2 Robotic With Pic18
Intro2 Robotic With Pic18Moayadhn
 
8051 microcontroller
8051 microcontroller8051 microcontroller
8051 microcontrollerjokersclown57
 
8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED C8051 programming skills using EMBEDDED C
8051 programming skills using EMBEDDED CAman Sharma
 
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 microcontrollerCorrado Santoro
 
Chapter 7 8051 programming in c
Chapter 7  8051 programming in cChapter 7  8051 programming in c
Chapter 7 8051 programming in cAbdelrahman Elewah
 
Io (2)
Io (2)Io (2)
Io (2)Aisu
 
Modules and ports in Verilog HDL
Modules and ports in Verilog HDLModules and ports in Verilog HDL
Modules and ports in Verilog HDLanand hd
 
Controlling Motors for Robot
Controlling Motors for RobotControlling Motors for Robot
Controlling Motors for RobotVarad Manglekar
 
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 mazidiMuhammad Abdullah
 

Tendances (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
 

En vedette

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 Atmega16Robo India
 
7 Analog Digital Converter
7 Analog Digital Converter7 Analog Digital Converter
7 Analog Digital ConverterSimon Patabang
 
AVR Microcontroller
AVR MicrocontrollerAVR Microcontroller
AVR MicrocontrollerÖzcan Acar
 
Analog to digital converter
Analog to digital converterAnalog to digital converter
Analog to digital converterCorrado Santoro
 
Advance Microcontroller AVR
Advance Microcontroller AVRAdvance Microcontroller AVR
Advance Microcontroller AVRDaksh Raj Chopra
 
Introduction to AVR Microcontroller
Introduction to AVR Microcontroller Introduction to AVR Microcontroller
Introduction to AVR Microcontroller Mahmoud Sadat
 

En vedette (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
 

Similaire à Programming avr microcontroller digital i

Hardware interfacing basics using AVR
Hardware interfacing basics using AVRHardware interfacing basics using AVR
Hardware interfacing basics using AVRMohamed Abdallah
 
Basics Of Embedded Systems
Basics Of Embedded SystemsBasics Of Embedded Systems
Basics Of Embedded Systemsarlabstech
 
Microcontroller
MicrocontrollerMicrocontroller
MicrocontrollerSpitiq
 
Embedded System Practical manual (1)
Embedded System Practical manual (1)Embedded System Practical manual (1)
Embedded System Practical manual (1)Niraj Bharambe
 
ECET 330 Massive Success--snaptutorial.com
ECET 330 Massive Success--snaptutorial.comECET 330 Massive Success--snaptutorial.com
ECET 330 Massive Success--snaptutorial.comsantricksapiens71
 
Ecet 330 Success Begins / snaptutorial.com
Ecet 330 Success Begins / snaptutorial.comEcet 330 Success Begins / snaptutorial.com
Ecet 330 Success Begins / snaptutorial.comWilliamsTaylorzm
 
Ecet 330 Enthusiastic Study / snaptutorial.com
Ecet 330 Enthusiastic Study / snaptutorial.comEcet 330 Enthusiastic Study / snaptutorial.com
Ecet 330 Enthusiastic Study / snaptutorial.comStephenson033
 
ECET 330 Technology levels--snaptutorial.com
ECET 330 Technology levels--snaptutorial.comECET 330 Technology levels--snaptutorial.com
ECET 330 Technology levels--snaptutorial.comsholingarjosh102
 
Embedded c programming22 for fdp
Embedded c programming22 for fdpEmbedded c programming22 for fdp
Embedded c programming22 for fdpPradeep Kumar TS
 
microcontroller basics
microcontroller basicsmicrocontroller basics
microcontroller basicssagar Ramdev
 
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 copymkazree
 
Basics of arduino uno
Basics of arduino unoBasics of arduino uno
Basics of arduino unoRahat Sood
 
Microcontroladores: programación con microcontrolador AVR
Microcontroladores: programación con microcontrolador AVRMicrocontroladores: programación con microcontrolador AVR
Microcontroladores: programación con microcontrolador AVRSANTIAGO PABLO ALBERTO
 
AVR Micro controller Interfacing
AVR Micro controller Interfacing AVR Micro controller Interfacing
AVR Micro controller Interfacing Raghav Shetty
 

Similaire à 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
 

Plus de Manas Mantri

Raman Spectroscopy
Raman SpectroscopyRaman Spectroscopy
Raman SpectroscopyManas Mantri
 
2nd sem mechanical syllabus
2nd sem mechanical syllabus2nd sem mechanical syllabus
2nd sem mechanical syllabusManas Mantri
 
semiconductor electronics
semiconductor electronicssemiconductor electronics
semiconductor electronicsManas Mantri
 
Digital image processing questions
Digital  image processing questionsDigital  image processing questions
Digital image processing questionsManas Mantri
 
Dip 5 mathematical preliminaries
Dip 5 mathematical preliminariesDip 5 mathematical preliminaries
Dip 5 mathematical preliminariesManas Mantri
 
Dip 1 introduction
Dip 1 introductionDip 1 introduction
Dip 1 introductionManas Mantri
 
Connected component labeling algorithm
Connected component labeling algorithmConnected component labeling algorithm
Connected component labeling algorithmManas Mantri
 

Plus de 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
 

Dernier

Comparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxComparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxAvaniJani1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...Nguyen Thanh Tu Collection
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptxmary850239
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfChristalin Nelson
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroomSamsung Business USA
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEPART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEMISSRITIMABIOLOGYEXP
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationdeepaannamalai16
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...Nguyen Thanh Tu Collection
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptxmary850239
 

Dernier (20)

Comparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptxComparative Literature in India by Amiya dev.pptx
Comparative Literature in India by Amiya dev.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
BÀI TẬP BỔ TRỢ TIẾNG ANH 11 THEO ĐƠN VỊ BÀI HỌC - CẢ NĂM - CÓ FILE NGHE (GLOB...
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 
Chi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical VariableChi-Square Test Non Parametric Test Categorical Variable
Chi-Square Test Non Parametric Test Categorical Variable
 
4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx4.9.24 Social Capital and Social Exclusion.pptx
4.9.24 Social Capital and Social Exclusion.pptx
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
DiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdfDiskStorage_BasicFileStructuresandHashing.pdf
DiskStorage_BasicFileStructuresandHashing.pdf
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom6 ways Samsung’s Interactive Display powered by Android changes the classroom
6 ways Samsung’s Interactive Display powered by Android changes the classroom
 
Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,Spearman's correlation,Formula,Advantages,
Spearman's correlation,Formula,Advantages,
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
Plagiarism,forms,understand about plagiarism,avoid plagiarism,key significanc...
 
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEPART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
Congestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentationCongestive Cardiac Failure..presentation
Congestive Cardiac Failure..presentation
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 - I-LEARN SMART WORLD - CẢ NĂM - CÓ FILE NGHE (BẢN...
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx4.9.24 School Desegregation in Boston.pptx
4.9.24 School Desegregation in Boston.pptx
 

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; }