0% found this document useful (0 votes)
36 views53 pages

CSE 391 02 ATmega32 Basics

The document provides an overview of the ATmega32 microcontroller, including its architecture, programming process, and digital I/O capabilities. It highlights the importance of understanding the microcontroller's architecture for optimizing performance and efficient hardware interaction. Additionally, it covers programming in C and assembly, along with practical examples and exercises for using the digital I/O ports.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views53 pages

CSE 391 02 ATmega32 Basics

The document provides an overview of the ATmega32 microcontroller, including its architecture, programming process, and digital I/O capabilities. It highlights the importance of understanding the microcontroller's architecture for optimizing performance and efficient hardware interaction. Additionally, it covers programming in C and assembly, along with practical examples and exercises for using the digital I/O ports.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

GETTING STARTED

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

▪ Use of 2 byte variables will result in


▪ Larger executable files
▪ Slower execution of program
▪ More memory usage

▪ So we need to learn architecture to


▪ Optimize performance
▪ Efficiently interact with resident hardware
SUMMARY
▪ Register-based Harvard Architecture coupled with the RISC-based
instruction set
▪ Enables fast and efficient program execution
Von Neumann vs Harvard
REGISTER-BASED ARCHITECTURE
▪ 32 8-bit registers coupled with ALU within CPU
▪ Both operands of an operation are stored in registers
▪ Before an operation is performed,
CPU loads all necessary data for the operation
▪ The result of the operation is also stored in a register
RISC
▪ Reduced Instruction Set Computer
▪ Equipped with very simple and efficient basic operations

▪ ATmega32 is equipped with 131 RISC-type instructions


▪ Most can be executed in a single clock cycle
AVR MEMORIES
▪ 3 main memory sections
▪ in-System Re-programmable nonvolatile Flash Memory, 32 KB
▪ Machine instructions go here
▪ contains Bootloader
▪ volatile SRAM to feature stack and data memory, 2 KB
▪ For runtime data
▪ nonvolatile EEPROM, 1 KB

▪ EEPROM and Flash memories have a limited lifetime of erase/write cycles


BLOCK DIAGRAM
HARVARD ARCHITECTURE
▪ Separate, dedicated memories and
buses for instruction and data
▪ Advantages?

● simultaneous fetch & execution (SLP)


● read-only instruction memory

Dr. Tim Margush - Assembly Language Programming 6/30/19 12


SINGLE LEVEL PIPELINING
▪ Single Level Pipelining
▪ Next instruction is fetched while the current one is executing
SOME MEMBERS OF THE ATmega FAMILY
PROGRAMMING
COMPLETE PROCESS
1. Writing the code
2. Compiling it to a .hex file
3. Inserting the .hex file into the microcontroller using a
programming software and a programming device
4. Setting the microcontroller in the working circuit and turning the
power on
PROGRAMMING LANGUAGE
▪ ATmega32 CPU has 131 machine instructions
▪ Assembly language should be used for
▪ the most efficient and fast execution
▪ Smaller executable program

▪ We would use C language for some conveniences


▪ So what do we need?
▪ A Compiler
▪ Several third-party companies develop C compilers for the AVR microcontroller
▪ We would use AVR GCC compiler integrated with Atmel Studio IDE
SOFTWARE TOOLS
▪ IDE: Atmel Studio
▪ Simulator: Proteus
▪ Burner: Extreme Burner
REVIEW ON C
DATA TYPE
REPRESENTATION OF 8-BIT DATA
Decimal Binary Hexadecimal
0 0b00000000 0x00

255 0b11111111 0xFF

170 0b10101010 0xAA


BIT-WISE LOGIC OPERATORS
BIT-WISE SHIFT OPERATORS
DIGITAL I/O
DIGITAL I/O
▪ Atmega32 has fours 8-bit digital IO ports:
▪ PORT A
▪ PORT B
▪ PORT C
▪ PORT D

▪ Each port has 8 data pins


▪ Used for simple I/O

▪ Every port is bi-directional. Each of the 8 pins can be individually


configured as
▪ input (receiving data into microcontroller) or
▪ output (sending data from microcontroller)
I/O PORTS
PORT REGISTERS
REGISTERS & PINS
PORT CONFIGURATION

▪ 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

If PORTxn is written logic one when the pin is configured as an input


pin, the pull-up resistor is 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)
{

DDRA = 0b11111111; //configure PORTA as output

PORTA = 0b00000001; //output logic 1 to PORTA.0


_delay_ms(500); //delay for 500 ms
PORTA = 0b00000000;
_delay_ms(500);

}
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

▪ PORT B connected to 8 LEDs


▪ Increment count when pressed the push button
CODE
int main(void)
{
DDRA = 0xFE;
DDRB = 0xFF;
PORTB = 0;
while(1)
{
if(PINA & 1)
{
PORTB++;
}
}
}
CODE
int main(void)
{
DDRA = 0xFE;
DDRB = 0xFF;
PORTB = 0;
while(1)
{
if(PINA & 1)
{
PORTB++;
_delay_ms(500);
}
}
}
PERIPHERAL
FEATURES
OTHER BUILT-IN FEATURES
▪ Clock
▪ Timer
▪ Pulse Width Modulator
▪ Serial Communications
▪ Analog-to-Digital Converter
▪ Interrupt
CLOCK
▪ Internal clock
▪ operating frequency of 1, 2, 4, or 8 MHz

▪ external clock source


▪ More precise than internal clock
TIMER
▪ Generate precision output signal
▪ Measure incoming digital signal characteristics
▪ period
▪ duty cycle
▪ frequency

▪ Count external events


PULSE WIDTH MODULATION
▪ Control the position of servo motor
▪ Control the speed of DC motor
SERIAL COMMUNICATIONS
▪ communication with other devices
▪ wireless data transmission
ANALOGUE-TO-DIGITAL CONVERTER
▪ Most of the sensors produce analogue signal
▪ Sound
▪ Temperature
▪ Pressure
INTERRUPTS
▪ High priority event management
▪ Both internal and external interrupts
RESOURCES
▪ Atmel AVR Microcontroller Primer: Programming and Interfacing
▪ Chapter 1

▪ The AVR Microcontroller and Embedded Systems


▪ Chapter 2
▪ Chapter 4
▪ Chapter 7

▪ ATmega32 Datasheet
THANKS TO
▪ Abdus Salam Azad
▪ Tanvir Ahmed Khan
▪ Muhammad Ali Nayeem

You might also like