Bit Masking To access or affect only the bits we want, we need to construct a byte with bits set in the locations of interest – This byte is called a ‘bit.

Slides:



Advertisements
Similar presentations
The 8051 Microcontroller and Embedded Systems
Advertisements

Microcontroller Fundamentals
LOGIC DESIGN AND CIRCUITS SEVEN SEGMENT LED DISPLAY Res. Assist. Hale İnan 1.
FLOWCHART BASED DESIGN A flowchart is ideal for a process that has sequential process steps. The steps will be executed in a simple order that may change.
Programming Microcontrollers B. Furman 19MAR2011.
 | bit OR & bit AND ~ bit NOT ^ bit EXLUSIVE OR (XOR) > bit RIGHT SHIFT.
Programming the ATmega16
Chapter 10-Arithmetic-logic units
Butterfly I/O Ports CS-212 Dick Steflik. I/O for our labs To get data into and out of our Butterfly its a little trickier than using printf and scanf.
AVR Programming CS-212 Dick Steflik. ATmega328P I/O for our labs To get data into and out of our Arduino its a little trickier than using printf and.
Bit Operations C is well suited to system programming because it contains operators that can manipulate data at the bit level –Example: The Internet requires.
A bit can have one of two values: 0 or 1. The C language provides four operators that can be used to perform bitwise operations on the individual bits.
Robotics Research Laboratory Louisiana State University.
Binary Arithmetic Math For Computers.
Binary Arithmetic Adding Binary numbers Overflow conditions
RM2F Input / Output (I/O) Pin grouping code!. I/O Pin Group Operations: The Spin language has provisions for assigning values to groups of bits in the.
I/O Ports CS-280 Dr. Mark L. Hornick 1. CS-280 Dr. Mark L. Hornick 2 Ports are channels from the CPU to external hardware and software Atmega32 has: 4.
A Simple Tour of the MSP430. Light LEDs in C LEDs can be connected in two standard ways. Active high circuit, the LED illuminates if the pin is driven.
Computer Sensing and Control How is binary related to what we are trying to accomplish in electronics? The PC GadgetMaster II uses binary to communicate.
ABCDNumber = Off 1 = On Binary Coded Decimal (BCD)
1 Homework Turn in HW2 tonight HW3 is on-line already Questions?
1 ARM University Program Copyright © ARM Ltd 2013 General Purpose I/O.
MCU: Interrupts and Timers Ganesh Pitchiah. What’s an MCU ?
11.1/36 Repeat: From Bits and Pieces Till Strings.
Number System. Number Systems Important Number systems – Decimal – Binary – Hexadecimal.
Bit Operations Horton pp Why we need to work with bits Sometimes one bit is enough to store your data: say the gender of the student (e.g. 0.
Working with 8-bit bytes and hexadecimal
Printer Port * 0= * 1= * 6= 60 1 * 4 = 4 = 164 Decimal Binary We use Ten Symbols
Warmup – 16FEB2012 This one is for practice. I have paper if you need it. Suppose there are eight, single-pole, single-throw (SPST) switches connected.
BM-305 Mikrodenetleyiciler Güz 2015 (4. Sunu) (Yrd. Doç. Dr. Deniz Dal)
CEC 220 Digital Circuit Design Number Systems & Conversions Friday, January 9 CEC 220 Digital Circuit Design Slide 1 of 16.
Introduction to Combinational Verilog EECS270 rev 9/25/12.
ECE Lecture 1 1 L15 –I/O Part II Department of Electrical and Computer Engineering The Ohio State University ECE 2560.
OFF = 0 ON = 1 = 63 BINARY system
CSCI1600: Embedded and Real Time Software Lecture 14: Input/Output II Steven Reiss, Fall 2015.
Arithmetic-logic units1 An arithmetic-logic unit, or ALU, performs many different arithmetic and logic operations. The ALU is the “heart” of a processor—you.
Arduino Mega Arduino Mega 2560 Arduino Mega 2560.
CEC 220 Digital Circuit Design Number Systems & Conversions Wednesday, Aug 26 CEC 220 Digital Circuit Design Slide 1 of 16.
AND Gate Inputs Output Input A (Switch) Input B (Switch) Output Y (Lamp) 0 (Open) 0 (OFF) A B Lamp.
Bit Manipulation in 'C' 'C' bit operators
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
The Hexadecimal System is base 16. It is a shorthand method for representing the 8-bit bytes that are stored in the computer system. This system was chosen.
1 Bits, Bytes, Binary & Hex CIS TAG ▪ Byte Bit.
Winter 2016CISC101 - Prof. McLeod1 Today How transistors can carry out commands in a CPU. For example, how to add two integers in an integrated circuit.
AVR Architecture Prepared By: Avdhesh Soni ( ) Sarthak Patel ( ) Akshay Parekh ( ) Fenil Sachla ( ) Guided.
LAB1 TYWU. Devices Dip Switch (4 Switches) Triple Output LED (RGB) –Common Cathode.
1 Transistor. 2 Transistors are used to turn components on and off They come in all different shapes and sizes.
IOT Design: An Embedded System Overview MicroControllers
Fundamentals of Computer Engineering
Chapter 4 Operations on Bits.
Bit Operations Horton pp
RFID - EN Encoding information Encoding information J.-D. Chatelain.
What to bring: iCard, pens/pencils (They provide the scratch paper)
Lesson 9: Digital Input-Output Signal Interfacing
Chapter 2 Push button and Potentiometer
I/O Ports in AVR Sepehr Naimi
Introduction to Programming
CISC101 Reminders Your group name in onQ is your grader’s name.
Bitwise Operations and Bitfields
Bitwise Operators CS163 Fall 2018.
I/O Ports in AVR Sepehr Naimi
Binary Lesson 3 Hexadecimal
Binary Lesson 3 Hexadecimal
CS 206D Computer Organization
Binary Lesson 4 Hexadecimal and Binary Practice
LCD (LIQUID CRISTAL DISPLAY) SCREENS
Multiplexing seven-segment displays
Binary Lesson 7 Review of Binary and Hexadecimal
Bit Operations Horton pp
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

Bit Masking To access or affect only the bits we want, we need to construct a byte with bits set in the locations of interest – This byte is called a ‘bit mask’ Use the bit mask with the appropriate bit-wise operator to get the desired result: – To set bits: bit-wise OR ( | ) with the bit mask – To clear bits: bit-wise AND ( & ) with the negated bit mask

Bit Masking Example 1 Set bits 3, 5, and 7 in DDRD (make pins to be OUTPUTS) without affecting the other bits (port-style): – recall: DDRD = 0b ; but this would make pins 6, 4, 2, 1, and 0 to be INPUTS, which might not be what we want – Instead: 1.Construct a bit mask with 1s in bit positions 3, 5, and 7 and 0s everywhere else 2.Bit-wise OR with DDRD and assign back to DDRD

Bit-Wise OR and AND XYZ Bit-wise OR (X | Y) XYZ Bit-wise AND (X & Y)

Bit Masking Example 1, cont. (setting bits 3, 5, and 7 in DDRD) 1.Bit mask with bits set in positions 3, 5, and 7: Many ways to do this: 1)Write directly in binary: 0b )Write in hex or decimal: 0xA8 or 168 3)Left-shift with OR: (1<<7 | 1<<5 | 1<<3) 2.Bit-wise OR with DDRD and assign back DDRD = DDRD | (bit mask from step 1) DDRD = DDRD | (1<<7 | 1<<5 | 1<<3); DDRD | = (1<<7 | 1<<5 | 1<<3); // ‘shortcut’

Bit Masking Example 2 Your turn: turn on the pull-up resistors for pins 1, 4, and 6 (assuming that they are already made to be INPUTS) without affecting the other pins PORTD | = ( (1<<1) | (1<<4) | (1<<6) );

Bit Masking Example 3 Turn off the pull-up resistors for pins 1, 4, and 6 without affecting the other pins – Need to clear bits 1, 4, and 6 in PORTD register without affecting the other bits 1.Construct a bit mask with 1s in bit positions 1, 4, and 6 2.Bit-wise AND PORTD with the negated bit mask and re-assign to PORTD

Negate Bits Negate bits with the ~ operator ~ 

Bit Masking Example 3, cont. (clear bits 1, 4, and 6 in PORTD) Your turn: 1.Construct a bit mask with 1s in bit positions 1, 4, and 6 2.Bit-wise AND with the negated bit mask: byte bit_mask = (1<<6) | (1<<4) | (1<<1); PORTD &= ~bit_mask;

Summary So Far To ‘set’ bits port-style: 1.Construct a bit-mask with bits in the location(s) of interest 2.Execute bit-wise OR ( | )with assignment back to the register of interest To ‘clear’ bits port-style: 1.Construct a bit-mask with bits in the location(s) of interest 2.Execute bit-wise AND ( & ) (with the negated bit mask) with assignment back to the register of interest

Determining if a bit is set or cleared Arduino style (easy!): if( digitalRead(pin) ) Port-style: 1.Construct a bit mask with bits in the locations of interest 2.Bit-wise AND the PINx register with the bit mask 3.Test the result  Example:  Is a SPST switch on pin D1 closed?

Example for determining if a bit is set or cleared Suppose a SPST switch between pin D1 and ground Take action if it is closed Arduino style (easy!) if ( digitalRead(pin_D1) == LOW ) { do stuff; } Port-style: – Construct the bit mask: byte bit_mask = ( 1<<1 ); – Bit-wise AND the bit mask with the PIND register – Test the result if ( (PIND & bit_mask) == bit_mask ) { do stuff; }