Presentation on theme: "AVR ATMega32 Elementary Input / Output Module 9. ATMega32 Input / Output 20112012-IModule 8/2 One of the basic and essential features designed in a computer."— Presentation transcript:
AVR ATMega32 Elementary Input / Output Module 9
ATMega32 Input / Output IModule 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.
Microprocessor I/O Interfacing 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 Interrupts Module 8/3
Microprocessor I/O Interfacing IModule 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 space o 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 loactions o Disadvantages: Take memory addressing space
ATMega32 I/O Ports IModule 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.
ATMega32 I/O Port Registers IModule 8/6 There are three registers associated with Input/ Output Ports in AVR.
ATMega32 I/O Port Registers IModule 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
ATMega32 I/O Port Registers IModule 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)
ATMega32 I/O Pull UP/DOWN Resistors IModule 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).
ATMega32 I/O Pull UP Resistor IModule 8/10
ATMega32 I/O Port: Example IModule 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 0b in R16 OUT DDRA, R16 ; Configure PortA as an Output port LDI R16, 0x00 ; Load 0b in R16 OUT DDRB, R16 ; Configure PortB as an Input port LDI R16, 0xF0 ; Load 0b in R16 OUT DDRC, R16 ; Configure first four pins on PortC ; as input and the others as output
ATMega32 I/O Port: Example IModule 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 = 0b OUT DDRB,R16 ;make Port B an output port ( ) L1: LDI R16,0x55 ;R16 = 0x55 = 0b OUT PORTB,R16 ;put 0x55 on port B pins CALL DELAY LDI R16,0xAA ;R16 = 0xAA = 0b OUT PORTB,R16 ;put 0xAA on port B pins CALL DELAY RJMP L1
ATMega32 I/O Port: Example IModule 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" LDIR16,0x00 ;R16 = (binary) OUTDDRC,R16 ;make Port C an input port LDIR16,0xFF ;R16 = (binary) OUTDDRB,R16 ;make Port B an output port(1 for Out) L2:INR16,PINC ;read data from Port C and put in R16 LDIR17,5 ADD R16,R17 ;add 5 to it OUTPORTB,R16 ;send it to Port B RJMPL2 ;continue forever
ATMega32 I/O Port: SBI & CBI Instructions IModule 8/14 SBI (Set Bit in IO register) – SBI ioReg, bit ;ioReg.bit = 1 – Examples: SBIPORTD,0;PORTD.0 = 1 SBIDDRC,5;DDRC.5 = 1 CBI (Clear Bit in IO register) – CBI ioReg, bit ;ioReg.bit = 0 – Examples: CBIPORTD,0;PORTD.0 = 0 CBIDDRC,5;DDRC.5 = 0
ATMega32 I/O Port: Example IModule 8/15 Write a program that toggles PORTA.4 continuously..INCLUDE “M32DEF.INC” SBI DDRA,4 L1: SBI PORTA,4 CBI PORTA,4 RJMP L1
ATMega32 I/O Port: SBIC & SBIS Instructions IModule 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=0 INC R20 LDI 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=1 INC R20 LDI R19,0x23
ATMega32 I/O Port: Example IModule 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 output AGAIN: 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
ATmega32 Elementary I/O I Module 8/18
ATMega32 Elementary I/O IModule 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
ATMega32 Elementary I/O: LEDs IModule 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
ATMega32 Elementary I/O: LEDs IModule 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 V voltage drop ( V L ) Typically draws mA ( I ) Unless you know what are you doing!
ATMega32 Elementary I/O: LEDs IModule 8/22 +5V R output pin V No current Current light Estimate R = voltage current = 5 – 1.8 – x = 215 Ω ~ 220 Ω easier to get LED no light +5V R output pin 0 LED +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
ATMega32 Elementary I/O: LEDs IModule 8/23
ATMega32 Elementary I/O: LEDs IModule 8/24
ATMega32 Elementary I/O: LEDs IModule 8/25.include "M32def.inc".ORG0 LDI R20,high(RAMEND) OUT SPH, R20 LDI R20,low(RAMEND) OUT SPL, R20 LDI R20,0xFF; Set Port A as output OUTDDRA, R20 LDI R20, 0x00 ;r0 <-- 0 loop: INCR20 ;r0 <-- r0+1 OUT PORTA, R20 ;Port A <-R20 CALL delay1s ;delay RJMPloop delay1s: LDIR16,200 delay1s0: LDIR17,0xFF delay1s1: DECR17 BRNEdelay1s1 DECR16 BRNEdelay1s0 RET
ATMega32 Elementary I/O: 7-Segment Display IModule 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 qgfedcbaqgfedcba A b C d E F
ATMega32 Elementary I/O: 7-Segment Display IModule 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
ATMega32 I/O Port: Example IModule 8/31 A 7-segment is connected to PORTA. Display 1 on the 7-segment. ATmega32 8 PORTC DDRC : PORTC:.INCLUDE “M32DEF.INC” LDI R20,0x06 ;R20 = (binary) OUT PORTC,R20 ;PORTC = R20 LDI R20,0xFF ;R20 = (binary) OUT DDRC,R20 ;DDRC = R20 ;Delay 1 ;cycle latency L1: RJMP L1
ATMega32 I/O Port: Example IModule 8/32
ATMega32 I/O Port: Example IModule 8/33
EXAMPLE 9 7SEGMENT USING ARRAY IIModule 9/34 Using the 7Segment connected in slide 29 or 30, write a program to display from 0 to 9 continuously..INCLUDE “M32DEF.INC” LDIR16,$FF;initialize OUTDDRB,R16 SEMULA:LDIR31, HIGH (CORAK<<1);or use ZH LDIR30, LOW(CORAK<<1) ;or use ZL LDIR17,#10 ULANG:LPMR18,Z OUTPORTB,R18 INCZL DECR17 BRNEULANG RJMPSEMULA CORAK:.DB$3F, $06, $5B, $4F, $66, $6D, $7D, $03, $7F, $6F
ATMega32 I/O Problems? IModule 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
MSI Devices IModule 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
MSI Devices - Buffer IModule 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
MSI Devices - PISO IModule 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
MSI Devices – Priority Encoder IModule 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)
MSI Devices – Example 10 - Inputs IModule 8/40 Increase number of input pins of µC: twice of ports Port_C, PIN_0 (enable) Port_B, PIN_7 VCC 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.
MSI Devices – Latch level Trigger IModule 8/42 To store data by level trigger Increase output pins, min. one control pin Output enable for bus sharing Latch: level trigger: 74LS373
MSI Devices – Latch Edge Trigger IModule 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)
MSI Devices - SIPO IModule 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 VCC PIN_0 (CLK)
MSI Devices – Example 11 - Outputs IModule 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 1 Port_C, PIN_3 (clk) Gnd Port_B, PIN_0 Port_B, PIN_7 Gnd Latch 0
MSI Devices – Example 11 - Outputs Module 8/46 LDIR18, 0xFF LDDDRB, R18 LDIR19, 0x89;Prepare data LED OUTPORTB, R19; CBIPORTC,2;PORTC.2 = 0 (enable) SBIDDRC,2;DDRC.2 = 1 (output) SBIPORTC,2;PORTC.2 = 1 (disable) LDIR20, 0xC3;Prepare data 7Segment OUTPORTB, R20; CBIPORTC,3;PORTC.3 = 0 (enable) SBIDDRC,3;DDRC.3 = 1 (output) SBIPORTC,3;PORTC.3 = 1 (disable) Prepare LED Send to LED Prepare 7Seg Send to 7Seg What is the pattern of the 7Segment?
MSI Devices – Bi-directional Buffer IModule 8/47 Can either be both Input or Output To control shared data bus Bi-directional buffer: 74LS245 PIN_0 (direction) PIN_1 (enable)
MSI Devices – Keypad Encoder IModule 8/48 MM74C922 Have both Input (push buttons) and Output (into µC): total 6 pins Use to read keypads PIN_1 (enable) PIN_0 interrupt
MSI Devices – Keypad Encoder Truth Table IModule 8/49 0 to 15 for MM74C922 0 to 19 for MM74C923
MSI Devices – Example Keypads IModule 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: CBIPORTC,5;PORTC.5 = 0 (enable) SBIDDRC,5;DDRC.5 = 1 (output) IN R21, PortB SBIPORTC,5;PORTC.5 = 1 (disable) LDIR22, 0x0F ANDR21,R22; filter unwanted data pin B4 to B7
ASSIGNMENT IModule 8/51 1.Enter 4 keys using keypads: a)Display every entry at 7Segment b)Press ‘#’ as enter key or end of entry 2.If password is correct, display pass 7Segment and turn off all red LED 3.If password is wrong, display fail at 7Segment and blink 1 red LED