Date post: | 14-Feb-2017 |
Category: |
Engineering |
Upload: | mohamed-fawzy |
View: | 57 times |
Download: | 4 times |
1
01-General Purpose Input/ Output
By : Mohamed Fawzy
Programming AVR Microcontrollers
© Mohamed F.A.B 2015
Lecture Notes:
2
© Mohamed F.A.B 2015
o Set Your Phone To Vibration Mode.
o Ask any time.
o During labs, Feel Free To Check Any Materials or
Internet.
o Slides are self content.
o Feel Free To Share This Materials With Your Friends.
o Work Hard For Achieving Most Benefits Of This Course.
3
Don't Forget !!!!
© Mohamed F.A.B 2015
Any Expert Was Once A Beginner
4
Agenda.
Lesson (4):
ATMEGA32 Overview and Main Hardware
Connections.
Lesson (5):
GPIO (General Purpose Input Output) Module.
© Mohamed F.A.B 2015
5
Lesson (4) Topics.
© Mohamed F.A.B 2015
Lesson (4):
ATMEGA32 Overview and Main Hardware Connections.
► Take a look in Datasheet.
► ATMEL Code Decoding.
► Main Hardware Connection.
6
ATMEL Code Decoder (OLD).
© Mohamed F.A.B 2015
AT-MEGA-8L-8PI
ATMEL
Family
Flash Size
Low Power(2.7V)
FOSC
PDIP
I:industerialC:comertialM:martial
7
ATMEL Code Decoder (New).
© Mohamed F.A.B 2015
AT-MEGA-32A-Px
ATMEL
Family
Flash Size
New Generation
PU:PDIPAU:SMD
8
Main Hardware Connections.
© Mohamed F.A.B 2015
RESET.
Power Pins (VCC,GND).
FOSC (XTAL1,XTAL2).
9
Lesson (5)
© Mohamed F.A.B 2015
Lesson (5):
GPIO (General Purpose Input Output) Module.
10
Lesson (5) Topics.
© Mohamed F.A.B 2015
Lesson (5):
GPIO (General Purpose Input Output) Module.
► GPIO in ATMEGA32.
► Control GPIO in ATMEGA32.
► Interfacing LED with MC.
► Interfacing Switch with MC.
► Project (1) Password.
► Project(2) The Game.
11
ATMEGA32 GPIO.
© Mohamed F.A.B 2015
o ATMEGA32 has 32 GPIO Pin.
o ATMEGA32 has PORTs (PORTA,PORTB,PORTC,PORTD).
12
GPIO Characteristics in ATMEGA32.
© Mohamed F.A.B 2015
o General Purpose Input Output (GPIO).
o Bi-directional I/O s with internal Pull Up
Resistors.
o Push Pull Outputs ( Source or Sink for current).
o GPIOs are Grouped Into Ports Of Up to 8 Pins.
o Each IO Pin Has Two Protection Diodes.
13
I/O Equivalent Schematic.
© Mohamed F.A.B 2015
14
Control GPIO in ATMEGA32.
© Mohamed F.A.B 2015
o DDRx To define if PIN or PORT is Input or Output.
o PINxTo receive output from out world.
o PORTxTo set value to be out from MC.
15
Example (PORTA)-Datasheet Page 66
© Mohamed F.A.B 2015
16
Accessing I/O ports in CodeVision.
© Mohamed F.A.B 2015
Controlling All PORT Pins:
Controlling Single Pin:
DDRC=0xFF;
//configure all PORTC as output (Hexadecimal Format).
DDRC=0b11111111;
//configure all PORTC as output (Binary Format).
DDRC=255;
//configure all PORTC as output (Decimal Format).
PORTC=0xF0;
PORTC=0b11110000;
PORTC=240;
DDRC.0=1;
//configure PORTC0 pin as output.
DDRC.1=0;
//configure PORTC1 pin as input.
PORTC.0=1;
//outing logic 1 (5V) on PORTC0 Pin.
17
Interfacing LEDs with µC
© Mohamed F.A.B 2015
18
LED (Light Emitting Diode) .
© Mohamed F.A.B 2015
o LED stands for Light Emitting Diode.
o LED has a polarity.(Anode & Cathode).
o LED need almost 10mA and 3V to work.
19
Source & Sink Current.
© Mohamed F.A.B 2015
Source Current. Sink Current
NOTES:
Source Current is better.
At high current:
Source: MC will hung up.
Sink: MC will be damaged.
See Datasheet page 297 (Electrical Characteristics).
20
Interfacing LED with µC.
© Mohamed F.A.B 2015
Now, We need to design a circuit of
interfacing LED with µC.
I led=10mA.
Vled= 3V.
V mc= 5V.
𝑅 =5−3
10𝑚𝐴=
2
10𝑚𝐴=200Ω.
Typical R Value=220Ω or 330 Ω.
We need to connect LED to PORTC0 and blinking it every 500 ms.
Exercise (1).
Don't forget to include delay library header file.#include <delay.h>
NOTE
21
Interfacing Buttons with µC
© Mohamed F.A.B 2015
22
Pull Up and Pull Down Resistors.
© Mohamed F.A.B 2015
Pull Up Resistor. Pull Down Resistor.
Why use Pull Up and Pull Down.
Why use R1?
R1 between 4.7K Ω and 10K Ω.
Why R1 10K Ω.
ATMEGA32 Has an Internal Pull Up Resistor.
NOTE
23
Switch bounce and de-bounce.
© Mohamed F.A.B 2015
o Switch De-Bounce
Software Solution.
Hardware Solution (RC Filter and schmitt Trigger).
We need to connect LED to PORTC0 and control it
using switch.
Exercise (2).
Predicted
True
24
Switch Key Click Vs. Key Press.
© Mohamed F.A.B 2015
EX:If (sw is pressed)
{
delay_ms (250); //debounce delay & press wait
//statements
}
o Key Click: A Function For This Button Doesn't Be Executed Until You
Release Your Hand From The Button.
EX:If (sw is pressed)
{
delay_ms (10); //debounce delay
//statements
while (sw is pressed); //still press do nothing
}
Key Press: A Function Of This Key will be executed as soon as you press the
Button.
25
Projects.
© Mohamed F.A.B 2015
We need to connect LED on PORTC0 and three
buttons (PORTC1,PORTC2,PORTC3)
When I press button1 one time,button2 two times
and button3 three times, LED on PORTC0 turn ON
for 0.5 second.
Project (1) Password.
We need to connect 8LEDs on PORTC , 8LEDs on
PORTB and two buttons (PORTA0 and PORTA1)
[the two players].
Every press on button1 (PORTA0) increment PORTC by 1 and Every press on button2 (PORTA1)
increment PORTB by 1 and first one reach 255 is the winner.
Project (2) The Game.
26
Motivation.
© Mohamed F.A.B 2015
We need to connect 5 LEDs to PORTD0 and Flashing it.
Exercise (3).
Let's Know what is Wrong Next Lecture.
NOTE
27
Questions:
© Mohamed F.A.B 2015