Documente Academic
Documente Profesional
Documente Cultură
(D13)
(D5) (D12)
(D6) (D11)
(D7) (D10)
(D8) (D9)
PORTx DRIVERS
Inside ATmega328
Pins on ATmega328
Px7 Px6 Px5 Px4 Px3 Px2 Px1 Px0
PORTx
Px.7
Px.6
Px.5
Px.4
Px.3
Px.2
Px.1
Px.0
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.9/34
4.3 ATmega328 Parallel Digital I/O
Data Register– store data to be sent to
corresponding pins of that I/O port. Data register
for Port x is PORTx.
When an I/O port is configured as Output:
writing a ‘1’ to bit PORTxn generate logic ‘1’ on
the port n pin.
writing a ‘0’ to bit PORTx.n generate logic ‘0’
on the port n pin.
reading from PORTx returns the previous value
written to PORTx.
PORTB = 0x00;
PORTD = 0x08;
digitalWrite(12, LOW);
digitalWrite(13, LOW);
digitalWrite(3, HIGH);
1 0 0 0 1 1 1 0 PINx
Px7-0
3.3V
0.1V
0.2V
0V
0V
5V
5V
3V
Exercise: Read the logic level on pin PB0 & send the logic to
pin PB1. Do not change other pins.
+V -
Anode Cathode
a a a
b b
f b c c
g d d
Cathodes e e
Anodes
f f
e c g g
dp dp dp
d
Common Anode Common Cathode
(CA) 7-segment (CC) 7-segment
Display Display
PD0 a a
PD0
b CA b 220
PD1 c PD1 c CA
PD2 PD2
PD3 d d
PD3
PD4 e e
PD4
PD5 f f
g PD5 g
PD6 dp PD6 dp
PD7 PD7
ROSBI ROSBI
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.22/34
r 1 0 1 0 1 1 1 1 $AF
o 1 0 1 0 0 0 1 1 $A3
S 1 0 0 1 0 0 1 0 $92
b 1 0 0 0 0 0 1 1 $83
I 1 1 0 0 1 1 1 1 $CF
These codes are stored in an array to serve as a
look-up table. C code for building look-up table:
const unsigned char myname[] = {0xAF, 0xA3, 0x92, 0x83, 0xCF};
AF A3 92 83 CF .. ..
myname
Memory Contents
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.23/34
4.4 Interfacing with LED Displays
Complete C code :
/* Display 'rOSbi' on 7-seg LED connect to Port D */
void setup() {
DDRD = 0xFF; /* Port D is output */
}
void loop() {
unsigned char ch;
int j;
74L 74L
S47 S48
: : : :
:
: : : :
:
dp dp dp
PD.7
Cathode Cathode Cathode
220 220 220
10K 10K 10K
PB.0
PB.1
PB.2
1 0 0 1 1 0 0 0 0 $30
2 0 1 0 1 1 0 1 1 $5B
3 0 1 0 0 1 1 1 1 $4F
1 23
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.28/34
10KΩ
10KΩ 10KΩ
Vo
Vo
Vo
Closed
Bouncing
'1'
Vo
'0'
'1'
Vo
C 74LS13
'0'
+5V
ATmega328 330
PB.1 PB.0
void loop() {
while (digitalRead(9) == HIGH) // Step 1: wait for key press
;
delay(20); // Step 2: wait for 20ms
if (digitalRead(9) == 0) // Step 3: check bouncing
{ // Finish bouncing
for (int i = 0; i < 5; i++)
{ // Blink 5 times
digitalWrite(8, LOW); // On
delay(500);
digitalWrite(8, HIGH); // Off
delay(500);
}
}
}
Microcontroller (SEL4533) c 2013 Dr. Rosbi Mamat – p.34/34