avr atmega32 elementary input / output

51
AVR ATMega32 Elementary Input / Output Module 9

Upload: gaston

Post on 25-Feb-2016

164 views

Category:

Documents


9 download

DESCRIPTION

Module 9 . AVR ATMega32 Elementary Input / Output. ATMega32 Input / Output. One of the basic and essential features designed in a computer system is its ability to exchange data with other external devices, and to allow the user to interact with the system: Input Devices include: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: AVR  ATMega32 Elementary Input / Output

AVR ATMega32 Elementary Input / Output

Module 9

Page 2: AVR  ATMega32 Elementary Input / Output

ATMega32 Input / Output

20112012-I Module 8/2

One of the basic and essential features designed in a computer system is its ability to exchange data with other external devices, and to allow the user to interact with the system: Input Devices include:

Switches, Keyboards, Mice, Scanners, Cameras, etc. Output devices include:

Lamp/LED/LCD displays, Video monitors, Speakers, Printers, etc. One or more interface circuits usually are used between I/O devices and

the CPU to: Handle transfer of data between CPU and I/O interface. Handle transfer of data between I/O device and interface. Enable the CPU to request the status of data sent/received by the interface.

Common I/O interfaces: Elementary I/O: Simple two-state devices such as LED and switches Parallel I/O: Date exchanged one byte at a time. Serial I/O: Data exchanged one bit at a time.

Page 3: AVR  ATMega32 Elementary Input / Output

Microprocessor I/O Interfacing

20102011-I

Most I/O requests are made by applications or the operating system, and involve moving data between a peripheral device and main memory.

There are three main ways that programs communicate with devices.

I/O Addressing

Direct Access Memory

InterruptsModule 8/3

Page 4: AVR  ATMega32 Elementary Input / Output

Microprocessor I/O Interfacing

20112012-I Module 8/4

Port-Based (Standard I/O-Direct I/O) I/O addresses are seperated from memory addresses Processor’s software reads and writes a port just like a register E.g., PA0 = 0xFF

o Advantages: Do not take memory addressing spaceo Disadvatage: Use only IN or OUT instrcutions to transfer data

Memory-Mapped I/O I/O ports treated as memory locations

o Advantages: Accessing I/O ports is like accessing memory loactionso Disadvantages: Take memory addressing space

Page 5: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Ports

20112012-I Module 8/5

The pins marked ‘Gnd’ are to be grounded, ‘Vcc & AVcc’ are to be given 5V. The Reset pin is also high but we usually prefer to put a switch at this point for the reset of the chips. If the switch is pressed for a minimum pulse of time then it will Reset the chip.

Note: ‘AVcc’ should be connected to 5V supply through a capacitor when using PORTA pins as ADC, though in simple applications capacitor is not necessary.

Page 6: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Port Registers

20112012-I Module 8/6

There are three registers associated with Input/ Output Ports in AVR.

Page 7: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Port Registers

20112012-I Module 8/7

Selecting the direction of pin:- DDRxn bit (in the DDRx Register) selects the direction of this pin.

• DDRxn =1, Portx nth pin is configured as an output pin (since there are eight pins in all in a particular port so ‘n’ can be any number between 0-7).

• DDRxn=0, Portx nth is configured as an input pin.

Activating the pull resistors:-• PORTxn= 1, the pull up resistors are activated (while the nth pin is configured as

input pin)

• PORTxn= 0, the pull up resistors are deactivated (for nth pin).

Inputs of the AVR are generally in Hi-Z state. This makes them prone to catching noise and picking up false signals. So it is advisable to activate the pull up resistor to reduce noise

Page 8: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Port Registers

20112012-I Module 8/8

Status of pin when configured as an output pin:- • PORTxn= 1, portx nth pin is driven high (one)• PORTxn= 0, portx nth pin is driven low (zero)

PINx Register:-To put it bluntly, the PINx register contains the status of all the pins in that port. • If the pin is an input pin, then its corresponding bit will mimic the

logic level given to it. • If it is an output pin, its bit will contain the data that has been

previously output on that pin. (The value of an output pin is latched to PINxn bit, you can observe it when we do step by step execution in AVR-Studio shown below)

Page 9: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Pull UP/DOWN Resistors

20112012-I Module 8/9

• Pull up resistors are used to pull logic signals up (to logic 1). So when some input signal needs to be set to logic 1, but might need to be changed for some reason to 0 at some other time, a pull-up resistor can keep the signal at logic 1 until the signal is pulled down by something. Typical application for pull up resistors is to connect 4.7 k-ohm (or some other suitable resistor value usually between 1 k-ohm and 10 k-ohm) from the circuit operating voltage (+5V usually) to the input pin. This resistor keep the signal at logic 1. When the signal needs to be set to 0 it is pulled down by connecting that input pin to ground (usually through a button, DIP switch or open collector output of some other part of circuit).

• Pull-down resistors work in the opposite way. They keep signal a logic 0 until something connect to signal input to +5V (or whatever the operating voltage of the logic circuit is).

Page 10: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Pull UP Resistor

20112012-I Module 8/10

PINx.n

vcc

PORTx.n1 = Close

0 = Open

pin n of port x

Inside the AVR chip

Outside the AVR chip

Page 11: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Port: Example 1

20112012-I Module 8/11

The AVR assembly code below shows how to configure the pins on portA of an AVR ATMega32 microcontroller.

.include "m32def.inc" LDI R16, 0xFF ; Load 0b11111111 in R16 OUT DDRA, R16 ; Configure PortA as an Output port LDI R16, 0x00 ; Load 0b00000000 in R16 OUT DDRB, R16 ; Configure PortB as an Input port LDI R16, 0xF0 ; Load 0b11110000 in R16 OUT DDRC, R16 ; Configure first four pins on PortC ; as input and the others as output

Page 12: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Port: Example 2

20112012-I Module 8/12

• The following code will toggle all 8 bits of Port B forever with some time delay between “on” and “off” states:

LDI R16,0xFF ;R16 = 0xFF = 0b11111111 OUT DDRB,R16 ;make Port B an output port (1111 1111)

L1: LDI R16,0x55 ;R16 = 0x55 = 0b01010101 OUT PORTB,R16 ;put 0x55 on port B pins CALL DELAY LDI R16,0xAA ;R16 = 0xAA = 0b10101010 OUT PORTB,R16 ;put 0xAA on port B pins CALL DELAY RJMP L1

Page 13: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Port: Example 3

20112012-I Module 8/13

The following code gets the data present at the pins of port C and sends it to port B indefinitely, after adding the value 5 to it:

.INCLUDE "M32DEF.INC"LDI R16,0x00 ;R16 = 00000000 (binary)OUT DDRC,R16 ;make Port C an input port LDI R16,0xFF ;R16 = 11111111 (binary)OUT DDRB,R16 ;make Port B an output port(1 for Out)

L2: IN R16,PINC ;read data from Port C and put in R16LDI R17,5ADD R16,R17 ;add 5 to itOUT PORTB,R16 ;send it to Port BRJMP L2 ;continue forever

Page 14: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Port: SBI & CBI Instructions

20112012-I Module 8/14

• SBI (Set Bit in IO register)– SBI ioReg, bit ;ioReg.bit = 1– Examples:

• SBI PORTD,0 ;PORTD.0 = 1• SBI DDRC,5 ;DDRC.5 = 1

• CBI (Clear Bit in IO register)– CBI ioReg, bit ;ioReg.bit = 0– Examples:

• CBI PORTD,0 ;PORTD.0 = 0• CBI DDRC,5 ;DDRC.5 = 0

Page 15: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Port: Example 4

20112012-I Module 8/15

Write a program that toggles PORTA.4 continuously.

.INCLUDE “M32DEF.INC” SBI DDRA,4L1: SBI PORTA,4 CBI PORTA,4 RJMP L1

Page 16: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Port: SBIC & SBIS Instructions

20112012-I Module 8/16

• SBIC (Skip if Bit in IO register Cleared)– SBIC ioReg, bit ; if (ioReg.bit = 0) skip next instruction

– Example:SBIC PORTD,0 ;skip next instruction if PORTD.0=0INC R20LDI R19,0x23

• SBIS (Skip if Bit in IO register Set)– SBIS ioReg, bit ; if (ioReg.bit = 1) skip next instruction

– Example:SBIS PORTD,0 ;skip next instruction if PORTD.0=1INC R20LDI R19,0x23

Page 17: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Port: Example 5

20112012-I Module 8/17

• Write a program to perform the following:• (a) Keep monitoring the PB2 bit until it becomes HIGH;• (b) When PB2 becomes HIGH, write value $45 to Port C, and also send a

HIGH-to-LOW pulse to PD3.

.INCLUDE "M32DEF.INC" CBI DDRB, 2 ;make PB2 an input SBI PORTB,2 LDI R16, 0xFF OUT DDRC, R16 ;make Port C an output port SBI DDRD, 3 ;make PD3 an outputAGAIN: SBIS PINB, 2 ;Skip if Bit PB2 is HIGH RJMP AGAIN ;keep checking if LOW LDI R16, 0x45 OUT PORTC, R16 ;write 0x45 to port C SBI PORTD, 3 ;set bit PD3 (H-to-L) CBI PORTD, 3 ;clear bit PD3 HERE: RJMP HERE

Page 18: AVR  ATMega32 Elementary Input / Output

ATmega32 Elementary I/O

20112012-IModule 8/

18

Page 19: AVR  ATMega32 Elementary Input / Output

ATMega32 Elementary I/O

20112012-I Module 8/19

Two-state peripheral devices May involve more than one bit - e.g., BCD Requirements

Bit signals can be written to output devices under program control Bit signals can be read from input devices under program control

Devices required Latches such as 74LS374 for output Tri-state buffers such as 74LS244 for input

Example Output devices LED, bulbs Relay coils 7-segment display

Example Input devices Push button Proximity switch Rotary BCD coder

Page 20: AVR  ATMega32 Elementary Input / Output

ATMega32 Elementary I/O: LEDs

20112012-I Module 8/20

• LEDs must be connected the correct way round, the diagram may be labeled a or + for anode and k or - for cathode (yes, it really is k, not c, for cathode!). The cathode is the short lead and there may be a slight flat on the body of round LEDs. If you can see inside the LED the cathode is the larger electrode (but this is not an official identification method).

• LEDs can be damaged by heat when soldering, but the risk is small unless you are very slow. No special precautions are needed for soldering most LEDs.

• LEDs are available in red, orange, amber, yellow, green, blue and white. Blue and white LEDs are much more expensive than the other colors. The color of an LED is determined by the semiconductor material, not by the coloring of the 'package' (the plastic body).

An LED is a semiconductor device that converts electrical energy directly into a discrete color of light

Page 21: AVR  ATMega32 Elementary Input / Output

ATMega32 Elementary I/O: LEDs

20112012-I Module 8/21

• Never connect an LED directly to a battery or power supply! It will be destroyed almost instantly because too much current will pass through and burn it out.

• LEDs must have a resistor in series to limit the current to a safe value, for quick testing purposes a 1k resistor is suitable for most LEDs if your supply voltage is 12V or less.

• The resistor value, R is given by:• Estimate 1.5 - 2 V voltage drop (VL)• Typically draws 10-20 mA (I)

Unless you know what are you doing!

Page 22: AVR  ATMega32 Elementary Input / Output

ATMega32 Elementary I/O: LEDs

20112012-I Module 8/22

+5VR

output pin1

+3.2V

No current

Currentlight

Estimate R = voltagecurrent = 5 – 1.8 – 0.4

13 x 10 -3 = 215 Ω ~ 220 Ω easier to get

LED

no light+5V

+5VR

output pin0LED

+0.4V

Turning on an LED

Setting the pin to high will not turn ON the LED

Setting the pin to low will turn ON the LED

Page 23: AVR  ATMega32 Elementary Input / Output

ATMega32 Elementary I/O: LEDs

20112012-I Module 8/23

Page 24: AVR  ATMega32 Elementary Input / Output

ATMega32 Elementary I/O: LEDs

20112012-I Module 8/24

Page 25: AVR  ATMega32 Elementary Input / Output

ATMega32 Elementary I/O: LEDs

20112012-I Module 8/25

.include "M32def.inc"

.ORG 0LDI R20,high(RAMEND)OUT SPH, R20LDI R20,low(RAMEND)OUT SPL, R20LDI R20,0xFF;Set Port A as output

OUT DDRA, R20LDI R20, 0x00;r0 <-- 0

loop:INC R20;r0 <-- r0+1

OUT PORTA, R20 ;Port A <-R20

CALL delay1s ;delay

RJMP loop

delay1s:LDI R16,200

delay1s0:LDI R17,0xFF

delay1s1:DEC R17BRNE delay1s1DEC R16BRNE delay1s0

RET

Page 26: AVR  ATMega32 Elementary Input / Output

ATMega32 Elementary I/O: 7-Segment Display

20112012-I Module 8/26

There are applications where you need to display a decimal digit using a seven segment LED display.

The display could represent e.g. the number of times a switch was pressed.

Digits 0-9 and hex A-F can be displayed by giving the proper 7-segment codes

q g f e d c b a q g f e d c b a0 0 1 1 1 1 1 1 8 1 1 1 1 1 1 1

1 0 0 0 0 1 1 0 9 1 1 0 0 1 1 1

2 1 0 1 1 0 1 1 A 1 1 1 0 1 1 1

3 1 0 0 1 1 1 1 b 0 0 1 1 1 1 1

4 1 1 0 0 1 1 0 C 0 1 1 1 0 0 1

5 1 1 0 1 1 0 1 d 1 0 1 1 1 1 0

6 1 1 1 1 1 0 1 E 1 1 1 1 0 0 1

7 0 0 0 0 1 1 1 F 1 1 1 0 0 0 1

Page 27: AVR  ATMega32 Elementary Input / Output

ATMega32 Elementary I/O: 7-Segment Display

20112012-I Module 8/27

Common-anode : requires VCC LED ON when Output is LOW.

Common-cathode : NO VCC , LED ON when Output is HIGH.

TTL and CMOS devices are normally not used to drive the common-cathode display directly because of current (mA) requirement. A buffer circuit is used between the decoder chips and common-cathode display

Page 28: AVR  ATMega32 Elementary Input / Output

ATMega32 Elementary I/O: 7-Segment Display

20112012-I Module 8/28

Page 29: AVR  ATMega32 Elementary Input / Output

ATMega32 Elementary I/O: 7-Segment Display

20112012-I Module 8/29

A 7-Segment LED could be attached as shown:

Page 30: AVR  ATMega32 Elementary Input / Output

ATMega32 Elementary I/O: 7-Segment Display

20112012-I Module 8/30

Or:

Page 31: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Port: Example 6

20112012-I Module 8/31

A 7-segment is connected to PORTA. Display 1 on the 7-segment.

ATmega32

8PORTC

0

1

2

3

5 6

4

1 1 1 1 1 1 1 1

0 0 0 0 0 1 1 0

DDRC:

PORTC:

.INCLUDE “M32DEF.INC” LDI R20,0x06 ;R20 = 00000110 (binary) OUT PORTC,R20 ;PORTC = R20 LDI R20,0xFF ;R20 = 11111111 (binary) OUT DDRC,R20 ;DDRC = R20 ;Delay 1

;cycle latencyL1: RJMP L1

Page 32: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Port: Example 7

201120123232-I Module 8/32

Page 33: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Port: Example 8

20112012-I Module 8/33

Page 34: AVR  ATMega32 Elementary Input / Output

EXAMPLE 9 7SEGMENT USING ARRAY

20112012-II Module 9/34

Using the 7Segment connected in slide 29 or 30, write a program to display from 0 to 9 continuously.

.INCLUDE “M32DEF.INC”LDI R16,$FF ;initializeOUT DDRB,R16

SEMULA: LDI R31, HIGH (CORAK<<1) ;or use ZHLDI R30, LOW(CORAK<<1) ;or use ZLLDI R17,#10

ULANG: LPM R18,ZOUT PORTB,R18INC ZLDEC R17BRNE ULANGRJMP SEMULA

CORAK: .DB $3F, $06, $5B, $4F, $66, $6D, $7D, $03, $7F, $6F

Page 35: AVR  ATMega32 Elementary Input / Output

ATMega32 I/O Problems?

20112012-I Module 8/35

I/O’s are SCARCE Not enough time to buy µC with more I/O’s Required model not readily available Compatibility issues esp. using assembly language Or simply no budget Solution? Use MSI Devices

Page 36: AVR  ATMega32 Elementary Input / Output

MSI Devices

20112012-I Module 8/36

MUST read the datasheet for logic details Input

Buffer: 74LS541 PISO: 74LS165 Priority Encoder: 74LS148 (in tutorial only)

Output Latch: level trigger: 74LS373 Latch: edge trigger: 74LS574 SIPO: 74LS195

Both Input and Output Bi-directional buffer: 74LS245 Keypad Encoder: MM74C922

Page 37: AVR  ATMega32 Elementary Input / Output

MSI Devices - Buffer

20112012-I Module 8/37

Isolate shared data bus especially for input into µC Only one control pin, the rest for data: total 9 pins

PIN_0

GND

Page 38: AVR  ATMega32 Elementary Input / Output

MSI Devices - PISO

20112012-I Module 8/38

Parallel Input Serial Output, 74LS165 To save number of input pins into µC Two control pins, one data input into µC: total 3 pins

PIN_0

VCC/Gnd

PIN_1

PIN_2

Page 39: AVR  ATMega32 Elementary Input / Output

MSI Devices – Priority Encoder

20112012-I Module 8/39

To select the most important input first Opps… can only learn using Mr. Zulfakar Wise AVR board

and attend the tutorial Indigenous solution to replace expensive 74HC922 (RM6

compared to RM24)

Page 40: AVR  ATMega32 Elementary Input / Output

MSI Devices – Example 10 - Inputs

20112012-I Module 8/40

Increase number of input pins of µC: twice of ports

Port_C, PIN_0

(enable)

Port_B, PIN_7

VCC

Gnd Gnd

Port_C, PIN_1

(enable)

Port_B, PIN_0

Port_B, PIN_7

Port_B, PIN_0

VCC

Buffer 0 Buffer 1

Can be replaced with push buttons etc.

Page 41: AVR  ATMega32 Elementary Input / Output

MSI Devices – Example 10 - Inputs

Module 8/41

CBI PORTC,0 ;PORTC.0 = 0 (enable)SBI DDRC,5 ;DDRC.5 = 1 (output)IN R16, PORTBSBI PORTC,0 ;PORTC.0 = 1 (disable)

CBI PORTC,1 ;PORTC.0 = 0 (enable)SBI DDRC,5 ;DDRC.5 = 1 (output)IN R17, PORTBSBI PORTC,1 ;PORTC.1 = 1 (disable)

Enable Buffer 0

Read Buffer 0

Store Buffer 0

Enable Buffer 1

Read Buffer 1

Store Buffer 1

Page 42: AVR  ATMega32 Elementary Input / Output

MSI Devices – Latch level Trigger

20112012-I Module 8/42

To store data by level trigger Increase output pins, min. one control pin Output enable for bus sharing

Latch: level trigger: 74LS373

Page 43: AVR  ATMega32 Elementary Input / Output

MSI Devices – Latch Edge Trigger

20112012-I Module 8/43

To store data by edge trigger Increase output pins, min. one control pin Output enable for bus sharing

Latch: edge trigger: 74LS574 compatible with 74LS374 but different pins arrangement

PIN_0 (clk)

PIN_1 (enable)

Page 44: AVR  ATMega32 Elementary Input / Output

MSI Devices - SIPO

20112012-I Module 8/44

Serial Input Parallel Output 74LS195 To store data by edge trigger Increase output pins from µC:

use only two pins

PIN_1 (data out)

Gnd

VCCPIN_0 (CLK)

Page 45: AVR  ATMega32 Elementary Input / Output

MSI Devices – Example 11 - Outputs

20112012-I Module 8/45

Increase number of input pins of µC: twice of ports

Port_C, PIN_2

(clk)

Port_B, PIN_0

Port_B, PIN_7

No bus sharing, output can always enable

Latch 1Port_C,

PIN_3 (clk)

Gnd

Port_B, PIN_0

Port_B, PIN_7

Gnd

Gnd

Latch 0

Page 46: AVR  ATMega32 Elementary Input / Output

MSI Devices – Example 11 - Outputs

Module 8/46

LDI R18, 0xFFLD DDRB, R18

LDI R19, 0x89 ;Prepare data LEDOUT PORTB, R19 ;CBI PORTC,2 ;PORTC.2 = 0 (enable)SBI DDRC,2 ;DDRC.2 = 1 (output)SBI PORTC,2 ;PORTC.2 = 1 (disable)

LDI R20, 0xC3 ;Prepare data 7SegmentOUT PORTB, R20 ;CBI PORTC,3 ;PORTC.3 = 0 (enable)SBI DDRC,3 ;DDRC.3 = 1 (output)SBI PORTC,3 ;PORTC.3 = 1 (disable)

Prepare LED

Send to LED

Prepare 7Seg

Send to 7Seg

What is the pattern of the 7Segment?

Page 47: AVR  ATMega32 Elementary Input / Output

MSI Devices – Bi-directional Buffer

20112012-I Module 8/47

Can either be both Input or Output To control shared data bus

Bi-directional buffer: 74LS245

PIN_0 (direction)

PIN_1 (enable)

Page 48: AVR  ATMega32 Elementary Input / Output

MSI Devices – Keypad Encoder

20112012-I Module 8/48

MM74C922 Have both Input (push buttons) and Output (into µC):

total 6 pins Use to read keypads

PIN_1 (enable)

PIN_0interrupt

Page 49: AVR  ATMega32 Elementary Input / Output

MSI Devices – Keypad Encoder Truth Table

20112012-I Module 8/49

0 to 15 for MM74C922 0 to 19 for MM74C923

Page 50: AVR  ATMega32 Elementary Input / Output

MSI Devices – Example Keypads

20112012-I Module 8/50

Interrupt to PortC, Pin 4 Enable PortC, Pin 5 Data connected to PortB, pin 0 to 3

Upon interrupt pin 4 call, do:CBI PORTC,5 ;PORTC.5 = 0 (enable)SBI DDRC,5 ;DDRC.5 = 1 (output)IN R21, PortBSBI PORTC,5 ;PORTC.5 = 1 (disable)LDI R22, 0x0FAND R21,R22 ; filter unwanted data pin B4 to B7

Page 51: AVR  ATMega32 Elementary Input / Output

ASSIGNMENT

20112012-I Module 8/51

1. Enter 4 keys using keypads:a) Display every entry at 7Segmentb) Press ‘#’ as enter key or end of entry

2. If password is correct, display pass 7Segment and turn off all red LED3. If password is wrong, display fail at 7Segment and blink 1 red LED