Download Input/Output ports ATmega32

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Mains electricity wikipedia , lookup

Voltage optimisation wikipedia , lookup

Control system wikipedia , lookup

Variable-frequency drive wikipedia , lookup

Resistive opto-isolator wikipedia , lookup

Current source wikipedia , lookup

Rectifier wikipedia , lookup

Scattering parameters wikipedia , lookup

Flip-flop (electronics) wikipedia , lookup

Voltage regulator wikipedia , lookup

Integrating ADC wikipedia , lookup

Power electronics wikipedia , lookup

Power dividers and directional couplers wikipedia , lookup

Buck converter wikipedia , lookup

Schmitt trigger wikipedia , lookup

Two-port network wikipedia , lookup

Switched-mode power supply wikipedia , lookup

Immunity-aware programming wikipedia , lookup

Opto-isolator wikipedia , lookup

Transcript
2015-10-17
Input/Output ports
ATmega32
Erasmus 2015/2016, WIEiK, PK
Microcontroller
Power Supply
clock
fx
(Central Proccesor Unit)
Hardware
Interrupts
system
IRQ
Internal address bus
Internal data bus
Internal control bus
Reset
CPU
Program memory
ROM
Data memory
RAM
Basic Input/Output
Devices
•Parallel Port
•Serial Port
•Counters
General purpose
Input/output ports
Microcontroller – all components of computer system in one an integrated circuit (chip)
Erasmus 2015/2016, WIEiK, PK
1
2015-10-17
Input/output devices
• In computing, input/output or I/O is the communication between an computer system
and the outside world, possibly a human or another information processing system.
• Inputs are the signals or data received by the system, and outputs are the signals or data
sent from it.
• The term can also be used as part of an action; to "perform I/O" is to perform an input
or output operation.
• I/O devices are used by a person (or other system) to communicate with a computer.
• For instance, a keyboard or a mouse may be an input device for a computer, while LED
diodes, displays , monitors and printers are considered output devices for a computer.
• All microntrollers contain I/O ports to allow data to be read from switches, sensors etc.
and to write to LED’s, LCD displays, motors etc.
Erasmus 2015/2016, WIEiK, PK
Input/Ouput ports in ATmega32
• The ATmega32 has four 8-bit I/O ports named PORTA, PORTB,
PORTC and PORTD, which are all general purpose and most
have dual functions.
• The PORTA can be used for reading analogue quantities via
the A/D converter.
• When we are going to use input/output ports, we have to set
up these ports as either inputs or outputs prior to sending or
receiving data.
• Each line (pin) of port can be set up separately (individually).
• If a logic ‘1’ is entered into the data direction register switch A
will be enabled and will be set up as an output port.
• If a logic ‘0’ is entered into the data direction register switch B
will be enabled and will be set up as an input port.
Erasmus 2015/2016, WIEiK, PK
2
2015-10-17
Port C
Port A
Block
diagram
ATMEGA32
32 general purpose
programmable I/O lines
(four 8-bit ports)
Port B
Port D
Erasmus 2015/2016, WIEiK, PK
Input/Output
Peripheral Features
• 32 general purpose Programmable I/O Lines (4
ports)
• Two 8-bit Timer/Counters with Separate Prescalers
and Compare Modes
• One 16-bit Timer/Counter with Separate Prescaler,
Compare Mode, and Capture Mode
• Real Time Counter with Separate Oscillator
• Four PWM Channels
• 8-channel, 10-bit ADC
• 8 Single-ended Channels
– 7 Differential Channels in TQFP Package Only
– 2 Differential Channels with Programmable Gain at 1x, 10x, or
200x
•
•
•
•
Byte-oriented Two-wire Serial Interface
Programmable Serial USART
Master/Slave SPI Serial Interface
Programmable Watchdog Timer with Separate Onchip Oscillator
• On-chip Analog Comparator
Erasmus 2015/2016, WIEiK, PK
3
2015-10-17
Port Operation Registers
The following registers are related to the various port
operations that we can perform with the GPIO (General
Purpose Input Outout) pins.
– DDRx – Data Direction Register
– PORTx – Pin Output Register
– PINx – Pin Input Register
where x = GPIO port name (A, B, C or D)
Data Direction Register
Pin Output Register
Pin Input Register
Erasmus 2015/2016, WIEiK, PK
Configuration ports and pins (lines)
•
•
Each port pin consists of three register bits: DDxn, PORTxn, and PINxn.
The DDxn bit in the DDRx Register selects the direction of this pin.
– If DDxn is written logic one, Pxn is configured as an output pin.
– If DDxn is written logic zero, Pxn is configured as an input pin.
– If PORTxn is written logic one when the pin is configured as an input pin, the pull-up resistor is
activated.
– To switch the pull-up resistor off, PORTxn has to be written logic zero or the pin has to be
configured as an output pin.
•
•
•
•
The port pins are tri-stated when a reset condition becomes active, even if no
clocks are running.
If PORTxn is written logic one when the pin is configured as an output pin, the port
pin is driven high (one).
If PORTxn is written logic zero when the pin is configured as an output pin, the
port pin is driven low (zero).
Each line (pin) of port can be set up individually (independent)..
Erasmus 2015/2016, WIEiK, PK
4
2015-10-17
Ports features:
• All AVR ports have true Read-Modify-Write
functionality when used as general digital I/O
ports.
• This means that the direction of one port pin can
be changed without unintentionally changing the
direction of any other pin.
• The same applies when changing drive value (if
configured as output) or enabling/disabling of
pull-up resistors (if configured as input).
• Each output buffer has symmetrical drive
characteristics with both high sink and source
capability.
• The pin driver is strong enough to drive LED
displays directly.
• All port pins have individually selectable pull-up
resistors with a supply-voltage invariant
resistance.
• All I/O pins have protection diodes to both VCC
and Ground
• Most port pins are multiplexed with alternate
functions for the peripheral features on the
device.
Erasmus 2015/2016, WIEiK, PK
Ports in
40-pin PDIP Packages
Basic configuration
R3
10k
1
2
3
4
5
6
7
8
PB0/T0
PB1/T1
PB2/AIN0
PB3/AIN1
PB4/SS
PB5/MOSI
PB6/MISO
PB7/SCK
XTAL2
PB0
PB1
PB2
PB3
PB4
PB5
PB6
PB7
XTAL1
U1
Vcc
C2
PA0/ADC0
PA1/ADC1
PA2/ADC2
PA3/ADC3
PA4/ADC4
PA5/ADC5
PA6/ADC6
PA7/ADC7
AVREF
RESET
/RESET
9
S8
RESET
AGND
1uF/10V
PD0/RXD
PD1/TXD
PD2/INT0
PD3/INT1
PD4/OC1B
PD5/OC1A
PD6/ICP
PD7/OC2
+
ATMEGA32
GND
14
15
16
17
18
19
20
21
PC0/SCL
PC1/SDA
PC2/TCK
PC3/TMS
PC4/TDO
PC5/TDI
PC6/TOSC1
PC7/TOSC2
PA0
PA1
PA2
PA3
PA4
PA5
PA6
PA7
40
39
38
37
36
35
34
33
32
AVREF
31
AGND
30
AVCC
22
23
24
25
26
27
28
29
PC0
PC1
PC2
PC3
PC4
PC5
PC6
PC7
R4
Vcc
100
C5 0.1uF
Vcc
L1
10uH
C7
0.1uF
Vcc
11
PD0
PD1
PD2
PD3
PD4
PD5
PD6
PD7
VCC
C6
AVCC
10
+
Analog inputs on port A
22pF
12
Y1
16MHz
13
C1
22pF
C8
100uF/10V
PB6 1
PB7 3
/RESET 5
C9
0.1uF
J10
MISO Vcc
SCK MOSI
RES GND
2
4
6
PB5
ISP
Vcc
• external crystal,
• external RESET button,
• additional components for AD converter
Erasmus 2015/2016, WIEiK, PK
5
2015-10-17
Driving Loads – source or sink configurations
+Vcc
+Vcc
source current
MCU
MCU
Ro
or Zo
Ro
or Zo
Load
Load
sink current
GND
•
•
•
SOURCE
SINK
GND
GND
GND
An output pin not connected to anything is not of much interest. To do something
useful it must be connected to another device, referred here simply as a “load”.
A real world load could be an LED, a lamp, a transistor, or some other circuit
element.
Generally there are two ways to connect an output pin to a load.
– The first is where the microcontroller supplies the current to drive the device. The
microcontroller is referred to as the source. Current flows from the microcontroller power to
the output pin, through the load and to ground.
– A microcontroller I/O pin can be set by the program to operate as an output. The output
voltage will be close to the supply voltage when the output is set to a logical 1. The output
voltage will be close to 0 volts when the pin is set to a logical 0.
– The microcontroller pin can also “sink” the current as shown in Figure. Here the current flows
from the power supply through the load and through the output pin to ground. It is important
that the load be connected to the same power supply line as the microcontroller, or you can
Erasmus 2015/2016, WIEiK, PK
destroy the IC.
Digital Output
•
•
•
Microcontrollers are somewhat delicate devices and the I/O lines can only
carry a relatively small amount of current (+/-25mA). The current limit will
depend on the type of microcontroller, and the specific pin. There will
usually be a maximum total current the pins of a single 8 bit port can
handle, as well as a limit for all of the outputs for the entire
microcontroller. Exceeding the limits will destroy the microcontroller.
To find out what the maximum currents are, you need to look at the data
sheet for the microcontroller. PDF formatted data sheets can be
downloaded from the manufacturer. These can be quite large, several
hundred pages for a fairly complex one. Look for a section titled
“Electrical Specifications” or something similar. Usually in the section
there will be a table called “Absolute Maximum Ratings” or similar. You will
find a table containing a number of specifications - Output Source Current
(Ioh) and Output Sink Current (Iol)
An output pin can either source or sink the current to or from a load. It is
important not to exceed the maximum current ratings, which can be found
in the microcontroller’s data sheet.
Erasmus 2015/2016, WIEiK, PK
6
2015-10-17
Digital input
+Vcc
+Vcc1 > +Vcc2
+Vcc1 < +Vcc2
+Vcc1
Digital
Circuit
Digital
Circuit
MCU
MCU
Vin
Vin
GND
+Vcc2
GND
GND
• A pin with a low input voltage (ideally 0 volts) will read as a logical 0.
• A pin with a input voltage near Vcc (or Vdd) will read as a logic 1.
• As a first approximation, any input less than half of Vcc will read as a 0, and any
input over Vcc/2 will read as a 1.
• There is a voltage band however, right around Vcc/2, where there is no
guarantee what state will be read. Figure shows the logic thresholds of a
typical microcontroller.
• The X axis represents the supply voltage, Vcc (Vdd on some data sheets). The
Y axis represents the voltage on an input pin. The black dashed line is where
the input is 1/2 Vcc.
• For a given supply voltage if the input pin voltage is below the green line the
pin will be read as 0. Above the blue line the input will read as 1.
• Between the green and blue lines is a “no man’s land” where it might read as a
0 or it might read as a 1. If the signal is near Vcc/2, a little noise can cause the
voltage to jump over to the other side, and cause an incorrect reading.
Erasmus 2015/2016, WIEiK, PK
Digital input – pull up or pull down resistor
Internal or external pull up resistor
+Vcc
Internal or external pull down resistor
+Vcc
Rpu
MCU
Switch
MCU
Switch
Vin
Rpd
Vin
GND
•
•
•
•
GND
Sometimes you want an input to read as a 1 or 0 as a default. Suppose you have a sensor on a cable that plugs
into your device. It is possible that the user will disconnect the cable. If the input pin is left floating, it might
sometimes read as a 1, sometimes as a 0. Your code might interpret this as changes from a sensor and not act the
way you want.
Putting a pull up resistor will set the input voltage near Vcc and it will read as a 1. A pull down resistor will bring
the voltage near 0V, and it will read as a zero. Figure shows pull up and pull down resistors. The switch, sensor, or
other component that generates the normal 1 and 0 voltages must be able to over drive the resistor.
Some microcontrollers have internal pull ups. You set a bit in an internal register to turn them on. These are nice
because you don’t have to add the pull up resistors in the hardware design, saving cost, board space and assembly
time. The value of internal pull ups is usually pretty high (weak pull up), and the actual value often has a huge
range. Internal pull ups are handy, but evaluate the parameters in the data sheet before using them.
So, what values should be used for pull ups and pull downs? There are no hard and fast rules, but generally
anything under a few thousand ohms is a strong pull up. Weak pull ups often reach 40-50KΩ.
Erasmus 2015/2016, WIEiK, PK
7
2015-10-17
Digital input
• The DDR register in the ATmega32 will control if a pin is an
input or output. You need your program to set up the port
direction registers as an early step when power is applied to
the chip or it comes out of reset.
• I/O pins are usually configured in groups of 8 bit I/O
ports. The program can read the port and will get a value
between 0 and 255 depending on the states of the input
pins.
• In assembly language programming there will usually be op
code instructions that allow reading a single pin of a port.
• C compilers will usually implement single bit functions as
well. Otherwise the programmer will have to read the entire
port and mask off the other bits.
Erasmus 2015/2016, WIEiK, PK
8