microprocessors and microcontrollers (practical) · avr’s cpu pr ogr am r om os c ports timers...

36
Microprocessors And Microcontrollers (Practical) By: Dr. Attiya Baqai Semester : 4 th , 5 th (TL, ES) Assistant Professor, Course Code : ES256 , ES313 Department of Electronics, MUET.

Upload: lamtruc

Post on 17-Apr-2019

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Microprocessors And Microcontrollers (Practical)

By: Dr. Attiya Baqai

Semester : 4th , 5th (TL, ES) Assistant Professor,

Course Code : ES256 , ES313 Department of

Electronics, MUET.

Page 2: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Introduction to Programming and AVR Microcontroller

3

By: Dr. Attiya Baqai

Semester : 4th , 5th (TL, ES) Assistant Professor,

Course Code : ES256 , ES313 Department of

Electronics, MUET.

Page 3: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

AVR’s CPU

PROGRAM

ROM

PortsOSC

Timers

Other

Peripherals

Program

BusData

Bus

RAM

I/O

PINS

EEPROM

Interrupt

Unit

General

Purpose

Registers

CPU

data busaddress bus

control busData

Bus

Page 4: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Available Microcontrollers

◉ Altera ◉ Analog Devices ◉ Atmel ◉ Cypress Semiconductor ◉ Dallas Semiconductor ◉ELAN Microelectronics Corp. ◉ Energy Micro ◉ EPSON Semiconductor ◉ Freescale Semiconductor ◉ Parallax ◉ NXP Semiconductors ◉ Texas Instruments

◉ Fujitsu ◉ Holtek ◉Infineon ◉Intel ◉Lattice Semiconductor ◉Maxim Integrated ◉Microchip Technology ◉National Semiconductor ◉NEC ◉Panasonic ◉Rabbit Semiconductor ◉Silicon Laboratories

Page 5: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

ATMEGA 8 Microcontroller

◉ 8-bit Atmel with 8KB in-System Programmable Flash ◉32 × 8 General Purpose Working Registers ◉Up to 16MIPS Throughput at 16MHz ◉512 B EEPROM ◉1KB Internal SRAM ◉Two 8-bit Timer/Counters and One 16-bit Timer/Counter

◉Three PWM Channels ◉Two-wire Serial Interface ◉Programmable Serial USART ◉Master/Slave SPI Serial Interface ◉23 Programmable I/O Lines ◉0 - 8MHz (ATmega8L) ◉0 - 16MHz (ATmega8)

Page 6: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

ATMEGA 16L Microcontroller

Page 7: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Programming Methods

◉In System Programming (ISP)

◉JTAG

◉DebugWire (dW)

◉Pre-programmed Bootloader

◉High Voltage Parallel Programming (HVPP)

◉High Voltage Serial Programming (HVSP)

◉Program and Debug Interface (PDI)

◉Tiny Programming Interface (TPI)

◉aWire (aW)

Page 8: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

In Circuit System Programming (ICSP)

◉ In System Programming is perhaps the most common method of programming the Flash, EEPROM, fuse and lock bytes of the entire AVR line.

◉ ISP can program AVRs at extremely high clock rates (assuming the target AVR is running at a high frequency and the programmer supports it) and is the method of choice for almost all AVR hobbyists.

◉ There are many, many AVRISP clones and AVR910 programmers on the market in addition to simple do-it-yourself dongles which connect to your computer's parallel port.

◉Recent new dongle designs may use the computer's serial port, however anecdotal evidence has said that this method is *extremely* slow due to technical limitations.

◉ISP requires that the target AVR is running at a clock rate of at least four times that of the ISP clock.

Page 9: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

AVR Programming Languages

◉ Programming Language for AVR is Assembly, however C language Compilers for AVRs are also available which convert C language program to Assembly or Machine (HEX) language.

Page 10: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

AVR Programming Tools

◉ Simulation: Proteus Simulator v7.9 (Simulation)

AVR Studio 4.18 (Programming)

Win AVR (GCC Compiler)

◉Experiment: USBAsp Programmer

AVR Studio 4.18

Extreme Burner (for Programming through ICSP)

AVR Trainer or AVR ATMEGA 16 Chip

WinAVR

Page 11: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

USBAsp Programmer Connection Diagram

ICSP Header

VCC

MOSI MISO

SCK

RST

GND

ATMEGA 16

Page 12: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

AVR Microcontroller Trainer

Page 13: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

AVR Microcontroller Trainer

LCD

10-segment LED

bargraph display

ICSP header

ATMEGA16

4 Switches

Power Socket

Page 14: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Introduction to AVR Studio 4

Page 15: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Select New Project

Page 16: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Atmel AVR Assembler for Assembly AVR GCC for C-language

Page 17: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Enter details and click NEXT

Page 18: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

AVR Simulator > ATmega16

Page 19: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Programming Window Project Files

uC I/O Info

I/O

Status / value Compilation Status

Compile and Build Options

Page 20: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Input & Output Operations

◉ To initialize ports as Input / Output: DDRx = 0xFF; // Output

DDRx = 0x00; // Input

◉ Getting / Writing Data on Ports: PINx // for Input

PORTx // for Output

x is the Port name i.e. A, B, C etc

Page 21: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Program#01: Send Data on Port B

.INCLUDE "M16DEF.INC"

.ORG 0000

LDI R16,0xFF

OUT DDRB,R16 ; making PORT B as output

LDI R17, 0xF0 ; sending data “F0” to PORT B

OUT PORTB,R17

AGAIN: JMP AGAIN

Page 22: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Simulate & Debug Program in AVR Studio

.INCLUDE "M16DEF.INC"

.ORG 0000

LDI R16,0xFF

OUT DDRB,R16 ; making PORT B as output

LDI R17, 0xF0 ; sending data “F0” to PORT B

OUT PORTB,R17

AGAIN: JMP AGAIN

Page 23: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera
Page 24: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera
Page 25: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera
Page 26: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera
Page 27: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera
Page 28: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Proteus Output

Page 29: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Proteus Output

Browse Hex File from PC

Run Simulation

Page 30: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Program # 02: Get Data from Port C and Put it on Port B

.INCLUDE "M16DEF.INC"

.ORG 0000 LDI R16,00 OUT DDRC, R16 ; making PORT C as input LDI R17,0xFF OUT DDRB,R17 ; making PORT B as output AGAIN: IN R0,PINC ; Read continuously from PORT C OUT PORTB,R0 ; & send it to PORT B JMP AGAIN

Page 31: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Example # 02: Proteus

Page 32: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Example # 02: Proteus

Page 33: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Khazama AVR Programmer

Page 34: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Exercise Questions

◉ Get Data from Switches connected at Port C and Output complimented data to Port B

◉ Get data from lower nibble of Port C and put it to upper nibble of port B

◉ Count numbers from 0 to 255 on Port B

◉ Write a program to generate these random numbers: 100, 255, 123, 80, 66, 36, 15, 00

Page 35: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Acknowledgement

◉ Originally taken from Practical Lecture slides and handouts by Engr Azam Rafique Memon

◉ Modified by Dr. Attiya Baqai

Page 36: Microprocessors And Microcontrollers (Practical) · AVR’s CPU PR OGR AM R OM OS C Ports Timers Other Periphera ls Progra m Bus Da ta Bus R AM I/O PINS E E PR OM Interrupt Unit Genera

Any questions ?

Thanks!