0% found this document useful (0 votes)
4 views51 pages

3.IO Ports

Embedded_C

Uploaded by

yhya Tarek
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)
4 views51 pages

3.IO Ports

Embedded_C

Uploaded by

yhya Tarek
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/ 51

AVR Microcontrollers - I/O PORTS

by:
Eng. Mohamed Tarek.

© 2014 by Eng. Mohamed Tarek. 1


I/O PORTS – AVR Microcontroller

 Agenda
• I/O Ports.
• I/O Ports Programming.
• Interfacing with Switches and Leds.
• Interfacing with 7-Segment.
• Interfacing with DC-Motor.
• Interfacing with Keypad.
• Interfacing with LCD.

© 2014 by Eng. Mohamed Tarek. 2


I/O Ports

© 2014 by Eng. Mohamed Tarek. 3


I/O Ports(Cont.)

© 2014 by Eng. Mohamed Tarek. 4


I/O Ports(Cont.)

 ATmega16 has 32 programmable I/O lines divided


into 4 PORTS(groups):
1. PORTA(PA7……..PAO)
2. PORTB(PB7….…..PBO)
3. PORTC(PC7….…..PCO)
4. PORTD(PD7……..PDO)
 Each PORT is controlled by 3 registers:
1. DDRx (Data Direction Register)
2. PORTx (Output Register)
3. PINx (Input Register)

Note: Most pins in µC make more than one


function(multiplexed functions)

© 2014 by Eng. Mohamed Tarek. 5


I/O Ports(Cont.)

© 2014 by Eng. Mohamed Tarek. 6


I/O Ports Programming

1. Decided which input and which output .

2. Configure the port direction use register DDRX

• 1 for Output.

• 0 for Input.

3. In Read(input case) : Use register PINx.

4. In Write(output case) : Use register PORTx.

© 2014 by Eng. Mohamed Tarek. 7


I/O Ports Programming

 Note in case you set any PIN as input you can activate
the internal pull up resistor by setting the corresponding
bit in PORTX register.

 Example
To set the pin 2 in PORTB as input pin and use the
internal pull up resistor of this pin.

DDRB = DDRB & (~(1<<PB2));


PORTB= PORTB | (1<<PB2);

© 2014 by Eng. Mohamed Tarek. 8


I/O Ports Programming(Cont.)

 How to set values in registers

• DDRA=5; /*(decimal)mean I activate pin 0 and


pin 2 as output and the rest as
input pins */

• DDRB=0x14; /*(hexadecimal)mean I activate pin


2 and pin 4 as output and the rest
as input pins */

• DDRC=0b00000011; /*(binary)mean I activate pin 0 and


pin 1 as output pins and the rest
as input pins */

© 2014 by Eng. Mohamed Tarek. 9


I/O Ports Programming(Cont.)

 How to set a specific pin with conserving other pins


• To set specified pin with 1:
Make OR operation on the register with The pin no
Example: We want to set pin number 5 in PORTA with 1
PORTA=PORTA | (1<<PA5);
• To set specified pin with 0:
Make AND operation on the register with (NOT) The pin no
Example: We want to set pin number 3 in PORTB with 0
PORTB=PORTB & ( ~(1<<PB3) );
• To toggle specified pin:
Make XOR operation on the register with The pin no
Example: We want to toggle pin number 2 in PORTC
PORTC=PORTC ^ (1<<PC2);

© 2014 by Eng. Mohamed Tarek. 10


I/O Ports Programming(Cont.)

© 2014 by Eng. Mohamed Tarek. 11


I/O Port applications

 As Output
• LED and 7-Segemnt
• LCD display
• Motors.
• Buzzer.
• PC Serial Port.
 As Input
• Switches(push button, keypad etc.)
• Analog/Digital sensors.
• Signal from another µC.
• PC Serial Port.

© 2014 by Eng. Mohamed Tarek. 12


Interfacing with Switches

 Typical Switch Configuration

Pull Down Resistor Pull UP Resistor

R = 10K

© 2014 by Eng. Mohamed Tarek. 13


Interfacing with Leds

 Typical LED Configuration

Negative Logic Positive Logic

R = 220 : 350

© 2014 by Eng. Mohamed Tarek. 14


Exercise 1

 Write Embedded C code using ATmega16 µC to


control a led.
 Requirements:
• Configure the µC control with internal 8Mhz Clock.
• The led is connected to pin 3 in PORTD.
• Connect the Led using Negative Logic configuration.
• Flash the led every 1 second.

© 2014 by Eng. Mohamed Tarek. 15


Exercise 2

 Write Embedded C code using ATmega16 µC to


control a led using a push button.
 Requirements:
• Configure the µC control with internal 1Mhz Clock.
• The switch 1 & 2 are connected to pin 0 & 1 in PORTB.
• Connect both switches using Pull Down configuration.
• The Led 1 & 2 is connected to pin 0 & 1 in PORTC.
• Connect both leds using Positive Logic configuration.
• If switch1 is pressed just turn on the first led1 only and if switch2
is pressed just turn on led2 only.
• In case no switches are pressed both leds are off.

© 2014 by Eng. Mohamed Tarek. 16


Switch de-bounce problem

 Could be done in software or hardware.

 It relies on the fact that bouncing takes a maximum period of


20-30 ms.

 The basic idea is to implement a delay after the first detected edge,
during which no scanning for the switch is done. after the delay
period is finished, scanning can proceed.

 This way the bouncing can be avoided.

© 2014 by Eng. Mohamed Tarek. 17


Exercise 3

 Write Embedded C code using ATmega16 µC to


control a led using a push button.
 Requirements:
• Configure the µC control with internal 1Mhz Clock.
• The switch is connected to pin 0 in PORTB.
• Connect the switch using Internal Pull Up configuration.
• The Led is connected to pin 0 in PORTC.
• Connect the Led using Negative Logic configuration.
• If the switch is pressed just toggle the led.

© 2014 by Eng. Mohamed Tarek. 18


Interfacing with 7-Segment

Common Cathode

© 2014 by Eng. Mohamed Tarek. 19


Interfacing with 7-Segment(Cont.)

© 2014 by Eng. Mohamed Tarek. 20


Interfacing with 7-Segment(Cont.)

 Common Anode with 7447-Decoder

© 2014 by Eng. Mohamed Tarek. 21


Interfacing with 7-Segment(Cont.)

 The following table shows the required


decoder input to display the numbers from
0 to 9.

© 2014 by Eng. Mohamed Tarek. 22


Exercise 4

 Write Embedded C code using ATmega16 µC to


control a 7-segment using a push button.
 Requirements:
• Configure the µC control with internal 1Mhz Clock.
• The switch is connected to pin 4 in PORTD.
• Connect the switches using Pull Down configuration.
• The 7-segment is connected to first 4-pins of PORTC.
• If the switch is pressed just increase the number appeared in the
7 segment display, and if we reach the maximum number (9)
overflow occurs.

© 2014 by Eng. Mohamed Tarek. 23


Challenge 1

 Write Embedded c code using ATmega16 µC to


control a 7-segment using two push buttons.
 Requirements:
• Configure the µC control with internal 1Mhz Clock.
• The two switches are connected to pin 0 & 1 in PORTA.
• Connect both switches using Pull Down configuration.
• The 7-segment is connected to first 4-pins of PORTD.
• If the switch1 is pressed just increase the number appeared in
the 7 segment display, and if the number reach the maximum
number (9) do nothing.
• If the switch2 is pressed just decrease the number appeared in
the 7 segment display, and if the number reach the minimum
number (0) do nothing.

© 2014 by Eng. Mohamed Tarek. 24


DC-Motor

© 2014 by Eng. Mohamed Tarek. 25


DC-Motor
 This chip is designed to control 2 DC
motors.
• First DC Motor three µC pins are
needed to control the DC motor
(Input1, Input2, Enable1) and the
motor connected to Output1 &
Output2 pins.
• Second DC Motor three µC pins are
needed to control the DC motor
(Input3, Input4, Enable2) and the
motor connected to Output3 &
Output4 pins.
• Vss pin is connected to 5v.
• Vs pin is connected to the Motor
voltage 12v.

© 2014 by Eng. Mohamed Tarek. 26


DC-Motor

© 2014 by Eng. Mohamed Tarek. 27


DC-Motor

 How to control the Motor using the L293D chip?

© 2014 by Eng. Mohamed Tarek. 28


Exercise 5

 Write Embedded C code using ATmega16 µC to


control a DC-Motor using three push buttons.
 Requirements:
• Configure the µC control with internal 1Mhz Clock.
• The three switches are connected to pin 0 & 1 & 2 in
PORTA.
• Connect switches using Pull Down configuration.
• IN1 & IN2 of the L293 chip are connected to first two
pins in PORTC.
• If the first switch is pressed motor should rotate clock
wise.
• If the first switch is pressed motor should rotate anti-
clock wise.
• If the first switch is pressed motor should stop.
© 2014 by Eng. Mohamed Tarek. 29
Interfacing with Keypad

 Many application requires large number of keys


connected to a computing system. Example includes a
PC keyboard, Cell Phone keypad and Calculators. If we
connect a single key to MCU, we just connect it directly
to i/o line. But we cannot connect, say 10 or 100 keys
directly MCUs i/o. Because It will eat up precious i/o line.
 Keypad is most widely used input device to provide input
from the outside world to the microcontroller. The keypad
makes an application more users interactive.
 Keypad is widely used with user interface
applications.

© 2014 by Eng. Mohamed Tarek. 30


Interfacing with Keypad

 We want to avoid all these troubles so we use some


clever technique. The technique is called multiplexed
matrix keypad. In this technique keys are connected in a
matrix (row/column) style.

© 2014 by Eng. Mohamed Tarek. 31


Interfacing with Keypad

 The rows R0 to R3 are connected to Input lines of


Microcontroller. The i/o pins where they are connected
are made Input. This is done by setting the proper DDR
Register in AVR. The column C0 to C3 are also
connected to MCUs i/o line. These are kept at High
Impedance State in high z state (z= impedance) state
these pins are neither HIGH or LOW they are in
TRISTATE. And in their PORT value we set them all as
low, so as soon as we change their DDR bit to 1 they
become output with value LOW.
 One by One we make each Column LOW (from high Z
state) and read state of R0 to R3. other columns still high
impedance.

© 2014 by Eng. Mohamed Tarek. 32


Interfacing with Keypad

 C0 is made LOW while all other Columns are in HIGH Z State. The
Value of R0 to R3 is read to get their pressed status. Since the
internal pull-ups have been enabled, if the keys are in the high state,
the buttons are “NOT PRESSED”. These pull-ups keep their value
high when they are floating (that means NOT connected to
anything). But as soon as the key is pressed it gets connected to
LOW line from the column thus making it LOW.

© 2014 by Eng. Mohamed Tarek. 33


Interfacing with Keypad

 After that we make the C0 High Z again and make C1


LOW. And read R0 to R3 again. This gives us status of
the second column of keys. Similarly all columns are
scanned.

© 2014 by Eng. Mohamed Tarek. 34


Keypad Software Driver

Implement Keypad Software Driver


Keypad.c and Keypad.h
contains:

1. KeyPad_GetPressedKey()
2. KeyPad_4x3_AdjustSwitchNumber()
3. KeyPad_4x4_AdjustSwitchNumber()

© 2014 by Eng. Mohamed Tarek. 35


Exercise 6

 Write Embedded c code using ATmega16 µC to


control a 7-segemtn using keypad.
 Requirements:
• Configure the µC control with internal 1Mhz Clock.
• 4x3 Keypad is connected at PORTA.
• Use Internal Pull Up Resistors with Keypad.
• 7-segemtn is connected at PORTC.
• Get number from keypad and display it on 7-segment.
• Try to make the same using 4x4 Keypad.

© 2014 by Eng. Mohamed Tarek. 36


Character LCD

 LCD stands for Liquid Crystal Display. It can be used


to display anything. They are of many types. The ones
we commonly use for embedded systems, robotics, etc.
are of two types character LCD and graphical LCD.
 It is often used in battery - powered electronic devices
because it requires very small amounts of electric power.
 LCDs have become a cheap and easy
way to get text display for an
embedded system.

© 2014 by Eng. Mohamed Tarek. 37


Character LCD

 Common displays are set up as 16 to 20 characters by 1


to 4 lines.
 The most commonly used ALPHANUMERIC displays
are 1x16 (Single Line & 16 characters), 2x16 (Double
Line & 16 character per line) & 4x20 (four lines & Twenty
characters per line).
 The model described here is for its low price and great
capabilities most frequently used in practice. It is based
on the HD44780 microcontroller (Hitachi) and can
display messages in two lines with 16 characters each. It
can display all the letters of alphabet, Greek letters,
punctuation marks, mathematical symbols etc. It is also
possible to display symbols made up by the user. Other
useful features include automatic message shift (left and
right), cursor appearance, LED backlight etc.
© 2014 by Eng. Mohamed Tarek. 38
Character LCD

© 2014 by Eng. Mohamed Tarek. 39


Character LCD

 A 5X8 dots display consists of a matrix of lights


or mechanical indicators arranged in a
rectangular configuration.
 By switching on or off selected lights, text or
graphics can be displayed.

© 2014 by Eng. Mohamed Tarek. 40


Character LCD

 LCD Pins

© 2014 by Eng. Mohamed Tarek. 41


Character LCD

 8 data pins D7:D0


Bi-directional data/command pins. Alphanumeric characters are sent
in ASCII format.

 RS (Register Select)
RS = 0  Command Register is selected
RS = 1  Data Register is selected

 R/W (Read or Write)


0  Write
1  Read

 E (Enable data)
Used to enable the LCD display.

 V0 (contrast control)
For maximum constrains connect it to ground.

© 2014 by Eng. Mohamed Tarek. 42


Character LCD

 LCD Connections

© 2014 by Eng. Mohamed Tarek. 43


Character LCD

 Display Data RAM(DDRAM) Addresses


DDRAM memory inside LCD is used for storing
characters to be displayed.

© 2014 by Eng. Mohamed Tarek. 44


Character LCD

 CGROM Memory
Character Generator ROM memory contains a standard character
map with all characters that can be displayed on the screen. Each
character is assigned to one memory location.

© 2014 by Eng. Mohamed Tarek. 45


Character LCD

 LCD Commands

© 2014 by Eng. Mohamed Tarek. 46


LCD Software Driver

Implement LCD SW Module


LCD.c and LCD.h
contains:
1. LCD_ Init()
2. LCD_SendCommand()
3. LCD_DisplayCharacter()
4. LCD_DisplayString()
5. LCD_DisplayString_XY()
6. LCD_GoTo_XY()
7. LCD_ClearScreen()
8. LCD_IntgerToString()

© 2014 by Eng. Mohamed Tarek. 47


Exercise 7

 Write Embedded c code using ATmega16 µC to


control 2x16 LCD Display.
 Requirements:
• Configure the µC control with internal 1Mhz Clock.
• Connect the LCD Data Path to PORTC.
• Connect the “RS” LCD pin to PD4.
• Connect the “R/W” LCD pin to PD5.
• Connect the “E” LCD pin to PD6.
• Display String message on the LCD Display.

© 2014 by Eng. Mohamed Tarek. 48


Challenge 2

 Write Embedded c code using ATmega16 µC to


control 4x16 LCD Display using 4x4 Keypad.
 Requirements:
• Configure the µC control with internal 1Mhz Clock.
• Connect the LCD Data Path to PORTC.
• Connect the “RS” LCD pin to PD4.
• Connect the “R/W” LCD pin to PD5.
• Connect the “E” LCD pin to PD6.
• 4x4 Keypad is connected at PORTA.
• If the push button kept pressed, Every 500ms should be treated
as a new press.
• Display the pressed keypad button on the LCD Display.

© 2014 by Eng. Mohamed Tarek. 49


© 2014 by Eng. Mohamed Tarek. 50
Contact Details
Eng. Mohamed Tarek.
Embedded Software Engineer
Tel: 01115154316
mtarek.2013@gmail.com

© 2014 by Eng. Mohamed Tarek. 51

You might also like