Introduction - ATmega328
Introduction - ATmega328
N
M
3/8/2019 Dr. Majid Naeem 3 3/8/2019 Dr. Majid Naeem 4
D
1
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem
N
M
3/8/2019 Dr. Majid Naeem 9 3/8/2019 Dr. Majid Naeem 10
D
2
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem
N
M
3/8/2019 Dr. Majid Naeem 15 3/8/2019 Dr. Majid Naeem 16
D
3
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem
N
M
3/8/2019 Dr. Majid Naeem 21 3/8/2019 Dr. Majid Naeem 22
D
4
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem
N
M
3/8/2019 Dr. Majid Naeem 27 3/8/2019 Dr. Majid Naeem 28
D
5
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem
N
M
3/8/2019 Dr. Majid Naeem 33 3/8/2019 Dr. Majid Naeem 34
D
6
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem
N
M
3/8/2019 Dr. Majid Naeem 39 3/8/2019 Dr. Majid Naeem 40
D
7
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem
N
M
3/8/2019 Dr. Majid Naeem 45 3/8/2019 Dr. Majid Naeem 46
D
8
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem
N
M
3/8/2019 Dr. Majid Naeem 51 3/8/2019 Dr. Majid Naeem 52
D
Bit Manipulation Bit Manipulation
• Shift Left (<<)
– 1 << 2 = 0000 0001 << 2 = 0000 0100 = 4 • AND (&) Both Bits are SAME
– 8 << 5 = 0000 1000 << 5 = 0000 0000 i = 15 & 170; 0000 1111
1010 1010
• Shift Right (>>) --------------------
– 4 >> 2 = 0000 0100 >> 2 = 0000 0001 = 1
0000 1010 = 10
– 8 >> 4 = 0000 1000 >> 4 = 0000 0000
• OR (|) Any Bit is ONE
In C i = 15 | 170; 0000 1111
i = 1; 1010 1010
i = i << 2; --------------------
i = i >> 2;
1010 1111 = 175
3/8/2019 Dr. Majid Naeem 53 3/8/2019 Dr. Majid Naeem 54
9
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem
• XOR (^) Both Bits are Different • To Find the Status / Value of a Bit in a Register
i = 15 ^ 170; 0000 1111 • A Mask is a Binary Number that contain 1s in
1010 1010 the bit position
--------------------
1010 0101 = 165
• (1 << 5) | (1 << 3) | (1 << 2)
• NOT (~) ZERO is ONE
i = ~ 0000 1111 // i become 1111 0000
• Set Bit
– PORTB |= (1 << 2)
• Clear Bit
Masking
N
M – PORTB &= ~(1 << 2)
• Flip Bit
– PORTB ^= (1 << 2)
• Read Bit
– PORTB & (1 << 2)
Data Register (PORTx) PORTB maps to Arduino digital pins 8 to 13 The two high bits (6 & 7) map to
used to write output data to the port, the crystal pins and are not usable
DDRB - The Port B Data Direction Register - read/write
PORTB - The Port B Data Register - read/write
PINB - The Port B Input Pins Register - read only
PORTC maps to Arduino analog pins 0 to 5. Pins 6 & 7 are only accessible on
Input Pin Address (PINx) the Arduino Mini
used to read input data from the port. DDRC - The Port C Data Direction Register - read/write
PORTC - The Port C Data Register - read/write
PINC - The Port C Input Pins Register - read only
10
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem
Sinking Output
Sourcing Output
Pull-up Resistor
N {
// this code sets PB5 to an input with a pull-up enable
void setup()
DDRB &= ~(1 << DDB5); // Clear the PB5 pin. PB5 is now an input
11
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem
3/8/2019
AVR memory layout
Dr. Majid Naeem 67 3/8/2019 Dr. Majid Naeem 68
N
M
D
12