Session 7 Slides - Wolf Den Electronics

Download Report

Transcript Session 7 Slides - Wolf Den Electronics

Session 7
John Wolf
(WolfDenElectronics.com)
Objectives – We’ll Learn
 74HC165 Parallel to Serial Shift Reg
 74HC595 Serial to Parallel Shift Reg
 555 Timer
Important because serial data links are everywhere
and fundamentally use shift registers to move data.
Also, tricky uses are found all over robotics projects
to set control bits, motor speeds, move response
data from sensors to microcontrollers.
74HC165 Shift Register
LSB
MSB
With a parallel load, the H bit on the right should represent the
LSB to match binary numbers, because it’s the first bit readout. But if a data array reads in this data, the first bit will go
into x[0], then x[1], etc., will preserve this reversed condition.
Care has to taken to keep the bit order correct or redefine the
order from the chip its self. Blame Texas Instruments!
Logic Gates: building block of digital electronics
A
C
B
A
C
B
D
P
Q
CLK
A
Q
CLR
C
B
A
A
L
H
D
P
Q
H
D
CLK
P
Q
D
CLK
CLR
Q
L
P
Q
D
CLK
CLR
Q
P
Q
CLK
CLR
Q
CLR
Q
Clear
Write
After Clear
L
L
L
L
After the clock
H
L
H
L
The “which end is LSB” ?
 Big-Endian – meaning the MSB is to
the right
 Little-Endian – the LSB is to the right,
matching binary representation
It matters not, but you have to know how your system
is designed in order to correctly do math.
/* Watches for a change of state of any of 8 buttons attached
to the A-H inputs of a 74HC165 shift register and displays
the results on LEDs. The A bit of the register is on the LSB
or right end of the button row in this design to match binary
numbering.
*/
#define DATA_WIDTH 8 // Could add more chips in series
const
const
const
const
const
int
int
int
int
int
SH_LD
= 2; // Parallel load pin 1 on the 165
CLK
= 3; // Clock pin 2 on the 165
CLK_INH = 4; // Clock Inhibit/Enable pin 15 on the 165
DATA_IN = 5; // Q7 pin 9 on the 165
led[]={6,7,8,9,10,11,12,13}; // index mapped to pins
unsigned int bitVal;
byte byteVal=0;
void setup()
{
Serial.begin(9600);// troubleshooting only
pinMode(SH_LD, OUTPUT); // HIGH-shift, LOW-load parallel bits
pinMode(CLK, OUTPUT);
// continuous or via software as a for loop
pinMode(CLK_INH, OUTPUT); // for inhibiting a steady input clock
pinMode(DATA_IN, INPUT); // serial data from the 165
for(int i=0; i<8; i++) { // efficient way to make LEDs OUTPUTs
pinMode(led[i], OUTPUT); // cool trick!
}
digitalWrite(CLK, LOW); // clock idle
digitalWrite(SH_LD, HIGH); // shift mode
}// end setup
void loop(){
read_shift_reg();
}// end loop
//*** FUNCTION ***//
unsigned int read_shift_reg() {
digitalWrite(CLK_INH, HIGH);
digitalWrite(SH_LD, LOW);
delayMicroseconds(1);
digitalWrite(SH_LD, HIGH);
digitalWrite(CLK_INH, LOW);
for(int i = 0; i < DATA_WIDTH; i++)
{
bitVal = digitalRead(DATA_IN);
byteVal |= (bitVal << ((DATA_WIDTH-1) - i));
digitalWrite(CLK, HIGH);
delayMicroseconds(5);
digitalWrite(CLK, LOW);
delayMicroseconds(5);
}
// only load with clock inhibited
// not needed, no continuous clk
// now clock enabled
// Qh pin 9 165
//load MSB to far left
00000000
1
10000000
//Serial.println(byteVal, BIN); // 10000000
for(int i = 0; i < DATA_WIDTH; i++){
if((byteVal >> i) & 1){
// byteVal is read from LSB side
digitalWrite(led[i], HIGH);
}
else{
digitalWrite(led[i], LOW);
}
}
byteVal=0; // clears the LEDs, but if commented out, LEDs accumulate being on
}//end read_shift_register function
UTF-8( also Unicode character set)
Other ‘bass ackward’ Concepts
 Current flow
 Movement of electrons in a circuit
 Measured in amperes
 Ben Franklin defined current flow
 Physics defines electron flow to be in
the opposite direction
 Sooo, we have two ways of defining
current flow in the world today!
“Conventional” Current Definition vs Electron Flow
Conventional Current Flow
 Uncle Ben’s approach says current
leaves a positive pole and goes
around a circuit to the negative pole
 Electrical engineering is based on this
concept although it’s incorrect
 All electronic part diagrams and
symbols use this convention
 Must be aware of this when reading
schematics
Electron Flow
 Electrons actually move from a
negative pole towards a positive pole
 When we need to understand how
capacitors, inductors, motors, or
electrolytic circuits work, we use
electron flow
 Welcome to the real world!
Arduino ShiftOut/In Statements
shiftOut(pin, clock, MSBFIRST, data);
This Arduino method is available to send serial data out
a data line with a clock line. You can send data
LSBFIRST also
byte result = shiftIn(pin, clock, LSBFIRST);
Shifting data in is just as easy. Always check the
resulting variable to make sure the data is in the
correct order.
Earlier example is called “bit banging” because each bit is
clocked in a shift register and moved to where you want
individually in your sketch instead of using a Class method or
Library
Also, the SPI Library can be used on pins 11,12,13 to move serial
data faster, because SPI works at the Assembly Language level.
ULN2003 Darrington Pair Driver – 500ma with flyback diodes
If you use a shift register to turn on
relays or drive anything above 10 to
20ma, use a hardware driver like this
one.
In
GND connection for all
Typical 74HCxxx chips can supply up
to ~10ma
The 74HC595 Shift Register
 74HC595 is a serial in/parallel out device
for 8 bits
 Used to load command bits into the
register, ‘flash’ them as an output, and then
load up the next 8 bits and repeat
 If a bit is used for motor speed, whether it’s
a HIGH for that cycle or LOW can be seen
over time as PWM duty-cycle
 Other bits could represent motor direction
 With just 4 bits you could control a DC
motor, so two motors could be controlled
with just three control pins to this SR
74HC595 Shift Register
Tri-stated outputs are not seen by
a bus when OE is high. When low,
the Q data is on the output pins.
The pin names are terrible. DS
is your serial data coming in.
Q7S is serial data if you
continue to run the clock, but
this isn’t used much. Master
reset and the Q outputs are
easy to understand. Think of
SHCP as the data clock. STCP
is really a one pulse latch to
store the data to be seen on
the output pins. On top of all
this is a tri-state output buffer
so you can place this chip on a
bus with other chips.
/* Use 74HC595 SR to display:
opt1: B10101010
opt2: or running lights
*/
const
const
const
const
int
int
int
int
SER = 2;
LATCH = 3;
CLK = 5;
MR = 6; // LOW resets the outputs to LOW
// leave in for option 2
int seq[14] = {1, 2, 4, 8, 16, 32, 64, 128, 64, 32, 16, 8, 4, 2};
void setup() { // OE is hardwired low so Q outputs are seen
pinMode(SER, OUTPUT);
pinMode(LATCH, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(MR, OUTPUT);
digitalWrite(CLK, LOW);
digitalWrite(MR, LOW);
digitalWrite(MR, HIGH);
digitalWrite(LATCH, LOW);
// leave in for opt1
//shiftOut(SER, CLK, MSBFIRST, B10101010); // outputs are Q0 to Q7 so this will match
//digitalWrite(LATCH, HIGH);
}// end setup
void loop() {
//opt1 leave out - blank loop
for (int i = 0; i < 14; i++) { // 14 numbers in the array
digitalWrite(LATCH, LOW);
digitalWrite(CLK, LOW); //per shiftOut syntax rules
shiftOut(SER, CLK, MSBFIRST, seq[i]);
digitalWrite(LATCH, HIGH);
delay(100);
}
}// end loop
Notice the Q outputs are labeled where the first bit to enter the
register rolls down to Q7, so you need to fill with MSBFIRST so the
bits Q0 thru Q7 make sense.
555 Timers
John Wolf
555 Running Modes
 Astable - free running mode: oscillator
 Monostable mode: "one-shot“
 Bistable mode: “flip-flop”
What’s Inside…
So how do comparators work?
 The + and – mean non-inverting and
inverting respectfully
 Whichever has an input voltage that
is larger than the other, dominates
the output
 + > - output is high
 - > + output is low
Test set up to see how a comparator works
Vref = 2.5V because resistors are equal -> half the supply voltage.
When the pot delivers > 2.5V, the output shifts from 0 to 5 volts
+ > - , output is 5V, LED off
- > +, output is 0V, LED on
555 Pin Out

Pin 1 (Ground):Connects to the power supply ground.

Pin 2 (Trigger):Detects 1/3 of rail voltage to make output HIGH. Pin 2 has
control over pin 6. If pin 2 is LOW, and pin 6 LOW, output goes and stays
HIGH. If pin 6 HIGH, and pin 2 goes LOW, output goes LOW while pin 2 LOW.
This pin has a very high impedance (about 10M) and will trigger with about
1uA.

Pin 3 (Output):(Pins 3 and 7 are "in phase.") Goes HIGH (about 2v less than
rail) and LOW (about 0.5v above 0v) and will deliver up to 200mA.

Pin 4 (Reset):Internally connected HIGH via 100k. Must be taken below 0.8v
to reset the chip.

Pin 5 (Control):A voltage applied to this pin will vary the timing of the RC
network (considerably).
Pin Out (cont.)

Pin 6 (Threshold):Detects 2/3 of rail voltage to make output LOW only if
pin 2 is HIGH. This pin has a very high impedance (about 10M) and will
trigger with about 0.2uA.

Pin 7 (Discharge):Goes LOW when pin 6 detects 2/3 rail voltage but pin
2 must be HIGH. If pin 2 is HIGH, pin 6 can be HIGH or LOW and pin 7
remains LOW. Goes OPEN (HIGH) and stays HIGH when pin 2 detects
1/3 rail voltage (even as a LOW pulse) when pin 6 is LOW. (Pins 7 and
3 are "in phase.") Pin 7 is equal to pin 3 but pin 7 does not go high - it
goes OPEN. But it goes LOW and will sink about 200mA.

Pin 8 (Supply):Connects to the positive power supply (Vs). This can be
any voltage between 4.5V and 15V DC, but is commonly 5V DC when
working with digital ICs.
Trigger Comparator
 Notice the non-inverting input is pre-set to 1/3
of rail voltage in the 555 timer
 The voltage on the cap rises to near rail value
(less the voltage drop of the external resistors)
 If a negative going pulse shows up on the
trigger, as soon as it passes below 1/3 rail
value, the “set” on the R-S flip-flop is triggered
 Prior to a trigger, the Threshold comparator
has a high out and the Trigger comparator a
low value, keeping the output low
Threshold Comparator
 Normally output is high giving a reset
to the flip-flop, because the noninverting input is about 1/3 higher
than the reference of 2/3 of rail.
 A trigger sets the flip-flop causing the
cap to be discharged
 This condition stays until the cap
charges up to 2/3 rail, assuming the
trigger pulse has gone back to high
Modes: astable, bistable, monostable
 Depending on whether the external
resistors and cap are arranged, we
can cause a single pulse to be output,
or oscillate, or have one pulse for on
and one pulse for off modes
Astable Mode
 Using a resistor between the trigger
and threshold pins, the charge and
discharge of the cap continues with
one RC constant determining the first
halve wavelength and the sum of the
resistors and the cap’s RC constant
determining the second halve of the
wavelength or duty-cycle.
Bistable Mode
 Leave the threshold and discharge
pins open
 Use the trigger to start the output
pulse
 Use the reset (pin 4) to reset the
output low
Monostable Mode
 Short the threshold to the discharge
pins between the R and the C
 Use a fast negative spike to trigger
the timer
 The trigger starts the output pulse
and the RC time constant to 2/3 rail
dictates the duration to reset the
output to low
 Trigger is quick to get out of the way
of the cap charging
Astable Circuit
Bistable Circuit
Monostable Circuit
Viable Trigger Circuit
Cap is normally charged up. Switch to GND causes a negative
going spike from +5 to GND with an RC constant large
enough to provide the 555 chip a good input trigger as cap
recharges. When the switch goes back to +5, a positive spike
tries to develop. The diode keeps positive spike near +5. At
0.2V (diode forward value), the spike is clipped (Schottky
diode 0.2, silicon diode 0.7, high current diode 1.2).
Trigger pulse on oscilloscope
trigger level
1.67V
Alternative Trigger
Use an R-S flip-flop and a momentary set-reset via an inverting
buffer to get the negative going pulse. The timing has to be
shorter than the output pulse or output is continuous until the
trigger goes away plus the pulse time.
This type of circuit can get fancy so you can have an LED
indication, etc.
Or use a microcontroller to create an accurate, short pulse and
keep track in an activity data log. This approach is useful if the
555 is running at a high voltage or long output pulse so controller
isn’t hung waiting around for the pulse to end. Or the controller
gates a astable 555 or PWM stream independent of the controller
activity.
Timer 1 triggers timer 2, output variable width
+5
R1=150
R1
8
7
R3
C2
1
8
7
4
3
2
C3
880usec to 15.2ms
555
6
3
5
C1
4
2
R2
R3=2k
555
6
R2=1M
+5
5
1
C3=2.2uF
C1=1uF
C2=.01uF
1Hz
Diodes 1N4148
152usec
+5
R4
R4=50k
OUTPUT
So what’s next…?
 Any questions from the first 5
sessions
 Possible topics:
 Network based connection to Arduino
 Other serial interfaces – MIDI, one-wire
 Data logging
 Build something using these
techniques or at least design it