0% found this document useful (0 votes)
29 views

Introduction - ATmega328

The document discusses microprocessor interfacing and bit manipulation techniques including shifting, masking, AND, OR and XOR operations. It also describes data direction registers (DDRx), data registers (PORTx), and input pin registers (PINx) and their usage for Arduino digital pins.

Uploaded by

Hasnain Kashif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Introduction - ATmega328

The document discusses microprocessor interfacing and bit manipulation techniques including shifting, masking, AND, OR and XOR operations. It also describes data direction registers (DDRx), data registers (PORTx), and input pin registers (PINx) and their usage for Arduino digital pins.

Uploaded by

Hasnain Kashif
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Microprocessor Interfacing 3/8/2019

Dr. Majid Naeem

3/8/2019 Dr. Majid Naeem 1 3/8/2019 Dr. Majid Naeem 2

N
M
3/8/2019 Dr. Majid Naeem 3 3/8/2019 Dr. Majid Naeem 4
D

3/8/2019 Dr. Majid Naeem 5 3/8/2019 Dr. Majid Naeem 6

1
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem

3/8/2019 Dr. Majid Naeem 7 3/8/2019 Dr. Majid Naeem 8

N
M
3/8/2019 Dr. Majid Naeem 9 3/8/2019 Dr. Majid Naeem 10
D

3/8/2019 Dr. Majid Naeem 11 3/8/2019 Dr. Majid Naeem 12

2
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem

3/8/2019 Dr. Majid Naeem 13 3/8/2019 Dr. Majid Naeem 14

N
M
3/8/2019 Dr. Majid Naeem 15 3/8/2019 Dr. Majid Naeem 16
D

3/8/2019 Dr. Majid Naeem 17 3/8/2019 Dr. Majid Naeem 18

3
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem

3/8/2019 Dr. Majid Naeem 19 3/8/2019 Dr. Majid Naeem 20

N
M
3/8/2019 Dr. Majid Naeem 21 3/8/2019 Dr. Majid Naeem 22
D

3/8/2019 Dr. Majid Naeem 23 3/8/2019 Dr. Majid Naeem 24

4
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem

3/8/2019 Dr. Majid Naeem 25 3/8/2019 Dr. Majid Naeem 26

N
M
3/8/2019 Dr. Majid Naeem 27 3/8/2019 Dr. Majid Naeem 28
D

3/8/2019 Dr. Majid Naeem 29 3/8/2019 Dr. Majid Naeem 30

5
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem

3/8/2019 Dr. Majid Naeem 31 3/8/2019 Dr. Majid Naeem 32

N
M
3/8/2019 Dr. Majid Naeem 33 3/8/2019 Dr. Majid Naeem 34
D

3/8/2019 Dr. Majid Naeem 35 3/8/2019 Dr. Majid Naeem 36

6
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem

3/8/2019 Dr. Majid Naeem 37 3/8/2019 Dr. Majid Naeem 38

N
M
3/8/2019 Dr. Majid Naeem 39 3/8/2019 Dr. Majid Naeem 40
D

3/8/2019 Dr. Majid Naeem 41 3/8/2019 Dr. Majid Naeem 42

7
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem

3/8/2019 Dr. Majid Naeem 43 3/8/2019 Dr. Majid Naeem 44

N
M
3/8/2019 Dr. Majid Naeem 45 3/8/2019 Dr. Majid Naeem 46
D

3/8/2019 Dr. Majid Naeem 47 3/8/2019 Dr. Majid Naeem 48

8
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem

3/8/2019 Dr. Majid Naeem 49 3/8/2019 Dr. Majid Naeem 50

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

Bit Manipulation Masking

• 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

3/8/2019 Dr. Majid Naeem 55 3/8/2019 Dr. Majid Naeem 56

• Set Bit
– PORTB |= (1 << 2)
• Clear Bit
Masking

N
M – PORTB &= ~(1 << 2)
• Flip Bit
– PORTB ^= (1 << 2)
• Read Bit
– PORTB & (1 << 2)

3/8/2019 Dr. Majid Naeem 57 3/8/2019 Dr. Majid Naeem 58


D
Data Direction Register (DDRx) DDR and PORT registers may be both written to, and read.
used to set a specific port pin to either output (1) or input (0), and PIN registers correspond to the state of inputs and may only be read.

PORTD maps to Arduino digital pins 0 to 7


DDRD - The Port D Data Direction Register - read/write
PORTD - The Port D Data Register - read/write
PIND - The Port D Input Pins Register - read only

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

3/8/2019 Dr. Majid Naeem 59 3/8/2019 Dr. Majid Naeem 60

10
Microprocessor Interfacing 3/8/2019
Dr. Majid Naeem

Sinking Output

Sinking and Sourcing On The Same Output

Sourcing Output

3/8/2019 Dr. Majid Naeem 61 3/8/2019 Dr. Majid Naeem 62

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

PORTB |= (1 << PORTB5); // turn On the Pull-up


M }
// PB5 is now an input with pull-up enabled

Way To Hook Up A Switch With a Pull-up Activated


void loop()
{
if( (PINB & (1 << PINB5)) == 0) // we are using pull-up. 1 = switch off
{
// do something when PD5 is on
}
}

3/8/2019 Dr. Majid Naeem 63 3/8/2019 Dr. Majid Naeem 64


D
Register Summary

DDRD |= (1 << DDD0); // sets bit DDD0 to 1 within register DDRD


// PD0 is now an output WOOT !!!

PORTD |= (1 << PORTD0); // turn on PD0


// PD0 is now an output and is sourcing VCC

PORTD &= ~(1 << PORTD0); // turn off PD0


// PDO is still an output and is now sinking 0V

3/8/2019 Dr. Majid Naeem 65 3/8/2019 Dr. Majid Naeem 66

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

You might also like