This is only a preview of the November 2020 issue of Practical Electronics. You can view 0 of the 72 pages in the full issue. Articles in this series:
|
USB Digital and SPI
Interface Board
We originally designed this simple, low-cost interface to control the LED
Christmas Tree elsewhere in this issue. But then we realised that with a
minor tweak here, a slight adjustment there, we would have a generalpurpose controller which could handle up to eight digital lines from your PC,
including an SPI serial interface. So here it is: use it for the LED Christmas
Tree or anything else that comes to mind!
T
his small board uses a low-
cost PIC16F1455 microcontroller, which incorporates a USB
interface, to drive up to seven digital
outputs and one input, including
three used for SPI (serial peripheral
interface) communications.
This means that you can use it to
easily control some external circuitry
from your PC.
The LED Christmas Tree earlier in
this issue has an SPI-compatible interface and so it can be controlled using
this board, but there are many other ICs
which also use an SPI bus.
As a bonus, if the Digital Interface
Board is powered up but not connected
to a computer (say, it’s connected to a
USB phone charger) it will output random patterns to allow the LED Christmas Tree to be used without a computer.
So, if you want to develop a project
around one of those ICs, this board
would be a really easy way to experiment with such chips and test them out.
It can even be used to drive colour
TFT LCD screens as many of these are
based around an SPI interface, with the
addition of a few digital control pins;
this board can also drive those pins,
using its four extra digital outputs.
You could also drive a standard
alphanumeric LCD using this module.
They typically require around 7 digital
control pins; four for data and three for
clocking/control.
by Tim Blythman
Luckily, that’s exactly what this
board can provide. You can even use it
to communicate over an I2C interface.
It’s based on a PIC
The PIC16F1455 used here is one of
the smallest (and cheapest) PICs with a
USB interface; and impressively, it only
needs three other components to work.
You don’t even need to solder a USB
socket onto the PCB (although there is
space to do so), as we’ve made the end
of the PCB into a plug that will fit into
a standard USB-A socket.
It isn’t completely compliant with
the USB specifications, but it’s a technique that is quite widely used and it
works fine.
Just keep in mind that the copper
tracks can wear out if you’re plugging
and unplugging it a lot. In that case, a
proper USB connector would be the
way to go.
You might remember that the
PIC16F1455 was used as the basis for
the popular Microbridge PIC Programmer and USB Serial Converter, from the
May 2018 issue.
The software we are using here is
similar, in that it presents itself to the
host computer as a serial port, but
instead of producing a serial UART
stream (compatible with RS-232), it
generates an SPI stream instead.
What this means is that any program
that can interface with a serial port on
your computer can be used to control
most SPI devices, including our stackable LED Christmas Tree. We’ve provided a sample program in the Python
language to use with our LED Christmas
Tree, but you can also use a terminal
program such as the Arduino Serial
Monitor to test out the commands and
manually send SPI data.
Two control modes
The way the Digital Interface Board
works is as follows. If it is configured
with a board rate of 9600, then the
interface works in hexadecimal SPI
mode. If the baud rate is set to 19,200,
then it works in binary SPI mode. A
baud rate of 38,400 selects I2C mode
(the data rate is 400kHz).
Note that these baud rates do not
affect how fast the data is clocked out;
they are just a convenient way of signalling to the Digital Interface Board
which mode you want to use.
Hexadecimal SPI mode
Three of the seven digital output pins
on the board can be used as an SPI serial
bus. They are labelled SCK (the serial
clock), MOSI ([data] master out, slave in)
and MISO ([data] master in, slave out).
Here the Interface module is driving the stackable LED
Christmas Tree, using CON4 to make a direct connection.
24
Practical Electronics | November | 2020
LED Tree Control Board
Fig.1: circuit diagram for the Digital Interface Board. IC1 is programmed to provide a USB interface via CON1, which can
be either an SMD USB socket or tracks on the PCB which fit into a USB port. All of the PIC’s free pins are wired to CON3 and
CON4, to provide seven programmable digital outputs as well as an SPI or I2C serial bus, to communicate with external circuitry.
In hexadecimal SPI mode, the unit accepts the hex digits 0-9 and
A-F (and their lowercase equivalents) over the USB serial interface.
The letters T-Z and t-z are also accepted, as described below.
Any hex digit received will cause four bits of SPI data to be
transmitted on the MOSI and SCK pins, with the most-significant
bit being sent first.
Any data that is received on the MISO pin (pin 10, RC0) is read
back simultaneously with data being transmitted on MOSI. In this
case, a hex digit is echoed back to the serial monitor.
The characters T-Z and t-z can be used to set the state of the pins
directly, with the uppercase character setting the pin to high and
the lowercase character setting the pin low – see Table1 below.
This feature is used with the LED Christmas Tree to latch the
data when required. Any other characters received over the USB
interface are ignored.
For example, using the hexadecimal mode, we can turn off all
the LEDs on a single board of the LED Christmas Tree by sending
the string ‘v00V’. This brings pin 9 low, then sends eight bits of
zeros over the SPI bus, then brings pin 9 high, transferring the
shifted data into the device’s output latches. Similarly, sending
the string ‘vFFV’ will turn all the LEDs on.
Because other characters are ignored, line endings don’t matter
and practically any terminal program can be used to send this data.
Note that there are only seven pins listed in Table1 because the
eighth pin, pin 10, is only used as an input and only in SPI mode.
Binary SPI mode
In binary SPI mode, we take advantage of the fact that USB data
is sent in packets. Each time the Digital Interface Board receives
a packet from the host, it sets LT low, clocks out the data using
SCK and MOSI and then sets LT high again. It also reports serial
Table1: pin connections and control characters
Control
characters
IC1 pin
pin
CON3
pin
CON4
pin
t/T
u/U
(input only)
v/V
w/W
x/X
y/Y
z/Z
3 (RA4)
2 (RA5)
10 (RC0, MISO/DO)
9 (RC1, LT)
8 (RC2, MOSI/DI/SDA)
7 (RC3, SCK/CK/SCL)
6 (RC4)
5 (RC5)
9
10
3
4
5
6
7
8
–
–
4
5
3
6
–
–
Practical Electronics | November | 2020
data received on the MISO pin back to the host in
binary format.
While this mode provides faster communications,
it can only be used with a host terminal program
that sends multiple bytes together, so that the data
is received by the unit as a single packet.
This is possible with the Arduino Serial Monitor,
provided that line endings are turned off, as these will
otherwise appear as binary data to the unit.
If you are driving an LED Christmas Tree board in
binary mode and see LED2 and LED4 on when you
are not expecting them to be on, that indicates that
you may have line endings still turned on, as this
combination corresponds to the character that is used
to terminate a line (line feed [LF], binary 00001010,
ASCII code 10).
While trickier to use manually, this mode is more
convenient for writing software which delivers data
to the serial port directly.
Hexadecimal I2C mode
To make this board even more flexible, we have
also added an I2C mode. In this mode, RC2 is used
as SDA while RC3 is used as SCL.
To use it, you write one or more bytes to the
serial port in hex format (ie, pairs of characters
0-9 or A-F), followed by a ‘newline’. When the
newline character is received, the previous bytes
are transmitted over the I2C bus.
Alternatively, you can prefix the bytes with ‘S’ to
start communication and follow with ‘P’ to finish.
The first byte contains the 7-bit device address plus
one bit to indicate read or write mode. The board
scans this byte to determine whether you are doing
a read or write and acts accordingly.
Each byte read is followed with a ‘K’ to indicate
if an ACK signal was received or an ‘N’ if it did not
receive the ACK. In read mode, after the initial address byte, you simply send ‘FF’ for each byte you
wish to read back. The response will then be read
back and displayed along with the ACK/NACK
indicators mentioned earlier.
The Digital Interface Board also supports 10-bit
addressing mode. In this mode, the top five bits
25
Parts list – USB/SPI
Interface Board
1 double-sided PCB coded 16107182,
55mm x 28mm availabel from the
PE PCB Service
1 PIC16F1455-I/P microcontroller
programmed with 1610718A.HEX (IC1)
1 14-pin DIL IC socket (optional, for IC1)
1 mini USB type B SMD socket
(CON1b; optional)
1 5-way right-angle (or straight) pin
header (CON2, ICSP; optional)
1 10-way pin header or socket (CON3)
1 6-way female header socket (CON4)
2 100nF MKT capacitors
1 10k 1% or 5% resistor, 1/4W or 1/2W
of the address byte are 11110, and a
second address byte follows. It will
detect this and act accordingly.
The clock rate for I2C mode is always
400kHz. (Note: there are no I2C bus pullup resistors on the board. If your slave
lacks pull-ups you’ll need to fit some.)
Check the I2C specifications to determine the correct pull-up resistors
to use for your circuit.
Circuit description
The circuit of the Digital Interface
Board is shown in Fig.1. A 10k pullup resistor from pin 4 (MCLR) of IC1 to
VCC enables the power-on reset feature
and allows for normal operation of the
chip after power is applied.
One 100nF capacitor between VDD
(pin 1) and VSS (pin 14) provides overall supply bypassing, while another
capacitor from pin 11 (VUSB3V3) to
ground filters the internally generated
USB 3.3V supply.
The proper USB socket and PCB
track socket are wired in parallel,
with the D– and D+ signal lines going
to pins 12 and 13 of IC1 respectively.
The software sets these pins to operate
as USB signal lines rather than general
purpose I/O pins.
An optional six-pin header for incircuit serial programming (ICSP) is
provided (CON2), to allow IC1 to be
programmed in situ. If you’re using a
pre-programmed chip, you can leave
CON2 off the board.
Finally, CON3 and CON4 break out
the digital I/O pins. 10-pin header
CON3 provides connections to GND
(0V) and the USB 5V rail, as well as
the eight I/Os that the unit can control
(RC0-RC5 and RA4-RA5).
By comparison, 6-pin header CON4
only includes the four signal connections which are required for SPI or I2C
communications, along with the GND
and 5V connections.
26
This suits the LED Christmas Tree
board, which can be plugged straight
into this header. But it could also be
used in any other situation where you
just need to communicate with an SPI
or I2C device.
As mentioned earlier, the RC0 pin
on IC1 is used as an input only, in
SPI mode, while the other seven pins
are digital outputs. Outputs RC2 and
RC3 can be used for either SPI or I2C
serial communications, or as general
purpose I/Os.
Programming the PIC
If you have a blank PIC, you can program
it using a PICkit 3 or PICkit 4, in conjunction with the MPLAB X IPE (Integrated
Programming Environment) software.
This is bundled with the MPLAB X IDE
(Integrated Development Environment),
which can be downloaded from: https://
www.microchip.com/mplab/mplab-x-ide
Having installed it, launch the IPE
program. From the Setting Menu, select
Advanced Mode and log in using the default password. Click the Power button on
the left and ensure ‘Power Target Circuit
from Tool’ is ticked.
Click the Operate button and select
PIC16F1455 from the Device list, select
your programmer from the Tool list and
click Connect.
Once it indicates success, use the
Browse button to select a source HEX file
and open the HEX file from the software
download file.
Connect the programmer to the PCB,
ensuring that the arrowed pin on the
programmer lines up with pin 1 (arrowed)
on the PCB. Click the Program button and
check that the messages in the bottom
of the window indicate that IC1 was successfully programmed.
To test the chip, unplug the programmer and connect the board to a USB
socket. Your computer should show that
a new USB serial port has been detected.
Construction
Use the PCB overlay diagram (Fig.2)
as a guide during constructions. The
USB Digital and SPI Interface Board is
built on a PCB coded 16107182, which
measures 55 x 28mm and is available
from the PE PCB Service.
If you intend to install the optional
USB socket, we recommend doing
that first, before any other components are in the way. To do this, the
USB projection on the PCB needs to
be snapped or cut off; otherwise, it
would foul the cable.
First, score along the line of ‘mouse
bites’ to help the PCB break cleanly.
This will also help to sever the PCB
traces so that they don’t tear when the
board comes apart. Flex the board at the
score line and it should snap. Clean up
any rough edges with a file.
The USB socket is the only SMD
component used. We recommend that
you put a thin smear of flux paste on
the pads before soldering.
The socket has plastic pegs on its
underside to locate it on the PCB. Once
positioned, ensure it is flat and solder
the large mechanical tabs on the sides
to lock it in place.
With a clean, fine tip loaded with
a bit of extra solder, carefully apply
the iron to the pins and pads. The
flux should draw the solder up and
onto the pins. Solder all the pins and
inspect them to ensure there are no
bridges between adjacent pads. If there
are bridges, remove them with some
additional flux paste and a piece of
desoldering braid (solder wick).
We suggest that you solder one pin
in place and then check the alignment
is correct before soldering the rest.
You don’t need to fit CON2 if you
have purchased a pre-programmed PIC.
But note that even if you will be using it
to program IC1, you can plug it in and
hold it in place while programming the
chip, then remove it.
If you are programming IC1 using
an external programmer, do so now
(see the above panel for instructions),
then plug the programmed chip into
the socket.
Or, if you’re not using a socket, solder
it to the board now but make sure it is
oriented correctly first.
Required components
There is just one resistor on the board,
so solder that in place next. Follow
with the two identical capacitors. None
of these components are polarised.
If you are using an IC socket for IC1
(which is recommedned if you plan to
use an external programmer), fit it next,
ensuring the notch is facing towards
the top of the board, as shown in Fig.2.
If you will be plugging CON4 into a
stackable LED Christmas Tree board,
you should ensure that it will line up
nicely before soldering it.
Enlargement of the USB ‘plug’ section
of the PCB, which is removed if a micro
USB socket is used (as shown opposite,
above). Score along the ‘mouse bite’
holes before snapping this section off
and clean the edge with a file.
Practical Electronics | November | 2020
Fig.2: it doesn’t get much easier
than this. IC1 is the only polarised
component; make sure to fit
it with the orientation shown
here. You can use a socket if
you don’t want to solder the
chip directly to the board. The ICSP
header (CON2) is not required if your
micro has already been programmed.
Using it
To use the Digital Interface Board to
drive the stackable LED Christmas Tree,
plug the root board of the tree into the
six-way socket on the Digital Interface
Board, with both boards facing up so
that the pin names match.
Plug the Interface Board into a USB
port on your computer and open a terminal program such as the Arduino Serial
Monitor, PuTTY or TeraTerm. Select a
baud rate of 9600 (usually the default).
Type ‘vFFV’ into the terminal and
press Enter. All the LEDs should light
up on the root board, indicating that it’s
all working properly. Typing ‘v00V’ and
pressing enter should cause all the LEDs
on the root board to switch off.
If your tree has multiple boards, use a
longer string such as ‘vFFFFFFV’ (which
suits three boards). Each hex digit corresponds to four LEDs, so you will need
two hex digits for each board in the tree.
If you don’t supply enough hex digits,
the furthest downstream boards will be
fed old data from other upstream boards.
To use this board to drive a different
SPI or I2C device, refer to Table 1 to find
which connections on your device need
to go to which pin on CON3 or CON4.
You then set the baud rate to any of
those mentioned under the ‘Two control
modes’ cross-heading above and use a
terminal program as described to send
test commands and check responses.
It’s much easier to use the hexadecimal control modes initially to test the
unit out, even if you’re planning on using
the binary SPI mode later.
Python program
We have provided a small example
script written in the Python programming language to drive the LED Christmas Tree using this Interface Board. You
will need some Python experience (or at
least some script programming experience) to modify it. (The Python language
The simplest connection method is to
plug the PCB straight into a USB port,
but if you fit a socket as shown here, the
result is a bit more robust. It also makes
the board slightly more compact.
can be downloaded (www.python.org/
downloads/). You will also need the
pyserial library to access the serial port.
This can be added by running the following from the Python command line:
pip install pyserial
Download the program, Serial Tree.py,
from the November 2020 page of the
PE website and change the port name
to suit your system (eg COMx on windows, /dev/ttySx on Mac/Linux), and
then run the program using the Python
interpreter. It generates random patterns
to give a twinkling effect.
Reproduced by arrangement with
SILICON CHIP magazine 2020.
www.siliconchip.com.au
STEWART OF READING
17A King Street, Mortimer, near Reading, RG7 3RS
Telephone: 0118 933 1111 Fax: 0118 933 2375
USED ELECTRONIC TEST EQUIPMENT
Check website www.stewart-of-reading.co.uk
Fluke/Philips PM3092 Oscilloscope
2+2 Channel 200MHz Delay TB,
Autoset etc – £250
LAMBDA GENESYS
LAMBDA GENESYS
IFR 2025
IFR 2948B
IFR 6843
R&S APN62
Agilent 8712ET
HP8903A/B
HP8757D
HP3325A
HP3561A
HP6032A
HP6622A
HP6624A
HP6632B
HP6644A
HP6654A
HP8341A
HP83630A
HP83624A
HP8484A
HP8560E
HP8563A
HP8566B
HP8662A
Marconi 2022E
Marconi 2024
Marconi 2030
Marconi 2023A
(ALL PRICES PLUS CARRIAGE & VAT)
Please check availability before ordering or calling in
PSU GEN100-15 100V 15A Boxed As New
£400
PSU GEN50-30 50V 30A
£400
Signal Generator 9kHz – 2.51GHz Opt 04/11
£900
Communication Service Monitor Opts 03/25 Avionics
POA
Microwave Systems Analyser 10MHz – 20GHz
POA
Syn Function Generator 1Hz – 260kHz
£295
RF Network Analyser 300kHz – 1300MHz
POA
Audio Analyser
£750 – £950
Scaler Network Analyser
POA
Synthesised Function Generator
£195
Dynamic Signal Analyser
£650
PSU 0-60V 0-50A 1000W
£750
PSU 0-20V 4A Twice or 0-50V 2A Twice
£350
PSU 4 Outputs
£400
PSU 0-20V 0-5A
£195
PSU 0-60V 3.5A
£400
PSU 0-60V 0-9A
£500
Synthesised Sweep Generator 10MHz – 20GHz
£2,000
Synthesised Sweeper 10MHz – 26.5 GHz
POA
Synthesised Sweeper 2 – 20GHz
POA
Power Sensor 0.01-18GHz 3nW-10µW
£75
Spectrum Analyser Synthesised 30Hz – 2.9GHz
£1,750
Spectrum Analyser Synthesised 9kHz – 22GHz
£2,250
Spectrum Analsyer 100Hz – 22GHz
£1,200
RF Generator 10kHz – 1280MHz
£750
Synthesised AM/FM Signal Generator 10kHz – 1.01GHz
£325
Synthesised Signal Generator 9kHz – 2.4GHz
£800
Synthesised Signal Generator 10kHz – 1.35GHz
£750
Signal Generator 9kHz – 1.2GHz
£700
HP/Agilent HP 34401A Digital
Multimeter 6½ Digit £325 – £375
HP33120A
HP53131A
HP53131A
Audio Precision
Datron 4708
Druck DPI 515
Datron 1081
ENI 325LA
Keithley 228
Time 9818
Practical Electronics | November | 2020
HP 54600B Oscilloscope
Analogue/Digital Dual Trace 100MHz
Only £75, with accessories £125
Marconi 2305
Modulation Meter
£250
Marconi 2440
Counter 20GHz
£295
Marconi 2945/A/B
Communications Test Set Various Options
POA
Marconi 2955
Radio Communications Test Set
£595
Marconi 2955A
Radio Communications Test Set
£725
Marconi 2955B
Radio Communications Test Set
£800
Marconi 6200
Microwave Test Set
£1,500
Marconi 6200A
Microwave Test Set 10MHz – 20GHz
£1,950
Marconi 6200B
Microwave Test Set
£2,300
Marconi 6960B
Power Meter with 6910 sensor
£295
Tektronix TDS3052B Oscilloscope 500MHz 2.5GS/s
£1,250
Tektronix TDS3032
Oscilloscope 300MHz 2.5GS/s
£995
Tektronix TDS3012
Oscilloscope 2 Channel 100MHz 1.25GS/s
£450
Tektronix 2430A
Oscilloscope Dual Trace 150MHz 100MS/s
£350
Tektronix 2465B
Oscilloscope 4 Channel 400MHz
£600
Farnell AP60/50
PSU 0-60V 0-50A 1kW Switch Mode
£300
Farnell XA35/2T
PSU 0-35V 0-2A Twice Digital
£75
Farnell AP100-90
Power Supply 100V 90A
£900
Farnell LF1
Sine/Sq Oscillator 10Hz – 1MHz
£45
Racal 1991
Counter/Timer 160MHz 9 Digit
£150
Racal 2101
Counter 20GHz LED
£295
Racal 9300
True RMS Millivoltmeter 5Hz – 20MHz etc
£45
Racal 9300B
As 9300
£75
Solartron 7150/PLUS 6½ Digit DMM True RMS IEEE
£65/£75
Solatron 1253
Gain Phase Analyser 1mHz – 20kHz
£600
Solartron SI 1255
HF Frequency Response Analyser
POA
Tasakago TM035-2
PSU 0-35V 0-2A 2 Meters
£30
Thurlby PL320QMD PSU 0-30V 0-2A Twice
£160 – £200
Thurlby TG210
Function Generator 0.002-2MHz TTL etc Kenwood Badged
£65
Function Generator 100 microHz – 15MHz
Universal Counter 3GHz Boxed unused
Universal Counter 225MHz
SYS2712 Audio Analyser – in original box
Autocal Multifunction Standard
Pressure Calibrator/Controller
Autocal Standards Multimeter
RF Power Amplifier 250kHz – 150MHz 25W 50dB
Voltage/Current Source
DC Current & Voltage Calibrator
£350
£600
£350
POA
POA
£400
POA
POA
POA
POA
Marconi 2955B Radio
Communications Test Set – £800
27
|