Bit Fiddling and Arduino Digital IO
Bit Fiddling and Arduino Digital IO
These slides use the resources online from Prof. Murali Annavaram,
Prof. Mark Redekopp, and Prof. Allan Weber,
School of Engineering, USC Viterbi.
2
Introduction
• The primary way that software controls hardware is
by manipulating individual bits
• We need to learn how to:
– Set a bit to a1
– Clear a bit to a0
– Check the value of a given bit (is it 0 or 1)
• Because computers do not access anything smaller
than a byte (8-bits) we must use logic operations to
manipulate individual bits within abyte
3
BIT FIDDLING
4
0 0 0 0 0 0 0 0 0
Force Pass
Invert Pass
'0'
0 1 0 0 1 1 0 1 1
1 0 0 1 0 1 1 0 1
1 1 0
'1'
1 1 1 1 1 1
1 AND x = 0 1 OR x = x 1 XOR x = x
2 AND x = x 2 OR x = 1 2 XOR x = NOT x
x AND x = x x OR x = x x XOR x = 0
You already T1 X + 0 =X T1' X • 1= X
knew the above
ideas. It is just
T2 X + 1 =1 T2' X • 0= 0
T1-T3. T3 X+ X= X T3' X • X =X
7
Bitwise Operations
• The CAND, OR, XOR, NOT bitwise operationsperform
the operation on each pair of bits of 2 numbers
#include <stdio.h> // C-Library
// for printf()
0xF0 1111 0000
AND 0x3C AND 0011 1100 int main()
{
0x30 0011 0000 char a = 0xf0;
char b = 0x3c;
fiddling" v ? ? ? ? ? ? ? ?
– Change the value of a bit in a registerw/o affecting
other bits &
• Determine appropriate constant bit patterns v ? ? ? ? ? ? ? 0
(aka masks) that will change some bits while
leaving others unchanged
• Masks can be written in binary, hex, or even decimal (it v ? ? ? ? ? ? ? ?
is the programmer's choice…whatever is easiest)
|
• Examples (Assume an 8-bit variable, v)
v 1 ? ? ? ? ? ? ?
– Clear the LSBto '0' w/o affecting otherbits
• v = v & 0xfe; or equivalently
• v = v & ~(0x01); v ? ? ? ? ? ? ? ?
– Set the MSB to '1' w/o affecting other bits
• v = v | 0x80; ^ 0 0 0 0 1 1 1 1
– Flip the LS 4-bits w/o affecting other bits v ? ? ? ? ? ? ? ?
• v = v ^ 0x0f;
9
v ? ? ? ? ? ? ? ?
• Some practice problems:
|
– Set bit 0 of v to a ‘1’
v = v | 0x01;
– Clear the 4 upper bits in v to ‘0’s v ? ? ? ? ? ? ? ?
v = v & 0x0f;
&
– Flip bits 4 and 5 in v
v = v ^ 0b00110000;
v ? ? ? ? ? ? ? ?
v ? ? ? ? ? ? ? ?
• Tocheck for a given set of bits we usea
bitwise-AND to isolate just thosebits & 1 0 0 0 0 0 0 0
– The result will then have 0's in the bit locations not
of interest
– The result will keep the bit values of interest v ? ? ? ? ? ? ? ?
• Examples & 0 0 0 0 0 1 0 0
– Check if bit 7 ofv = '1'
if ( (v & 0x80) == 0x80) { code } or
if (v & 0x80) { code } v ? ? ? ? ? ? ? ?
– Check if bit 2 ofv = '0' & 0 0 0 0 0 1 1 1
if ( (v & 0x04) == 0x00) { code } or
if ( ! (v & 0x04) ) { code }
– Check if bit 2:0 ofv = "101" v ? ? ? ? ? ? ? ?
if ( (v & 0b00000111) == 0b00000101) {..}
& 0 0 1 1 0 0 0 0
– Check if bit 5-4 ofv = "01"
if ( (v & 0x30) == 0x10) { code }
11
Shift Operations
• In C, operators '<<' and '>>' are the shift operators
<< = Left shift
>> = Right shift
• Format: data << bit_places_to_shift_by
• Bits shifted out and dropped on oneside
• Usually (but not always) 0’s are shifted in on the other side
x = x >> 2; x = x << 2;
Right Shift by 2 bits: Left Shift by 2 bits:
x 0 0 0 0 1 1 0 0 0 0 0 0 1 0 1 0 x
Original x Original x
0’s shifted in… 0’s shifted in…
0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 1 0 0 0
x x
x Shifted by 2 bits x Shifted by 2 bits
16
Another Example
• To get a 1 in a particular bit location it is easier to
shift the constant 1 some number of places thantry
to think of the hex or binary constant
0x01
0 0 0 0 0 0 0 1 = +1
1 << 3 1 << 5
0’s shifted in… 0’s shifted in…
0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0
0x08 0x20
Suppose we want a 1 in bit Suppose we want a 1 in bit
location 3. Just take the value location 5. Shift 1 5 spots to
1 and shift it 3 spots to the left the left. Easier than coming
up with 0x20…
17
Putting it AllTogether
• Values for working with bits can be made using the ‘<<‘ shift operator
– OK: PORTB |= 0x20; Better: PORTB|= (1 <<5) ;
– OK: DDRD|= 0x04; Better: DDRD|= (1 <<2);
• This makes the code more readable and your intention easierto
understand…
• More examples
– Set DDRC,bit 5: DDRC|= (1 <<5)
– Invert PORTB,bit 2: PORTB^= (1 << 2)
? ? ? ? ? ? ? ? PORTD
– Clear PORTD,bit 3: PORTD&= (1 << 3) 0 0 0 0 1 0 0 0
&
• WRONG! Why?
0 0 0 0 ? 0 0 0
? ? ? ? ? ? ? ? PORTD
– Clear PORTD,bit 3: PORTD &= (0 <<3)
& 0 0 0 0 0 0 0 0
• WRONG! Why?
0 0 0 0 0 0 0 0
Arduino Uno
• The Arduino Uno is a Printed circuit (PC) board with
processor and other circuits for
microcomputer development programming the system and
interfacing other devices
board based on the Atmel
ATmega328P 8-bit processor.
• Most microcomputer
manufacturers (Atmel,
Freescale, etc.) produce small
PCboards with their chips on
them for engineers to
experiment with and http://arduino.cc/en/Main/ArduinoBoardUno
hopefully generate sales of
Atmega328P 8-bit
the product. processor
22
Arduino Uno
• Arduino
– An Italian company
– They make numerous boards with different processors
– Hardware and software are open source.
– Very popular with hobbyists, due in a large part to their low cost.
http://arduino.cc/en/Main/Products
23
Arduino Uno
• What’s on an Arduino Uno board?
Connectors for I/O lines D0 – D13
Reset button
USB interface
Power connector
(for use when not
connected to USB) Power and I/O lines A0 –A5
ground pins
24
Arduino Uno
• Arduino Unos can be stacked with "shield"
boards to add additional capabilities (Ethernet,
wireless, D/A, LCDs, sensors, motor control,etc.)
25
Flashback to Week1
• Recall the computer interacts with any input or output (I/O)
device by simply doing reads/writes tothe memory locations
(often called registers) in the I/O interfaces…
• The Arduino has many of these I/O interfaces all connectedvia
the data bus
Processor Memory
0
…
3FF
A D C
Video
Interface 800
FE may 800 FE FE
signify a WRITE
…
white dot at
a particular 01
location
Keyboard
Interface
Atmel ATmega328P
• The Arduino Uno is
based on an Atmel
Mem.
ATmega328P 8-bit
microcontroller Processor
– 32kb of FLASHROM
– 2048 bytes of RAM
– 23 I/O lines
– 3 timer/counters
– Serial/SPI/I2C interfaces
– A/D converter Data Bus
28
Main Point: Individual pins on the Arduino can be used as inputs ORoutputs
29
Groups B, C and D
• The Arduino provides around 20
separate digital input/output bits that
we can use to interface to external
devices
• Recall computers don't access individual
bits but instead the byte (8-bits) is the
Group D Group B
smallest unit of access
• Thus to deal with our digital inputs we
Group C
will put the bits into 3 groups: Group B,
C, and D
– We often refer to these groups as"ports"
but you'll see that "port"is used in multiple
Software to Arduino Name Mapping
places so we'll generally use "group" Group B bit5-bit0 = DIG13-DIG8
Group C bit5-bit0 = A5-A0
Group D bit7-bit0 = DIG7-DIG0
30
Main Point: Each pin has a name the software uses (Portx) and a name used on the Arduino circuit board (Anx or DIGx)
31
A button
Arduino A0 ó GroupC bit 0
32
Overview
• In the next few slides you will learn
– What your software needs to do to setup
#include <avr/io.h>
the pins for use as digital inputs and/or int main()
{
outputs // check input Group C
// bit 0 value
– Toset bits (to 1) and clear bits (to0) using // set output Group B
// bit 2 to Logic 1=5V
bitwise operations (AND, OR, NOT) to // ...
control individual I/O pins }
PORTD PORTB
PORT PORT PORT PORT PORT PORT PORT PORT PORT PORT PORT PORT PORT PORT
D7 D6 D5 D4 D3 D2 D1 D0 B5 B4 B3 D2 D1 D0
PIND PINB
PIND7 PIND6 PIND5 PIND4 PIND3 PIND2 PIND1 PIND0 PINB5 PINB4 PINB3 PIND2 PIND1 PIND0
34
http://www.toro.com/en-
us/homeowner/yard-
PD[7:4] PD[3:0] PB[5] tools/blowers-
vacs/pages/series.aspx?sid
=gasblowervacsseries
35
PORTD PORTB
PORTD7 PORTD6 PORTD5 PORTD4 PORTD3 PORTD2 PORTD1 PORTD0 PORTB5 PORTB4 PORTB3 PORTB2 PORTB1 PORTB0
1 1 0 0 1 0 0 1 0 0 0 0 1 0
Main Point: For pins configured as outputs, the values you put in the PORT register will be the outputvoltages
36
EXAMPLES
41
Can be
discrete
LED is on when LED is on when gate or
gate outputs '1' gate outputs '0' Arduino
output pin
42
Vin R ≈ inf.
Blinking an LED
• Hardware and software to make an LEDconnected to D7blink
#include<avr/io.h> DDRD
#include<util/delay.h>
int main() ? ? ? ? ? ? ? ?
{
// Init. D7 to output |
DDRD |= 0x80;
// Repeat forever 1 ? ? ? ? ? ? ?
while(1){
// PD7 = 1 (LED on)
PORTD |= 0x80;
PORTD
_delay_ms(500);
? ? ? ? ? ? ? ?
// PD7 = 0 (LED off)
PORTD &= ~(0x80);
_delay_ms(500);
|
}
// Never reached 1 ? ? ? ? ? ? ?
return 0;
}
&
0 ? ? ? ? ? ? ?
44
Vdd Vdd
PB Arduino + V -
PD4 PD7
LED
GND
45
Pull Up Resistors
• Adding and wiring pull-up resistors for input buttons can be time
consuming…
• Thankfully, each Arduino input bit has an optional internal“pull-
up resistor” associated with it.
– If the pull-up is enabled, in the absence of an input signal, the input bit
will be “pulled” up toa logical one.
– The pull-up has no effect on the input if an active signal is attached.
This pull-up resistor can be
built separately on your
circuit board ORthere is one
on each pin of the Arduino
that can be enabled
Arduino
Arduino Arduino
PORTD
PORTD7 PORTD6 PORTD5 PORTD4 PORTD3 PORTD2 PORTD1 PORTD0
1 1 1 1 0 0 1 1
Enable Pull-Up Resistors Actual output values from PD3-0
PIND
PIND7 PIND6 PIND5 PIND4 PIND3 PIND2 PIND1 PIND0
0 1 1 0 ? ? ? ? PD[7:4] PD[3:0]
Made-up values read from the push- (connected
buttons (which don't require you to to buttons)
0011
wire up external pull-up resistors)
48
Arduino
Arduino
Arduino
Enabled in the Arduino
Built Separately
49
DEBOUNCING SWITCHES
51
Counting Presses
• Consider trying to build a systemthat #include<avr/io.h>
counted button presses on PC2 int main()
{
PC2
Arduino
PC2
cnt 0 0 1 2 3 3
52
PC2
Arduino
PC2
cnt 0 0 0 0 0 1 1
53
Arduino
Button Button
Press Release
54
bouncing
PC2
Arduino
PC2
cnt 0 0 0 0 0 1 1
55
Exercise 1
• We want to use Group C(Port C) bit 5 as an
output. Show how you should initialize your
program then write a statement to turn the
output 'ON' to 5V.
58
Exercise 2
• Now turn write a statement to turn that
same output ‘OFF’ (i.e. to output 0V)
59
Exercise 3
• We want to use Group B(Port B) bit 3 as an input connected
to a switch. You have no separate resistors available to you.
Show how you should initialize your program and then write
an if statement to checkif the input voltage isHIGH (5V).
60
Common Mistakes
// Clearing a bit to 0
7 6 5 4 3 2 1 0
7 6 5 4 3 2 1 0
TCCR0B WGM03 WGM02 CS02 CS01 CS00
ADCH WGM23 WGM22 CS22 CS21 CS20
TCCR2B
(8-bit ADCres.)
7 6 5 4 3 2 1 0
I/O tasks
15 14 13 12 11 10 9 8 TIMSK0 OCIE0B OCIE0A
OCIE2B OCIE2A
ADC
0 0 0 0 0 0 TIMSK2
7 6 5 4 3 2 1 0
(10-bit ADCres./ OCR0A/ OCR2A
7 6 5 4 3 2 1 0
16-bit value)
(8-bitvalue)
TimerMAXor PWM
After ADCSRA |= (1 << ADSC); 7 6 5 4 3 2 1 0
initialization, while( (ADCSRA & (1 << ADSC)) != 0 ) { } OCR0B/ OCR2B
start,poll, unsigned char result = ADCH; (8-bit value)for PWM
capture // if 10-bit result (ADLAR = 0) use this
7 6 5 4 3 2 1 0
// unsigned int result = ADC; TCNT0 / TCNT2
7 6 5 4 3 2 1 0
Pin Change Interrupts
RXC0 UDRE0
UCSR0A
PCICR PCIE2 PCIE1 PCIE0
7 6 5 4 3 2 1 0 (D) (C) (B)
Int. Enables