Atmega128 I/O Ports: Versus

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

ATMega128 I/O Ports

The AVR I/O ports are the path to the outside world. Understand how to use them and life is good. Failure to understand how the ports are used will cause grief and possibly cost $'s. An abused I/O port is fairly easy to burn out with excessive current or static damage. -most all the I/O ports are floating inputs that can build up large static charge Never carry your AVR board in a non static-dissipative bag. -dry fall days are perfect for creating conditions for ESD damage -practice safe electronics, use the pink bag Using proper port software conventions will keep code transportable, readable and more bug free. i.e., TIMSK|=(1<<TOIE0);//setsabit Versus TIMSK=b01000000;//clobberstheregister

ATMega128 I/O Ports


I/O Port input structure

-protection diodes -programmable pull-up resistor -what happens if voltages exceeding Vcc are applied to an I/O pin? -can you power a chip from an I/O pin?

ATMega128 I/O Ports


All ports........ -have bit-selectable pull-up resistors -have bit-selectable tri-state outputs (what are these?) -have schmitt trigger input buffers (what is that?) (can you draw one?) -are synchronized to the system clock to prevent metastability (what is that?) -have symmetrical DC drive capability (what does that mean?) All ports have read-modify-write capability, i.e., -i.e., you can change pin direction, pin value, or pin pull-up resistor without effecting any other pins in the port Control of all ports and pins is done with three registers -DDRx (i.e., DDRB is data direction register port B) -PORTx (i.e. PORTB is the output register for port B) -PINx (i.e. PINB in the input register for port B) All of these ports may be read. Writing the PINx register does nothing.

ATMega128 I/O Ports


AVR port architecture
1=out

DDRx 0=in

pull-up resistor PORTx

port pin

schmitt trigger buffer

analog switch

latch

flip flop

PINx

ATMega128 I/O Ports


AVR I/O port usage
bit 0: output mode, logic '1' asserted, if PINB.0 is read, it returns a '1' bit 1: output mode, logic '0' asserted, if PINB.1 is read, it returns a '0' bit 2: input mode, no pullup resistor, if PINB2 read returns state of pin bit 3: input mode, pullup resistor on, thus if PINB3 is read it...... returns a '0' if pin is driven '0' returns a '1' if the pin is not driven

DDRB

7 6 5 4 0 0 0 0 7 6 5 4 0 0 0 0 7 6 5 4 ? ? ? ?

3 2 1 0 0 0 1 1 3 2 1 0 1 0 0 1 3 2 1 0 ? ? 0 1

PORTB

PINB

ATMega128 I/O Ports


AVR I/O port usage
DDRx PORTx PINx selects pin direction (in or out) determines the driven pin value if the pin is an output determines if a pullup is present if the pin is an input holds the value of the pin

Port usage fine points


Regardless of the setting of the DDRx register, the port pin can be read from PINx Thus, an driven output value in PORTx can always be read in PINx. When the pull-up disable bit in the Special Function I/O Register (SFIOR) is set, all pull-ups are disabled regardless of the setting of DDRx and PORTx. Pullups are also disabled during reset. Input pins have a 1.5 clock cycle delay before a new value can be read -1 NOP instruction necessary to read updated pin Use pull-ups on unused I/O pins to lower power consumption. Using alternative functions of some port pins does not effect other pins.

ATMega128 I/O Ports


AVR I/O port programming
In the file iom128.h, define statements are used to make shorthand notation for ports and bits. For example........ /*DataRegister,PortB*/ #definePORTB _SFR_IO8(0x18) and also...... /*PortBDataRegisterPORTB*/ #define PB7 7 #define PB6 6 #define PB5 5 #define PB4 4 #define PB3 3 #define PB2 2 #define PB1 1 #define PB0 0

ATMega128 I/O Ports


AVR I/O port programming
The #defines allow us to program like this.... PORTB=0x05; DDRB=0x0A; OR SPCR=(1<<SPE)|(1<<MSTR); DDRB|=(1<<DDB2)|(1<<DDB0); AND temp0=PINB; Which way of setting SPCR and DDRB is better? What is the difference?

ATMega128 I/O Ports


AVR I/O port programming
By using AND, OR and XOR, we can manipulate individual bits //togglebit5 PORTB=PORTB^0x20;//invert PORTB^=0x20;//invertagainanotherway //setbits7and2,don'tbotherothers PORTB=PORTB|0x84; PORTB|=(1<<PB7)|(1<<PB2);//shorter,moreportable //clearbit0and1,butnothingelse PORTB=PORTB&0xfc;//oneway PORTB&=~0x03;//maybeclearer PORTB&=~((1<<PB0)|(1<<PB1));//moreportable

ATMega128 I/O Ports


AVR I/O port programming
By using a mask we can get the value of individual bits. //lookingforbitzeroofportDtobeaone if(PIND&0x01){take_some_action();} The value 0x01 as used here is called a mask. It allows us to zero out the other bits and determine if one particular bit is a one. We can look for a zero also.... //lookingforbit5ofportBtobeazero if(~PIND&0x20){take_action();} Best yet is to use the avr-libc functions bit_is_set() and bit_is_clear(): if(bit_is_set(PINC,PC2){return0;} while(bit_is_clear(SPSR,SPIF)){}

ATMega128 I/O Ports


AVR I/O port programming
Assume (correctly!) that after reset, the following is true: DDRD = 0x00 DDRB = 0x00 Assume a mega128 board has a normally open switch attached to port D bit zero. When the switch is closed, the port D bit is connected to ground. Write code that reads port D bit zero, inverts its value and outputs that value to port B bit 0. Do not disturb the values of any other pins. (5 minutes)

ATMega128 I/O Ports


AVR I/O port programming
Assume a mega128 board has a normally open switch attached to port D bit zero. When the switch is closed, the port D bit is connected to ground. Write code that reads port D bit zero, inverts its value and outputs that value to port B bit 0. Do not disturb the values of any other pins. #include<avr/io.h> //readportdbit0,invertandoutputtoportbbit0 intmain() { while(1){ DDRD&=0xFE;//makeportdbitzeroinputmode PORTD|=0x01;//turnonbit0pullup DDRB|=0x01;//setportbbit0tooutputmode if(PIND&0x01){PORTB&=0xFE;} else{PORTB|=0x01;} }//while }//main

ATMega128 I/O Ports


AVR I/O port DC characteristics
The Absolute Maximum Ratings are not where you want to operate an IC

Where do these ratings come from?

ATMega128 I/O Ports


AVR I/O port DC output characteristics
Output Buffer characteristics (mega128 datasheet page 321)

Always read the footnotes

ATMega128 I/O Ports


AVR I/O port DC input characteristics
Input Buffer characteristics (mega128 datasheet page 321)

ATMega128 I/O Ports


AVR I/O port interfacing
Pull-ups are handy for terminating switch terminals but watch the leakage current! -Quadrature encoder (90 deg. phased outputs) -How would you hook it up? -What about protection of output drivers? -What about protection for input buffers? Led Drive Circuits -Highside or lowside drive -How do you determine the correct values of current limit resistors? -When do you need external drive help with a transistor? Motor or relay drive circuits -catch diodes for inductive kickback Interface directly to a speaker? Can it be done? 5V to 3.3V or 3.3V to 5V interfacing -resistor and protection diode,or two resistors, or resistor and zener diode? -74LVC244, TXB0108

You might also like