01 GPIO||General Purpose Input Output.2016
-
Upload
mohamed-fawzy -
Category
Engineering
-
view
56 -
download
4
Transcript of 01 GPIO||General Purpose Input Output.2016
![Page 1: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/1.jpg)
1
01-General Purpose Input/ Output
By : Mohamed Fawzy
Programming AVR Microcontrollers
© Mohamed F.A.B 2015
![Page 2: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/2.jpg)
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.
![Page 3: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/3.jpg)
3
Don't Forget !!!!
© Mohamed F.A.B 2015
Any Expert Was Once A Beginner
![Page 4: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/4.jpg)
4
Agenda.
Lesson (4):
ATMEGA32 Overview and Main Hardware
Connections.
Lesson (5):
GPIO (General Purpose Input Output) Module.
© Mohamed F.A.B 2015
![Page 5: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/5.jpg)
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.
![Page 6: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/6.jpg)
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
![Page 7: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/7.jpg)
7
ATMEL Code Decoder (New).
© Mohamed F.A.B 2015
AT-MEGA-32A-Px
ATMEL
Family
Flash Size
New Generation
PU:PDIPAU:SMD
![Page 8: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/8.jpg)
8
Main Hardware Connections.
© Mohamed F.A.B 2015
RESET.
Power Pins (VCC,GND).
FOSC (XTAL1,XTAL2).
![Page 9: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/9.jpg)
9
Lesson (5)
© Mohamed F.A.B 2015
Lesson (5):
GPIO (General Purpose Input Output) Module.
![Page 10: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/10.jpg)
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.
![Page 11: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/11.jpg)
11
ATMEGA32 GPIO.
© Mohamed F.A.B 2015
o ATMEGA32 has 32 GPIO Pin.
o ATMEGA32 has PORTs (PORTA,PORTB,PORTC,PORTD).
![Page 12: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/12.jpg)
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.
![Page 13: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/13.jpg)
13
I/O Equivalent Schematic.
© Mohamed F.A.B 2015
![Page 14: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/14.jpg)
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.
![Page 15: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/15.jpg)
15
Example (PORTA)-Datasheet Page 66
© Mohamed F.A.B 2015
![Page 16: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/16.jpg)
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.
![Page 17: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/17.jpg)
17
Interfacing LEDs with µC
© Mohamed F.A.B 2015
![Page 18: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/18.jpg)
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.
![Page 19: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/19.jpg)
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).
![Page 20: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/20.jpg)
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
![Page 21: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/21.jpg)
21
Interfacing Buttons with µC
© Mohamed F.A.B 2015
![Page 22: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/22.jpg)
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
![Page 23: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/23.jpg)
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
![Page 24: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/24.jpg)
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.
![Page 25: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/25.jpg)
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.
![Page 26: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/26.jpg)
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
![Page 27: 01 GPIO||General Purpose Input Output.2016](https://reader034.fdocuments.us/reader034/viewer/2022042618/58a336481a28ab9b6d8b596d/html5/thumbnails/27.jpg)
27
Questions:
© Mohamed F.A.B 2015