0% found this document useful (0 votes)
107 views93 pages

Topic 10 - AVR IO in C (ISMAIL - FKEUTM 2018)

This document discusses AVR input/output interfaces. It describes various real-world input and output devices that can be connected to an AVR microcontroller. It then explains how the AVR uses I/O ports, port registers (DDRx, PINx, PORTx), and pin configuration to interface with these external devices. Examples are given to configure pin directions as input or output and to write/read data from the ports. Pull-up resistors and code samples for toggling port pins are also covered at a high level.

Uploaded by

Aya Amir
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)
107 views93 pages

Topic 10 - AVR IO in C (ISMAIL - FKEUTM 2018)

This document discusses AVR input/output interfaces. It describes various real-world input and output devices that can be connected to an AVR microcontroller. It then explains how the AVR uses I/O ports, port registers (DDRx, PINx, PORTx), and pin configuration to interface with these external devices. Examples are given to configure pin directions as input or output and to write/read data from the ports. Pull-up resistors and code samples for toggling port pins are also covered at a high level.

Uploaded by

Aya Amir
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/ 93

Topic 10:

AVR Input/Output
Interface with C
ISMAIL ARIFFIN
FKE UTM
CONTENTS
Real World I/O
AVR I/O Interface
Elementary I/O Interface
LED Control
MSI devices
Real World Input/Output
o Real world devices for sending and receiving data in electronic
system.
o Input devices – data provider – sense heat, pressure, image: Switch,
Keypad, Keyboard, Microphone,, Sensor (Temperature, Gas. Water
Level) , Transducer, Scanner, Camera, Rain Gauge )
o Output devices – data controller – beacon signal, flare light, siren
sound, run motor, display text : Light Emitting Diode (LED), Liquid
Crystal Display (LCD), Lamps (Home, Street) Electrical Motor ,
Actuator (Rotational TV Aerial ), Video monitor, Speaker, Printer
o Real world external devices need an interface circuit to
interconnect with the AVR microcontroller.
o I/O interface scheme:
o Elementary I/O: Simple two-state devices such as LED and switch.
o Parallel I/O: Data exchanged one byte at a time.
o Serial I/O: Data exchanged one bit at a time.
Real World
Air
Interface to AVR
Water Gas
Soil
Sensors Valves
LDR Real world Sprinklers
IR Environment
CCD

Input AVR Output


transducers Microcontroller transducers

Switches LED
Keypad LCD
RFID, Human Light
Keyboard Alarm
Buzzer
DC Motor
AVR I/O Interface Features
(I/O Memory – I/O Task)
AVR Interface Pins
FORTY (40) interface pins:
Power Supply (4)
- Vcc, Avcc, Gnd (2)
Reset (1) – Active low
 Aref (ADC volt ref ) (1)
External Clock (2)
Xtal2, Xtal1
Dual function pins - shared
1. I/O (32) = PORT (4)
2. ADC (8), External
Interrupts (3), Serial, Timers
ATMega32 Supply & Reset
o The pins marked ‘Gnd’ are to
be grounded.
o ‘Vcc & AVcc’ are to be given
5V.
o 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 Digital I/O
• Real world I/O output pin
– External Input device external
• External Data Source input pin devices
– External Output device
• External Data Destination micro-
• ATmega32 provides FOUR 8-bit digital IO ports controller
or 32 I/O channels:
– PORT A, 8-channels
– PORT B, 8-channels
– PORT C, 8-channels and Port A
– PORT D, 8 channels
Port B
Each port has 8 data pins.
Every port is bi-directional, can
become input or output Port C
Port D
Each of the 8 pins can be individually
configured as input or output.
Digital I/O in ATmega32
─ PORT REGISTER
For each port, there are three relevant
8-bit registers.
Data Direction Register (DDRx)
Input Pins Address (PINx)
Data Register (PORTx)
Here, x denotes A, B, C or D.
AVR I/O Port Registers = I/O Memory
Digital I/O in ATmega32
PORT DATA DIRECTION DECLARATION
Data Direction Register (DDRx) is used to configure a specific port pin as
output (1) or input (0).

 Example: To set Port A pins 0 to 3 for input, pins 4 to 7 for output, we write
C code
DDRA = 0b11110000; // configure pins

bit 7 6 5 4 3 2 1 0
DDRA 1 1 1 1 0 0 0 0

for output for input


Digital I/O in ATmega32
─ Read/Write Port
Register Data Register (PORTx) is used to write output data to port.
 Example: To write a binary 0 to output pin 6, binary 1 to other pins of Port
A, we write C code
PORTA = 0b10111111; // write output

Register Input Pins Address (PINx) is used to read input data from port.
 Example: To read the input pins of Port A, we write C code
unsigned char temp; // temporary variable
temp = PINA; // read input

• Where do the C names PINA, PORTA, DDRA come from?


// extract from header file <avr/iom32> …
#define PINA _SFR_IO8(0x19)
#define DDRA _SFR_IO8(0x1A)
#define PORTA _SFR_IO8(0x1B)…
AVR header file
• To access all AVR microcontroller registers, your program must include
the header file <io.h>, which is found in the WinAVR folder.
#include <avr/io.h>

Depending on the device selected in your project, file ‘io.h’ will automatically
redirect to a specific header file.

Example
 For ATmega32, the specific header file is ‘avr/iom32.h’.
 This header file is printed in Appendix A of the lab notes.
 It lists the C names and addresses for all ATmega32 registers.
 We always use the C names in our code.
Configuring AVR I/O Pin

Declare Port’s I/O pin direction


Declare the content of Data Direction Register (DDR)
bit
- DDR bit = 0 -> I/O pin = input
- DDR bit = 1 -> I/O pin = output
AVR I/O Port Configuration
o How to make the I/O ports as input or output?
o Ans: Initialize DDRx register by writing AVR I/O initialise program

o To make a port an output, write 1s to the DDRx register.


o To make a port an input, write 0s to the DDRx register

o Example: Make port B as output, thus write 0b11111111 into


DDRB register to make all of the pins output.
DDRB=0xFF; // make all pins at PORTB as outputs

o Example: Make port B as output, thus write 0b00000000 into


DDRB register to make all of the pins input.
DDRB=0xFF; // make all pins at PORTB as inputs
AVR I/O Pin Configuration/Direction
o Declare the direction of input/output pins:
o DDRx n-bit (in the DDRx Register) selects the direction of this
pin:
o DDRx-n =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).
o DDRx-n=0, Portx nth is configured as an input pin.
Note: AVR I/O is pin configurable
o Example: Make pin7-4 of Port C as input while the rest pins
(pin 3-0) as output.
DDRC=0b00001111
o Example: Make odd pins (pin 7,5,3,1) of Port D as input while
the even pins (pin 6,4,2,0) as output.
o DDRD=0b01010101
I/O Port Registers: Configure I/O Program
o The AVR C code below shows how to configure the pins on
portA, portB and portC

#include <avr/io.h>

int main (void) {


DDRA=0xFF ; // Configure PortA as an Output port
DDRB= 0x00 ; // Configure PortB as an Input port
DDRC= 0xF0 ; //Configure the first four lower pins on PortC
//as input and the others as output
}
I/O Port Registers: Pull-Up Resistor
o There is a pull-up resistor, for
each of the AVR pins.
o This pull-up resistor is useful to
reduce noise. Since, input of
AVR are generally n Hi state,
thus make them prone to
catching noise and picking up
false signals.
o To activate pull-up resistor;
put 1s into bits of the PORTx
register.
Make AVR pin high
o To deactivate pull-up resistor;
put 0s into bits of the PORTx Make AVR pin low
register.
I/O Port Registers: Pull-Up Resistor
o Example of codes without pull-up resistor.

#include <avr/io.h>

Int main () {
DDRB=0x00 ;
DDRC=0xFF ;

volatile unsigned char temp, sum ;

while (1) {
temp = PINB ; // read data from port B
sum = temp + 5 ;
PORTC = sum ; // send data to port C
}
. return 1 ;
}
I/O Port Registers: Pull-Up Resistor
o Example of codes with pull-up resistor is activated for port C.
#include <avr/io.h>

Int main () {
DDRB=0x00 ; // make port B as input
DDRC=0xFF ; // make port C as output
PORTB = 0xFF ; // pull-up resistors of PORTB

volatile unsigned char temp, result ;

while (1) {
temp = PINB ; // read input from port B
result = temp + 5 ; // add input to 5
PORTC = result ; // send result to port C
}
return 1 ;
}
I/O Port Registers: Example I/O Program
o The following code will toggle all 8 bits of Port B forever with some time delay
between “on” and “off” states:

#define F_CPU 1000000 // CPU frequency 1 MHz


#include <avr/io.h> // io memory
#include <util/delay.h> // delay

int main (void) {


DDRB=0xFF ; // make Port B an output port
PORTB=0xFF ; // pull-up resistors

while (1) {
PORTB=0xFF ;put 0xFF on port B pins
_delay_ms(50) ; // delay 50ms
PORTB=0x00 ;put 0x00 on port B pins
_delay_ms(50) ; // delay 50ms
}
return 1;
}
I/O Port Registers: Example I/O Program
o Write a program that toggles PORTA.4 continuously.
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
volatile unsigned char temp ;
DDRA= (1<<PORTA4) ; // Porta.4 as output
temp = (1<<PORTA4) ; // initial LED display

while (1) {
PORTA = temp ; // PORTA4 LED display
_delay_ms(75)
temp = ~temp ; // toggel LED
}
return 1 ;
}
I/O Port Registers: Example I/O Program
o 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.
volatile unsigned char temp ;

int main () {
DDRB = ~ (1 << PINB2); // PINB2 as input
DDRC = 0xFF ; // PORTC = output PB2
DDRD =(1<<PORTD3) ; PD3 as output
while {
temp = PINB |(1<< PINB2) ;
if (temp == (1<<PINB2)) { PD3 PortC
PORTC = 0x45 ;
PORTD = (1<<PORTD3) ; // high PD3
_delay_ms(75) ;
PORTD =~ (1<<PORTD3); //low PD3
}
else
break;
}
return 1 ;
}
Elementary I/O Interface
Elementary I/O: Introduction
o Elementary I/O is a two-state peripheral devices which allowing
to a state condition. e.g. bulb, the bulb can only either light On or
light Off.
o In AVR, it can involve more than one bit at a time, i.e BCD
o Requirements:
o Bit signals can be written to output devices via AVR program
o Bit signals can be read from input devices via AVR program
o Support devices:
o Latches such as 74LS374 for output.
o Tri-state buffers such as 74LS244 for input.
o Output devices:
o LED, bulbs, 7-segment display, iquid crsytal display (LCD), relay coils.
o Input devices:
o Push/toggle/DIP switched, Proximity switch, Rotary BCD coder,
Matrix keypad, Temperature sensor, Light Dependent Resistor (LDR)
Elementary Output: LED
o LED is a semiconductor device that
converts electrical energy directly into
a discrete colour of light.
o LED has two terminals and must be
connected correctly.
o Anode (+) – long lead and has shorter
end inside the led.
o Cathode (-) - short lead, has larger end
inside the led and sometimes slight flat
on the body of round led.
o LED is easily damaged by heat.
o It has variety of colors depending by
the semiconductor material used.
8-bit Parallel LED panel
Active High Panel (Common Cathode) Active Low Panel (Common Anode)
RGB LED
Wiring RGB LED
Active Low Device (Common Anode)
Active High Device(Common Cathode)
Elementary Output: LEDs
o 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.
o 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.
Elementary Output: LEDs
o The resistor value, R is given by:

R = (VS – VL) / I

o Estimate 1.5 - 2 V voltage drop (VL)


o Typically draws 10-20 mA (I)
Elementary Output: LED
Turning on an LED
no light No current
+5V +5V output pin
R 1
LED
Setting the pin to high will
not turn ON the LED
light
Current
+3.2V +0.4V
+5V output pin
R 0
LED
Setting the pin to low will
turn ON the LED
voltage 5 – 1.8 – 0.4
Estimate R = = = 215 Ω ~ 220 Ω
current 13 x 10 -3 easier to get
Elementary Output: LED connection type
Elementary Output: Common Anode Parallel
LED at PortB
Elementary Output: Common Anode Parallel
LED at PortA

LSB

PORTA
MSB

AVR interfaces to the common anode parallel LEDs


Elementary Ouput : LED program at port C
o Write an AVR program to perform an 8-bit binary count up and
show the results of the binary counting up by displaying at the 8-
leds connected to port C of AVR. Time interval between binary
counting display is about 1 sec. Choose led connection type
#define F_CPU 1000000 // CPU frequency 1 MHz
#include <avr/io.h> // io memory
#include <util/delay.h> // delay

int main (void) {


DDRC=0xFF ; // Port C as an output

volatile unsigned char temp = 0xFF ; // initial common anode LEDS display

while (1) {
PORTC=temp ; // display LED
_delay_ms(1000) ; // hold LED for 1s
--temp : // next count-up display
}
return 1;
}
Elementary Output : Exercises
o Write a FLASH LED program to on and off all the common anode
eight LED at port C in the interval of 1 second.
#define F_CPU 1000000 // CPU frequency 1 MHz
#include <avr/io.h> // io memory
#include <util/delay.h> // delay

int main (void) {


DDRC=0xFF ; // Port C as an output
volatile unsigned char temp = 0xFF; // initail common anode LEDS display

while (1) {
PORTC=temp ; // LED display
_delay_ms(1000) ; // hold on 1s
temp = ~temp : // toggle
}
return 1;
} Project: Milestone 1
Elementary Output: 7-Segment Display
o Two types of common in 7
segment display:
o Common-anode - requires VCC,
LED ON when Output is LOW.
o Common-cathode - NOVCC, LED
ON when Output is HIGH.
o Note: 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.
Seven Segment
Elementary Output: 7-Segment Display
o There are applications where you need to display a
decimal digit using a seven segment LED display.
o The display could represent e.g. the number of
times a switch was pressed. dp
o Digits 0-9 and hex A-F can be displayed by giving
the proper 7-segment codes
dp G f e d c b a 0x q g f e d c b a 0x
0 0 1 1 1 1 1 1 3f 8 1 1 1 1 1 1 1 7f
1 0 0 0 0 1 1 0 06 9 1 1 0 0 1 1 1 67
2 1 0 1 1 0 1 1 5b A 1 1 1 0 1 1 1 77
3 1 0 0 1 1 1 1 4f b 0 0 1 1 1 1 1
4 1 1 0 0 1 1 0 66 C 0 1 1 1 0 0 1
5 1 1 0 1 1 0 1 6d d 1 0 1 1 1 1 0
6 1 1 1 1 1 0 1 7d E 1 1 1 1 0 0 1
7 0 0 0 0 1 1 1 09 F 1 1 1 0 0 0 1

Common Cathode 7-segment display code table


Elementary Output: 7-Segment Display Vcc

o There are applications where you need to display a


decimal digit using a seven segment LED display.
o The display could represent e.g. the number of
times a switch was pressed.
o Digits 0-9 and hex A-F can be displayed by giving dp
the proper 7-segment codes
q g f e d c b a 0x q g f e d c b a 0x
0 1 0 0 0 0 0 0 C0 8 0 0 0 0 0 0 0 00
1 1 1 1 1 0 0 1 F9 9 0 0 1 ! 0 0 0 98
2 0 1 0 0 1 0 0 A4 A 0 0 0 1 0 0 0 08
3 0 1 1 0 0 0 0 B0 B 1 1 0 0 0 0 0 E0
4 0 0 1 1 0 0 1 99 C 1 0 0 0 1 1 0
5 0 0 1 0 0 1 0 92 D 0 1 0 0 0 0 1
6 0 0 0 0 0 1 0 02 E 0 0 0 0 1 1 0
7 1 1 1 1 0 0 0 F8 F 0 0 0 1 1 1 0

Common Anode 7-segment display code table


Elementary Output: 7-Segment Display
o The 7 segment display could be attached to AVR as below:

A common cathode 7-segment LED


Elementary Output: 7-Segment Display
Example
o Example: A common cathode 7-
segment is connected to PORTC.
Write a code that display 1 on the
7-segment. ATmega32
0
5 1
8 6
PORTC
4 2

3
#include <avr/io.h>

int main () [
DDRC=0xFF; //set portC as output
PORTC= 0xFF; // pull-up resistor of PORTC
while (1) {
PORTC=0x06 ; // segment=digit character 1
}
return 1;
}
Elementary Output: 7-Segment Display
Example
o Example: A common cathode 7-
segment is connected to PORTA.
Write a code that display 3 on the 7-
segment.

#include <avr/io.h>
Elementary Output: 7-Segment Display Using Array
o Example: Using the 7-segment display connected to PORT B, write C code to
display number 0 to 9 continuously in interval of 1 second. .
#define F_CPU 1000000
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>

unsigned const char digit[10]


PROGMEM={$3F,$06,$5B,$4F,$66,$6D,$7D,$03,$7F,&6F};

int main(){
unsigned char tempdata,i;
DDRB=0xFF ; //set portB as output
PORTB=0xFF ; //pul-up resistor
ATmega32
while (1) { 0
i=0 ;
8
5 6 1
do{ PORTB
tempdata= pgm_read_byte_near(&digit[i]); 4 2
PORTB=tempdata; 3
_delay_ms(1000); //1 sec
i++;
} while (i<=9) ;
} Project: Milestone 2
return 1;
}
Elementary Output: Two 7-Segment Displays
● What happens if you need to drive two 7-segment
LEDs connected to Port B, but don’t have enough
ports?
─ Assume you do have one more output pin
available
o Port C, bit 0
o Assume it has been configured as an output
o The following circuit would work …..
Elementary Output: Two 7-Segment Displays
● Your program must output the code for LED 1 on Port B and
then select LED 1 by placing a 0 on PC0
● Then your program must output the code for LED 2 on Port B
and then select LED 2 by placing a 1 on PC0
─ You must then continue to alternately light the two LEDs

CC
4-Digit 7-Segment LED Display
4-Digit 7-Segment LED Display Wiring
Segment (dp, g, f, e, d, b, c, b and a

Select Lines (T4, T3, T2 and T1


How to Display at a 4-digit 7-segment
LED Display
• The display of each digit is multiplexed (selecting one digit at a time from
DS1 to DS4 and rolls over back to 1.
• The digit is selected by enabling the common input of the Digit.
• Then data is sent to the segment .
• After waiting enough to for the LED to laminates, the next digit is selected
and then the data is sent to the segment.
• You display multiple digits at once by rapidly cycling through them and it
will look that each segment is displayed at the same time.
• To allow the cycling through is continuously, a timer interrupt service
routine that is called repetitively can be utilised to display the multiplexed
digit of a data taken from a global variable (environment variable) after
being processed to determine the segment data for each digit.
• At any position in the program execution, the display data can be sent to
the global variable, which will be taken and processed to be displayed to
the multiplexed 4-digit 7-segment.
Elementary Input: Switch Wiring
Atmega32
has internal
pull-up
resistor
which
implements
this circuit
Elementary Input: Switch
o Example: A switch is connected to
pin PB0 and an LED to PB7. write a
program to get the status of the
switch and display it at the LED.
.include <avr/io.h>

int main() {
DDRB = ~(1<<PORTB0) | (1<<PORTB7) ;// PB0input,PB7output
PORTB = (1<<PORTB0) ; // Pull-up PB0
volatile unsigned temp:

while (1) {
temp = PINB ; // read PB0

if (temp == ~(1<<PORTB0))
PORTB = 1 << PORTB7 ; // PB7 LED is ON
else
break;
}
return 1;
}
Elementary Input: Parallel Switches
o Example: Parallel switches are connected
at pin PA0 to PA7. write a program to get +5V
AVR 1K
the status of the switches and display
PA0
them at the common anode LEDs
PA1
connected to Port C.
PA2
#include <avr/io.h>
PA3
int main(){ PA4
DDRA=0x00 ;//make PORTA as input PA5
PORTA=0xFF ;//pull-up PORTA
DDRC=0xFF ;//make PORTC as output PA6
PA7
volatile unsigned char temp;

while (1) {
temp=PINA ;read switch data

PORTC=temp; //Led display data


}
return 1; Project: Milestone 2
}
ATMega32 I/O Problems
o Below are a few problems that related to ATMega32 I/O
o I/O are scarce.
o Not enough time to buy Micro-P with more I/Os.
o Required model not readily available.
o Compatibility issues. Especially using assembly language.
o Or simply no budget.
o Solution? – use MSI devices
MSI Devices
MSI Devices
o MSI is medium scale integrated circuit is a logic circuit that
contains 12 to 99 logic gates in a circuit.
o Example of MSI such mux, decoder, adder, latch, flip-flop, etc.
o The MSI can be divided into three categories:
o Input : Buffer (74LS541), PISO (74LS165), Priority Encoder (74LS148).
o Output: Latch (level trigger – 74LS373), latch (edge trigger –
74LS574), SIPO (74LS195).
o Both I/O: Bi-directional buffer (74LS245), Keypad Encoder
(MM74C922)
MSI Devices: Buffer
o Buffer is used to
temporarily store
data while it is being
move from one place
to another.
o Isolate shared data
bus especially for GND
input into µC
o Only one control pin,
the rest for data:
total 9 pins
o Used to increase PIN_0
number of input pins
in micro-p.
MSI Devices: Buffer
o Example: increase number of input to port B from 8 to 16.

Port_B, Port_B, Port_B, Port_B,


PIN_7 PIN_0 PIN_7 PIN_0
VCC

VCC

Buffer 0 Buffer 1

Gnd Gnd

Port_C, Port_C, Can be replaced


PIN_0 PIN_1 with push buttons
(enable) (enable) etc.
MSI Devices: Buffer
o Example: increase number of input to port B from 8 to 16.
#include <avr/io.h> Enable Buffer 0

Read Buffer 0

Store Buffer 0

Enable Buffer 1

Read Buffer 1

Store Buffer 1
MSI Devices: PISO
o PISO is parallel input serial output, 74LS165.
o It used to save number of input pins into µC.
o Has two control pins, one data input into µC: total 3 pins

PIN_2
VCC/Gnd

PIN_0
PIN_1
MSI Devices: Latch level trigger
o To store data by level trigger.
o It used to increase number of output pins.
o Has one control pin.
MSI Devices: Latch edge trigger
o To store data by edge trigger.
o It used to increase number of output pins.
o Has one control pin.

PIN_0
(clk)

PIN_1
(enable)
MSI Devices: SIPO
Gnd
PIN_0 (CLK)
VCC
PIN_1 (data out)

o SIPO is serial input parallel output,


74LS195.
o It used to store data by edge trigger.
o Increase number of output pins
from micro-p.
o Has two control pins.
MSI Devices: Output
o Example: increase number of output pins of port B from 8 to 16.
Port_B, Port_B, Port_B, Port_B,
PIN_0 PIN_7 PIN_0 PIN_7

Port_C,
Port_C, Latch 0 PIN_3 (clk) Latch 1
PIN_2 (clk)

Gnd Gnd

Gnd
No bus sharing, output can always enable
MSI Devices: Output
o Example: increase number of output pins of port B from 8 to 16.
#include (avr/io.h>

Prepare LED

Send to LED

Prepare 7Seg

Send to 7Seg
MSI Devices: Bi-directional Buffer
o Can either be input or output.
o It used to control shared data bus.
o Eg. 74LS245.
PIN_1 (enable)

PIN_0 (direction)
Matrix Keypad
Switch Input Arrangement
• Switch Input Arrangement depends on type application
of input needed.
• It one bit input to multiple bits (maximum of 8 per
group)
• Data from the switch may be read directly, or as a
certain protocol (example the matrix keyboard).
• If input data is read at a 8-bit word size, and the data
from the switch is not a full 8 bits (because the switch
arrangement is not a full 8-bit, caution must be taken
when reading the data from the 8 bit input such that
the bits that is not connected to switch are mask off
(not taken as part of data).
MSI Devices: Keypad Encoder
o Have both inputs (push-button) and output (to micro-p).
o It used to read keypads.
o Eg. MM74C922.
interrupt
PIN_0
PIN_1
(enable)
MSI Devices: Keypad Encoder

 0 to 15 for MM74C922
 0 to 19 for MM74C923
MSI Devices: Keypad Encoder
o Example: write a program in order to input the data from the
keypad. In the program you should:
o Interrupt to Port C, Pin4
o Enable Port C, Pin5
o Data connected to Port B, Pin 0-3

// Upon interrupt pin 4 call, do:


Matrix Keypad Connections
How to read Key for Matrix Keypad
• Scanning a Matrix Keypad
– We make the columns as i/p and we drive the rows making them o/p, this
whole procedure of reading the keyboard is called scanning.
– At each scan, the row selected (is set LOW), the column data is read.
– The data on the column bits that is LOW indicates that it is pressed which can
be identified by value of the 4 bit column data (after masking off the
remaining non-column data).
– If the first scan starts with row 0 and assuming only one button switch is
pressed at a time, the column reading can return the data for switch SW1,
SW2, SW3 or SW4.
– At the next row2 scan, the column reading can return the data for switch
SW5, SW6, SW7 or SW8.
– At the next row3 scan, the column reading can return the data for switch
SW9, SW10, SW11 or SW12.
– At the next row4 scan, the column reading can return the data for switch
SW5, SW6, SW7 or SW3.
– Scan then repeat the cycle from row 1.
How to read Key for Matrix Keypad
• This is how the scanning is done.
– So to scan the keypad completely, we need to make rows low one by
one and read the columns.
– If any of the button is pressed in a row, it will take the corresponding
column to a low state which tells us that a key is pressed in that row.
– If button 1 of a row is pressed then Column 1 will become low, if
button 2 then column2 and so on...
A Hex Key Pad Connections to the
ATmega 32A chip
• The following Figure shows the Hex Key Pad Connections to
Port B.
• As an example R1 is Row 1 and C1 is column 1 and
connected to PB0 and PB4 of Port B respectively.
• So R4 is Row 4 and C4 is column 4 and connected to PB3
and PB7 of Port B respectively.
• The following Figure shows the Hex Key Pad Connections to
Port B. As an example R1 is Row 1 and C1 is column 1 and
connected to PB0 and PB4 of Port B respectively.
• So R4 is Row 4 and C4 is column 4 and connected to PB3
and PB7 of Port B respectively.
• The key re renumbered such that the first key start with
“0”.
Hex keypad connections to Port B
PB0 PB1 PB2 PB3 PB4 PB5 PB6 PB7

0 1 2 3

4 5 6 7

8 9 A B

C D E F
Function ReadKeyPad() -1
int ReadKeyPad(void)
{
int keyPressed;
unsigned char upperNibble, keyCode, i;
upperNibble = 0xff;
for(i=0; i<4; i++)
{
_delay_ms(1);
KB_PORT_OUT = ~(0x01 << i);
_delay_ms(1); //delay for port o/p settling
upperNibble = KB_PORT_IN | 0x0f;
Function ReadKeyPad () -2
if (upperNibble != 0xff)
{
_delay_ms(20); //key debouncing delay
upperNibble = KB_PORT_IN | 0x0f;
if(upperNibble == 0xff) goto OUT;
keyCode = (upperNibble & 0xf0) | (0x0f & ~(0x01 << i));
while (upperNibble != 0xff)
upperNibble = KB_PORT_IN | 0x0f;
_delay_ms(20); //key debouncing delay
Function ReadKeyPad () -3
switch (keyCode) //generating key characetr to display on LCD
{
case (0xee): keyPressed = 0;
break;
case (0xde): keyPressed = 1;
break;
case (0xbe): keyPressed = 2;
break;
case (0x7e): keyPressed = 3;
break;
case (0xed): keyPressed = 4;
break;
Function ReadKeyPad () -4
case (0xdd): keyPressed = 5;
break;
case (0xbd): keyPressed = 6;
break;
case (0x7d): keyPressed = 7;
break;
case (0xeb): keyPressed = 8;
break;
case (0xdb): keyPressed = 9;
break;
case (0xbb): keyPressed = 0xA;
break;
Function ReadKeyPad () -5
case (0x7b): keyPressed = 0xB;
break;
case (0xe7): keyPressed = 0xC;
break;
case (0xd7): keyPressed = 0xD;
break;
case (0xb7): keyPressed = 0xE;
break;
case (0x77): keyPressed = 0xF;
break;
default : keyPressed = 255;
}//end of switch
OUT:;
}//end of if
}//end of for
return (keyPressed);
}
LCD panel RT204-1
A Full Wiring of LCD panel RT204-1
LCD conections: PortA --> LCD Data lines,
PD7 --> EN, PD6 --> RW, PD5 --> RS

Figure 1
Programming LCD
• Refer to LCD Commands.pdf for list of
commands.
• To write a command you have to do the
following steps:
1. Set LCD in command mode
2. Load data to port
3. Write data to LCD
4. Wait 2 clock cycle
5. Disable LCD
6. wait for 1ms
Function LCD_WriteCommand()
• The following Write a command instruction to the LCD based on LCD
Commands.pdf whose connection is as given in Figure 1. This function is
the used to write command to LCD for other function of the Lcd.
void LCD_WriteCommand (unsigned char Command)
{
PORTD &= ~0x20;// Set PORTD
LCD in &=
command
~0x80 mode
PORTA = Command;// Load data to port
PORTD |= 0x80; // Enable Write data to LCD
asm("nop"); //Wait 2 clock cycles
asm("nop");
PORTD &= ~0x80; // Disable Write data LCD
_delay_ms(1);// wait for 1ms
}
Function LCD_WriteDaTa()
• This function is the used to write one byte of data to LCD after the
command is selected. Some command may not need data, but some does.
For example the data of the character to print after selecting a command to
print character at LCD.
void LCD_WriteData (unsigned char Data)
{
PORTD |= 0x20 ;// Set LCD in data mode
PORTA = data ;// Load data to port
PORTD |= 0x80; // Enable Write data to LCD
asm("nop"); //Wait 2 clock cycles
asm("nop");
PORTD &= ~0x80; // Disable Write data LCD
_delay_ms(1);// wait for 1ms
}
Function LCD_init()
• Before you use the LCD you must initialize it as follows:
void LCD_init(void)
{
_delay_ms(100); // wait for 100ms
LCD_WriteCommand (0x38); // 8 data lines
LCD_WriteCommand (0x08); // display off
LCD_WriteCommand (0x01); // clear LCD memory
/*Clears all display and returns the cursor to
the home position (Address 0).*/
_delay_ms (10); // 10ms delay after clearing LCD
LCD_WriteCommand (0x06); // cursor setting
LCD_WriteCommand (0x0f); // display ON
}
Display ON/OFF
RS R/W DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0
=== === === === === === === === === ===
0 0 0 0 0 0 1 D C B
Command that Controls display of characters and cursor.
Details
D: The display is ON when D = 1 and OFF when D = 0.
C: The cursor is displayed when C = 1 and is not displayed when C = 0.
B: The character at the cursor position blinks when B = 1.
Functions to position cursor at LCD
• Position the LCD cursor at "row", "column"
void LCD_Cursor (char row, char column)
{
switch (row)
{
case 1: LCD_WriteCommand (0x80 + column - 1); break;
case 2: LCD_WriteCommand (0xc0 + column - 1); break;
default: break;
}
}

• Position the LCD cursor at "row 0 ", "column 0"


void LCD_CursorHome(void)
{
LCD_WriteCommand (0x02);
}
Functions Display String at LCD
• Display a string at the specified row and column
void LCD_DisplayString (char row, char column, char
*string)
{
LCD_Cursor (row, column);
while (*string)
LCD_WriteData(*string++);
}
• Display a string at the current row and column
void LCD_WriteString (char *string)
{
while (*string)
LCD_WriteData(*string++);
}
AVR I/O PROGRAM EXERCISE
ATMEGA32 CIRCUIT FOR LED
BLINKING
Exercise:
Write a LED
blinking
program in
AVR assembly
or C
languages
BCD to 7 Segment Decoder

Exercise:
Write an AVR
program to display
digit 0 until 9 at the
common cathode 7-
segment as shown
in the given circuit
SUMMARY
Learn Elementary I/O devices

Learn AVR PORT & Programming features

Learn AVR and Elementary I/O Interface

Learn AVR’s I/O Application & Programming

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy