Chapter 4
Chapter 4
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Topics
AVR pin out
The structure of I/O pins
I/O programming
Bit manipulation
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
ATmega16/mega32 pinout
1. Vital Pins:
1. Power (XCK/T0) PB0 1
40 PIN DIP
40 PA0 (ADC0)
VCC +5 V
(T1) PB1 2 39 PA1 (ADC1)
(INT2/AIN0) PB2 3 38 PA2 (ADC2)
Ground (OC0/AIN1) PB3 4 MEGA32 37 PA3 (ADC3)
PORTA, PORTB,
(RXD) PD0 14 27 PC5 (TDI)
•
(TXD) PD1 15 26 PC4 (TDO)
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
The structure of IO pins
Mega32/Mega16
(XCK/T0) PB0 0 00 00 0 PA0 (ADC0)
(T1) PB1 1 11 11 1 PA1 (ADC1)
(INT2/AIN0) PB2 2 22 22 2 PA2 (ADC2)
(OC0/AIN1) PB3 3 33 33 3 PA3 (ADC3)
(SS) PB4 4 44 44 4 PA4 (ADC4)
(MOSI) PB5 5 55 55 5 PA5 (ADC5)
(MISO) PB6
DDRx:
6
7
66 6 5 4 3
6 26 61 0
PA6 (ADC6)
(SCK) PB7 7 77 77 7 PA7 (ADC7)
PORTx: 7 6 5 4 3
DDRA 2 1 0
DDRB
PINB
PORTB
RESETPINx: 7 6 5
PORTA
4 3 2 1 0 AREF
PINA
VCC AGND
GND Px7 Px6 Px5 Px4 Px3 Px2 Px1 Px0 AVCC
XTAL2 PC7 (TOSC2)
XTAL1 PC6 (TOSC1)
(RXD) PD0 PC5 (TDI)
(TXD) PD1 PC4 (TDO)
DDRx
PC3 (TMS) 00 11
DDRx
(INT0) PD2
PORTx
DDRx.n PORTx
(INT1) PD3 PC2 (TCK)
00 high
highimpedance
impedance Out
Out00
(OC1B) PD4 PORTx.n PC1 (SDA)
(OC1A) PD5 PC011(SCL) pull-up
pull-up Out
Out11
(ICP) PD6 PINx.n PD7 (OC2)
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
There are three 8-bit registers associated with
each port
x indicates port – A, B, C, or D
If DDR bit is zero, then pin is input pin
Else if DDR bit is one, then pin is output
Port Registers
Port registers allow for lower-level and faster manipulation of
the i/o pins of the microcontroller on an Arduino board. The
chips used on the Arduino board (the ATmega328) have
three ports:
When using ports, the first thing you do is
•Port B (digital pin 8 to 13) Configure the port to be input or ouput.
•Port C (analog input pins) By writing a value to the DDR register.
•Port D (digital pins 0 to 7)
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 1
Write a program that makes all the pins of
PORTA one. (XCK/T0) PB0
(T1) PB1
Mega32/Mega16
PA0 (ADC0)
PA1 (ADC1)
PORTB
DDRB
PINB
RESET DDRA AREF
PORTA
VCC AGND
PORTC
GND DDRC AVCC
PINC
XTAL2 PC7 (TOSC2)
1. Configure port A as an output port XTAL1
(RXD) PD0
PC6 (TOSC1)
PC5 (TDI)
Send FF to DDRA (TXD) PD1
(INT0) PD2
PC4 (TDO)
PC3 (TMS)
2. Make all pins of port A high (INT1) PD3
(OC1B) PD4
PC2 (TCK)
PC1 (SDA)
Write FF to PORTA register (OC1A) PD5
(ICP) PD6
PC0 (SCL)
PD7 (OC2)
LDI R16,$FF
DDRx
OUT DDRA,R16 00 11
DDRx
PORTx
PORTx
OUT PORTA,R16
00 high
highimpedance
impedance
Out
Out0 0
11 pull-up
pull-up Out
Out1 1
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 2
The following code will toggle all 8 bits of Port D
forever with some time delay between “on” and
“off” states:
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Setup()
{pinMode(0,OUPUT);
pinMode(1,OUPUT);
pinMode(2,OUPUT);
pinMode(3,OUPUT);
Setup()
pinMode(0,OUPUT);
{
pinMode(0,OUPUT);
DDRD = 0XFF;
pinMode(0,OUPUT);
}
pinMode(0,OUPUT);}
Loop()
{
Loop()
PORTD = 0XFF;
{
Delay(500)
digitalWrite(0,HIGH);
PORTD = 0X00;
digitalWrite(1,HIGH);
Delay(500)
}
Delay(500)
digitalWrite(0,LOW);
digitalWrite(1,LOW);
Delay(500)
}
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 3
A 7-segment is connected to PORTB. Display 1
on the 7-segment.
DDRB: 1 1 1 1 1 1 1 1
PORTB: 0 0 0 0 0 1 1 0
ATmega32 0
5 1
.INCLUDE “M32DEF.INC” 8 6
PORTB
LDI R20,0xFF ;R20 = 11111111 (binary) 4 2
OUT DDRB,R20 ;DDRC = R20
3
LDI R20,0x06 ;R20 = 00000110 (binary)
DDRx
OUT PORTB,R20 ;PORTC = R20 00 11
DDRx
PORTx
L1: RJMP L1 PORTx
00 high
highimpedance
impedance
Out
Out0 0
11 pull-up
pull-up Out
Out1 1
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 4
A 7-segment is connected to PORTB. Display 3
on the 7-segment.
DDRB: 1 1 1 1 1 1 1 1
PORTB: 0 1 0 0 1 1 1 1
ATmega32 0
5 1
.INCLUDE “M32DEF.INC” 8 6
PORTB
LDI R20,0xFF ;R20 = 11111111 (binary) 4 2
OUT DDRB,R20 ;DDRC = R20
3
LDI R20,0x4F ;R20 = 01001111 (binary)
DDRx
OUT PORTB,R20 ;PORTC = R20 00 11
DDRx
PORTx
L1: RJMP L1 PORTx
00 high
highimpedance
impedance
Out
Out0 0
11 pull-up
pull-up Out
Out1 1
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 5: Input
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:
READ FROM PORT C AND ADD 5 TO IT AND SEND TO PORT B
LDI R16,0
OUT DDRC,R16
LDI R16,$FF
OUT DDRB,R16
L1: IN R16,PINC
LDI R17,5
ADD R16,R17
OUT PORTB,R16
RJMP L1
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Synchronizer Delay
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Pull-up resistor
vcc
1 = Close
PORTx.n 0 = Open
pin n of
port x PINx.n
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 6
Write a program that continuously sends out to
Port C the alternating values of 0x55 and 0xAA.
.INCLUDE "M32DEF.INC"
LDI R16,0xFF ;R16 = 11111111 (binary)
OUT DDRC,R16 ;make Port C an output port
L1: LDI R16,0x55 ;R16 = 0x55
OUT PORTC,R16 ;put 0x55 on Port C pins
LDI R16,0xAA ;R16 = 0xAA
OUT PORTC,R16 ;put 0xAA on Port C pins
RJMP L1
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example 7
40 PIN DIP
DDRx
00 11
DDRx
PORTx
PORTx
READ FROM PORT A AND WRITE TO PORT B
00 high
highimpedance
impedance
Out
Out0 0
11 pull-up
pull-up Out
Out1 1
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
I/O bit manipulation programming
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
SBI and CBI instructions
SBR (set bit in GPR)
CBR (clear bit in GPR)
DDRx
SBI (Set Bit in IO register) PORTx
SBI ioReg, bit ;ioReg.bit = 1 PINx
Examples:
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example
Write a program that toggles PA.4 continuously.
Toggle pin 4 of port A….
SBI DDRA,4
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example
An LED is connected to each pin of Port D. Write a
program to turn on each LED from pin D0 to pin D7. Call
a delay module before turning on the next LED.
.INCLUDE "M32DEF.INC"
LDI R20, 0xFF
OUT DDRD, R20 ;make PORTD an output port
SBI PORTD,0 ;set bit PD0
CALL DELAY ;delay before next one
SBI PORTD,1 ;turn on PD1
CALL DELAY ;delay before next one
SBI PORTD,2 ;turn on PD2
CALL DELAY
SBI PORTD,3
CALL DELAY
SBI PORTD,4
CALL DELAY
SBI PORTD,5
CALL DELAY
SBI PORTD,6
CALL DELAY
SBI PORTD,7
CALL DELAY
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
SBIC and SBIS
SBIC (Skip the next instruction if Bit in IO register
Cleared)
SBIC ioReg, bit ; if (ioReg.bit = 0) skip next instruction
Example:
SBIC PORTD,0 ;skip next instruction if PORTD.0=0
INC R20
LDI R19,0x23
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
SBRC (Skip the next instruction if Bit in GPR
register Cleared)
SBRC GPR, bit ; if (GPR.bit = 0) skip next instruction
Example:
SBRC R20 ;skip next instruction if PORTD.0=0
INC R20
LDI R19,0x23
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example
Write a program to perform the following:
(a) Keep monitoring the PB2 bit until it becomes HIGH;
(b) When PB2 becomes HIGH, write value $45 to Port C, and also send a HIGH-to-LOW pulse to PD3.
LDI R16,$FF
OUT DDRC,R16
SBI DDRD,3
CBI DDRB,2
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Example
VCC
A switch is connected to pin 4.7k AVR
PB0 and an LED to pin PB7. PB0
Switch
Write a program to get the PB7
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
VCC
4.7k AVR
PB0
Switch
PB7
270
LED
CBI DDRB,0
SBI DDRB,7
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Generating Square Waves
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
(a) 50% Duty Cycle
SBI DDRC,3
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
(b) 60% Duty Cycle
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
WRITE A PROGRAM TO GENERATE A SQUARE WAVE OF 80% DUTY CYCLE
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights