English French German Italian Portuguese Russian Spanish

v1

Survey

Which microcontroller family do you prefer?
 

Did you know?

Electrocution is one of the top five causes of workplace deaths.

Forum elektronika, schematy, porady, naprawa

FacebookTwitter

 

Home > AVR tutorial > Input/Output (I/O) pins > AVR Input/Output pin project example
(1 vote, average 5.00 out of 5)
AVR tutorials - I/O pins
AVR Input/Output pin project example 5.0 out of 5 based on 1 votes.

logo

Here is example that is complete project with step by step guide that will show you how to use GPIO pins on ATmega8535.
When you start program use CodeWizardAVR to generate basic program structure.
In our case we used ATmega8535 so we will choose that modem from Chip tab in wizard window.
For frequency chose correct frequency that is used (external xtal, calibrated internal RC oscillator..).

 

 


1

 

Switch to port tabs and set pins you want to use to output. We will use portA pin 0.


2


Go to Program menu and chose Preview or Generate, Save and Exit option to create project and put generated code inside it. We will use Generate, Save and Exit option.

 

3


You will get next window on your screen.


4


Now we can change port state in main fuction by writing
 

PORTA.0 = 1;
PORTA.0 = 1;

 

Problem with this is that since microcontroller works very fast we shoulndt be able to see changes on this pin without oscilloscope. This is why we will introduce delay functions inside code.
To do this we have to declare that we will use libraries for delay.This is done at the top of the code

 

#include <mega8535.h>
#include <delay.h>


Now we can use delay functions in main
These functions are intended for generating delays in C programs.
Before calling the functions the interrupts must be disabled, otherwise the delays will be much longer than expected.
Also it is very important to specify the correct AVR chip Clock frequency in the Project|Configure|C Compiler|Code Generation menu.

The functions are:

void delay_us(unsigned int n)

      generates a delay of n useconds. n must be a constant expression.

void delay_ms(unsigned int n)

      generates a delay of n milliseconds.
This function automatically resets the watchdog timer every 1ms by generating the wdr instruction
Example:

void main(void) {
/* disable interrupts */
#asm("cli")
/* 100us delay */
delay_us(100);

/* 10ms delay */
delay_ms(10);
/* enable interrupts */
#asm("sei")
}

Now we will combine this functions with port change so we can see this on LED connected to it.
Complete code for this project would be:

 

/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.4 Evaluation
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project : 
Version : 
Date    : 12/1/2011
Author  : Freeware, for evaluation and
non-commercial use only
Company : 
Comments:

Chip type               : ATmega8535
Program type            : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 128
*****************************************************/
#include <mega8535.h>
#include <delay.h>
// Declare your global variables here
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=Out 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=0 State0=0 
PORTA=0x00;
DDRA=0x03;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
while (1)
      {
      // Place your code here
           PORTA.0 = 1; 
           delay_ms(300);
                  PORTA.0 = 0;  
                  delay_ms(300);
      }
}

 

 

All crucial lines of the code are highlited. Below is video showing led blinking caused by this code on EasyAVR6 development board.

 

 

Complete project with source code for this example can be downloaded from THIS LINK

and more information on I/O pins can be found HERE

 

Related Posts:
Microcontrollers – Input / Output pins
When working with GPIO (general purpose input/output) pins of microcontroller you must be aware of few things. Some people think it is enough to know how to pro
DC-DC chip has 2.7-40V input and 2.7-40V output
  Linear Technology has announced a synchronous buck-boost converter that will convert any voltage between 2.7 and 40V to any voltage over the same range.
Introduction to LABVIEW graphical programming, aka. LABVIEW for dummies
Electronics engineers often need additional PC software to log data, display data, provide user interface.. Without object programming experience this could be