IO Ports in AVR Sepehr Naimi www Nicer

  • Slides: 24
Download presentation
I/O Ports in AVR Sepehr Naimi www. Nicer. Land. com www. Micro. Digital. Ed.

I/O Ports in AVR Sepehr Naimi www. Nicer. Land. com www. Micro. Digital. Ed. com

Topics n n AVR pin out The structure of I/O pins I/O programming Bit

Topics n n AVR pin out The structure of I/O pins I/O programming Bit manipulating 2

I/O unit in AVR 3

I/O unit in AVR 3

ATmega 328 pinout 1. Vital Pins: 1. Power n n 2. • 3. •

ATmega 328 pinout 1. Vital Pins: 1. Power n n 2. • 3. • VCC Ground Crystal n 3. +5 V XTAL 1 XTAL 2 +5 V Reset I/O pins PORTB, PORTC, and PORTD Internal ADC pins AREF, AVCC, ADCn 4

The structure of I/O pins 5

The structure of I/O pins 5

Example 1 Write a program that makes all the pins of PORTB one. n

Example 1 Write a program that makes all the pins of PORTB one. n DDRB: PORTB: 1 1 1 1 LDI R 20, 0 x. FF ; R 20 = 1111 (binary) OUT PORTB, R 20 ; PORTB = R 20 OUT DDRB, R 20 ; DDRB = R 20 6

Example 2 n The following code will toggle all 8 bits of Port B

Example 2 n The following code will toggle all 8 bits of Port B forever with some time delay between “on” and “off” states: LDI OUT L 1: LDI OUT CALL RJMP R 16, 0 x. FF DDRB, R 16, 0 x 55 PORTB, R 16 DELAY R 16, 0 x. AA PORTB, R 16 DELAY L 1 ; R 16 = 0 x. FF = 0 b 1111 ; make Port B an output port (1111) ; R 16 = 0 x 55 = 0 b 0101 ; put 0 x 55 on port B pins ; R 16 = 0 x. AA = 0 b 1010 ; put 0 x. AA on port B pins 7

Example 3 n A 7 -segment is connected to PORTA. Display 1 on the

Example 3 n A 7 -segment is connected to PORTA. Display 1 on the 7 -segment. DDRD: PORTD: 1 1 1 1 0 0 0 1 1 0 ATmega 328 LDI R 20, 0 x 06 ; R 20 = 00000110 (binary) OUT PORTD, R 20 ; PORTD = R 20 LDI R 20, 0 x. FF ; R 20 = 1111 (binary) OUT DDRD, R 20 ; DDRD = R 20 PORTD 0 8 5 6 1 2 4 3 L 1: RJMP L 1 8

Example 4 n A 7 -segment is connected to PORTA. Display 3 on the

Example 4 n A 7 -segment is connected to PORTA. Display 3 on the 7 -segment. DDRD: PORTD: 1 1 1 1 0 1 0 0 1 1 ATmega 328 LDI R 20, 0 x 4 F ; R 20 = 01001111 (binary) OUT PORTD, R 20 ; PORTD = R 20 LDI R 20, 0 x. FF ; R 20 = 1111 (binary) OUT DDRD, R 20 ; DDRD = R 20 PORTD 0 8 5 6 1 2 4 3 L 1: RJMP L 1 9

Example 5: Input n The following code gets the data present at the pins

Example 5: Input n The following code gets the data present at the pins of port C and sends it to port B indefinitely, after adding the value 5 to it: L 2: LDI OUT IN LDI ADD OUT RJMP R 16, 0 x 00 DDRC, R 16, 0 x. FF DDRB, R 16, PINC R 17, 5 R 16, R 17 PORTB, R 16 L 2 ; R 16 = 0000 (binary) ; make Port C an input port ; R 16 = 1111 (binary) ; make Port B an output port(1 for Out) ; read data from Port C and put in R 16 ; add 5 to it ; send it to Port B ; jump L 2 10

Pull-up resistor 11

Pull-up resistor 11

Example 12

Example 12

I/O bit manipulation programming

I/O bit manipulation programming

SBI and CBI instructions n SBI (Set Bit in IO register) n n SBI

SBI and CBI instructions n SBI (Set Bit in IO register) n n SBI io. Reg, bit Examples: n n n SBI PORTD, 0 SBI DDRC, 5 ; io. Reg. bit = 1 ; PORTD. 0 = 1 ; DDRC. 5 = 1 CBI (Clear Bit in IO register) n n CBI io. Reg, bit Examples: n n CBI PORTD, 0 CBI DDRC, 5 ; io. Reg. bit = 0 ; PORTD. 0 = 0 ; DDRC. 5 = 0 14

Example n Write a program that toggles PORTB. 4 continuously. SBI DDRB, 4 L

Example n Write a program that toggles PORTB. 4 continuously. SBI DDRB, 4 L 1: SBI PORTB, 4 CBI PORTB, 4 RJMP L 1 15

Example n An LED is connected to each pin of Port D. Write a

Example n An LED is connected to each pin of Port D. Write a program to turn on each LED from pin D 0 to pin D 7. Call a delay module before turning on the next LED. LDI OUT SBI CALL SBI CALL R 20, 0 x. FF DDRD, R 20 PORTD, 0 DELAY PORTD, 1 DELAY PORTD, 2 DELAY PORTD, 3 DELAY PORTD, 4 DELAY PORTD, 5 DELAY PORTD, 6 DELAY PORTD, 7 DELAY ; make PORTD an output port ; set bit PD 0 ; delay before next one ; turn on PD 1 ; delay before next one ; turn on PD 2 16

SBIC and SBIS n SBIC (Skip if Bit in IO register Cleared) n n

SBIC and SBIS n SBIC (Skip if Bit in IO register Cleared) n n SBIC io. Reg, bit Example: SBIC INC LDI n ; if (io. Reg. bit = 0) skip next instruction PORTD, 0 ; skip next instruction if PORTD. 0=0 R 20 R 19, 0 x 23 SBIS (Skip if Bit in IO register Set) n n SBIS io. Reg, bit Example: SBIS INC LDI ; if (io. Reg. bit = 1) skip next instruction PORTD, 0 ; skip next instruction if PORTD. 0=1 R 20 R 19, 0 x 23 17

Example n n n Write a program to perform the following: (a) Keep monitoring

Example n n n Write a program to perform the following: (a) Keep monitoring the PB 2 bit until it becomes HIGH; (b) When PB 2 becomes HIGH, write value $45 to Port C, and also send a HIGH-to-LOW pulse to PD 3. CBI SBI LDI OUT SBI AGAIN: SBIS RJMP LDI OUT SBI CBI HERE: RJMP DDRB, 2 PORTB, 2 R 16, 0 x. FF DDRC, R 16 DDRD, 3 PINB, 2 AGAIN R 16, 0 x 45 PORTC, R 16 PORTD, 3 HERE ; make PB 2 an input ; make ; Skip ; keep Port C an output port PD 3 an output if Bit PB 2 is HIGH checking if LOW ; write 0 x 45 to port C ; set bit PD 3 (H-to-L) ; clear bit PD 3 18

Example n A switch is connected to pin PB 0 and an LED to

Example n A switch is connected to pin PB 0 and an LED to pin PB 5. Write a program to get the status of SW and send it to the LED. CBI SBI AGAIN: SBIC RJMP CBI RJMP OVER: SBI RJMP DDRB, 0 DDRB, 5 PINB, 0 OVER PORTB, 5 AGAIN ; make ; skip ; (JMP PB 0 an input PB 5 an output next if PB 0 is clear is OK too) ; we can use JMP too 19

The structure of I/O pins DDRx. n PORTx. n PINx. n OUTPUT INPUT 20

The structure of I/O pins DDRx. n PORTx. n PINx. n OUTPUT INPUT 20

Out 0 21

Out 0 21

Out 1 22

Out 1 22

The structure of I/O pins 23

The structure of I/O pins 23

Input (Tri-state vs. pull up) 24

Input (Tri-state vs. pull up) 24