This is only a preview of the November 2018 issue of Silicon Chip. You can view 41 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "Oh Christmas tree, oh Christmas tree...":
Items relevant to "USB digital and SPI interface board":
Items relevant to "Insomnia and Tinnitus killer":
Items relevant to "El cheapo modules, part 20: two tiny compass modules":
Articles in this series:
Items relevant to "GPS-synched, lab-quality frequency reference (Part 2)":
Purchase a printed copy of this issue for $10.00. |
USB Digital and SPI
Interface Board
We originally designed this simple, low-cost interface to control the
Christmas Light Tree elsewhere in this issue. But then we realised that
with a minor tweak here, a slight adjustment there, we would have a
general-purpose 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 Light Tree or anything else that comes to mind!
by Tim Blythman
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
control some external circuitry easily
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 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.
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 in this project
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 2017 issue (siliconchip.com.
au/Article/10648).
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 pro-
Here the Interface Module is driving the Stackable LED
Christmas Tree, using CON4 to make a direct connection.
32
Silicon Chip
Australia’s electronics magazine
siliconchip.com.au
Fig.1: the circuit diagram for the 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.
gram 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
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.
Table1: pin
connections
and control
characters
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).
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
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
-
siliconchip.com.au
Australia’s electronics magazine
setting the pin to high and the lowercase character setting the pin low, as
shown in Table1 below.
This feature is used with the 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 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 data received on
the MISO pin back to the host in binary format.
November 2018 33
Parts list – USB/SPI Interface Board
1 double-sided PCB coded 16107182, 55mm x 28mm
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
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 a 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
34
Silicon Chip
byte you wish to read back. The response will then be read back and
displayed along with the ACK/NACK
indicators mentioned earlier..
The Board also supports 10-bit addressing mode. In this mode, the top
five bits 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 that there are no I2C bus
pullup resistors on the board. If your
slave lacks pullups you will need to
fit some yourself.
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 operates as USB signal lines rather than
general purpose I/O pins.
An optional six-pin header in-circuit 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).
Australia’s electronics magazine
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.
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.
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.
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 ca ble.
Firstly, 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.
Here’s an enlargement of the USB
“plug” section of the PCB, which
is removed if a micro USB socket
is used (as shown opposite). Score
along the “mouse bite” holes before
snapping this section off and clean
the edge with a file.
siliconchip.com.au
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.
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).
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 handy 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.
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
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.
siliconchip.com.au
Programming the PIC
so now (see 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 orientated correctly first.
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 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 figure out which connections on
your device need to go to which pin
on CON3 or CON4.
You can then set the baud rate to any
of those mentioned under the “Two
control modes” cross-heading above
and then use a terminal program as
described to send test commands and
check the 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.
Australia’s electronics magazine
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
siliconchip.com.au/link/aalr
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.
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 can be downloaded from
www.python.org/downloads/
You will also need the pyserial library to access the serial port. This
can be added by running the following command from the Python command line:
pip install pyserial
Download the program, “Serial Tree.
py”, from the SILICON CHIP 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.
SC
November 2018 35
|