CSE-391--02--ATmega32-basics
CSE-391--02--ATmega32-basics
WITH ATmega32
LEARNING OBJECTIVE
▪ Brief overview of ATmega32 architecture
▪ Introduction to Programming ATmega32
▪ Review on C
▪ Programming the digital IO ports of ATmega32
ARCHITECTURE
WHY ARCHITECTURE?
▪ ATmega32 is a 8-bit microcontroller
▪ Registers and memory accesses are in 8-bit chunks
▪ DDRA = 0b11111111;
▪ Sets each pin of port A as output
▪ DDRB = 0b00000000;
▪ Sets each pin of port B as input
▪ DDRC = 0b01010101;
▪ ???
READING FROM/WRITING TO PORT
What about unused pins
● the internal pull-up resistor should be activated
● in PORT A
- PA0 and PA3 are used as input pins
- PA2 and PA5 are used as output pins, both initialized as 0
DDRA = 0b00100100;
PORTA = 0b11011011;
LET'S WRITE A CODE
▪ Suppose we want to a led glow for 500 ms and remain off for 500 ms
and repeat
▪ The led is connected to PORTA.0
CODE
#include <avr/io.h> //standard AVR header
#include <util/delay.h>
int main(void)
{
}
CODE
#include <avr/io.h> //standard AVR header
#include <util/delay.h>
int main(void)
{
DDRA = 0b11111111; //configure PORTA as output
while(1)
{
PORTA = 0b00000001; //output logic 1 to PORTA.0
_delay_ms(500); //delay for 500 ms
PORTA = 0b00000000;
_delay_ms(500);
}
}
GENERAL STRUCTURE
#include <avr/io.h>
//Necessary headers
int main(void)
{
//Initialization
while(1)
{
//Continuous processing
}
}
PRACTICE
▪ Toggle only the 4th bit of Port B continuously without disturbing the
rest of the pins of Port B.
▪ Use bitwise operator
PRACTICE
▪ Connect 8 LEDs with PORTA and glow the LEDs one at a time in a
rotating fashion
▪ Many alternatives
CODE
#include <avr/io.h> //standard AVR header
#define F_CPU 1000000 //Clock Frequency
#include <util/delay.h>
int main(void)
{
DDRA = 0xFF; //configure PORTA as output
PORTA = 0;
while(1)
{
if(PORTA == 0)
PORTA = 1;
else
PORTA = PORTA << 1;
_delay_ms(500);
}
}
PRACTICE
▪ Write a program to read a byte from PORTA and write it to PORTB
CODE
int main(void)
{
DDRA = 0x00;
DDRB = 0xFF;
while(1)
{
PORTB = PINA;
}
}
SIMPLE COUNTER
▪ PORTA.0 is connected with push button
▪ 1 when pressed
▪ ATmega32 Datasheet
THANKS TO
▪ Abdus Salam Azad
▪ Tanvir Ahmed Khan
▪ Muhammad Ali Nayeem