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

Programming avr microcontroller digital i

  • 1.
    Programming AVR MicrocontrollerDigital 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 programto 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 LEDwith 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; }