Shift register question - Page 1
Forum section: electronics / beginners

author: veedub565
author_rank: Frequent Contributor
timestamp: May 11, 2020, 08:46:23 pm
content: Hi, have a piece of R&S test kit. I have a new (to me) accessory for it, but the external serial interface output is disabled in software. To enable it, I need to program the shift register and set a bit (low to activate) I've never had to program a shift register before, There are plenty of videos out there which I will watch, but some pointers would also be appreciated.The shift register appears to be Serial/Parallel, 12 bit but I'm may be reading it wrong.The bits are written from 1 to 56, and every 8 bits is a byte. I need to set bit 47, byte 6 lowThe interface for accomplishing this is (TTL)CLK, DATA, STROBESo do I do this from a laptop, or do I need to make something to program it with ?

author: brucehoult
author_rank: Super Contributor
timestamp: May 12, 2020, 01:53:15 am
content: You need something with three digital outputs (GPIO). It doesn't matter what. You could use three pins on a PC parallel (centronics) port, or GPIO on a Raspberry Pi, or digital outputs on an Arduino connected to a PC using USB.Speed doesn't matter, so you could even do it using three toggle or push button switches. You also need to know what values all the other bits should have.You do something like this:Repeat 56 times {Set the DATA switch to the value for the next bitTurn the CLK switch on and then off}Turn the STROBE switch on then offBut is this interface even exposed for you to use?

author: veedub565
author_rank: Frequent Contributor
timestamp: May 12, 2020, 06:47:50 am
content: Thanks for the info BruceYes the port is exposed for me to use via the "control" socket at the rear of the instrument. Not sure what values the others bits should have, there is a table of what all the bits are but not what they are set to by default.

author: veedub565
author_rank: Frequent Contributor
timestamp: May 12, 2020, 08:07:35 am
content: Having watched a few videos now I think I understand. SO for my 56 bits I could have 56 dip switches (not really practical but in theory, to help visualise it) I'd set each DIP switch for the state I want that bit to be in. I'd then clock them all into the shift register, and strobe (or latch) would then output those states.So if I had 4 bits with an LED on each output. If I clocked in 1001 then the output would be on-off-off-on. To change it I'd need to clock in another 4 bits 1101 (on-on-off-on)So it doesn't seem possible to just set one individual bit on or off, you have to set all of them. Which could be problematic not knowing what state all the other bits are supposed to be in.

author: brucehoult
author_rank: Super Contributor
timestamp: May 12, 2020, 08:13:58 am
content: If there is a "data out" pin then you could possibly read that and toggle the CLOCK line 56 times and read the data out pin each time to find out what was put in the shift register by software when it booted up -- as long as nothing else was shifted into it since last time the STROBE input was set.

author: brucehoult
author_rank: Super Contributor
timestamp: May 12, 2020, 08:43:21 am
content: Quote from: veedub565 on May 12, 2020, 08:07:35 amWithout knowing anything about that particular piece of equipment but just going off general shift register principles, right.You could attach your 56 DIP switches to a "parallel to serial" shift converter such as the SN74LS165 or other parts with similar numbers e.g. 74HC165 etc.QuoteRight, except that if you have your 56 bit shift register then when you clocked in the 1101 the 1001 would just end up setting the next four bits you then you'd have 00000...000010011101.QuoteRight.You can make it *appear* that you only change 1 bit, but to do that you have to clock in all 56 bits. It takes a finite amount of time to clock them in -- for example with the 74HC595 (serial to parallel) the maximum clock speed is 4 MHz with a 2V power supply, or 25 MHz with a 4V power supply. So clocking in 56 bits can take as little as 2 us. And then you hit the STROBE and all the outputs change at the same time.Both the 595 and 165 have 8 parallel inputs/outputs but you can chain together as many as you want.

author: brucehoult
author_rank: Super Contributor
timestamp: May 12, 2020, 09:09:38 am
content: Here's a tutorial on how to use an Arduino to send things to a chain of 595 or similar shift registers.https://www.arduino.cc/en/tutorial/ShiftOutIf you haven't used an Arduino before, the standard one for general learning and prototyping and noodling around is the "Uno". You can get a real one for $22 or a Chinese clone (perfectly legal, it's open source) for about half that:amazon.com/Arduino-A000066-ARDUINO-UNO-R3/dp/B008GRTSV6amazon.com/ELEGOO-Board-ATmega328P-ATMEGA16U2-Compliant/dp/B01EWOE0UUOnce you've prototyped something you can of course just keep the Uno or you can get the same functionality on a smaller and cheaper board that is better for building into a product:https://www.amazon.com/Mini-ATMEGA328-ATMEGA328P-AU-Arduino-Atmega328P/dp/B082DSNTK2Or you can even just get a bare chip. I do a lot of little projects with the ATTiny85, which has 1/4 as much program space (8 KB) and RAM (512 bytes) and I/O pins (5-6) as the ATMega328 in the Uno, but doesn't need any external components at all and costs under $1 in volume, or typically $2 each for 5 of them:https://www.amazon.com/Original-Atmel-Dip-8-ATTINY85-20PU-Tiny85-20Pu/dp/B06W9JBJJ6The Uno has a USB port to connect it to a PC (Windows / Mac / Linux) and power it. You write C++ code in the Arduino IDE, compile it, and download it via the USB. It's very easy to get started with.The Pro Mini or a bare chip are a bit harder to get your program into because they don't have USB. You either use a special USB to serial cable (Pro Mini) or a USB programmer (bare chip) -- or connect a handful of wires from an Uno to the little board/chip and use a program running on the Uno to download your code to it.Sorry if you already knew this stuff.

author: veedub565
author_rank: Frequent Contributor
timestamp: May 12, 2020, 10:08:27 am
content: Thanks for the help Bruce,I don't know any of this stuff, never had to program a shift register or used an arduino. I have a Raspberry pi3, but never had much luck getting things to work on it.

author: veedub565
author_rank: Frequent Contributor
timestamp: May 15, 2020, 04:08:17 pm
content: I found out a bit more information about the shift register I need to program (attached). Bit 47 EXON is the one I need to change.I'm going to try and do this with the Raspberry Pi3 I have.First I think I'll breadboard some daisy chained shift registers with LED's and practise. Make sure I've got the hang of it before trying to program the instrument.Will be a nice little project for me.

author: brucehoult
author_rank: Super Contributor
timestamp: May 16, 2020, 07:09:00 am
content: That looks as if you've got the default values documented there too.Nice to see they explicitly label the first and last bits shifted in.

author: veedub565
author_rank: Frequent Contributor
timestamp: May 16, 2020, 09:17:47 pm
content: Yes I realised afterwards that it shows what appear to be the default values for the various bits.

author: veedub565
author_rank: Frequent Contributor
timestamp: May 18, 2020, 07:28:12 pm
content: Ok so I understand how a shift register works now. I'm a bit confused by programming it. Looking at some online guides:"A byte is really nothing but a sequence of 8 bits like 0b10100000. Shifting bits does exactly what you might think- you move them to the left or to the right."Well "0b10100000" that's 10 characters not 8Lets say I have 1 byte and I want to set 8 bits as follows 11010010. There's no "b" in thatIt's a Raspberry Pi I'm trying to do it with, so maybe 0b10100000 is some odd complicated way of doing a simple job.

author: helius
author_rank: Super Contributor
timestamp: May 18, 2020, 08:26:48 pm
content: 0b10100000is the syntax used by C++14 (the programming language) to denote an integer in base 2 (binary). The author of the guide you read probably assumed that everyone speaks C.The simple way to write it is just160(entirely equivalent).

author: james_s
author_rank: Super Contributor
timestamp: May 18, 2020, 08:57:24 pm
content: You should probably familiarize yourself with base notation and conversions, ie how base 2 (binary), base 10 (decimal) and base 16 (hex) numbers are represented and converted from one to the other. You don't need to memorize how to convert them, there are countless tools to do that but it's good to have a basic understanding of what's going on. You will deal with this a LOT in most programming.

author: brucehoult
author_rank: Super Contributor
timestamp: May 19, 2020, 12:27:56 am
content: Quote from: helius on May 18, 2020, 08:26:48 pmIt's true C++ added binary literals only in C++14. Java got them in Java 7 in 2011.C has had octal and hexadecimal literals (and char of course) right since the beginning in the early 70s -- or at least in the 1st edition of K&R in 1978.BCPL used #x and #o as a prefix for hex and octal literals as early as 1967 I think.PL/I in 1966 used '101101'B for bit string literals (which support &, |, ^ operations) and 101101B for binary literals for an integer value. (You could convert between them, but a trap was that a bit string literal ends up left justified in the register)

author: veedub565
author_rank: Frequent Contributor
timestamp: May 19, 2020, 12:24:10 pm
content: I can see I'm going to have to definitely breadboard this with all 56 bits to be sure it's doing what I think it is, before applying it to the instrument.I'm fairly familiar with converting between decimal, binary, and hex. So my example of 11010010 is binary, as a decimal it would be 210, and as hex it would be D2.I think what's confused me here then is the conversion from that to C++14.